8000 Address @ihrpr comments · modelcontextprotocol/python-sdk@172db96 · GitHub
[go: up one dir, main page]

Skip to content

Commit 172db96

Browse files
Address @ihrpr comments
- Use InitializeResult type for parsing server init response - Add explanatory comment for DEFAULT_NEGOTIATED_VERSION constant - Include supported protocol versions in error response when version mismatch occurs
1 parent 56c1b0a commit 172db96

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/mcp/client/streamable_http.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from mcp.shared.message import ClientMessageMetadata, SessionMessage
2323
from mcp.types import (
2424
ErrorData,
25+
InitializeResult,
2526
JSONRPCError,
2627
JSONRPCMessage,
2728
JSONRPCNotification,
@@ -138,13 +139,14 @@ def _maybe_extract_protocol_version_from_message(
138139
) -> None:
139140
"""Extract protocol version from initialization response message."""
140141
if isinstance(message.root, JSONRPCResponse) and message.root.result:
141-
# Check if result has protocolVersion field
142-
result = message.root.result
143-
if "protocolVersion" in result:
144-
self.protocol_version = result["protocolVersion"]
142+
try:
143+
# Parse the result as InitializeResult for type safety
144+
init_result = InitializeResult.model_validate(message.root.result)
145+
self.protocol_version = str(init_result.protocolVersion)
145146
logger.info(f"Negotiated protocol version: {self.protocol_version}")
146-
else:
147-
logger.warning(f"Initialization response does not contain protocolVersion: {result}")
147+
except Exception as exc:
148+
logger.warning(f"Failed to parse initialization response as InitializeResult: {exc}")
149+
logger.warning(f"Raw result: {message.root.result}")
148150

149151
async def _handle_sse_event(
150152
self,

src/mcp/server/streamable_http.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from mcp.shared.message import ServerMessageMetadata, SessionMessage
2828
from mcp.shared.version import SUPPORTED_PROTOCOL_VERSIONS
2929
from mcp.types import (
30-
DEFAULT_PROTOCOL_VERSION,
30+
DEFAULT_NEGOTIATED_VERSION,
3131
INTERNAL_ERROR,
3232
INVALID_PARAMS,
3333
INVALID_REQUEST,
@@ -699,12 +699,14 @@ async def _validate_protocol_version(self, request: Request, send: Send) -> bool
699699

700700
# If no protocol version provided, assume default version
701701
if protocol_version is None:
702-
protocol_version = DEFAULT_PROTOCOL_VERSION
702+
protocol_version = DEFAULT_NEGOTIATED_VERSION
703703

704704
# Check if the protocol version is supported
705705
if protocol_version not in SUPPORTED_PROTOCOL_VERSIONS:
706+
supported_versions = ", ".join(SUPPORTED_PROTOCOL_VERSIONS)
706707
response = self._create_error_response(
707-
f"Bad Request: Unsupported protocol version: {protocol_version}",
708+
f"Bad Request: Unsupported protocol version: {protocol_version}. "
709+
+ f"Supported versions: {supported_versions}",
708710
HTTPStatus.BAD_REQUEST,
709711
)
710712
await response(request.scope, request.receive, send)

src/mcp/types.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@
2323
"""
2424

2525
LATEST_PROTOCOL_VERSION = "2025-03-26"
26-
DEFAULT_PROTOCOL_VERSION = "2025-03-26"
26+
27+
"""
28+
The default negotiated version of the Model Context Protocol when no version is specified.
29+
We need this to satisfy the MCP specification, which requires the server to assume a
30+
specific version if none is provided by the client. See section "Protocol Version Header" at
31+
https://modelcontextprotocol.io/specification
32+
"""
33+
DEFAULT_NEGOTIATED_VERSION = "2025-03-26"
2734

2835
ProgressToken = str | int
2936
Cursor = str

0 commit comments

Comments
 (0)
0