Skip to content

fix(mcp): never re-send a tool call that may have reached the server - #2780

Merged
ChrisJBurns merged 1 commit into
mainfrom
fix/mcp-no-tools-call-replay
Nov 13, 2026
Merged

ChrisJBurns merged 1 commit into
mainfrom
fix/mcp-no-tools-call-replay

Conversation

@ChrisJBurns

Copy link
Copy Markdown
Contributor

Closes #2734.

Problem

withSession reconnected and re-ran every MCP session call after a connection drop, tools/call included. If the drop happened after the server ran the tool, the side effect ran twice. The design treats that as an unknown outcome that is never replayed automatically (credentials-and-lifecycle.md, "Pauses and restart"). This affected direct MCP servers and the MCP broker's protected caller, which goes through remoteTool.Execute. The broker claims each call before sending it (auth.go), but this retry happened underneath that check.

What

For tools/call only, a call is re-sent after a drop only when none of its HTTP requests could have reached the server:

  • the transport failed while still obtaining a connection (an httptrace GetConn with no GotConn), or the call made no request at all; or
  • the call's own request was answered with 404, an unknown MCP session that the server refuses before processing.

Otherwise the call fails with the existing "request outcome is unknown" error and is not re-sent. resources/read and prompts/get keep reconnect-and-retry, and concurrent failing calls still lead to one reconnect.

  • dispatchRecordingTransport (internal/adapter/mcp/reconnect.go) wraps the SDK's HTTP client transport. For a request whose context carries the call's record, it notes whether the server may have received it.
  • withToolCall gives each attempt a fresh record and turns a drop into errOutcomeUnknown when a request may have reached the server. withSession never retries that error. remoteTool.Execute and callTool (the CallMcpWithQuery path) use it.
  • unavailableMessage extends the existing unknown-outcome path (isAmbiguousTransportFailure) instead of adding a second message.

Why record each request instead of trusting ErrSessionMissing: the SDK fails the whole connection and gives every pending call the same error. A call whose POST was accepted (200, SSE) and whose resume GET then got a 404 receives ErrSessionMissing too, although the tool ran. Only the status of the call's own request tells them apart.

Tests

New tests in internal/adapter/mcp/transport_semantics_test.go:

  • A call dropped after the server ran it is not re-sent, through remoteTool.Execute and through callTool. The tests assert one request, one side effect, no reconnect, and the exact unknown-outcome message.
  • A call dropped after the server accepted an SSE response, with the SDK's resume GET answered by 404, is not re-sent.
  • Five concurrent calls dropped after the server ran them give five unknown outcomes and no reconnect. The next call then works.
  • A call whose connection is refused before sending reconnects and retries once, and the side effect runs once.
  • A dropped resources/read still reconnects and retries.

Existing reconnect and session-missing tests pass unchanged.

Mutation checks:

  • Restoring the old re-send fails every new not-re-sent test.
  • Treating ErrSessionMissing as safe to re-send fails the SSE test.
  • Counting every request as reached fails the refused-connection, session-missing, reconnect and concurrent-reconnect tests.

Two independent reviews found no path that re-sends a call that may have reached the server, including the SDK's own retries, net/http's internal retry and HTTP/2.

Notes

  • More unknown-outcome errors: some tools/call drops that used to recover silently through a re-send now report an unknown outcome, for example a reset or EOF after the request was sent. This is intended.
  • A few never-sent calls report unknown: net/http retrying a stale keep-alive connection, or a failure inside our own transport wrappers before any network I/O, such as a token-source error. Both err towards not re-sending.
  • The issue's tools/list line doesn't apply: "a drop during tools/list still reconnects" doesn't match the code, because tools/list never went through withSession. The test covers resources/read instead.
  • MCP tool annotations (idempotentHint, readOnlyHint) are untrusted server hints and are not used to allow a re-send.

API compatibility

No schema, proto, or wire field changes. The model sees a behaviour change: when an MCP tools/call loses its connection after the request may have reached the server, the harness no longer reconnects and re-sends it. The tool result is an error reading mcp call failed: MCP server "<name>" unavailable; request outcome is unknown, with a call MCP tool failed: prefix through CallMcpWithQuery. Such calls previously could succeed silently through a re-send, possibly running a side effect twice, or fail with "unavailable after reconnect". The text reuses the existing unknown-outcome wording, and calls that provably never reached the server keep their retry.

After a connection drop, withSession reconnected and re-ran every session
call, tools/call included, so a tool that ran before the drop could run
twice. A tools/call is now re-sent only when none of its HTTP requests
could have reached the server: the connection was never obtained, or its
own request got a 404 for an unknown session. Otherwise it fails with the
existing "request outcome is unknown" error. A recording transport notes,
per call, whether its requests reached the server, because the SDK gives
every pending call the same session-missing error. resources/read and
prompts/get keep reconnect-and-retry.

Closes #2734

Co-authored-by: Mecatl <[email protected]>

@rdimitrov rdimitrov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Checked the per-request record against go-sdk v1.8.0: streamableClientConn.Write builds the POST with the call's ctx, so the dispatchKey value reaches dispatchRecordingTransport, and a 404 on that POST is exactly the ErrSessionMissing path in checkResponse. The GetConn-without-GotConn rule, counting 401/3xx/5xx as reached, and the fresh record per attempt all fail towards not re-sending, which is the right direction for side-effecting calls. The tests cover both the replay and the refused-connection retry, and the mutation checks show they'd catch a regression.

@ChrisJBurns
ChrisJBurns enabled auto-merge (squash) November 13, 2026 21:15
@ChrisJBurns
ChrisJBurns merged commit 93c282e into main Nov 13, 2026
43 checks passed
@ChrisJBurns
ChrisJBurns deleted the fix/mcp-no-tools-call-replay branch November 13, 2026 21:18
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Nov 18, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP client re-sends a tool call after a connection drop

2 participants