Skip to content

fix: consume response body on unexpected content type to prevent client hang#2438

Open
vishal-vanam wants to merge 3 commits intomodelcontextprotocol:mainfrom
vishal-vanam:fix/handle-unexpected-content-type-hang
Open

fix: consume response body on unexpected content type to prevent client hang#2438
vishal-vanam wants to merge 3 commits intomodelcontextprotocol:mainfrom
vishal-vanam:fix/handle-unexpected-content-type-hang

Conversation

@vishal-vanam
Copy link
Copy Markdown

Summary

Fixes #2432

When an MCP server returns an unexpected Content-Type (e.g. text/plain instead of application/json or text/event-stream), session.initialize() hangs indefinitely because the response body is never consumed, causing the httpx streaming context to block on exit.

The fix: Call await response.aread() to consume the response body before sending the error message through the read stream. This allows the streaming context to close cleanly and the error to propagate to the caller.

Changes

  • src/mcp/client/streamable_http.py: Added await response.aread() in the unexpected content-type branch to drain the response body
  • tests/shared/test_streamable_http.py: Added test using httpx MockTransport to verify the client raises MCPError instead of hanging

Test plan

  • New test test_unexpected_content_type_does_not_hang passes locally
  • Verify existing streamable HTTP tests still pass

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.

Initialize call hangs forever if MCP server does not return a Content-Type: text/plain

1 participant