pub struct McpToolRegistry { /* private fields */ }Expand description
A clonable collection of MCP tool definitions and in-process handlers.
Resources, prompts, server identity, and transport lifecycle remain owned
by McpServer.
Output schemas are compiled at registration and shared by cloned registries.
Successful structured results are checked against the advertised schema on
direct, async, and protocol calls; handler error results bypass this check.
See tool_definition for dialect, format, and reference behavior.
Implementations§
Source§impl McpToolRegistry
impl McpToolRegistry
Sourcepub fn add_tool<Call>(
&mut self,
definition: ToolDefinition,
call: Call,
) -> Result<(), McpToolError>
pub fn add_tool<Call>( &mut self, definition: ToolDefinition, call: Call, ) -> Result<(), McpToolError>
Register a synchronous MCP tool handler.
§Errors
Returns McpToolError when definition is invalid or a tool with the
same name is already registered.
Sourcepub fn add_typed_tool<Input, Call>(
&mut self,
definition: McpTypedTool<Input>,
call: Call,
) -> Result<(), McpToolError>
pub fn add_typed_tool<Input, Call>( &mut self, definition: McpTypedTool<Input>, call: Call, ) -> Result<(), McpToolError>
Register a synchronous typed MCP tool handler.
§Errors
Returns McpToolError when definition is invalid or a tool with the
same name is already registered.
Sourcepub fn add_koruma_tool<Input, Call>(
&mut self,
definition: McpTypedTool<Input>,
call: Call,
) -> Result<(), McpToolError>where
Input: McpToolInput + ValidateExt,
Input::Error: ValidationIssues,
Call: Fn(Input) -> ToolCallResult + Send + Sync + 'static,
pub fn add_koruma_tool<Input, Call>(
&mut self,
definition: McpTypedTool<Input>,
call: Call,
) -> Result<(), McpToolError>where
Input: McpToolInput + ValidateExt,
Input::Error: ValidationIssues,
Call: Fn(Input) -> ToolCallResult + Send + Sync + 'static,
Register a typed handler with canonical Koruma domain validation.
Strict decoding runs first, then validate_koruma. A failing value
returns structured issues without invoking the handler. Successful
results still pass the advertised output-schema checks.
use koruma_collection::numeric::RangeValidation;
#[derive(component_shape_mcp::McpToolInput, koruma::Koruma)]
struct BatchArgs {
#[koruma(RangeValidation::<_>.min(1).max(5))]
count: u32,
}
let mut tools = component_shape_mcp::McpToolRegistry::new();
let definition = component_shape_mcp::tool_definition_for_input::<BatchArgs>(
"batch", None, None, None,
)?;
tools.add_koruma_tool(definition, |args: BatchArgs| {
component_shape_mcp::tool_structured_result(
component_shape_mcp::serde_json::json!({ "count": args.count }),
)
})?;
assert_eq!(tools.call_tool("batch", Some(
component_shape_mcp::serde_json::json!({ "count": 0 }),
)).is_error, Some(true));§Errors
Returns McpToolError for an invalid or duplicate tool definition.
Sourcepub fn add_tool_async<Call, Fut>(
&mut self,
definition: ToolDefinition,
call: Call,
) -> Result<(), McpToolError>where
Call: Fn(McpToolCall) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ToolCallResult> + Send + 'static,
pub fn add_tool_async<Call, Fut>(
&mut self,
definition: ToolDefinition,
call: Call,
) -> Result<(), McpToolError>where
Call: Fn(McpToolCall) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ToolCallResult> + Send + 'static,
Register an async MCP tool handler.
§Errors
Returns McpToolError when definition is invalid or a tool with the
same name is already registered.
Sourcepub fn add_typed_tool_async<Input, Call, Fut>(
&mut self,
definition: McpTypedTool<Input>,
call: Call,
) -> Result<(), McpToolError>where
Input: McpToolInput,
Call: Fn(Input) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ToolCallResult> + Send + 'static,
pub fn add_typed_tool_async<Input, Call, Fut>(
&mut self,
definition: McpTypedTool<Input>,
call: Call,
) -> Result<(), McpToolError>where
Input: McpToolInput,
Call: Fn(Input) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ToolCallResult> + Send + 'static,
Register an async typed MCP tool handler.
§Errors
Returns McpToolError when definition is invalid or a tool with the
same name is already registered.
Sourcepub fn add_koruma_tool_async<Input, Call, Fut>(
&mut self,
definition: McpTypedTool<Input>,
call: Call,
) -> Result<(), McpToolError>where
Input: McpToolInput + ValidateExt,
Input::Error: ValidationIssues,
Call: Fn(Input) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ToolCallResult> + Send + 'static,
pub fn add_koruma_tool_async<Input, Call, Fut>(
&mut self,
definition: McpTypedTool<Input>,
call: Call,
) -> Result<(), McpToolError>where
Input: McpToolInput + ValidateExt,
Input::Error: ValidationIssues,
Call: Fn(Input) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ToolCallResult> + Send + 'static,
Register an async typed handler with Koruma domain validation.
Decoding and validation run before the handler creates its future.
See Self::add_koruma_tool for issue and output-schema behavior.
§Errors
Returns McpToolError for an invalid or duplicate tool definition.
Sourcepub fn list_tools(&self) -> Vec<ToolDefinition>
pub fn list_tools(&self) -> Vec<ToolDefinition>
Return registered MCP tool definitions.
Sourcepub fn contains_tool(&self, name: &str) -> bool
pub fn contains_tool(&self, name: &str) -> bool
Whether a tool name is already registered.
Sourcepub fn tool_count(&self) -> usize
pub fn tool_count(&self) -> usize
Number of registered tools.
Sourcepub fn call_tool(&self, name: &str, arguments: Option<Value>) -> ToolCallResult
pub fn call_tool(&self, name: &str, arguments: Option<Value>) -> ToolCallResult
Calls a registered tool and converts validation or handler failures into a protocol-level tool result.
Sourcepub async fn call_tool_async(
&self,
name: &str,
arguments: Option<Value>,
) -> ToolCallResult
pub async fn call_tool_async( &self, name: &str, arguments: Option<Value>, ) -> ToolCallResult
Asynchronously calls a registered tool and converts validation or handler failures into a protocol-level tool result.