Repository navigation
fix(mcp): connect to servers that reject server/discover with a session ID - #354
Conversation
…on ID go-sdk v1.8.0 keeps the Mcp-Session-Id of every response it accepts, the rejection of server/discover included. A server that rejects discover with a session ID of its own, and then answers the initialize fallback with another session ID, makes the client fail the connection with "mismatching session IDs". Redpanda AI Gateway puts such a server in front of each remote MCP member, so an agent loses every tool on that member and stops before its first model call. Retry with the newest MCP revision that does not send server/discover. The session then opens with initialize, and only one session ID ever exists. Remove the retry after go.mod carries a go-sdk release that includes modelcontextprotocol/go-sdk#1523.
✅ AI review: approved — merging is the author's call
Review summary Adds a narrow connect-time workaround for a go-sdk v1.8.0 bug. clientImpl.connect now goes through connectSession, which first calls Connect with default options, as before. Only when the error text contains "mismatching session IDs" does it build a fresh transport from the factory and retry once with ProtocolVersion 2025-11-25, which skips server/discover and goes straight to initialize. Every other result (success or a different error) is returned unchanged, so servers that handle discover correctly see no behaviour change. The retry stays inside the caller's ctx, so reconnect attempts are still bounded by reconnectTimeout, because connect is shared by Start and performReconnect. Both errors are wrapped together when the retry fails, and a Warn log records when the fallback runs. An integration test stands up a server that rejects discover with HTTP 200, a JSON-RPC error and a session ID. It asserts that the client connects, lists tools, sends no second discover, and sends no stale session ID on the initialize that opens the session. CI (Test, Lint, vuln scan) is green. No dependency manifests changed. Notes: the match on the error string is fragile by design and is documented, since go-sdk exports no sentinel. registry_pool.go still calls client.Connect directly, so that path does not get the workaround. That is not a regression from this PR. The go-sdk behaviour and the upstream fix version are not verified externally. Evidence cited by the engine
Shadow engines (recorded for comparison; did not decide)
Generated by the AI review gate. The bot only posts an approval; the author merges. This is an audit record; the branch ruleset is what enforces it. |
There was a problem hiding this comment.
✅ AI review: approved — merging is the author's call
| Field | Value |
|---|---|
| Config version | 13 |
| Prompt version | agent.md@3959e093b050 |
| Model | claude-opus-5-5 |
| Engine | agent-action (agent-action@base-action@756cc22e19660d20e8cc9496b4f242475a7f7790) |
| AI verdict | approve (confidence 0.82, reviewed fully: True, scope: localized) |
| Files / lines (all) | 3 / 228 |
| CI at this commit | passed |
| Reviewable diff | 9417 chars (limit 120000) |
| Reviewable files / lines | 3 / 228 |
| Generated (CI-verified) files / lines | 0 / 0 |
| Test / source lines | 146 / 82 (tests changed with source: True) |
| Dependency update | False |
| Run | https://github.com/redpanda-data/ai-sdk-go/actions/runs/45696116255 |
Review summary
Changes the MCP client's connect path. client.go now calls the new connectSession instead of mcpClient.Connect directly. connectSession makes the normal Connect call first and returns its result unchanged unless the error text contains "mismatching session IDs" (and the context is not cancelled). Only in that case does it build a fresh transport from the existing transportFactory and retry Connect with ProtocolVersion "2025-11-25". If the retry also fails, both errors are wrapped together, and a successful retry logs a warning. Servers that complete the handshake normally take exactly the same path as before. Start and reconnect both go through connect(), so both get the fallback. The new integration test runs a server that rejects server/discover with HTTP 200, a JSON-RPC error and its own Mcp-Session-Id, then answers initialize with a different session ID. The test calls Start and ListTools end to end. go.mod still pins go-sdk v1.8.0 and no dependency manifest changed. CI (Test, Lint, vuln scan, license) is green. Not verified externally: the go-sdk v1.8.0 behaviour and the upstream fix in PR #1523 / v1.9.0. Notes, not blockers: matching the go-sdk error by its text is brittle across SDK upgrades, but if the text changes the retry just stops firing (the pre-PR behaviour), and the file is marked for removal after v1.9.0. This is a wire/handshake change rather than metadata, so it was reviewed as code; the retry is narrowly gated and low risk.
Evidence cited by the engine
tool/mcp/discover_session_mismatch.go:51 — Retry only triggers on the specific mismatching-session-IDs error and a live ctx; all other outcomes are returned unchanged.tool/mcp/discover_session_mismatch.go:56 — Retry uses a fresh transport from the existing factory, with errors wrapped together.tool/mcp/discover_session_mismatch.go:61 — Retry pins ProtocolVersion 2025-11-25 so the session opens with initialize only.tool/mcp/client.go:470 — Single call-site change; connect() is shared by Start and reconnect.tool/mcp/discover_session_mismatch_integration_test.go:81 — Test server reproduces the discover rejection with its own session ID, then a different initialize session.tool/mcp/discover_session_mismatch_integration_test.go:137 — Test asserts Start succeeds and tools are listed end to end.
Shadow engines (recorded for comparison; did not decide)
| Engine | Verdict | Confidence | Reviewed fully | Scope |
|---|---|---|---|---|
single-call |
comment |
0.6 | True | localized |
Generated by the AI review gate. The bot only posts an approval; the author merges. This is an audit record; the branch ruleset is what enforces it.
Remove the context check from the retry guard. A cancelled context gives an error without the mismatch text, so the text check already covers it. Fold isDiscoverSessionMismatch into its only caller. Remove the null ID branch from the test server. The client always sends server/discover as a request, so it always has an ID.
This comment has been minimized.
This comment has been minimized.
Withdrawn: this PR is no longer AI-approved (new commits, opt-out, or a failed run).
Remove the production impact from the godoc of connectSession and of its test. The PR and the first commit already record it.
There was a problem hiding this comment.
✅ AI review: approved — merging is the author's call
| Field | Value |
|---|---|
| Config version | 13 |
| Prompt version | agent.md@3959e093b050 |
| Model | claude-opus-5-5 |
| Engine | agent-action (agent-action@base-action@756cc22e19660d20e8cc9496b4f242475a7f7790) |
| AI verdict | approve (confidence 0.82, reviewed fully: True, scope: localized) |
| Files / lines (all) | 3 / 209 |
| CI at this commit | passed |
| Reviewable diff | 8532 chars (limit 120000) |
| Reviewable files / lines | 3 / 209 |
| Generated (CI-verified) files / lines | 0 / 0 |
| Test / source lines | 138 / 71 (tests changed with source: True) |
| Dependency update | False |
| Run | https://github.com/redpanda-data/ai-sdk-go/actions/runs/45701146257 |
Review summary
Adds a narrowly scoped fallback to the MCP client connect path: clientImpl.connect now calls connectSession, which calls mcpClient.Connect as before and, only if the error text contains go-sdk's "mismatching session IDs", builds a fresh transport from the existing transportFactory and retries once with ClientSessionOptions.ProtocolVersion="2025-11-25" (initialize-only handshake). Every other success or error path returns exactly what it did before, so servers that handle server/discover correctly see no change. Errors from the factory or the retry wrap the original error with %w, and a successful fallback logs a warning. Matching on error text is brittle, but the integration test pins it: it stands up a streamable-HTTP server that rejects server/discover with HTTP 200 plus its own Mcp-Session-Id, then checks that Start and ListTools succeed. The CI 'Test' job runs task test without -short, so the test ran and passed at this commit. Note: the registry pool connect path (tool/mcp/registry_pool.go:191) still calls client.Connect directly and does not get this fallback. That code is unchanged by this PR, so it is not counted against it, but the author may want to check whether registry-backed connections hit the same gateway. The upstream go-sdk behaviour and the gateway's behaviour were not verified externally. No dependency manifests changed.
Evidence cited by the engine
tool/mcp/discover_session_mismatch.go:46 — Retry runs only when the error mentions mismatching session IDs; every other result is returned unchangedtool/mcp/discover_session_mismatch.go:51 — Uses a fresh transport from the existing factory for the retry; factory and retry errors wrap the original errortool/mcp/discover_session_mismatch.go:56 — Retry pins ProtocolVersion to the initialize-only revisiontool/mcp/client.go:470 — One-line swap from mcpClient.Connect to c.connectSession; the error wrapping around it is unchangedtool/mcp/discover_session_mismatch_integration_test.go:81 — Test server rejects server/discover with HTTP 200 and its own session ID, reproducing the production failuretool/mcp/discover_session_mismatch_integration_test.go:129 — Checks that Start and ListTools succeed end to end against that serverTaskfile.yaml:87 —task test(used by the CI Test job) runs without -short, so the integration test ran in the green CItool/mcp/registry_pool.go:191 — Separate connect path that is unchanged by this PR and does not get the fallback (informational)
Shadow engines (recorded for comparison; did not decide)
| Engine | Verdict | Confidence | Reviewed fully | Scope |
|---|---|---|---|---|
single-call |
comment |
0.6 | True | localized |
Generated by the AI review gate. The bot only posts an approval; the author merges. This is an audit record; the branch ruleset is what enforces it.
…oduct The workaround applies to any server that rejects server/discover with a session ID of its own.
There was a problem hiding this comment.
✅ AI review: approved — merging is the author's call
| Field | Value |
|---|---|
| Config version | 13 |
| Prompt version | agent.md@3959e093b050 |
| Model | claude-opus-5-5 |
| Engine | agent-action (agent-action@base-action@756cc22e19660d20e8cc9496b4f242475a7f7790) |
| AI verdict | approve (confidence 0.85, reviewed fully: True, scope: localized) |
| Files / lines (all) | 3 / 209 |
| CI at this commit | passed |
| Reviewable diff | 8507 chars (limit 120000) |
| Reviewable files / lines | 3 / 209 |
| Generated (CI-verified) files / lines | 0 / 0 |
| Test / source lines | 138 / 71 (tests changed with source: True) |
| Dependency update | False |
| Run | https://github.com/redpanda-data/ai-sdk-go/actions/runs/45703112046 |
Review summary
Adds a narrow workaround for a go-sdk v1.8.0 bug: when the server/discover rejection carries a session ID and initialize returns a different one, Connect fails with "mismatching session IDs". The change sends clientImpl.connect through a new connectSession helper. connectSession calls Connect exactly as before (nil options), so the success path and every other error path behave as they did. Only when the error text contains "mismatching session IDs" does it build a new transport from the existing transportFactory and retry once with ProtocolVersion 2025-11-25. Retry failures wrap both errors, and a transport-factory failure is reported. The retry reuses the same sdkmcp.Client and the caller's context, so it stays inside the existing reconnect timeout. Matching on the error text is fragile, but the comment explains why (go-sdk exports no sentinel error), and the file is marked for removal once go.mod moves to go-sdk v1.9.0. registry_pool.go still calls client.Connect directly, so registry connections do not get the workaround. That is not a regression. The integration test runs a real streamable HTTP MCP server behind a wrapper that rejects discover with its own session ID, and checks that tools are listed after the retry. CI is green. I did not check externally the go-sdk behaviour, the linked upstream PR, or the protocol revision dates.
Evidence cited by the engine
tool/mcp/discover_session_mismatch.go:44 — The first attempt calls Connect with nil options, same as before.tool/mcp/discover_session_mismatch.go:46 — The retry only runs on the specific 'mismatching session IDs' error; every other result is returned unchanged.tool/mcp/discover_session_mismatch.go:51 — The retry gets a new transport from the configured factory, and factory errors are propagated.tool/mcp/client.go:470 — The only call site change: connect now goes through connectSession.tool/mcp/discover_session_mismatch_integration_test.go:114 — The integration test reproduces the mismatched-session-ID server and checks that a usable session and tool listing result.
Shadow engines (recorded for comparison; did not decide)
| Engine | Verdict | Confidence | Reviewed fully | Scope |
|---|---|---|---|---|
single-call |
comment |
0.6 | True | localized |
Generated by the AI review gate. The bot only posts an approval; the author merges. This is an audit record; the branch ruleset is what enforces it.
…smatch The stand-in server now records each request and its Mcp-Session-Id. The test checks that the retry sends no server/discover, and that the initialize that opens the session carries no session ID. Both checks also pass on go-sdk v1.9.0-pre.1. A comment on the ProtocolVersion option of the retry now says that this option is the fix.
Withdrawn: this PR is no longer AI-approved (new commits, opt-out, or a failed run).
There was a problem hiding this comment.
✅ AI review: approved — merging is the author's call
| Field | Value |
|---|---|
| Config version | 13 |
| Prompt version | agent.md@3959e093b050 |
| Model | claude-opus-5-5 |
| Engine | agent-action (agent-action@base-action@756cc22e19660d20e8cc9496b4f242475a7f7790) |
| AI verdict | approve (confidence 0.82, reviewed fully: True, scope: localized) |
| Files / lines (all) | 3 / 266 |
| CI at this commit | passed |
| Reviewable diff | 10501 chars (limit 120000) |
| Reviewable files / lines | 3 / 266 |
| Generated (CI-verified) files / lines | 0 / 0 |
| Test / source lines | 193 / 73 (tests changed with source: True) |
| Dependency update | False |
| Run | https://github.com/redpanda-data/ai-sdk-go/actions/runs/45705411420 |
Review summary
Adds a narrow connect-time workaround for a go-sdk v1.8.0 bug. clientImpl.connect now goes through connectSession, which first calls Connect with default options, as before. Only when the error text contains "mismatching session IDs" does it build a fresh transport from the factory and retry once with ProtocolVersion 2025-11-25, which skips server/discover and goes straight to initialize. Every other result (success or a different error) is returned unchanged, so servers that handle discover correctly see no behaviour change. The retry stays inside the caller's ctx, so reconnect attempts are still bounded by reconnectTimeout, because connect is shared by Start and performReconnect. Both errors are wrapped together when the retry fails, and a Warn log records when the fallback runs. An integration test stands up a server that rejects discover with HTTP 200, a JSON-RPC error and a session ID. It asserts that the client connects, lists tools, sends no second discover, and sends no stale session ID on the initialize that opens the session. CI (Test, Lint, vuln scan) is green. No dependency manifests changed. Notes: the match on the error string is fragile by design and is documented, since go-sdk exports no sentinel. registry_pool.go still calls client.Connect directly, so that path does not get the workaround. That is not a regression from this PR. The go-sdk behaviour and the upstream fix version are not verified externally.
Evidence cited by the engine
tool/mcp/discover_session_mismatch.go:46 — Retry triggers only on the specific mismatching-session-ID error; all other outcomes returned unchangedtool/mcp/discover_session_mismatch.go:51 — Fresh transport from the factory for the retry, with transport-creation errors wrappedtool/mcp/discover_session_mismatch.go:56 — Retry pins ProtocolVersion to legacy 2025-11-25 to avoid server/discovertool/mcp/client.go:470 — Single call-site change; connect is used by both Start and performReconnect under the caller/attempt contexttool/mcp/discover_session_mismatch_integration_test.go:188 — Test pins that discover is sent once and that the session-opening initialize carries no stale session ID
Shadow engines (recorded for comparison; did not decide)
| Engine | Verdict | Confidence | Reviewed fully | Scope |
|---|---|---|---|---|
single-call |
comment |
0.6 | True | localized |
Generated by the AI review gate. The bot only posts an approval; the author merges. This is an audit record; the branch ruleset is what enforces it.
An agent whose MCP server rejects
server/discoverwith a session ID fails to connect withllmagent: tools: tool: snapshot input 5: calling "initialize": mismatching session IDs. The agent stops before its first model call, so it loses every tool on that server.MCP 2026-07-28 removed the
initializehandshake and sessions, and addedserver/discover(SEP-2575, changelog). go-sdk opens each connection withserver/discover, and falls back toinitializeon the same transport when a server that runs an older revision rejects it. go-sdk v1.8.0 keeps the firstMcp-Session-Idit sees on a transport and fails any later response that carries a different one (streamable.go#L2372). The bug is that it also keeps the ID on the discover rejection, which opens no session. So a server that puts a session ID on the rejection, and then correctly starts a new session forinitialize, makes the client fail the whole connection.sequenceDiagram participant Client as ai-sdk-go (go-sdk v1.8.0) participant Server as MCP server Client->>Server: server/discover Server-->>Client: 200, JSON-RPC error, Mcp-Session-Id: A Note over Client: keeps A Client->>Server: initialize (go-sdk fallback) Server-->>Client: 200, Mcp-Session-Id: B Note over Client: A ≠ B: "mismatching session IDs" Client->>Server: initialize only, no server/discover (this PR) Server-->>Client: 200, Mcp-Session-Id: C Note over Client: one session ID, connectedThis retries the connection with 2025-11-25, the newest MCP revision that opens a session with
initializeinstead ofserver/discover. Only one session ID then exists, so the check cannot fail. The retry reacts to the error only, so a server that answers discover correctly is untouched.apps/aigwalready carries the same workaround for Code MCP members, inapps/aigw/internal/mcp/sdk_connect.go(redpanda-data/cloudv2#32443).Removal
The upstream fix is modelcontextprotocol/go-sdk#1523, merged but released only in
v1.9.0-pre.1.tool/mcp/discover_session_mismatch.goand its test go away oncego.modcarries v1.9.0 or newer. The file says so next to the implementation.Evidence
The integration test stands up an MCP server that rejects
server/discoverwith HTTP 200, a JSON-RPC error, and a session ID of its own, then answersinitializewith a different one.mismatching session IDs "session-from-the-discover-rejection" and "BPVS4WBHSC7456HFBOZ5X42455", the production error.A plain HTTP 400 rejection, which the MCP Python SDK sends on its own, does not reproduce this:
checkResponsedrops the response before the client reads the session header. The test documents that.