Skip to content

Comments

feat: add serializable session state for distributed deployments (#2111)#2126

Open
gspeter-max wants to merge 7 commits intomodelcontextprotocol:mainfrom
gspeter-max:python-sdk/issue_2111
Open

feat: add serializable session state for distributed deployments (#2111)#2126
gspeter-max wants to merge 7 commits intomodelcontextprotocol:mainfrom
gspeter-max:python-sdk/issue_2111

Conversation

@gspeter-max
Copy link

Summary

This PR adds serializable session state to enable distributed deployments of the MCP Python SDK. Users can now store session context in external storage (Redis, databases, etc.) and restore it across different server instances.

  • SessionState Pydantic model: Serializable snapshot containing session_id, protocol_version, next_request_id, server_capabilities, server_info, and initialized_sent
  • ClientSession.get_session_state(): Extracts current session state as a serializable object
  • ClientSession.from_session_state(): Creates a new session from previously saved state
  • Public API exports: SessionState available via from mcp import SessionState

Changes

  • Added src/mcp/shared/session_state.py with SessionState BaseModel
  • Modified src/mcp/client/session.py:
    • Added _session_id, _server_info, _initialized_sent attributes
    • Added get_session_state() method
    • Added from_session_state() classmethod
  • Added exports in src/mcp/__init__.py and src/mcp/shared/__init__.py
  • Comprehensive tests: 19 tests (5 for SessionState, 14 for ClientSession integration)

Test Plan

  • All 1130 existing tests pass (no regressions)
  • SessionState serialization tests (JSON/dict round-trip)
  • get_session_state() extraction test
  • from_session_state() restoration test
  • Full round-trip integration test
  • Type checking passes (pyright: 0 errors)
  • 100% branch coverage on session_state.py

Usage Example

from mcp import ClientSession, SessionState

# Save session state
async with ClientSession(...) as session:
    await session.initialize()
    state = session.get_session_state()
    json_str = state.model_dump_json()  # Store in Redis, DB, etc.

# Restore session state
state = SessionState.model_validate_json(json_str)
async with ClientSession.from_session_state(state, ...) as session:
    # Request IDs continue from saved state
    result = await session.send_request(...)

Related

Resolves #2111

This adds a Pydantic BaseModel that can be serialized to JSON for
storing session state in external storage (Redis, databases, etc.)

Related: modelcontextprotocol#2111
This method extracts serializable session state that can be stored
in external storage for distributed deployments.

Related: modelcontextprotocol#2111
This classmethod creates a new ClientSession from a previously saved
SessionState, enabling distributed deployments.

Related: modelcontextprotocol#2111
This test verifies that session state can be extracted, serialized
to JSON, deserialized, and restored with all fields intact.

Related: modelcontextprotocol#2111
Make SessionState available via
and .

Related: modelcontextprotocol#2111
@gspeter-max gspeter-max force-pushed the python-sdk/issue_2111 branch from e4bb236 to 33cc03c Compare February 22, 2026 10:03
This test ensures the branches where server_capabilities and server_info
are None are covered, achieving 100% branch coverage.

Related: modelcontextprotocol#2111
@gspeter-max
Copy link
Author

@maxisbey @Kludex @felixweinberger - PTAL, this PR adds serializable session state for distributed deployments (#2111). Ready for review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support serializable session state for distributed deployments

2 participants