pub struct AppRouter<S> { /* private fields */ }Expand description
An rmcp tool router and exact-URI registry of HTML resources.
Use this directly in a custom rmcp::ServerHandler when that handler owns
authorization, other resources, prompts, tasks or custom lifecycle behavior.
Request dispatch uses the existing rmcp tool framework. The application state
and callbacks must be Send + Sync; this API does not promise rmcp’s relaxed
local callback support. Imported tools require Self::validate after all
resources have been registered and before serving.
Implementations§
Source§impl<S: Send + Sync + 'static> AppRouter<S>
impl<S: Send + Sync + 'static> AppRouter<S>
Sourcepub fn from_tool_router(tools: ToolRouter<S>) -> Self
pub fn from_tool_router(tools: ToolRouter<S>) -> Self
Adopt rmcp routes, including their disabled markers and notifier.
This does not validate their existing UI associations: register the
corresponding resources and call Self::validate before serving.
Duplicate names already overwritten by rmcp cannot be recovered here.
Sourcepub fn register_resource(
&mut self,
route: UiResourceRoute<S>,
) -> Result<(), RegistrationError>
pub fn register_resource( &mut self, route: UiResourceRoute<S>, ) -> Result<(), RegistrationError>
Register a static or dynamic resource by its exact validated URI.
The callback is not executed. No name restrictions, normalization, asset loading or HTML validation are introduced.
§Errors
Returns RegistrationError::DuplicateResource without replacing the
existing route when the URI is already registered.
Sourcepub fn register_static<R: StaticUiResource>(
&mut self,
) -> Result<(), RegistrationError>
pub fn register_static<R: StaticUiResource>( &mut self, ) -> Result<(), RegistrationError>
Register a statically defined UI resource and its complete content metadata.
Composes StaticUiResource::ui with StaticUiResource::meta, preserving
unrelated extension data and rejecting conflicting declarations.
§Errors
Propagates URI and metadata errors from StaticUiResource, or returns
RegistrationError::DuplicateResource for an existing URI. Nothing is
inserted until the complete definition has been prepared successfully.
Sourcepub fn ensure_static<R: StaticUiResource>(
&mut self,
) -> Result<UiResourceUri, RegistrationError>
pub fn ensure_static<R: StaticUiResource>( &mut self, ) -> Result<UiResourceUri, RegistrationError>
Register or reuse an identical static declaration and return its URI.
Application macros use this when several tools share a view. Reuse requires
equal descriptors, HTML and prepared content metadata, even when different
Rust types or type aliases declare the same URI. A dynamic route cannot be
reused as a static declaration. No callback or filesystem access occurs.
Open metadata and typed UI settings are composed as in Self::register_static.
Unlike Self::register_static, an identical registration is accepted.
§Errors
Propagates URI and metadata errors while preparing the declaration, or
returns RegistrationError::ConflictingResource if the existing route
differs. Failure leaves the existing registration unchanged.
Sourcepub fn register_tool<R, A>(&mut self, route: R) -> Result<(), RegistrationError>where
R: IntoToolRoute<S, A>,
pub fn register_tool<R, A>(&mut self, route: R) -> Result<(), RegistrationError>where
R: IntoToolRoute<S, A>,
Register an existing rmcp route without changing its handler or schema.
Associated resources must already be registered. Tool factory functions,
rmcp::handler::server::router::tool::ToolRoute and rmcp’s other
IntoToolRoute implementations are accepted directly.
§Errors
Rejects duplicate names, including disabled routes, and malformed, conflicting or unresolved UI associations before inserting anything.
Sourcepub fn associate_tool(
&mut self,
name: &str,
ui: &ToolUi,
) -> Result<(), RegistrationError>
pub fn associate_tool( &mut self, name: &str, ui: &ToolUi, ) -> Result<(), RegistrationError>
Associate a registered tool with an already registered resource.
Preserves its handler, schemas, annotations and unrelated metadata. A disabled route can be associated without re-enabling it. This operation declares UI visibility; it does not enforce access control.
§Errors
Returns RegistrationError::UnknownTool,
RegistrationError::MissingResourceUri,
RegistrationError::UnregisteredResource or the foundation’s atomic
metadata error, leaving the existing tool unchanged.
Sourcepub fn associate_static_tool<R: StaticUiResource>(
&mut self,
name: &str,
) -> Result<(), RegistrationError>
pub fn associate_static_tool<R: StaticUiResource>( &mut self, name: &str, ) -> Result<(), RegistrationError>
Associate a tool with an already registered static resource definition.
§Errors
Propagates the definition’s URI error and the same registration or metadata
errors as Self::associate_tool. It does not register the resource.
Sourcepub fn merge(&mut self, other: Self) -> Result<(), RegistrationError>
pub fn merge(&mut self, other: Self) -> Result<(), RegistrationError>
Merge another registration set after checking every name and URI collision.
Both registration sets must have matching tool lookup keys and declared names. No destination route changes if an identity mismatch or collision exists, even when a resource collision is discovered after checking tools. Disabled routes still reserve their names. A source disabled-name marker without a route also conflicts with an existing destination tool; it cannot implicitly hide that tool. Destination markers for future tools and disabled source routes retain rmcp’s behavior. The destination notifier is preserved; the source notifier is discarded, matching rmcp’s merge behavior. No notification is emitted by this assembly operation. Validate the assembled associations before serving.
§Errors
Returns RegistrationError::ToolNameMismatch,
RegistrationError::DuplicateTool or
RegistrationError::DuplicateResource without changing the destination.
The source is consumed even on error.
Sourcepub fn validate(&self) -> Result<(), RegistrationError>
pub fn validate(&self) -> Result<(), RegistrationError>
Verify every existing tool UI association without mutating registration.
Checks disabled routes too, so re-enabling an imported route cannot reveal
an unchecked association. Both nested ui.resourceUri and deprecated
ui/resourceUri declarations must be strings, valid UI URIs, agree when
both exist and refer to a registered resource. Unknown metadata is retained.
Ordinary tools without an association are valid. Imported rmcp map keys
must agree with their declarations so listing, lookup and dispatch use
the same tool identity.
§Errors
Returns URI, metadata, malformed-association, tool-name mismatch or missing-resource errors. It does not establish HTML validity, authorization or host compatibility.
Sourcepub fn get_tool(&self, name: &str) -> Option<Tool>
pub fn get_tool(&self, name: &str) -> Option<Tool>
Return an enabled tool’s declaration, including its composed UI metadata.
Disabled and unknown tools return None, matching rmcp routing behavior.
Custom handlers should use this for ServerHandler::get_tool as well as
this router’s list and call operations.
Sourcepub fn list_tools(
&self,
request: Option<PaginatedRequestParams>,
) -> Result<ListToolsResult, ErrorData>
pub fn list_tools( &self, request: Option<PaginatedRequestParams>, ) -> Result<ListToolsResult, ErrorData>
List all enabled tools, sorted by name, without pagination.
The result has no next cursor and advertises zero cache lifetime with private cache scope. A custom handler owns any per-user filtering.
§Errors
Returns an invalid-parameters error for any supplied cursor; this router cannot interpret another handler’s pagination state.
Sourcepub fn list_resources(
&self,
request: Option<PaginatedRequestParams>,
) -> Result<ListResourcesResult, ErrorData>
pub fn list_resources( &self, request: Option<PaginatedRequestParams>, ) -> Result<ListResourcesResult, ErrorData>
List registered resources sorted by exact URI, without invoking callbacks.
The result has no next cursor and advertises zero cache lifetime with private cache scope. Resource security metadata is returned on HTML content.
§Errors
Returns an invalid-parameters error for any supplied cursor. A custom handler must define its own pagination when combining other resource sets.
Sourcepub async fn call_tool(
&self,
state: &S,
request: CallToolRequestParams,
context: RequestContext<RoleServer>,
) -> Result<CallToolResponse, ErrorData>
pub async fn call_tool( &self, state: &S, request: CallToolRequestParams, context: RequestContext<RoleServer>, ) -> Result<CallToolResponse, ErrorData>
Invoke rmcp’s existing tool route with the original request and context.
Parameter extraction, tool execution, MRTR input responses and errors retain rmcp’s behavior. The state is borrowed for the call. The consuming handler remains responsible for any authorization before dispatch or within a tool.
§Errors
Propagates rmcp routing or handler errors without inventing fallback routes.
Sourcepub async fn read_resource(
&self,
state: &S,
request: ReadResourceRequestParams,
context: RequestContext<RoleServer>,
) -> Result<ReadResourceResponse, ErrorData>
pub async fn read_resource( &self, state: &S, request: ReadResourceRequestParams, context: RequestContext<RoleServer>, ) -> Result<ReadResourceResponse, ErrorData>
Read matching static HTML or invoke its dynamic callback exactly once.
Successful content receives the registered URI, MCP Apps MIME type and immutable prepared metadata. The callback receives the original context. The state is borrowed; no runtime or background task is created. Complete results have zero cache lifetime and private cache scope.
§Errors
Returns a resource-not-found error before invoking any callback for an unknown URI. Callback errors pass through unchanged. rmcp may apply its normal version-dependent error mapping when dispatching protocol requests.
Sourcepub fn contains_resource(&self, uri: &str) -> bool
pub fn contains_resource(&self, uri: &str) -> bool
Check exact resource identity before a custom handler chooses dispatch.
No normalization, resource read or authorization occurs. A positive result means registration exists, not that the caller is permitted to read it.
Sourcepub fn declare_capabilities(&self, capabilities: &mut ServerCapabilities)
pub fn declare_capabilities(&self, capabilities: &mut ServerCapabilities)
Advertise capabilities implemented by registered routes, preserving settings.
Enables tools and resources only when their registration sets are nonempty, and the UI extension when resources exist. Existing capabilities, extension settings and notifications flags are left intact. This declares support; it does not implement callbacks for other capabilities supplied by a server.