Skip to main content

McpToolRegistry

Struct McpToolRegistry 

Source
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

Source

pub fn new() -> Self

Create an empty tool registry.

Source

pub fn add_tool<Call>( &mut self, definition: ToolDefinition, call: Call, ) -> Result<(), McpToolError>
where Call: Fn(McpToolCall) -> ToolCallResult + Send + Sync + 'static,

Register a synchronous MCP tool handler.

§Errors

Returns McpToolError when definition is invalid or a tool with the same name is already registered.

Source

pub fn add_typed_tool<Input, Call>( &mut self, definition: McpTypedTool<Input>, call: Call, ) -> Result<(), McpToolError>
where Input: McpToolInput, Call: Fn(Input) -> ToolCallResult + Send + Sync + 'static,

Register a synchronous typed MCP tool handler.

§Errors

Returns McpToolError when definition is invalid or a tool with the same name is already registered.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn list_tools(&self) -> Vec<ToolDefinition>

Return registered MCP tool definitions.

Source

pub fn contains_tool(&self, name: &str) -> bool

Whether a tool name is already registered.

Source

pub fn tool_count(&self) -> usize

Number of registered tools.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for McpToolRegistry

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for McpToolRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more