Skip to main content

tool

Attribute Macro tool 

Source
#[tool]
Expand description

§#[tool] Attribute Macro

Generates a Tool implementation for annotated functions:

  1. Creates struct wrapper with function
  2. Implements Tool trait with:
    • name(): Function name
    • arguments(): JSON schema from doc comments
    • call(): Type-safe argument parsing

§Example

use promptrs::{tool, Tooling};

#[tool]
/// Calculator tool
/// a: First number
/// b: Second number
/// operation: +, -, *, /
fn calculate(a: f64, b: f64, operation: String) -> String {
    match operation.as_str() {
        "+" => (a + b).to_string(),
        "-" => (a - b).to_string(),
        "*" => (a * b).to_string(),
        "/" => a / b > 0.0 ? (a / b).to_string() : "Division by zero".into(),
        _ => "Invalid operation".into(),
    }
}

let tooling = Tooling::new().add(calculate);
let args = Arguments::from_value(json!({
    "a": 5.5,
    "b": 2.5,
    "operation": "*"
}));

let result = tooling.call("calculate", &args).unwrap();
println!("Result: {}", result); // 13.75

§Documentation Comments

Use /// name: Description to:

  1. Create parameter documentation in JSON schema
  2. Provide user-facing parameter descriptions
  3. Enable parameter auto-completion in development tools