Skip to main content

Tool

Trait Tool 

Source
pub trait Tool: Send + Sync {
    // Required methods
    fn name(&self) -> &str;
    fn description(&self) -> &str;
    fn schema(&self) -> ToolSchema;
    fn execute(&self, input: Value) -> ToolFuture;

    // Provided method
    fn execute_structured(&self, input: Value) -> ToolResultFuture { ... }
}
Expand description

A callable tool the LLM can invoke via tool_calls.

Implementations: FunctionTool (simple fns), MCP adapter (future), graph-as-tool (Plan 014), handoff tools (Plan 009).

§Example

struct WeatherTool;

impl Tool for WeatherTool {
    fn name(&self) -> &str { "get_weather" }
    fn description(&self) -> &str { "Get current weather for a city" }
    fn schema(&self) -> ToolSchema {
        ToolSchema {
            name: "get_weather".into(),
            description: "Get current weather for a city".into(),
            parameters: serde_json::json!({
                "type": "object",
                "properties": { "city": { "type": "string" } },
                "required": ["city"]
            }),
            strict: false,
        }
    }
    fn execute(&self, input: Value) -> ToolFuture {
        Box::pin(async move {
            Ok(serde_json::json!({ "temp": 72, "unit": "F" }))
        })
    }
}

Required Methods§

Source

fn name(&self) -> &str

Tool name — must be unique within a super::ToolRegistry. Sent to the LLM in the tools list.

Source

fn description(&self) -> &str

Human-readable description of what this tool does. The LLM reads this to decide when to call the tool.

Source

fn schema(&self) -> ToolSchema

JSON Schema for this tool’s input parameters. InjectedState and InjectedStore parameters MUST be excluded.

Source

fn execute(&self, input: Value) -> ToolFuture

Execute the tool with the given input. Called by super::ToolNode.

Provided Methods§

Source

fn execute_structured(&self, input: Value) -> ToolResultFuture

Execute with structured result metadata.

Default implementation wraps execute() in ToolResult::ok(). Override to add metadata (result count, source, confidence, etc.).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§