fix(opencode): reconnect MCP transport on session-expiration error and retry once#25135
Open
iggerask wants to merge 1 commit intoanomalyco:devfrom
Open
fix(opencode): reconnect MCP transport on session-expiration error and retry once#25135iggerask wants to merge 1 commit intoanomalyco:devfrom
iggerask wants to merge 1 commit intoanomalyco:devfrom
Conversation
Contributor
|
Hey! Your PR title Please update it to start with one of:
Where See CONTRIBUTING.md for details. |
…d retry once Closes anomalyco#23997. When a remote MCP server invalidates a streamable-HTTP session (server restart, idle timeout, or periodic rotation), the cached StreamableHTTPClientTransport keeps sending the now-stale mcp-session-id header and every tool call on that server fails with "Error POSTing to endpoint: {\"error\":\"Session not found\"}" until the user manually disconnects and reconnects. Wrap client.callTool in convertMcpTool with a single-retry recovery: on an error matching session-expiration ("session not found" / "invalid session" / "mcp-session-id"), trigger a reconnect via the existing createAndStore() machinery and replay the call against the fresh client. Bound to one retry to avoid hammering on persistent failures. The reconnect callback re-enters Effect-land from the AI SDK's plain async Tool.execute path by capturing the current Effect context inside tools() and using Effect.runPromiseWith(ctx). The resolver reads s.clients directly so it observes the post-reconnect client. Includes test/mcp/session-recovery.test.ts: - recovers from one-shot Session-not-found - propagates after exactly one retry on persistent failure (no loop) - non-session errors are not treated as recoverable
d132b39 to
57e2337
Compare
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
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 #25137.
Type of change
What does this PR do?
When a remote streamable-HTTP MCP server invalidates a session (server restart, idle timeout), opencode keeps sending the stale
mcp-session-id. Every tool call after that fails withError POSTing to endpoint: {"error":"Session not found"}until the user manually disconnects and reconnects.This wraps
client.callToolinconvertMcpToolwith a one-shot recovery: on a session-expired error, call the existingcreateAndStore()to rebuild the transport, then replay the call once. Bounded to one retry — a fresh transport that fails again signals a real problem where retrying won't help.Doesn't touch the related
listTools()eviction path (#17099) or the desktop session-restore crash (#25131).How did you verify your code works?
bun --cwd packages/opencode test test/mcp/— 36/36 pass (3 new insession-recovery.test.ts).mcp session expired, reconnectinglog line and the next tool call succeeded. Pre-fix, every call after that failed.Checklist