Skip to content

feat: Add protocol_version parameter to ClientSession#2321

Open
goingforstudying-ctrl wants to merge 1 commit intomodelcontextprotocol:mainfrom
goingforstudying-ctrl:add-protocol-version-param
Open

feat: Add protocol_version parameter to ClientSession#2321
goingforstudying-ctrl wants to merge 1 commit intomodelcontextprotocol:mainfrom
goingforstudying-ctrl:add-protocol-version-param

Conversation

@goingforstudying-ctrl
Copy link

Summary

Fixes #2307 - Adds the ability to specify a custom protocol version in ClientSession.initialize().

Problem

ClientSession.initialize() hardcodes protocolVersion=types.LATEST_PROTOCOL_VERSION with no way for the caller to override it. When connecting to an MCP server that requires a specific protocol version (e.g., Snowflake's managed MCP server requires "2025-06-18"), the only workaround is to mutate the module-level constant before calling initialize().

Solution

Added an optional protocol_version parameter to ClientSession.init():

def __init__(
    self,
    ...
    protocol_version: str | None = None,  # NEW — defaults to LATEST_PROTOCOL_VERSION
    *,
    ...
) -> None:
    self._protocol_version = protocol_version or types.LATEST_PROTOCOL_VERSION

Then use it in initialize():

protocolVersion=self._protocol_version,

Changes

File Change Reason
session.py Added protocol_version parameter Allow custom protocol version
session.py Store as _protocol_version instance var Use in initialize()
session.py Update initialize() to use self._protocol_version Use custom version
test_session.py Added test_client_session_custom_protocol_version Verify custom version works
test_session.py Added test_client_session_default_protocol_version Verify backwards compatibility

Testing

  • Added test for custom protocol version
  • Added test for default protocol version (backwards compatibility)
  • Both tests pass

Backwards Compatibility

Fully backwards compatible - existing callers get the current default (LATEST_PROTOCOL_VERSION) when protocol_version is not specified.

Closes #2307

This change allows users to specify a custom protocol version when
initializing a ClientSession, instead of always using LATEST_PROTOCOL_VERSION.

This is needed for connecting to MCP servers that require a specific
protocol version (e.g., Snowflake's managed MCP server requires 2025-06-18).

Changes:
- Added protocol_version parameter to ClientSession.__init__()
- Stored protocol_version as instance variable _protocol_version
- Updated initialize() to use self._protocol_version instead of hardcoded constant
- Added tests for both custom and default protocol version scenarios

Backwards compatible: defaults to LATEST_PROTOCOL_VERSION when not specified.

Fixes modelcontextprotocol#2307
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.

[mcp/python-sdk] ClientSession has no way to specify protocolVersion

1 participant