pub struct CompositeHandler { /* private fields */ }Expand description
Server composition through handler mounting. A composite handler that mounts multiple handlers with prefixes.
This enables modular server design by combining multiple handlers into a single namespace. Each mounted handler’s tools, resources, and prompts are automatically prefixed to avoid naming conflicts.
§Namespacing Rules
- Tools:
{prefix}_{tool_name}(e.g.,weather_get_forecast) - Resources:
{prefix}://{original_uri}(e.g.,weather://api/forecast) - Prompts:
{prefix}_{prompt_name}(e.g.,weather_forecast_prompt)
§Thread Safety
CompositeHandler is Send + Sync when all mounted handlers are.
Implementations§
Source§impl CompositeHandler
impl CompositeHandler
Sourcepub fn with_description(self, description: impl Into<String>) -> Self
pub fn with_description(self, description: impl Into<String>) -> Self
Set the server description.
Sourcepub fn mount<H: McpHandler>(self, handler: H, prefix: impl Into<String>) -> Self
pub fn mount<H: McpHandler>(self, handler: H, prefix: impl Into<String>) -> Self
Mount a handler with the given prefix.
All tools, resources, and prompts from the handler will be namespaced with the prefix.
§Panics
Panics if a handler with the same prefix is already mounted. This prevents silent shadowing of tools/resources/prompts which could lead to confusing runtime behavior.
§Example
ⓘ
let server = CompositeHandler::new("main", "1.0.0")
.mount(weather_handler, "weather")
.mount(news_handler, "news");Sourcepub fn try_mount<H: McpHandler>(
self,
handler: H,
prefix: impl Into<String>,
) -> Result<Self, String>
pub fn try_mount<H: McpHandler>( self, handler: H, prefix: impl Into<String>, ) -> Result<Self, String>
Sourcepub fn handler_count(&self) -> usize
pub fn handler_count(&self) -> usize
Get the number of mounted handlers.
Trait Implementations§
Source§impl Clone for CompositeHandler
impl Clone for CompositeHandler
Source§fn clone(&self) -> CompositeHandler
fn clone(&self) -> CompositeHandler
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CompositeHandler
impl Debug for CompositeHandler
Source§impl McpHandler for CompositeHandler
impl McpHandler for CompositeHandler
Source§fn server_info(&self) -> ServerInfo
fn server_info(&self) -> ServerInfo
Returns server information (name, version, description, etc.) Read more
Source§fn call_tool<'a>(
&'a self,
name: &'a str,
args: Value,
ctx: &'a RequestContext,
) -> impl Future<Output = McpResult<ToolResult>> + MaybeSend + 'a
fn call_tool<'a>( &'a self, name: &'a str, args: Value, ctx: &'a RequestContext, ) -> impl Future<Output = McpResult<ToolResult>> + MaybeSend + 'a
Calls a tool by name with the given arguments. Read more
Source§fn read_resource<'a>(
&'a self,
uri: &'a str,
ctx: &'a RequestContext,
) -> impl Future<Output = McpResult<ResourceResult>> + MaybeSend + 'a
fn read_resource<'a>( &'a self, uri: &'a str, ctx: &'a RequestContext, ) -> impl Future<Output = McpResult<ResourceResult>> + MaybeSend + 'a
Reads a resource by URI. Read more
Source§fn get_prompt<'a>(
&'a self,
name: &'a str,
args: Option<Value>,
ctx: &'a RequestContext,
) -> impl Future<Output = McpResult<PromptResult>> + MaybeSend + 'a
fn get_prompt<'a>( &'a self, name: &'a str, args: Option<Value>, ctx: &'a RequestContext, ) -> impl Future<Output = McpResult<PromptResult>> + MaybeSend + 'a
Gets a prompt by name with optional arguments. Read more
Source§fn list_tasks<'a>(
&'a self,
_cursor: Option<&'a str>,
_limit: Option<usize>,
_ctx: &'a RequestContext,
) -> impl Future<Output = Result<ListTasksResult, McpError>> + MaybeSend + 'a
fn list_tasks<'a>( &'a self, _cursor: Option<&'a str>, _limit: Option<usize>, _ctx: &'a RequestContext, ) -> impl Future<Output = Result<ListTasksResult, McpError>> + MaybeSend + 'a
Lists all active and recent tasks. Read more
Source§fn get_task<'a>(
&'a self,
_task_id: &'a str,
_ctx: &'a RequestContext,
) -> impl Future<Output = Result<Task, McpError>> + MaybeSend + 'a
fn get_task<'a>( &'a self, _task_id: &'a str, _ctx: &'a RequestContext, ) -> impl Future<Output = Result<Task, McpError>> + MaybeSend + 'a
Gets the current state of a specific task. Read more
Source§fn cancel_task<'a>(
&'a self,
_task_id: &'a str,
_ctx: &'a RequestContext,
) -> impl Future<Output = Result<Task, McpError>> + MaybeSend + 'a
fn cancel_task<'a>( &'a self, _task_id: &'a str, _ctx: &'a RequestContext, ) -> impl Future<Output = Result<Task, McpError>> + MaybeSend + 'a
Cancels a running task. Read more
Source§fn get_task_result<'a>(
&'a self,
_task_id: &'a str,
_ctx: &'a RequestContext,
) -> impl Future<Output = Result<Value, McpError>> + MaybeSend + 'a
fn get_task_result<'a>( &'a self, _task_id: &'a str, _ctx: &'a RequestContext, ) -> impl Future<Output = Result<Value, McpError>> + MaybeSend + 'a
Gets the result of a completed task. Read more
Auto Trait Implementations§
impl Freeze for CompositeHandler
impl !RefUnwindSafe for CompositeHandler
impl Send for CompositeHandler
impl Sync for CompositeHandler
impl Unpin for CompositeHandler
impl UnsafeUnpin for CompositeHandler
impl !UnwindSafe for CompositeHandler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> McpHandlerExt for Twhere
T: McpHandler,
impl<T> McpHandlerExt for Twhere
T: McpHandler,
Source§impl<T> McpServerExt for Twhere
T: McpHandler,
impl<T> McpServerExt for Twhere
T: McpHandler,
Source§fn builder(self) -> ServerBuilder<Self>
fn builder(self) -> ServerBuilder<Self>
Create a server builder for this handler. Read more