Skip to main content

Tool

Trait Tool 

Source
pub trait Tool:
    Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &str;
    fn schema(&self) -> Value;
    fn invoke<'life0, 'async_trait>(
        &'life0 self,
        input: Value,
    ) -> Pin<Box<dyn Future<Output = Result<Value, ToolError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided method
    fn description(&self) -> Option<&str> { ... }
}
Available on crate feature async only.
Expand description

A tool the model can invoke during generation.

§Contract

  • name() returns a stable identifier. Names must be unique within a single registry; the model uses them to refer to specific tools.
  • schema() returns a JSON Schema describing the tool’s input. The schema is sent to the model as part of the request and is used to guide what invoke will receive. Must be valid JSON Schema.
  • invoke(input) runs the tool. input is the raw Value the model produced and is not validated against the schema by the SDK – impls should validate themselves and return ToolError::InvalidInput on failure. invoke may take arbitrary time; the agent-loop runner in #20 supports per-iteration timeouts.

All methods take &self so a single instance can be shared via Arc. The trait is Send + Sync + 'static so tools can live in concurrent contexts.

§Example

use async_trait::async_trait;
use claude_api::tool_dispatch::{Tool, ToolError};
use serde_json::{json, Value};

struct AddTool;

#[async_trait]
impl Tool for AddTool {
    fn name(&self) -> &str { "add" }
    fn schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "a": {"type": "number"},
                "b": {"type": "number"}
            },
            "required": ["a", "b"]
        })
    }
    async fn invoke(&self, input: Value) -> Result<Value, ToolError> {
        let a = input.get("a").and_then(Value::as_f64)
            .ok_or_else(|| ToolError::invalid_input("missing 'a'"))?;
        let b = input.get("b").and_then(Value::as_f64)
            .ok_or_else(|| ToolError::invalid_input("missing 'b'"))?;
        Ok(json!({"sum": a + b}))
    }
}

Required Methods§

Source

fn name(&self) -> &str

Stable identifier the model uses to refer to this tool.

Source

fn schema(&self) -> Value

JSON Schema describing the tool’s expected input.

Source

fn invoke<'life0, 'async_trait>( &'life0 self, input: Value, ) -> Pin<Box<dyn Future<Output = Result<Value, ToolError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Run the tool with the model-supplied input and return its result.

Provided Methods§

Source

fn description(&self) -> Option<&str>

Optional human-readable description; helps the model decide when to invoke. Default returns None.

Implementors§

Source§

impl<A, F, Fut> Tool for TypedTool<A, F, Fut>
where A: JsonSchema + DeserializeOwned + Send + Sync + 'static, F: Fn(A) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Value, ToolError>> + Send + 'static,

Available on crate feature schemars-tools only.
Source§

impl<F, Fut> Tool for FnTool<F, Fut>
where F: Fn(Value) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Value, ToolError>> + Send + 'static,