Skip to content

fix(opencode): reconnect MCP servers when a tool call fails - #40062

Closed
zhang17-24 wants to merge 1 commit into
anomalyco:devfrom
zhang17-24:fix/40015-mcp-tool-reconnect
Closed

zhang17-24 wants to merge 1 commit into
anomalyco:devfrom
zhang17-24:fix/40015-mcp-tool-reconnect

Conversation

@zhang17-24

Copy link
Copy Markdown

Issue for this PR

Closes #40015

Type of change

  • Bug fix

What does this PR do?

A restarted MCP server leaves the client's StreamableHTTP connection dead without reliably firing onclose, so the cached tool set stayed stale: long-lived sessions kept calling the old server until the process restarted. McpTool now carries a reconnect hook that re-establishes the server session through the normal connect path (which refreshes the cached tool definitions), and session tool execution triggers it when a tool call fails so the agent's retry uses a fresh client.

How did you verify your code works?

  • bun test test/mcp/ — 62 tests pass (incl. new reconnect-hook test)
  • bun run typecheck — pass

Screenshots / recordings

N/A (not a UI change)

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

A restarted MCP server leaves the client's StreamableHTTP connection dead
without always firing onclose, so the cached tool set stayed stale forever:
Desktop (and any long-lived session) kept calling the old server until the
process restarted (issue anomalyco#40015).

- McpTool now carries a reconnect hook that re-establishes the server's
  session through the normal connect/createAndStore path (which refreshes
  the cached tool definitions)
- session tool execution triggers that hook when a tool call fails, so the
  agent's retry uses a fresh client once the server is back
- MCP connection close also kicks off a backoff reconnect loop so a dead
  connection recovers without waiting for the next tool call
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Desktop caches MCP tools at startup, never refreshes on reconnect

1 participant