pub trait ToolHandler: Send + Sync {
// Required methods
fn definition(&self) -> Tool;
fn call(
&self,
ctx: &McpContext,
arguments: Value,
) -> Result<Vec<Content>, McpError>;
// Provided methods
fn icon(&self) -> Option<&Icon> { ... }
fn version(&self) -> Option<&str> { ... }
fn tags(&self) -> &[String] { ... }
fn annotations(&self) -> Option<&ToolAnnotations> { ... }
fn output_schema(&self) -> Option<Value> { ... }
fn timeout(&self) -> Option<Duration> { ... }
fn call_async<'a>(
&'a self,
ctx: &'a McpContext,
arguments: Value,
) -> Pin<Box<dyn Future<Output = Outcome<Vec<Content>, McpError>> + Send + 'a>> { ... }
}Expand description
Handler for a tool.
This trait is typically implemented via the #[tool] macro.
§Sync vs Async
By default, implement call() for synchronous execution. For async tools,
override call_async() instead. The router always calls call_async(),
which defaults to running call() in an async block.
§Return Type
Async handlers return McpOutcome<Vec<Content>>, a 4-valued type supporting:
Ok(content)- Successful resultErr(McpError)- Recoverable errorCancelled- Request was cancelledPanicked- Unrecoverable failure
Required Methods§
Sourcefn definition(&self) -> Tool
fn definition(&self) -> Tool
Returns the tool definition.
Sourcefn call(
&self,
ctx: &McpContext,
arguments: Value,
) -> Result<Vec<Content>, McpError>
fn call( &self, ctx: &McpContext, arguments: Value, ) -> Result<Vec<Content>, McpError>
Calls the tool synchronously with the given arguments.
This is the default implementation point. Override this for simple
synchronous tools. Returns McpResult which is converted to McpOutcome
by the async wrapper.
Provided Methods§
Sourcefn icon(&self) -> Option<&Icon>
fn icon(&self) -> Option<&Icon>
Returns the tool’s icon, if any.
Default implementation returns None. Override to provide an icon.
Note: Icons can also be set directly in definition().
Sourcefn version(&self) -> Option<&str>
fn version(&self) -> Option<&str>
Returns the tool’s version, if any.
Default implementation returns None. Override to provide a version.
Note: Version can also be set directly in definition().
Returns the tool’s tags for filtering and organization.
Default implementation returns an empty slice. Override to provide tags.
Note: Tags can also be set directly in definition().
Sourcefn annotations(&self) -> Option<&ToolAnnotations>
fn annotations(&self) -> Option<&ToolAnnotations>
Returns the tool’s annotations providing behavioral hints.
Default implementation returns None. Override to provide annotations
like destructive, idempotent, read_only, or open_world_hint.
Note: Annotations can also be set directly in definition().
Sourcefn output_schema(&self) -> Option<Value>
fn output_schema(&self) -> Option<Value>
Returns the tool’s output schema (JSON Schema).
Default implementation returns None. Override to provide a schema
that describes the structure of the tool’s output.
Note: Output schema can also be set directly in definition().
Sourcefn timeout(&self) -> Option<Duration>
fn timeout(&self) -> Option<Duration>
Returns the tool’s custom timeout duration.
Default implementation returns None, meaning the server’s default
timeout applies. Override to specify a per-handler timeout.
When set, creates a child budget with the specified timeout that overrides the server’s default timeout for this handler.
Sourcefn call_async<'a>(
&'a self,
ctx: &'a McpContext,
arguments: Value,
) -> Pin<Box<dyn Future<Output = Outcome<Vec<Content>, McpError>> + Send + 'a>>
fn call_async<'a>( &'a self, ctx: &'a McpContext, arguments: Value, ) -> Pin<Box<dyn Future<Output = Outcome<Vec<Content>, McpError>> + Send + 'a>>
Calls the tool asynchronously with the given arguments.
Override this for tools that need true async execution (e.g., I/O-bound operations, database queries, HTTP requests).
Returns McpOutcome to properly represent all four states: success,
error, cancellation, and panic.
The default implementation delegates to the sync call() method and
converts the McpResult to McpOutcome.