Skip to content

Conversation

@kxbnb
Copy link

@kxbnb kxbnb commented Jan 23, 2026

Summary

Fixes #1717

Adds handling for EndOfStream and ClosedResourceError in send_request(). These exceptions can be raised by response_stream_reader.receive() but weren't being caught, leading to UnboundLocalError at the subsequent isinstance check.

Changes

  • Added except (anyio.EndOfStream, anyio.ClosedResourceError) block after the existing TimeoutError handler
  • Converts these exceptions to McpError with CONNECTION_CLOSED error code

Validation

  • uv run pyright passes with 0 errors
  • All 758 tests pass

Testing

tests/shared/test_session.py::test_connection_closed PASSED

Closes #1717

Fixes modelcontextprotocol#1717

The `send_request` method only catches `TimeoutError` from the
`response_stream_reader.receive()` call. If `receive()` raises
`EndOfStream` or `ClosedResourceError` (e.g., when the connection
closes unexpectedly), these exceptions propagate without being
caught, potentially leaving `response_or_error` unassigned and
causing an `UnboundLocalError` at the subsequent isinstance check.

This adds explicit handling for these stream closure exceptions,
converting them to `McpError` with `CONNECTION_CLOSED` error code.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

UnboundLocalError in send_request when response_or_error is not assigned

1 participant