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::{McpTool, McpContext};
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: McpContext<R>,
) -> Result<EchoOutput, agent_client_protocol::Error> {
Ok(EchoOutput {
echoed: format!("Echo: {}", input.message),
})
}
}Required Associated Types§
Sourcetype Input: JsonSchema + DeserializeOwned + Send + 'static
type Input: JsonSchema + DeserializeOwned + Send + 'static
The type of input the tool accepts.
Sourcetype Output: JsonSchema + Serialize + Send + 'static
type Output: JsonSchema + Serialize + Send + 'static
The type of output the tool produces.
Required Methods§
Sourcefn description(&self) -> String
fn description(&self) -> String
A description of what the tool does
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.