Skip to main content

McpTool

Trait McpTool 

Source
pub trait McpTool: Send + Sync {
    // Required methods
    fn definition(&self) -> McpToolDef;
    fn call<'a>(&'a self, args: Value) -> BoxFuture<'a, ToolCallResult>;
}
Expand description

Trait for implementing MCP tools.

§Example

use mcp::{McpTool, ToolCallResult, McpToolDef};
use serde_json::Value;

struct CalculatorTool;

impl McpTool for CalculatorTool {
    fn definition(&self) -> McpToolDef {
        McpToolDef {
            name: "add".to_string(),
            description: Some("Add two numbers".to_string()),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": {
                    "a": { "type": "number" },
                    "b": { "type": "number" }
                },
                "required": ["a", "b"]
            }),
        }
    }

    fn call<'a>(&'a self, args: Value) -> BoxFuture<'a, ToolCallResult> {
        Box::pin(async move {
            let a = args["a"].as_f64().unwrap_or(0.0);
            let b = args["b"].as_f64().unwrap_or(0.0);
            Ok(vec![ToolContent::text(format!("{}", a + b))])
        })
    }
}

Required Methods§

Source

fn definition(&self) -> McpToolDef

Returns the tool definition (name, description, input schema).

Source

fn call<'a>(&'a self, args: Value) -> BoxFuture<'a, ToolCallResult>

Executes the tool with the given arguments.

Implementors§

Source§

impl<F> McpTool for FnTool<F>
where F: Fn(Value) -> BoxFuture<'static, ToolCallResult> + Send + Sync,