Skip to content

Commit a3f2133

Browse files
kxbnbclaude
andcommitted
fix: handle EndOfStream and ClosedResourceError in send_request
Fixes #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>
1 parent a1d330d commit a3f2133

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/mcp/shared/session.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,16 @@ async def send_request(
280280
),
281281
)
282282
)
283+
except (anyio.EndOfStream, anyio.ClosedResourceError) as e:
284+
raise McpError(
285+
ErrorData(
286+
code=CONNECTION_CLOSED,
287+
message=(
288+
f"Connection closed while waiting for response to "
289+
f"{request.__class__.__name__}: {e}"
290+
),
291+
)
292+
)
283293

284294
if isinstance(response_or_error, JSONRPCError):
285295
raise McpError(response_or_error.error)

0 commit comments

Comments
 (0)