Skip to content

Conversation

@jayhemnani9910
Copy link
Contributor

@jayhemnani9910 jayhemnani9910 commented Jan 7, 2026

Summary

Fixes #1811

When an SSE stream disconnects before the server sends any events with IDs (no resumption token), the client now sends a JSONRPCError to the session layer instead of hanging indefinitely.

Changes

  • Added error handling in _handle_sse_response for streams that close without sending resumption tokens
  • Error response uses CONNECTION_CLOSED (-32000) error code with descriptive message
  • Added unit test that exercises the new code path by mocking EventSource

Test Plan

  • Added test_sse_disconnect_without_resumption_token_sends_error unit test
  • All 1091 existing tests pass

When SSE stream disconnects before receiving any events with IDs,
the client would hang forever waiting for a response. This adds
an else branch to send a JSONRPCError to the session layer when
reconnection is not possible.

Fixes modelcontextprotocol#1811
Copilot AI review requested due to automatic review settings January 7, 2026 00:51
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes issue #1811 where SSE stream disconnections without receiving a resumption token would cause the client to hang indefinitely. The fix adds an else branch to send a JSONRPCError to the session layer when no event ID has been received before disconnection, preventing deadlock.

Key Changes:

  • Added error handling for SSE disconnections without resumption tokens in _handle_sse_response()
  • Sends a JSONRPCError with code -32000 (CONNECTION_CLOSED) to notify the session layer
  • Includes comprehensive test coverage for the new error path

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/mcp/client/streamable_http.py Adds else branch in _handle_sse_response() to send JSONRPCError when SSE stream disconnects without receiving an event ID, preventing indefinite hangs
tests/shared/test_streamable_http.py Adds regression test that verifies error is sent to session layer when SSE stream disconnects before receiving resumption token

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

jsonrpc="2.0",
id=request_id,
error=ErrorData(
code=-32000,
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error code -32000 should use the CONNECTION_CLOSED constant defined in mcp.types instead of a magic number. The constant is already imported and available. Using the constant improves maintainability and makes the error code's meaning clearer.

Suggested change
code=-32000,
code=CONNECTION_CLOSED,

Copilot uses AI. Check for mistakes.
jsonrpc="2.0",
id=request_id,
error=ErrorData(
code=-32000,
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error code -32000 should use the CONNECTION_CLOSED constant defined in mcp.types instead of a magic number. Using the constant improves test maintainability and ensures consistency with the production code.

Copilot uses AI. Check for mistakes.
assert isinstance(received, SessionMessage)
assert isinstance(received.message.root, JSONRPCError)
assert received.message.root.id == "test-request-123"
assert received.message.root.error.code == -32000
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error code assertion should use the CONNECTION_CLOSED constant from mcp.types instead of the magic number -32000. This would make the test more maintainable and ensure consistency with the constant definition.

Copilot uses AI. Check for mistakes.
@maxisbey maxisbey closed this Jan 7, 2026
@maxisbey maxisbey reopened this Jan 7, 2026
@maxisbey maxisbey marked this pull request as draft January 7, 2026 15:42
@maxisbey
Copy link
Contributor

maxisbey commented Jan 7, 2026

As there's no linked issue or description I've set this PR to draft for now.

Jay Hemnani and others added 3 commits January 7, 2026 18:12
Rewrite test to actually call _handle_sse_response() instead of
duplicating the implementation logic inline. This ensures lines
440-451 in streamable_http.py are properly covered.

The test mocks EventSource to yield no events, simulating a stream
that closes immediately without sending any resumption tokens.

Github-Issue: modelcontextprotocol#1811
The else branch on the isinstance check is unreachable in practice
since POST SSE responses always contain JSONRPCRequest messages.

Github-Issue: modelcontextprotocol#1811
@jayhemnani9910
Copy link
Contributor Author

As there's no linked issue or description I've set this PR to draft for now.

Yea my bad, my setup crashed. lol

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.

client's read_stream_writer open after SSE disconnection hanging .receive()

2 participants