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 whatinvokewill receive. Must be valid JSON Schema.invoke(input)runs the tool.inputis the rawValuethe model produced and is not validated against the schema by the SDK – impls should validate themselves and returnToolError::InvalidInputon failure.invokemay 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§
Provided Methods§
Sourcefn description(&self) -> Option<&str>
fn description(&self) -> Option<&str>
Optional human-readable description; helps the model decide when to
invoke. Default returns None.