Skip to main content

tool

Function tool 

Source
pub fn tool<F, Fut>(
    name: &str,
    description: &str,
    input_schema: Value,
    handler: F,
) -> SdkMcpTool
where F: Fn(Value) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<Value, Error>> + Send + 'static,
Expand description

Creates a new SdkMcpTool with the given name, description, schema, and handler.

This is the primary factory function for defining custom tools.

§Arguments

  • name — Unique name for the tool.
  • description — What the tool does (shown to the model).
  • input_schema — JSON Schema for the tool’s input parameters.
  • handler — Async function implementing the tool logic. Receives input as a JSON Value and should return a JSON Value in MCP result format.

§Example

let my_tool = tool(
    "greet",
    "Greet someone by name",
    json!({"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}),
    |args: Value| async move {
        let name = args["name"].as_str().unwrap_or("world");
        Ok(json!({"content": [{"type": "text", "text": format!("Hello, {name}!")}]}))
    },
);