Skip to main content

rust_mcp_sdk/mcp_traits/
mcp_http_server.rs

1use crate::error::SdkResult;
2use crate::mcp_runtimes::server_runtime::ServerRuntime;
3use async_trait::async_trait;
4use rust_mcp_transport::SessionId;
5use std::sync::Arc;
6
7/// Common interface for running MCP servers over HTTP transports.
8///
9/// Implemented by framework-specific runtimes (e.g. `AxumRuntime` in `rust-mcp-axum`,
10/// `ActixRuntime` in `rust-mcp-actix`) to provide a uniform API for:
11///
12/// - Graceful shutdown
13/// - Session enumeration
14/// - Per-session runtime access
15///
16/// Users coding against `dyn McpHttpServer` can swap HTTP frameworks without
17/// changing their runtime interaction code.
18#[async_trait]
19pub trait McpHttpServer: Send + Sync {
20    /// Gracefully shuts down the server, waiting for in-flight requests to complete.
21    async fn graceful_shutdown(&self);
22
23    /// Returns all active session IDs on this server.
24    async fn sessions(&self) -> Vec<SessionId>;
25
26    /// Returns the runtime for a given session ID.
27    ///
28    /// Returns an error if the session does not exist or has been closed.
29    async fn runtime_by_session(&self, id: &SessionId) -> SdkResult<Arc<ServerRuntime>>;
30}