1+ //! Session management for the Streamable HTTP transport.
2+ //!
3+ //! A *session* groups the logically related interactions between a single MCP
4+ //! client and the server, starting from the `initialize` handshake. The server
5+ //! assigns each session a unique [`SessionId`] (returned to the client via the
6+ //! `Mcp-Session-Id` response header) and the client includes that ID on every
7+ //! subsequent request.
8+ //!
9+ //! Two tool calls carrying the same session ID come from the same logical
10+ //! session; different IDs mean different clients or conversations.
11+ //!
12+ //! # Implementations
13+ //!
14+ //! * [`local::LocalSessionManager`] — in-memory session store (default).
15+ //! * [`never::NeverSessionManager`] — rejects all session operations, used
16+ //! when stateful mode is disabled.
17+ //!
18+ //! # Custom session managers
19+ //!
20+ //! Implement the [`SessionManager`] trait to back sessions with a database,
21+ //! Redis, or any other external store.
22+
123use futures:: Stream ;
224
325pub use crate :: transport:: common:: server_side_http:: { ServerSseMessage , SessionId } ;
@@ -9,40 +31,66 @@ use crate::{
931pub mod local;
1032pub mod never;
1133
34+ /// Controls how MCP sessions are created, validated, and closed.
35+ ///
36+ /// The [`StreamableHttpService`](super::StreamableHttpService) calls into this
37+ /// trait for every HTTP request that carries (or should carry) a session ID.
38+ ///
39+ /// See the [module-level docs](self) for background on sessions.
1240pub trait SessionManager : Send + Sync + ' static {
1341 type Error : std:: error:: Error + Send + ' static ;
1442 type Transport : crate :: transport:: Transport < RoleServer > ;
15- /// Create a new session with the given id and configuration.
43+
44+ /// Create a new session and return its ID together with the transport
45+ /// that will be used to exchange MCP messages within this session.
1646 fn create_session (
1747 & self ,
1848 ) -> impl Future < Output = Result < ( SessionId , Self :: Transport ) , Self :: Error > > + Send ;
49+
50+ /// Forward the first message (the `initialize` request) to the session.
1951 fn initialize_session (
2052 & self ,
2153 id : & SessionId ,
2254 message : ClientJsonRpcMessage ,
2355 ) -> impl Future < Output = Result < ServerJsonRpcMessage , Self :: Error > > + Send ;
56+
57+ /// Return `true` if a session with the given ID exists and is active.
2458 fn has_session ( & self , id : & SessionId )
2559 -> impl Future < Output = Result < bool , Self :: Error > > + Send ;
60+
61+ /// Close and remove the session. Corresponds to an HTTP DELETE request
62+ /// with `Mcp-Session-Id`.
2663 fn close_session ( & self , id : & SessionId )
2764 -> impl Future < Output = Result < ( ) , Self :: Error > > + Send ;
65+
66+ /// Route a client request into the session and return an SSE stream
67+ /// carrying the server's response(s).
2868 fn create_stream (
2969 & self ,
3070 id : & SessionId ,
3171 message : ClientJsonRpcMessage ,
3272 ) -> impl Future <
3373 Output = Result < impl Stream < Item = ServerSseMessage > + Send + Sync + ' static , Self :: Error > ,
3474 > + Send ;
75+
76+ /// Accept a notification, response, or error message from the client
77+ /// without producing a response stream.
3578 fn accept_message (
3679 & self ,
3780 id : & SessionId ,
3881 message : ClientJsonRpcMessage ,
3982 ) -> impl Future < Output = Result < ( ) , Self :: Error > > + Send ;
83+
84+ /// Create an SSE stream not tied to a specific client request (HTTP GET).
4085 fn create_standalone_stream (
4186 & self ,
4287 id : & SessionId ,
4388 ) -> impl Future <
4489 Output = Result < impl Stream < Item = ServerSseMessage > + Send + Sync + ' static , Self :: Error > ,
4590 > + Send ;
91+
92+ /// Resume an SSE stream from the given `Last-Event-ID`, replaying any
93+ /// events the client missed.
4694 fn resume (
4795 & self ,
4896 id : & SessionId ,
0 commit comments