pub fn tool<F, Fut>(
name: &str,
description: &str,
input_schema: Value,
handler: F,
) -> SdkMcpToolExpand 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 JSONValueand should return a JSONValuein 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}!")}]}))
},
);