GeezerVison is showing github.com — fix(mcp): reconnect dropped servers with backoff by afonsoft · Pull Request #52943 · anomalyco/opencode
Conversation
afonsoft
commented
Oct 3, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Issue for this PR
Closes #52237
Closes #52564
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
When an MCP server connection drops (client.onclose), the server is marked failed and stays dead until the app restarts or the user manually reconnects. Same for servers that fail to connect at startup. There is no recovery path.
This PR adds automatic reconnect with exponential backoff:
scheduleReconnect forks a retry loop per server (deduped via a reconnects map on instance State). Each attempt re-runs createAndStore; the loop retries while the status stays failed with Schedule.exponential("1 second") (jittered, capped at 10 attempts).
- Triggered from three paths: unexpected
onclose, failed initial create at state init, and failed createAndStore (add/connect/finishAuth).
- Stops cleanly when: config removed or
enabled === false, status is no longer failed (connected, disabled, needs_auth, needs_client_registration — auth-required states never spin), disconnect() interrupts the fiber, or instance disposal interrupts all pending reconnects in the finalizer.
disconnect() also cancels any in-flight reconnect so an explicit user disconnect can't be raced by a retry.
How did you verify your code works?
bun typecheck + bun turbo typecheck across the monorepo — clean.
bun test test/mcp/ — 66/66 pass.
- New regression test
reconnects after the local server process exits: kills the spawned stdio fixture (process.kill(pid)), then polls until status returns to connected with a different transport pid and tools are re-listed.
- Relaxed
remote timeout aborts both real HTTP transport attempts to assert the first two requests (["POST","GET"]) — the reconnect loop legitimately issues further attempts afterward.
Screenshots / recordings
N/A — server-side behavior.
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
cc @Hona @Brendonovich — could you review and approve? Adds MCP auto-reconnect: dropped or failed-at-startup servers now retry with exponential backoff (10 attempts), instead of staying failed until restart.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for your contribution!
This PR doesn't have a linked issue. All PRs must reference an existing issue.
Please:
- Open an issue describing the bug/feature (if one doesn't exist)
- Add
Fixes #<number> or Closes #<number> to this PR description
See CONTRIBUTING.md for details.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
The following comment was made by an LLM, it may be inaccurate:
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍
|
afonsoft reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Scope check for #52237: that report is from V2 (2.0.20), while this PR currently targets dev and changes packages/opencode/src/mcp/index.ts.
On current V2 63b7de94, the server MCP handler calls the separate packages/core/src/mcp/index.ts service. This patch does not change that implementation. The reconnect regression needs to exercise that V2 path before the reported V2 issue can be considered covered; the current contribution guide also asks for V2 bases.
This is a source/target review; I have not independently run the reconnect test or reproduced the sleep/wake failure.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2.
— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
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.
|