Available on crate features
async and schemars-tools only.Expand description
Schemars-driven typed tool registration.
Layers on top of ToolRegistry and the
Tool trait: instead of a raw Fn(Value) -> Future handler, you
supply a typed Fn(Args) -> Future where Args: JsonSchema + DeserializeOwned. The schema is derived automatically; the registry
deserializes the model’s input into Args before invoking, returning
ToolError::InvalidInput when the shape doesn’t match.
Gated on the schemars-tools feature.
use claude_api::tool_dispatch::ToolRegistry;
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::json;
#[derive(JsonSchema, Deserialize)]
struct WeatherArgs {
city: String,
#[serde(default)]
units: Option<String>,
}
let mut registry = ToolRegistry::new();
registry.register_typed::<WeatherArgs, _, _>(
"get_weather",
|args| async move {
Ok(json!({"city": args.city, "units": args.units.unwrap_or_default()}))
},
);