feat: optional session store (resumabillity support)#775
Open
glicht wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
feat: optional session store (resumabillity support)#775glicht wants to merge 2 commits intomodelcontextprotocol:mainfrom
glicht wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
In horizontally-scaled deployments or after restarting a server, MCP clients use
Mcp-Session-Idto resume sessions. When routed to a different server instance or after a restart, the session is unknown and clients get a 404, forcing re-initialization.This PR adds an optional SessionStore trait that persists InitializeRequestParams after a successful handshake. When a request arrives at an instance without a matching in-memory session, the store is consulted and the session is transparently restored by replaying initialize.
The
SessionManagertrait gains arestore_sessionmethod with a default no-op implementation. Custom session manager implementations can override it to integrate with their own logic if needed. The built-inLocalSessionManagerhas an implementation that re-creates the in-memory session worker.Additionally to provide indication to
ServerHandlerimplementation that a call to initialize and on_initialized is as a result of a restore, a markerSessionRestoreMarkeris added to the context.extensions so implementors can act appropriately when a session is restored.The feature is opt-in. Configurations without a session_store configured are unaffected.
Note: for full resumability and multi-server (behind a load balancer) support there is need to also implement an Event store so events aren't lost. This is being discussed at: #330
How Has This Been Tested?
Added a new integration test suite at:
test_streamable_http_session_store.rswith three test scenarios.Breaking Changes
None. Existing logic remains if session_store is not configured (defaults to None).
Types of changes
Checklist
Additional context
Related to #330