Skip to main content

Router

Struct Router 

Source
pub struct Router { /* private fields */ }
Expand description

Routes MCP requests to the appropriate handlers.

Implementations§

Source§

impl Router

Source

pub fn new() -> Self

Creates a new empty router.

Source

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.

Source

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.

Source

pub fn strict_input_validation(&self) -> bool

Returns whether strict input validation is enabled.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn add_resource_template(&mut self, template: ResourceTemplate)

Adds a resource template definition.

Source

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.

Source

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.

Source

pub fn tools(&self) -> Vec<Tool>

Returns all tool definitions.

Source

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.

Source

pub fn resources(&self) -> Vec<Resource>

Returns all resource definitions.

Source

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.

Source

pub fn resource_templates(&self) -> Vec<ResourceTemplate>

Returns all resource templates.

Source

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.

Source

pub fn prompts(&self) -> Vec<Prompt>

Returns all prompt definitions.

Source

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.

Source

pub fn tools_count(&self) -> usize

Returns the number of registered tools.

Source

pub fn resources_count(&self) -> usize

Returns the number of registered resources.

Source

pub fn resource_templates_count(&self) -> usize

Returns the number of registered resource templates.

Source

pub fn prompts_count(&self) -> usize

Returns the number of registered prompts.

Source

pub fn get_tool(&self, name: &str) -> Option<&Box<dyn ToolHandler>>

Gets a tool handler by name.

Source

pub fn get_resource(&self, uri: &str) -> Option<&Box<dyn ResourceHandler>>

Gets a resource handler by URI.

Source

pub fn get_resource_template( &self, uri_template: &str, ) -> Option<&ResourceTemplate>

Gets a resource template by URI template.

Source

pub fn resource_exists(&self, uri: &str) -> bool

Returns true if a resource exists for the given URI (static or template match).

Source

pub fn get_prompt(&self, name: &str) -> Option<&Box<dyn PromptHandler>>

Gets a prompt handler by name.

Source

pub fn handle_initialize( &self, _cx: &Cx, session: &mut Session, params: InitializeParams, instructions: Option<&str>, ) -> McpResult<InitializeResult>

Handles the initialize request.

Source

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.

Source

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 tracing
  • request_id - Internal request ID for tracking
  • params - The tool call parameters including tool name and arguments
  • budget - Request budget for timeout enforcement
  • session_state - Session state for per-session storage
  • notification_sender - Optional callback for sending progress notifications
  • bidirectional_senders - Optional senders for sampling/elicitation
Source

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.

Source

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.

Source

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 tracing
  • request_id - Internal request ID for tracking
  • params - The resource read parameters including URI
  • budget - Request budget for timeout enforcement
  • session_state - Session state for per-session storage
  • notification_sender - Optional callback for sending progress notifications
  • bidirectional_senders - Optional senders for sampling/elicitation
Source

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.

Source

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 tracing
  • request_id - Internal request ID for tracking
  • params - The prompt get parameters including name and arguments
  • budget - Request budget for timeout enforcement
  • session_state - Session state for per-session storage
  • notification_sender - Optional callback for sending progress notifications
  • bidirectional_senders - Optional senders for sampling/elicitation
Source

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.

Source

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.

Source

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.

Source

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

Source

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"
Source

pub fn mount_tools( &mut self, other: Router, prefix: Option<&str>, ) -> MountResult

Mounts only tools from a router.

Source

pub fn mount_resources( &mut self, other: Router, prefix: Option<&str>, ) -> MountResult

Mounts only resources from a router.

Source

pub fn mount_prompts( &mut self, other: Router, prefix: Option<&str>, ) -> MountResult

Mounts only prompts from a router.

Trait Implementations§

Source§

impl Default for Router

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for Router

§

impl !RefUnwindSafe for Router

§

impl Send for Router

§

impl Sync for Router

§

impl Unpin for Router

§

impl !UnwindSafe for Router

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<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: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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