# What happens when an instance replays a Streamable HTTP MCP tool call across a reconnect and the server has no idempotency key to deduplicate it **Date of Record:** 2026 December 2 **Scope:** Applies to MCP clients using the Streamable HTTP transport with resumable SSE streams and `Last-Event-ID` replay, where tool calls carry side effects (writes, sends, payments) and the server-side tool handler is not itself idempotent. Does not apply to stateless request/response transports without replay, nor to tool calls that are provably read-only. ## Core Distinction The failure is not "the connection dropped." Connections drop constantly and the transport is *designed* to survive that. The failure is the mismatch between two independent guarantees that operators routinely conflate: 1. **Transport-level at-least-once delivery.** Streamable HTTP with `Last-Event-ID` resume guarantees that an event the client never acknowledged will be redelivered. This is a *delivery* invariant. 2. **Tool-level exactly-once effect.** A tool call that charges a card or appends a row has an *effect* invariant that must be enforced by the tool itself, typically via an idempotency key. The transport can only promise at-least-once. When the agent runtime treats "the stream reconnected and replayed my request" as "my request will be applied once," it silently upgrades an at-least-once channel into an assumed exactly-once one. The gap between those two guarantees is where duplicate side effects live. The non-obvious part: the duplicate is *not* caused by the client retrying. It is caused by the client **correctly** honoring the replay protocol. The agent is behaving exactly as specified, and that correctness is what produces the double-charge. ## What Was Observed A write-capable MCP server exposed a `create_ticket` tool. The client (an agent runtime) issued `tools/call` as a POST that opened an SSE stream. The server executed the tool, emitted a `progress` event, then a `result` event with the new ticket ID, and the client consumed the result and emitted `Last-Event-ID: 42` on the next request. The break sequence: - Server processed `create_ticket`, committed the row (ticket `T-1001`), and emitted `progress` as event id `41`. - Before emitting `result` as event id `42`, the process was killed by a rolling deploy / OOM / network partition. - The client's SSE read failed mid-stream. It had seen `41` but not `42`. It reconnected with `Last-Event-ID: 41`. - The server, restarted, had **no memory of the in-flight call** — the tool execution state lived in the dead process, not in a durable store keyed by event id. On resume it had nothing to replay, so the client (or a proxy) re-issued the original `tools/call` body. - `create_ticket` ran again. Ticket `T-1002` was created. The client received `result` for `T-1002` and believed it had one ticket. The observable signature is subtle: the agent's transcript shows a single successful tool call. The system of record shows two rows. There is no error, no retry log line, no exception — because from the client's perspective nothing failed. The only evidence is the duplicate side effect in the downstream system, discovered later by a human reconciling counts. A second variant: the server *did* persist execution state, but keyed it on the SSE event id rather than on a client-supplied idempotency key. On resume it correctly replayed `result` for `42` — but a *different* client instance (the agent had been checkpointed and cold-started on another node) issued a fresh `tools/call` with no `Last-Event-ID` at all, because the checkpoint did not carry the stream cursor. Same double effect, opposite cause: the first was replay-without-state, the second was state-without-cursor. ## Condition of Validity The invariant only holds — that is, exactly-once tool effects survive reconnect — when **all** of the following are true simultaneously: - The client supplies a stable idempotency key per logical tool call, generated *before* the first send and persisted across checkpoint/resume, not regenerated on retry. - The server's tool handler performs a dedup check on that key inside the same transaction as the side effect (check-and-insert atomically, or insert-with-unique-constraint and treat conflict as "already applied, return stored result"). - The server stores the *result* alongside the key, so a replayed call returns the original result (`T-1001`) rather than re-executing and returning a new one (`T-1002`). - The `Last-Event-ID` cursor and the idempotency key are checkpointed together. If the agent cold-starts with the key but without the cursor, it must still send the key; if it cold-starts with the cursor but without the key, it must not assume replay is safe. If any one is missing, the system degrades to at-least-once effects. That is not a bug in the transport — it is the transport working as specified against a tool that assumed a stronger guarantee than it was given. ## Decision Boundary The boundary the agent runtime must decide, on every reconnect, is: **do I have enough durable state to make this replay safe, or must I refuse and escalate?** - If the persisted idempotency key exists *and* the server is known to dedup on it → replay is safe; re-send with the same key. - If the key exists but the server's dedup behavior is unknown or unversioned → replay is *unsafe by default*; the agent must not silently re-send a side-effecting call. It should surface a `ReplaySafetyError` and require either a read-back verification (`get_ticket` by key) or human/auditor confirmation. - If the cursor exists but the key does not → the agent cannot reconstruct which logical call the cursor belonged to; it must treat the in-flight call as *unknown outcome* and reconcile before proceeding, never blindly re-issue. The wrong default — and the one most runtimes ship — is "reconnect and resend." That default is correct for read-only tools and catastrophically wrong for write tools. The boundary is per-tool, not per-transport. ## Operational Effect Without the guardrail, the operational effect is silent duplication proportional to reconnect frequency: every mid-stream failure during a side-effecting call produces one extra effect. Under flaky networks or rolling deploys this compounds, and because each duplicate looks like a clean success in the agent transcript, it is invisible to observability built on agent logs. Reconciliation cost lands on downstream systems and humans. With the guardrail — durable idempotency key + atomic dedup + stored result + cursor/key co-checkpointing — the transport's at-least-once delivery becomes exactly-once *effect* at the tool layer, and reconnect replay becomes safe by construction. The agent's replay path must be explicitly gated on the presence of a verified key, and must fail closed (raise) rather than fail open (resend) when the key or the server's dedup capability is in doubt. ```python import hashlib from dataclasses import dataclass, field from typing import Optional class ReplaySafetyError(RuntimeError): """Raised when a side-effecting MCP tool call cannot be safely replayed.""" pass @dataclass class ToolCallState: logical_call_id: str tool_name: str idempotency_key: Optional[str] last_event_id: Optional[int] side_effecting: bool server_dedups_on_key: bool result: Optional[str] = None def make_idempotency_key(logical_call_id: str, tool_name: str, args: str) -> str: # Deterministic across checkpoint/resume: never regenerate on retry. h = hashlib.sha256(f"{logical_call_id}:{tool_name}:{args}".encode()).hexdigest() return f"idem-{h[:32]}" def plan_reconnect(state: ToolCallState) -> str: """Decide whether a reconnect may resend, must reconcile, or must refuse.""" if not state.side_effecting: return "resend_safe" # read-only: at-least-once is harmless if state.result is not None: return "already_applied" # stored result; do not re-execute if state.idempotency_key is None: # Cursor may exist, but we cannot bind the call to a dedup key. raise ReplaySafetyError( f"call {state.logical_call_id!r} to {state.tool_name!r} is side-effecting " f"but has no persisted idempotency key; outcome unknown, reconcile required" ) if not state.server_dedups_on_key: raise ReplaySafetyError( f"server does not dedup on idempotency key for {state.tool_name!r}; " f"replaying {state.logical_call_id!r} risks duplicate effect" ) if state.last_event_id is None: # Key present, cursor lost: safe to resend *with the same key* only. return "resend_with_key" return "resend_with_key" def resume_call(state: ToolCallState) -> str: action = plan_reconnect(state) if action == "already_applied": return state.result if action == "resend_safe": return "resend_read_only" if action == "resend_with_key": return f"resend:{state.idempotency_key}" raise ReplaySafetyError(f"unhandled replay action {action!r}") # Example: cold-start with cursor but no key on a write tool -> fail closed. bad = ToolCallState( logical_call_id="call-7f3a", tool_name="create_ticket", idempotency_key=None, last_event_id=41, side_effecting=True, server_dedups_on_key=True, ) try: resume_call(bad) except ReplaySafetyError as e: print(f"refused: {e}") ```