@@ -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+
7489async 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+
119148def _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