fix(opencode): auto-reconnect MCP clients on unexpected transport close#31312
Open
sulthonzh wants to merge 2 commits into
Open
fix(opencode): auto-reconnect MCP clients on unexpected transport close#31312sulthonzh wants to merge 2 commits into
sulthonzh wants to merge 2 commits into
Conversation
When an MCP server process dies or the HTTP transport drops, the client was left in connected state with stale tool definitions. No reconnect was attempted, so tools permanently vanished for the session. Register an onclose handler on each MCP client that: - Detects unexpected transport closure (not intentional disconnect) - Marks the server as failed and clears cached defs - Re-creates the client via createAndStore() using the original config - Publishes ToolsChanged event so the UI updates Guard all intentional close paths (disconnect, finalizer, closeClient) by deleting s.clients[name] BEFORE calling client.close(), so the onclose handler sees s.clients[name] !== client and returns early. Fixes anomalyco#17099
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
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.
Issue for this PR
Closes #17099
Type of change
What does this PR do?
When an MCP server process dies or the HTTP transport drops, the client stays in
connectedstate with stale tool definitions. Tools vanish permanently for the session because nothing listens for transport closure.The
watch()function only subscribes toToolListChangedNotification. It never registers anonclosehandler on the client, so when the SDK firesclient.oncloseafter transport death, nothing handles it.This PR registers
client.oncloseinwatch()that:s.clients[name] !== clientto skip intentional disconnectsfailedand clears cached defscreateAndStore()with the original config to reconnectToolsChangedso the UI updatesTo prevent intentional disconnects from triggering reconnect,
closeClient()now deletess.clients[name]before callingclient.close(). The finalizer also snapshots and clearss.clientsbefore closing clients during instance disposal.How did you verify your code works?
packages/opencode/test/mcp/lifecycle.test.ts:onclose triggers automatic reconnect when transport closes unexpectedly— simulates transport close by callingclient.onclose(), verifies the server reconnects and tools update from the new clientonclose does not trigger reconnect on intentional disconnect()— callsmcp.disconnect(), verifies status staysdisabledwith no reconnectbun turbo typecheck)MCP transport closed unexpectedlyScreenshots / recordings
N/A — no UI changes.
Checklist