fix(mcp): auto-reconnect on transport errors (#25287)#25670
fix(mcp): auto-reconnect on transport errors (#25287)#25670water-in-stone wants to merge 1 commit intoanomalyco:devfrom
Conversation
|
The following comment was made by an LLM, it may be inaccurate: Based on the search results, I found potentially related PRs:
These should be reviewed to ensure there's no conflicting implementation or duplicated effort, especially PR #25135 which appears to be directly addressing the same MCP reconnection problem. |
Relationship to existing issuesThank you for the pointer — this is indeed related but distinct from the existing issues:
Key distinction: two failure modes
|
5c803b8 to
631d74e
Compare
Issue for this PR
Closes #25287
Type of change
What does this PR do?
Long-running MCP sessions break silently when the remote server restarts, hot-swaps its StreamableHTTP session, or the socket drops. The client keeps the stale transport and every subsequent tool call fails with an unhelpful error, even though a fresh connect would recover. Users had to restart the whole opencode session to unstick things.
isTransportError()classifier that recognizes StreamableHTTPError (400/404/410/500 and SDK code -1) and raw socket failures (BunConnectionRefused, NodeECONNREFUSED/ECONNRESET, UndiciUND_ERR_SOCKET, fetch-failed fallback), while leaving 401/403 auth flows and ordinary business errors untouched.How it was validated
test/mcp/transport-error.test.tsandtest/mcp/mcp-reconnect.test.ts): 30+ cases covering every known error shape the classifier must accept or reject.How did you verify your code works?
I created an MCP Server called 'xingtian' locally and sent a question —
What features does the Xingtian editor have? — to simulate usage from a normal user. Then I manually restarted the xingtian MCP Server and sent another question:What is the latest version of the xingtian editor?. The second question failed with a Streamable HTTP error without my fix, but returned a correct response after applying my fix.Before Fix
After Fix
Checklist