fix(mcp): detect server death, fail loudly instead of raw ClosedResourceError#6298
Open
immuhammadfurqan wants to merge 4 commits into
Open
fix(mcp): detect server death, fail loudly instead of raw ClosedResourceError#6298immuhammadfurqan wants to merge 4 commits into
immuhammadfurqan wants to merge 4 commits into
Conversation
…rceError When an MCP server process dies mid-session, nothing linked the transport death to `_run_client`, which stayed parked on `await self._closing_ev.wait()`. As a result `initialized` kept returning True, the `finally` cleanup never ran, and every subsequent tool call fell through to `self._client.call_tool(...)` and raised a raw `anyio.ClosedResourceError` (empty message) into the LLM tool-call turn — a silent failure for the rest of the session, with no reconnect and no error surfaced to the agent. Detect a dead transport (`anyio.ClosedResourceError`/`BrokenResourceError`, or `McpError(CONNECTION_CLOSED)`) at the tool-call boundary: raise a clear `ToolError` and tear the dead client down so `initialized` reports False and the parked `_run_client` unwinds and runs its cleanup. `_run_client` now treats a post-startup connection close as an expected shutdown rather than an unexpected error. Normal tool-error results are unaffected and do not tear the connection down. This is the "fail loudly" fix; automatic reconnect is left as a follow-up. Closes livekit#6291
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #6291
This PR detects dead MCP transports at tool invocation time, tears down the dead client, and raises a clear ToolError instead of raw anyio errors. It also adds regression tests for dead connection detection and normal tool error propagation.