Skip to main content

tool

Macro tool 

Source
macro_rules! tool {
    (
        name: $name:expr,
        description: $description:expr,
        schema: $schema:expr,
        | $ctx:ident , $input:ident | $body:expr $(,)?
    ) => { ... };
    (
        name: $name:expr,
        description: $description:expr,
        schema: $schema:expr,
        context: $ctxty:ty,
        | $ctx:ident , $input:ident | $body:expr $(,)?
    ) => { ... };
}
Expand description

Define a tool inline, expanding to a fresh zero-sized struct plus a SimpleTool impl — the lowest-ceremony way to add a one-off tool in an example, test, or script.

This is the declarative counterpart to Tool: use the derive when you want a named, reusable tool type; reach for tool! when you just need a closure-like tool right where you register it.

The application context type defaults to (); pass context: MyCtx, before the closure to use a different one. The closure receives &ToolContext<Ctx> and the raw serde_json::Value arguments and must return a future resolving to anyhow::Result<ToolResult>.

§Example

use agent_sdk::{tool, ToolResult, ToolRegistry};
use serde_json::json;

let weather = tool! {
    name: "get_weather",
    description: "Get the current weather for a city",
    schema: json!({
        "type": "object",
        "properties": { "city": { "type": "string" } },
        "required": ["city"],
    }),
    |_ctx, input| async move {
        let city = input["city"].as_str().unwrap_or("Unknown");
        Ok(ToolResult::success(format!("Weather in {city}: Sunny")))
    }
};

let mut registry: ToolRegistry<()> = ToolRegistry::new();
registry.register_simple(weather);