Skip to main content

mcp_apps_server/
server.rs

1use rmcp::{ErrorData, RoleServer, ServerHandler, model::*, service::RequestContext};
2
3use crate::{AppRouter, RegistrationError};
4
5/// A ready-to-serve MCP server for a prepared application router and its state.
6///
7/// Requires the `server` feature. The router supplies tools and UI resources;
8/// rmcp supplies protocol handling. The consumer supplies the state, runtime and
9/// transport, and performs authorization inside tool/resource callbacks.
10///
11/// This adapter implements tool/resource listing, tool execution, resource reads
12/// and matching capabilities. For prompts, tasks, custom notifications, filtered
13/// listings or an existing server's policies, invoke [`AppRouter`] from your own
14/// [`ServerHandler`] instead. It does not wrap or forward an existing handler.
15/// Routes and declarations are fixed at construction; no list-change capability
16/// or resource subscription is advertised.
17pub struct AppServer<S> {
18    state: S,
19    router: AppRouter<S>,
20    info: ServerConfig,
21}
22
23impl<S: Send + Sync + 'static> AppServer<S> {
24    /// Validate all associations and construct the server with matching capabilities.
25    ///
26    /// Returns a [`RegistrationError`] if a UI tool refers to an unregistered or
27    /// invalid resource. No request is executed and no transport is started.
28    pub fn new(state: S, router: AppRouter<S>) -> Result<Self, RegistrationError> {
29        router.validate()?;
30        let mut info = ServerConfig::default();
31        router.declare_capabilities(&mut info.capabilities);
32        Ok(Self {
33            state,
34            router,
35            info,
36        })
37    }
38
39    /// Set the consuming server's identity, rather than the library's identity.
40    pub fn with_server_info(mut self, implementation: Implementation) -> Self {
41        self.info.server_info = implementation;
42        self
43    }
44
45    /// Add instructions that rmcp returns with this server's information.
46    pub fn with_instructions(mut self, instructions: impl Into<String>) -> Self {
47        self.info.instructions = Some(instructions.into());
48        self
49    }
50
51    /// Borrow the consumer state used by tool and resource callbacks.
52    pub fn state(&self) -> &S {
53        &self.state
54    }
55
56    /// Borrow the prepared router without exposing mutable registration state.
57    pub fn router(&self) -> &AppRouter<S> {
58        &self.router
59    }
60}
61
62impl<S: Send + Sync + 'static> ServerHandler for AppServer<S> {
63    fn get_info(&self) -> ServerConfig {
64        self.info.clone()
65    }
66
67    fn get_tool(&self, name: &str) -> Option<Tool> {
68        self.router.get_tool(name)
69    }
70
71    async fn list_tools(
72        &self,
73        request: Option<PaginatedRequestParams>,
74        _: RequestContext<RoleServer>,
75    ) -> Result<ListToolsResult, ErrorData> {
76        self.router.list_tools(request)
77    }
78
79    async fn call_tool(
80        &self,
81        request: CallToolRequestParams,
82        context: RequestContext<RoleServer>,
83    ) -> Result<CallToolResponse, ErrorData> {
84        self.router.call_tool(&self.state, request, context).await
85    }
86
87    async fn list_resources(
88        &self,
89        request: Option<PaginatedRequestParams>,
90        _: RequestContext<RoleServer>,
91    ) -> Result<ListResourcesResult, ErrorData> {
92        self.router.list_resources(request)
93    }
94
95    async fn read_resource(
96        &self,
97        request: ReadResourceRequestParams,
98        context: RequestContext<RoleServer>,
99    ) -> Result<ReadResourceResponse, ErrorData> {
100        self.router
101            .read_resource(&self.state, request, context)
102            .await
103    }
104}