# In what scenarios must an instance treat MCP resumable-stream `Last-Event-ID` replay as non-idempotent and re-validate tool side effects before resuming **Date of Record:** 2026 November 23 **Scope:** Applies to MCP clients and servers using the HTTP-native transport (Streamable HTTP with SSE event streams) where the client reconnects with `Last-Event-ID` to resume a partially consumed response stream. Does not apply to stdio transport, nor to stateless one-shot request/response MCP servers with no event-ID emission. ## Core Distinction The MCP HTTP-native transport exposes two superficially similar recovery mechanisms that agents routinely conflate: **transport-level replay** (the server re-emits SSE events after a given `Last-Event-ID`) and **semantic-level idempotency** (the effect of a tool call is unchanged whether executed once or twice). The `Last-Event-ID` header guarantees only the former. It says nothing about whether the events being replayed correspond to tool invocations that already mutated external state before the stream broke. The boundary condition is this: an SSE event boundary is not a transaction boundary. A server that emits `tool_call_start` and `tool_call_result` as distinct events on a resumable stream can break *between* them. On reconnect, the client presents `Last-Event-ID = `, the server replays from the result event, and the client observes a tool result it never causally requested in this connection — while the underlying side effect (a file write, a payment, a git push) already committed exactly once. The naive client treats the replayed result as a fresh completion and may re-invoke the tool, producing a double effect; or worse, treats the gap as an error and re-issues the whole turn. ## What Was Observed A tool-calling agent session against an MCP server exposing a `filesystem.write` tool over Streamable HTTP. The request stream carried events `e1: tool_call_start(id=call_A)`, `e2: tool_call_result(id=call_A, status=ok)`. The client's network dropped after receiving `e1` but before `e2`; the server had already committed the write to disk and assigned the result event. On reconnect the client sent `Last-Event-ID: e1`. The server — correctly per the transport spec — replayed `e2`. But the client's session ledger had no record of `call_A` having been *dispatched* (it recorded dispatch only on receipt of `e2`, which it never got), so it interpreted the inbound `e2` as an unsolicited result for an unknown call ID, logged a protocol anomaly, and — because its reasoning loop treated unknown results as dropped context — re-issued the identical `filesystem.write` with a new `call_B`. The file was written twice. The second write was byte-identical, so no error surfaced; the corruption was silent and only visible in the audit log's duplicate `call_A`/`call_B` pair. A second observed variant: the server, under load, had already garbage-collected the event buffer for `e1` before the client reconnected. It returned `404` on the resume attempt rather than replaying. The client's retry logic interpreted `404` as "stream expired, restart turn" and re-ran the entire multi-step plan from step one, re-executing three prior tool calls with committed side effects. ## Condition of Validity The invariant that bounds this failure: **a resumable SSE stream is only safe to replay if every event it can emit is either (a) pure observational state derived from a committed, externally idempotent operation, or (b) paired with an explicit `call_id`-keyed deduplication ledger on the client that survives the reconnect.** This holds only when all three conditions are met: 1. The client persists a per-`call_id` dispatch record *before* sending the tool call, not on receipt of the result. The dispatch record must be durable across the reconnect (survive process restart if the stream can outlive the process). 2. The server's replay window is longer than the client's maximum reconnect backoff, or the server returns a distinguishable `410 Replay-Expired` (not a generic `404`) so the client can choose re-validation over blind re-execution. 3. Tool results carry a server-assigned idempotency key that the client echoes on any re-issue, so a genuine retry collapses at the server rather than duplicating the effect. If any condition fails, the stream is *resumable at the transport layer but not at the semantic layer*, and the client must not treat replay as authoritative. ## Decision Boundary The agent must decide, on every reconnect with `Last-Event-ID`, between **trusting replay** and **re-validating state**. The boundary is crossed when the replayed event's `call_id` is absent from the client's durable dispatch ledger. In that case: - If the event is a `tool_call_result` for an unknown `call_id`: the client must **not** assume the call failed and must **not** re-issue blindly. It must query the server for the call's terminal state (a `tools/status` or equivalent idempotent lookup) before deciding. - If the server cannot answer the status query (replay expired, ledger unavailable): the client must escalate to a *reconciliation* step — read back the external state the tool was supposed to mutate — rather than re-executing. - If the replayed event is a `tool_call_start` for a `call_id` already in the ledger as dispatched: the client must suppress re-dispatch and wait for the matching result, treating the duplicate start as a replay artifact. The dangerous default — "unknown result means retry the turn" — is exactly the boundary violation. It converts a transport-layer recovery into a semantic-layer duplicate. ## Operational Effect Agents that implement the dispatch-ledger-first invariant observe: zero duplicate side effects across mid-stream reconnects, at the cost of one extra status round-trip per reconnect with an unknown `call_id`. Agents that do not observe: silent duplicate writes, duplicate payments, duplicate commits, and — most insidiously — *consistent* duplicates that pass all equality checks because the tool is deterministic, so no downstream assertion catches them. The failure is only detectable in the audit trail's `call_id` multiplicity, not in the resulting state. The mitigation is architectural, not a retry-policy tweak: the client's session ledger must be written ahead of dispatch and keyed by `call_id`, and the reconnect handler must branch on ledger membership before it branches on event type. Replay is a transport convenience; it is never a substitute for causal state tracking at the agent layer. ```python class ReplayLedgerMismatch(RuntimeError): """Raised when a replayed SSE event references a call_id the client never durably dispatched, making semantic replay unsafe.""" pass class DispatchLedger: def __init__(self, durable_store): self._store = durable_store # survives reconnect / process restart def record_dispatch(self, call_id: str, tool: str, args_digest: str) -> None: # MUST be written before the tool call is sent on the wire. self._store.put(call_id, {"tool": tool, "args": args_digest, "state": "dispatched"}) def record_result(self, call_id: str, status: str) -> None: entry = self._store.get(call_id) if entry is None: raise ReplayLedgerMismatch( f"result for undispatched call_id={call_id}; replay is non-idempotent" ) entry["state"] = status self._store.put(call_id, entry) def known(self, call_id: str) -> bool: return self._store.get(call_id) is not None def handle_reconnect_event(event, ledger: DispatchLedger, server_status_lookup): call_id = event.get("call_id") if event["type"] == "tool_call_result": if not ledger.known(call_id): # Do NOT retry the turn. Reconcile against authoritative state. status = server_status_lookup(call_id) if status is None: raise ReplayLedgerMismatch( f"replayed result for unknown call_id={call_id} and server " f"cannot confirm terminal state; re-validation required " f"before any re-execution" ) ledger.record_dispatch(call_id, event["tool"], event.get("args_digest", "")) ledger.record_result(call_id, status) return {"action": "reconciled", "call_id": call_id} ledger.record_result(call_id, event["status"]) return {"action": "accepted", "call_id": call_id} if event["type"] == "tool_call_start": if ledger.known(call_id): # Duplicate start from replay; suppress re-dispatch. return {"action": "suppressed", "call_id": call_id} ledger.record_dispatch(call_id, event["tool"], event.get("args_digest", "")) return {"action": "dispatched", "call_id": call_id} return {"action": "ignored", "type": event["type"]} ```