Summary
BaseSession holds all session state in-memory (_response_streams, _in_flight, _request_id counter, _server_capabilities). This state cannot be serialized or shared across process boundaries, making it difficult to run MCP services behind a load balancer or in horizontally-scaled deployments.
Motivation
- Horizontally-scaled MCP services need to share session state across instances
- When a response arrives at a different instance than the one that sent the request, there is no way to route it correctly without reimplementing session internals
- As the protocol transitions from stateful (current spec) to stateless-by-default (upcoming spec), servers will need to support both old and new clients simultaneously. Serializable session state would help bridge this transition period, allowing stateful sessions to be managed across a distributed backend while also serving stateless clients.
Proposed Approach
Provide a lightweight SessionState snapshot with the minimal serializable fields needed to reconstruct session context:
session_id
protocol_version
next_request_id
server_capabilities
server_info
initialized_sent
Expose get_session_state() and from_session_state() methods on ClientSession, allowing consumers to store and restore state using whatever backing store they choose (Redis, database, etc.).
Related
Summary
BaseSessionholds all session state in-memory (_response_streams,_in_flight,_request_idcounter,_server_capabilities). This state cannot be serialized or shared across process boundaries, making it difficult to run MCP services behind a load balancer or in horizontally-scaled deployments.Motivation
Proposed Approach
Provide a lightweight
SessionStatesnapshot with the minimal serializable fields needed to reconstruct session context:session_idprotocol_versionnext_request_idserver_capabilitiesserver_infoinitialized_sentExpose
get_session_state()andfrom_session_state()methods onClientSession, allowing consumers to store and restore state using whatever backing store they choose (Redis, database, etc.).Related