Skip to main content

AppRouter

Struct AppRouter 

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

Source

pub fn new() -> Self

Construct an empty registration set without performing external effects.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<S> Default for AppRouter<S>

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<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, 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