Drop one reply and an MCP tool runs twice
An agent calls a tool that writes something. The server does the write and answers, and the answer never arrives. The agent sees an error and calls the tool again. Now there are two notes, or two refunds.
This is an old problem, and payment APIs deal with it through idempotency keys. MCP tool calls have the same shape and, as of today, no rule about it. The specification says nothing on retrying a tools/call, and the question is open as issue 3394.
We had a test recipe for the HTTP version of this case and wanted to see whether it works against the real MCP stack. It does. The result is worth showing because the first fix one reaches for passes the test and still writes twice after a deploy.
The setup
The client is the official TypeScript SDK's Client over StreamableHTTPClientTransport. The server is the SDK's McpServer on a stateless StreamableHTTPServerTransport, version 1.32.2. One tool, create_note, appends a line to a file. We wrote the tool. The client, the transport and the protocol are the SDK's.
Between them sits a small proxy from the api-testing skill. It forwards every request. For one chosen request it reads the server's whole reply, throws it away and cuts the caller's connection. So the write is committed and the caller cannot know. Then the caller sends the same call again, and an independent read counts the notes.
The reply that got dropped was an ordinary success: HTTP 200 with the result as one server-sent event.
event: message
data: {"result":{"content":[{"type":"text","text":"note-1"}]},"jsonrpc":"2.0","id":1}
Five runs
| Server | Verdict | Notes written |
|---|---|---|
| The tool as written. The caller calls it again | duplicated | 2 |
The caller sends an idempotency_key. The tool ignores it | duplicated | 2 |
| The tool remembers keys in memory | one_effect | 1 |
| Keys in memory, server restarted between the two calls | duplicated | 2 |
| Keys on disk, server restarted between the two calls | one_effect | 1 |
Rows one and two are what you would expect. Row two is still worth a look: an argument named idempotency_key in the schema does nothing unless the handler reads it. The caller fills it in and gets two notes anyway.
Rows three and four are the reason to run this with a restart. A Map in the process is the first thing anyone writes. It passes. It keeps passing in every test that does not restart the server, and then the first lost reply that straddles a deploy or a crashed pod writes the note twice. Only the version that stores the key with the write survives.
What the SDK does and does not do
The SDK client did not retry by itself. In all five runs the first call ended in an error and the second call was ours, standing in for an agent that sees a failed tool call and tries again. That is what agents do. An agent host with its own retry policy does the same thing one layer down.
So this is not a bug in the SDK. The protocol allows it, and a server cannot tell a retry from a second request unless the tool's own arguments carry something stable. Until the specification says more, that is the tool author's job.
If you write MCP tools
- Give every tool that creates or moves something a key argument, and make the handler use it. Return the first result for a key you have seen.
- Store the key in the same place as the effect, in the same transaction where there is one. Memory is not enough.
- Where the effect is in someone else's system, pass the key through. Most payment and email providers accept one.
- Where a tool cannot be made idempotent, say so in its description and give the agent a way to look up what happened instead of calling again.
Running it on your server
The helper is one function in a Markdown file. The first POST of an MCP session is initialize, so the proxy has to be told which request to lose:
let lostOnce = false;
const outcome = await retryAfterLostReply({
target: serverUrl,
send: async baseUrl => {
const client = new Client({ name: 'agent', version: '1.0.0' });
try {
await client.connect(new StreamableHTTPClientTransport(new URL(`${baseUrl}/mcp`)));
const result = await client.callTool({ name: 'create_note', arguments: { text, idempotency_key: key } });
return result.content[0].text;
} finally {
await client.close().catch(() => {});
}
},
countEffects: async () => (await readNotes()).filter(note => note.text === text).length,
drop: exchange => !lostOnce && /"method":"tools\/call"/.test(exchange.requestBody) && (lostOnce = true),
restart: () => restartServer(),
});
expect(outcome.verdict, outcome.reason).toBe('one_effect');
countEffects is the part to get right. It has to read the effect itself, the row or the file or the provider's sandbox, and not the tool's reply. The helper answers inconclusive when nothing was lost, for example when the proxy dropped the handshake, and that is not a pass.
The recipe is lost-reply-retry.md in the api-testing skill. The server and the script behind the table are in the run's evidence folder.
What this does not show
One tool, one transport, one SDK version, on one machine. The proxy buffers the whole reply before it decides, which fits a call answered with one event; a server that keeps a stream open was not tried. And one_effect means one write survived one lost reply and one retry. It says nothing about two calls in flight at once, or a retry after the key has expired.