Skip to content

Commit 34dc68a

Browse files
author
Christian-Sidak
committed
test: add regression test for initialize hang on unexpected content-type (#2432)
Add a test that verifies initialize() raises MCPError immediately when the server returns an unexpected Content-Type (e.g. text/plain) instead of hanging forever waiting for a response that never arrives. Fixes #2432
1 parent d5b9155 commit 34dc68a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/client/test_notification_response.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ async def handle_mcp_request(request: Request) -> Response:
7171
return Starlette(debug=True, routes=[Route("/mcp", handle_mcp_request, methods=["POST"])])
7272

7373

74+
def _create_plain_text_server_app() -> Starlette:
75+
"""Create a server that returns text/plain for all requests, including initialize.
76+
77+
This reproduces the scenario from issue #2432 where a misconfigured server
78+
returns an unexpected content type on the initialize call, causing the client
79+
to hang forever.
80+
"""
81+
82+
async def handle_mcp_request(request: Request) -> Response:
83+
# Always return text/plain — never a valid MCP response.
84+
return Response(content="this is not json", status_code=200, media_type="text/plain")
85+
86+
return Starlette(debug=True, routes=[Route("/mcp", handle_mcp_request, methods=["POST"])])
87+
88+
7489
async def test_non_compliant_notification_response() -> None:
7590
"""Verify the client ignores unexpected responses to notifications.
7691
@@ -116,6 +131,20 @@ async def test_unexpected_content_type_sends_jsonrpc_error() -> None:
116131
await session.list_tools()
117132

118133

134+
async def test_initialize_does_not_hang_on_unexpected_content_type() -> None:
135+
"""Verify that initialize() raises MCPError immediately when server returns wrong content type.
136+
137+
Regression test for issue #2432: when a misconfigured server returns a content type
138+
other than application/json or text/event-stream in response to the initialize request,
139+
the client must raise MCPError right away instead of hanging forever.
140+
"""
141+
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=_create_plain_text_server_app())) as client:
142+
async with streamable_http_client("http://localhost/mcp", http_client=client) as (read_stream, write_stream):
143+
async with ClientSession(read_stream, write_stream) as session: # pragma: no branch
144+
with pytest.raises(MCPError, match="Unexpected content type: text/plain"): # pragma: no branch
145+
await session.initialize()
146+
147+
119148
def _create_http_error_app(error_status: int, *, error_on_notifications: bool = False) -> Starlette:
120149
"""Create a server that returns an HTTP error for non-init requests."""
121150

0 commit comments

Comments
 (0)