pub struct Router { /* private fields */ }Expand description
Routes MCP requests to the appropriate handlers.
Implementations§
Source§impl Router
impl Router
Sourcepub fn set_list_page_size(&mut self, page_size: Option<usize>)
pub fn set_list_page_size(&mut self, page_size: Option<usize>)
Sets the list pagination page size.
When set, list methods (tools/list, resources/list, resources/templates/list,
prompts/list, tasks/list) will page results using opaque base64 cursors.
Sourcepub fn set_strict_input_validation(&mut self, strict: bool)
pub fn set_strict_input_validation(&mut self, strict: bool)
Sets whether to use strict input validation.
When enabled, tool input validation will reject any properties not
explicitly defined in the tool’s input schema (enforces additionalProperties: false).
When disabled (default), extra properties are allowed unless the schema
explicitly sets additionalProperties: false.
Sourcepub fn strict_input_validation(&self) -> bool
pub fn strict_input_validation(&self) -> bool
Returns whether strict input validation is enabled.
Sourcepub fn add_tool<H: ToolHandler + 'static>(&mut self, handler: H)
pub fn add_tool<H: ToolHandler + 'static>(&mut self, handler: H)
Adds a tool handler.
If a tool with the same name already exists, it will be replaced.
Use add_tool_with_behavior for
finer control over duplicate handling.
Sourcepub fn add_tool_with_behavior<H: ToolHandler + 'static>(
&mut self,
handler: H,
behavior: DuplicateBehavior,
) -> Result<(), McpError>
pub fn add_tool_with_behavior<H: ToolHandler + 'static>( &mut self, handler: H, behavior: DuplicateBehavior, ) -> Result<(), McpError>
Adds a tool handler with specified duplicate behavior.
Returns Err if behavior is [DuplicateBehavior::Error] and the
tool name already exists.
Sourcepub fn add_resource<H: ResourceHandler + 'static>(&mut self, handler: H)
pub fn add_resource<H: ResourceHandler + 'static>(&mut self, handler: H)
Adds a resource handler.
If a resource with the same URI already exists, it will be replaced.
Use add_resource_with_behavior for
finer control over duplicate handling.
Sourcepub fn add_resource_with_behavior<H: ResourceHandler + 'static>(
&mut self,
handler: H,
behavior: DuplicateBehavior,
) -> Result<(), McpError>
pub fn add_resource_with_behavior<H: ResourceHandler + 'static>( &mut self, handler: H, behavior: DuplicateBehavior, ) -> Result<(), McpError>
Adds a resource handler with specified duplicate behavior.
Returns Err if behavior is [DuplicateBehavior::Error] and the
resource URI already exists.
Sourcepub fn add_resource_template(&mut self, template: ResourceTemplate)
pub fn add_resource_template(&mut self, template: ResourceTemplate)
Adds a resource template definition.
Sourcepub fn add_prompt<H: PromptHandler + 'static>(&mut self, handler: H)
pub fn add_prompt<H: PromptHandler + 'static>(&mut self, handler: H)
Adds a prompt handler.
If a prompt with the same name already exists, it will be replaced.
Use add_prompt_with_behavior for
finer control over duplicate handling.
Sourcepub fn add_prompt_with_behavior<H: PromptHandler + 'static>(
&mut self,
handler: H,
behavior: DuplicateBehavior,
) -> Result<(), McpError>
pub fn add_prompt_with_behavior<H: PromptHandler + 'static>( &mut self, handler: H, behavior: DuplicateBehavior, ) -> Result<(), McpError>
Adds a prompt handler with specified duplicate behavior.
Returns Err if behavior is [DuplicateBehavior::Error] and the
prompt name already exists.
Sourcepub fn tools_filtered(
&self,
session_state: Option<&SessionState>,
tag_filters: Option<&TagFilters<'_>>,
) -> Vec<Tool>
pub fn tools_filtered( &self, session_state: Option<&SessionState>, tag_filters: Option<&TagFilters<'_>>, ) -> Vec<Tool>
Returns tool definitions filtered by session state and tags.
Tools that have been disabled in the session state will not be included. If tag filters are provided, tools must match the include/exclude criteria.
Sourcepub fn resources_filtered(
&self,
session_state: Option<&SessionState>,
tag_filters: Option<&TagFilters<'_>>,
) -> Vec<Resource>
pub fn resources_filtered( &self, session_state: Option<&SessionState>, tag_filters: Option<&TagFilters<'_>>, ) -> Vec<Resource>
Returns resource definitions filtered by session state and tags.
Resources that have been disabled in the session state will not be included. If tag filters are provided, resources must match the include/exclude criteria.
Sourcepub fn resource_templates(&self) -> Vec<ResourceTemplate>
pub fn resource_templates(&self) -> Vec<ResourceTemplate>
Returns all resource templates.
Sourcepub fn resource_templates_filtered(
&self,
session_state: Option<&SessionState>,
tag_filters: Option<&TagFilters<'_>>,
) -> Vec<ResourceTemplate>
pub fn resource_templates_filtered( &self, session_state: Option<&SessionState>, tag_filters: Option<&TagFilters<'_>>, ) -> Vec<ResourceTemplate>
Returns resource templates filtered by session state and tags.
Templates that have been disabled in the session state will not be included. If tag filters are provided, templates must match the include/exclude criteria.
Sourcepub fn prompts_filtered(
&self,
session_state: Option<&SessionState>,
tag_filters: Option<&TagFilters<'_>>,
) -> Vec<Prompt>
pub fn prompts_filtered( &self, session_state: Option<&SessionState>, tag_filters: Option<&TagFilters<'_>>, ) -> Vec<Prompt>
Returns prompt definitions filtered by session state and tags.
Prompts that have been disabled in the session state will not be included. If tag filters are provided, prompts must match the include/exclude criteria.
Sourcepub fn tools_count(&self) -> usize
pub fn tools_count(&self) -> usize
Returns the number of registered tools.
Sourcepub fn resources_count(&self) -> usize
pub fn resources_count(&self) -> usize
Returns the number of registered resources.
Sourcepub fn resource_templates_count(&self) -> usize
pub fn resource_templates_count(&self) -> usize
Returns the number of registered resource templates.
Sourcepub fn prompts_count(&self) -> usize
pub fn prompts_count(&self) -> usize
Returns the number of registered prompts.
Sourcepub fn get_tool(&self, name: &str) -> Option<&Box<dyn ToolHandler>>
pub fn get_tool(&self, name: &str) -> Option<&Box<dyn ToolHandler>>
Gets a tool handler by name.
Sourcepub fn get_resource(&self, uri: &str) -> Option<&Box<dyn ResourceHandler>>
pub fn get_resource(&self, uri: &str) -> Option<&Box<dyn ResourceHandler>>
Gets a resource handler by URI.
Sourcepub fn get_resource_template(
&self,
uri_template: &str,
) -> Option<&ResourceTemplate>
pub fn get_resource_template( &self, uri_template: &str, ) -> Option<&ResourceTemplate>
Gets a resource template by URI template.
Sourcepub fn resource_exists(&self, uri: &str) -> bool
pub fn resource_exists(&self, uri: &str) -> bool
Returns true if a resource exists for the given URI (static or template match).
Sourcepub fn get_prompt(&self, name: &str) -> Option<&Box<dyn PromptHandler>>
pub fn get_prompt(&self, name: &str) -> Option<&Box<dyn PromptHandler>>
Gets a prompt handler by name.
Sourcepub fn handle_initialize(
&self,
_cx: &Cx,
session: &mut Session,
params: InitializeParams,
instructions: Option<&str>,
) -> McpResult<InitializeResult>
pub fn handle_initialize( &self, _cx: &Cx, session: &mut Session, params: InitializeParams, instructions: Option<&str>, ) -> McpResult<InitializeResult>
Handles the initialize request.
Sourcepub fn handle_tools_list(
&self,
_cx: &Cx,
params: ListToolsParams,
session_state: Option<&SessionState>,
) -> McpResult<ListToolsResult>
pub fn handle_tools_list( &self, _cx: &Cx, params: ListToolsParams, session_state: Option<&SessionState>, ) -> McpResult<ListToolsResult>
Handles the tools/list request.
If session_state is provided, disabled tools will be filtered out. If include_tags/exclude_tags are provided, tools are filtered by tags.
Sourcepub fn handle_tools_call(
&self,
cx: &Cx,
request_id: u64,
params: CallToolParams,
budget: &Budget,
session_state: SessionState,
notification_sender: Option<&NotificationSender>,
bidirectional_senders: Option<&BidirectionalSenders>,
) -> McpResult<CallToolResult>
pub fn handle_tools_call( &self, cx: &Cx, request_id: u64, params: CallToolParams, budget: &Budget, session_state: SessionState, notification_sender: Option<&NotificationSender>, bidirectional_senders: Option<&BidirectionalSenders>, ) -> McpResult<CallToolResult>
Handles the tools/call request.
§Arguments
cx- The asupersync context for cancellation and tracingrequest_id- Internal request ID for trackingparams- The tool call parameters including tool name and argumentsbudget- Request budget for timeout enforcementsession_state- Session state for per-session storagenotification_sender- Optional callback for sending progress notificationsbidirectional_senders- Optional senders for sampling/elicitation
Sourcepub fn handle_resources_list(
&self,
_cx: &Cx,
params: ListResourcesParams,
session_state: Option<&SessionState>,
) -> McpResult<ListResourcesResult>
pub fn handle_resources_list( &self, _cx: &Cx, params: ListResourcesParams, session_state: Option<&SessionState>, ) -> McpResult<ListResourcesResult>
Handles the resources/list request.
If session_state is provided, disabled resources will be filtered out. If include_tags/exclude_tags are provided, resources are filtered by tags.
Sourcepub fn handle_resource_templates_list(
&self,
_cx: &Cx,
params: ListResourceTemplatesParams,
session_state: Option<&SessionState>,
) -> McpResult<ListResourceTemplatesResult>
pub fn handle_resource_templates_list( &self, _cx: &Cx, params: ListResourceTemplatesParams, session_state: Option<&SessionState>, ) -> McpResult<ListResourceTemplatesResult>
Handles the resources/templates/list request.
If session_state is provided, disabled resource templates will be filtered out. If include_tags/exclude_tags are provided, templates are filtered by tags.
Sourcepub fn handle_resources_read(
&self,
cx: &Cx,
request_id: u64,
params: &ReadResourceParams,
budget: &Budget,
session_state: SessionState,
notification_sender: Option<&NotificationSender>,
bidirectional_senders: Option<&BidirectionalSenders>,
) -> McpResult<ReadResourceResult>
pub fn handle_resources_read( &self, cx: &Cx, request_id: u64, params: &ReadResourceParams, budget: &Budget, session_state: SessionState, notification_sender: Option<&NotificationSender>, bidirectional_senders: Option<&BidirectionalSenders>, ) -> McpResult<ReadResourceResult>
Handles the resources/read request.
§Arguments
cx- The asupersync context for cancellation and tracingrequest_id- Internal request ID for trackingparams- The resource read parameters including URIbudget- Request budget for timeout enforcementsession_state- Session state for per-session storagenotification_sender- Optional callback for sending progress notificationsbidirectional_senders- Optional senders for sampling/elicitation
Sourcepub fn handle_prompts_list(
&self,
_cx: &Cx,
params: ListPromptsParams,
session_state: Option<&SessionState>,
) -> McpResult<ListPromptsResult>
pub fn handle_prompts_list( &self, _cx: &Cx, params: ListPromptsParams, session_state: Option<&SessionState>, ) -> McpResult<ListPromptsResult>
Handles the prompts/list request.
If session_state is provided, disabled prompts will be filtered out. If include_tags/exclude_tags are provided, prompts are filtered by tags.
Sourcepub fn handle_prompts_get(
&self,
cx: &Cx,
request_id: u64,
params: GetPromptParams,
budget: &Budget,
session_state: SessionState,
notification_sender: Option<&NotificationSender>,
bidirectional_senders: Option<&BidirectionalSenders>,
) -> McpResult<GetPromptResult>
pub fn handle_prompts_get( &self, cx: &Cx, request_id: u64, params: GetPromptParams, budget: &Budget, session_state: SessionState, notification_sender: Option<&NotificationSender>, bidirectional_senders: Option<&BidirectionalSenders>, ) -> McpResult<GetPromptResult>
Handles the prompts/get request.
§Arguments
cx- The asupersync context for cancellation and tracingrequest_id- Internal request ID for trackingparams- The prompt get parameters including name and argumentsbudget- Request budget for timeout enforcementsession_state- Session state for per-session storagenotification_sender- Optional callback for sending progress notificationsbidirectional_senders- Optional senders for sampling/elicitation
Sourcepub fn handle_tasks_list(
&self,
_cx: &Cx,
params: ListTasksParams,
task_manager: Option<&SharedTaskManager>,
) -> McpResult<ListTasksResult>
pub fn handle_tasks_list( &self, _cx: &Cx, params: ListTasksParams, task_manager: Option<&SharedTaskManager>, ) -> McpResult<ListTasksResult>
Handles the tasks/list request.
Lists all background tasks, optionally filtered by status.
Sourcepub fn handle_tasks_get(
&self,
_cx: &Cx,
params: GetTaskParams,
task_manager: Option<&SharedTaskManager>,
) -> McpResult<GetTaskResult>
pub fn handle_tasks_get( &self, _cx: &Cx, params: GetTaskParams, task_manager: Option<&SharedTaskManager>, ) -> McpResult<GetTaskResult>
Handles the tasks/get request.
Gets information about a specific task, including its result if completed.
Sourcepub fn handle_tasks_cancel(
&self,
_cx: &Cx,
params: CancelTaskParams,
task_manager: Option<&SharedTaskManager>,
) -> McpResult<CancelTaskResult>
pub fn handle_tasks_cancel( &self, _cx: &Cx, params: CancelTaskParams, task_manager: Option<&SharedTaskManager>, ) -> McpResult<CancelTaskResult>
Handles the tasks/cancel request.
Requests cancellation of a running or pending task.
Sourcepub fn handle_tasks_submit(
&self,
cx: &Cx,
params: SubmitTaskParams,
task_manager: Option<&SharedTaskManager>,
) -> McpResult<SubmitTaskResult>
pub fn handle_tasks_submit( &self, cx: &Cx, params: SubmitTaskParams, task_manager: Option<&SharedTaskManager>, ) -> McpResult<SubmitTaskResult>
Handles the tasks/submit request.
Submits a new background task for execution.
Source§impl Router
impl Router
Sourcepub fn mount(&mut self, other: Router, prefix: Option<&str>) -> MountResult
pub fn mount(&mut self, other: Router, prefix: Option<&str>) -> MountResult
Mounts all handlers from another router with an optional prefix.
This consumes the source router and moves its handlers into this router.
Names/URIs are prefixed with prefix/ if a prefix is provided.
§Example
let mut main_router = Router::new();
let db_router = Router::new();
// ... add handlers to db_router ...
main_router.mount(db_router, Some("db"));
// Tool "query" becomes "db/query"Sourcepub fn mount_tools(
&mut self,
other: Router,
prefix: Option<&str>,
) -> MountResult
pub fn mount_tools( &mut self, other: Router, prefix: Option<&str>, ) -> MountResult
Mounts only tools from a router.
Sourcepub fn mount_resources(
&mut self,
other: Router,
prefix: Option<&str>,
) -> MountResult
pub fn mount_resources( &mut self, other: Router, prefix: Option<&str>, ) -> MountResult
Mounts only resources from a router.
Sourcepub fn mount_prompts(
&mut self,
other: Router,
prefix: Option<&str>,
) -> MountResult
pub fn mount_prompts( &mut self, other: Router, prefix: Option<&str>, ) -> MountResult
Mounts only prompts from a router.