Skip to main content

McpTool

Trait McpTool 

Source
pub trait McpTool<R: Role>: Send + Sync {
    type Input: JsonSchema + DeserializeOwned + Send + 'static;
    type Output: JsonSchema + Serialize + Send + 'static;

    // Required methods
    fn name(&self) -> String;
    fn description(&self) -> String;
    fn call_tool(
        &self,
        input: Self::Input,
        context: McpConnectionTo<R>,
    ) -> impl Future<Output = Result<Self::Output, Error>> + Send;

    // Provided method
    fn title(&self) -> Option<String> { ... }
}
Expand description

Trait for defining MCP tools.

Implement this trait to create a tool that can be registered with an MCP server. The tool’s input and output types must implement JSON Schema for automatic documentation.

§Example

use agent_client_protocol::mcp_server::{McpConnectionTo, McpTool};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(JsonSchema, Deserialize)]
struct EchoInput {
    message: String,
}

#[derive(JsonSchema, Serialize)]
struct EchoOutput {
    echoed: String,
}

struct EchoTool;

impl<R: agent_client_protocol::role::Role> McpTool<R> for EchoTool {
    type Input = EchoInput;
    type Output = EchoOutput;

    fn name(&self) -> String {
        "echo".to_string()
    }

    fn description(&self) -> String {
        "Echoes back the input message".to_string()
    }

    async fn call_tool(
        &self,
        input: EchoInput,
        _context: McpConnectionTo<R>,
    ) -> Result<EchoOutput, agent_client_protocol::Error> {
        Ok(EchoOutput {
            echoed: format!("Echo: {}", input.message),
        })
    }
}

Required Associated Types§

Source

type Input: JsonSchema + DeserializeOwned + Send + 'static

The type of input the tool accepts.

Source

type Output: JsonSchema + Serialize + Send + 'static

The type of output the tool produces.

Required Methods§

Source

fn name(&self) -> String

The name of the tool

Source

fn description(&self) -> String

A description of what the tool does

Source

fn call_tool( &self, input: Self::Input, context: McpConnectionTo<R>, ) -> impl Future<Output = Result<Self::Output, Error>> + Send

Define the tool’s behavior. You can implement this with an async fn.

Provided Methods§

Source

fn title(&self) -> Option<String>

A human-readable title for the tool

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§