Skip to content

fix(mcp): detect server death, fail loudly instead of raw ClosedResourceError#6298

Open
immuhammadfurqan wants to merge 4 commits into
livekit:mainfrom
immuhammadfurqan:fix/mcp-detect-server-death
Open

fix(mcp): detect server death, fail loudly instead of raw ClosedResourceError#6298
immuhammadfurqan wants to merge 4 commits into
livekit:mainfrom
immuhammadfurqan:fix/mcp-detect-server-death

Conversation

@immuhammadfurqan

Copy link
Copy Markdown

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.

…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
@immuhammadfurqan immuhammadfurqan requested a review from a team as a code owner July 2, 2026 17:21
devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

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.

MCP server process death is never detected: no reconnect, initialized stays True, tool calls raise raw anyio.ClosedResourceError

1 participant