Skip to content

Commit c6c2cc5

Browse files
address comments
1 parent 4048bfc commit c6c2cc5

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/mcp/client/streamable_http.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,10 @@ async def _handle_post_request(self, ctx: RequestContext) -> None:
279279
if is_initialization:
280280
self._maybe_extract_session_id_from_response(response)
281281

282-
content_type = response.headers.get(CONTENT_TYPE, "").lower()
283-
284282
# Per https://modelcontextprotocol.io/specification/2025-06-18/basic#notifications:
285283
# The server MUST NOT send a response to notifications.
286284
if isinstance(message.root, JSONRPCRequest):
285+
content_type = response.headers.get(CONTENT_TYPE, "").lower()
287286
if content_type.startswith(JSON):
288287
await self._handle_json_response(response, ctx.read_stream_writer, is_initialization)
289288
elif content_type.startswith(SSE):

tests/client/test_notification_response.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,13 @@ def non_sdk_server(non_sdk_server_port: int) -> Generator[None, None, None]:
114114

115115

116116
@pytest.mark.anyio
117-
async def test_notification_with_204_response(non_sdk_server: None, non_sdk_server_port: int) -> None:
117+
async def test_non_compliant_notification_response(non_sdk_server: None, non_sdk_server_port: int) -> None:
118118
"""
119-
This test verifies that the client does not parse responses to non-JsonRPCRequests, which matches the
120-
behavior of the TypeScript SDK. The test uses a 204 No Content (commonly seen from servers), but in reality
121-
any 2xx response should be handled the same way.
119+
This test verifies that the client ignores unexpected responses to notifications: the spec states they should
120+
either be 202 + no response body, or 4xx + optional error body
121+
(https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#sending-messages-to-the-server),
122+
but some servers wrongly return other 2xx codes (e.g. 204). For now we simply ignore unexpected responses
123+
(aligning behaviour w/ the TS SDK).
122124
"""
123125
server_url = f"http://127.0.0.1:{non_sdk_server_port}/mcp"
124126
returned_exception = None

0 commit comments

Comments
 (0)