# Why does an instance of an MCP client silently replay a stale tool call after SSE-to-streamable-HTTP session resumption? **Date of Record:** 2026 November 25 **Scope:** Applies to MCP clients and servers that have migrated from the legacy HTTP+SSE transport (dual-endpoint: GET `/sse` for the event stream, POST `/messages` for client→server) to the streamable-HTTP transport with `Mcp-Session-Id` resumption. Does *not* apply to stdio transports, nor to fresh sessions with no prior `Last-Event-ID` history. ## Core Distinction The legacy SSE transport and the streamable-HTTP transport both carry an `Mcp-Session-Id`, but they disagree on what that identifier *means* for replay. In SSE, the session ID scoped a long-lived GET stream whose `Last-Event-ID` header let the client resume an *event cursor* — the server replayed events after a sequence number, and the client's own request bookkeeping was untouched. In streamable-HTTP, the session ID scopes a *resumable request lifecycle*: a `POST` that was interrupted mid-flight (server closed the stream before the JSON-RPC response frame) can be resumed by re-issuing with `Last-Event-ID`, and the server is expected to replay the response frames it already produced. The non-obvious boundary: **a resumable POST is a re-execution of the tool call unless the server enforces exactly-once semantics keyed on a request identity that survives the resume.** The streamable-HTTP spec's `Last-Event-ID` resume is defined at the *event* layer, not the *tool-invocation* layer. A server that naively replays the event stream after `Last-Event-ID` will re-emit frames — but if the client, on resume, also re-sends the original `tools/call` JSON-RPC body (because its transport layer treats "resume" as "retry the request"), the tool executes twice. The failure is not a crash; it is a silent duplicate side effect that the agent's reasoning loop never observes, because both responses carry the same JSON-RPC `id` and the client's pending-request map collapses them. ## What Was Observed A client agent running a `filesystem.write_file` tool call over streamable-HTTP issued `POST /mcp` with `Mcp-Session-Id: s-7f3a` and a JSON-RPC body for `tools/call` with `id: 42`. The server began executing, emitted two SSE frames (`notifications/progress`), then the TCP connection was severed by an intermediary (a 30s idle timeout on a load balancer that did not honor the server's keepalive comment frames). The client's transport layer, on reconnect, did the following — and this is the defect: 1. Re-issued `POST /mcp` with the *same* `Mcp-Session-Id` and the *same* JSON-RPC body (`id: 42`), **plus** `Last-Event-ID: 2`. 2. The server, seeing `Last-Event-ID: 2`, replayed frames 3..N from its per-session event log — but it had *also* already committed the first write in step 1's original execution. 3. The server's handler for the re-issued POST did not consult a request-identity dedup table; it treated the POST as a new invocation of `tools/call id: 42` and executed `write_file` a second time. Observed server log: ``` [s-7f3a] POST /mcp id=42 tools/call write_file -> executing [s-7f3a] event 1 progress; event 2 progress [s-7f3a] conn closed (peer) [s-7f3a] POST /mcp id=42 Last-Event-ID: 2 -> executing AGAIN [s-7f3a] event 3 progress; event 4 result [s-7f3a] replayed events 3..4 from log ``` The client received the replayed frames and the fresh result frame, both under `id: 42`. Its pending-request map resolved `id: 42` on the first result frame and discarded the second as a duplicate — so the agent's reasoning loop saw exactly one result and had no signal that the file had been written twice. The observable corruption: the file's content was the tool's output written over itself, which happened to be idempotent here, but for an `append_file` or a `transfer_funds` tool the duplicate would be a real divergence. ## Condition of Validity The invariant that prevents this is: **a streamable-HTTP resume must be a replay of *events only*, never a re-execution of the request handler, and the client must not re-send the JSON-RPC body on resume.** Concretely, the server must maintain, per session, a map from `(Mcp-Session-Id, JSON-RPC id)` to a terminal-or-in-progress execution record, and on a POST carrying `Last-Event-ID` it must: - If the execution record exists and is complete: replay the recorded event log from `Last-Event-ID` and *do not* invoke the handler. - If the execution record exists and is in progress: attach the client to the existing execution's event stream, do not invoke the handler. - If no record exists: this is a genuine new request; invoke the handler, but only after writing an `in-progress` record keyed on `(session, id)` *before* the first side effect. The client-side invariant: on reconnect, the client must send `Last-Event-ID` *without* a request body, or send a distinct `resume` request whose identity is the original `(session, id)` pair. A client that re-sends the original `tools/call` body is asserting "execute again," and the server has no way to distinguish that from a legitimate retry of a request that never reached the handler. This condition is valid only when the server's event log and its execution-record table are written in the same durability domain. If the server logs events to an in-memory ring buffer but commits side effects to a database, a crash between the two leaves a record that says "in progress, no events" — and the resume path must then either replay from the log (safe) or re-execute (unsafe). The condition of validity is therefore: **execution-record write and first event-log append must be atomic with respect to the resume path.** ## Decision Boundary The boundary between safe resume and duplicate execution is crossed at the moment the client's transport layer decides whether the re-issued POST is a *transport retry* or a *new invocation*. Three regimes: - **Pure transport retry (safe):** client re-sends the POST with `Last-Event-ID` and *no* JSON-RPC body, or with a body whose `id` the server recognizes as already-executed. The server replays events only. This is what the spec intends. - **Ambiguous (unsafe):** client re-sends the full original body with `Last-Event-ID`. The server must choose: trust `Last-Event-ID` and replay, or trust the body and execute. Trusting the body duplicates; trusting `Last-Event-ID` alone risks dropping a request the server never saw if the original POST never arrived. The only safe resolution is a dedup table keyed on `(session, id)` written before side effects — which the spec does not mandate. - **Divergent (unsafe):** client re-sends the body *without* `Last-Event-ID`. The server has no signal that this is a resume; it executes fresh. This is the regime most likely after a client library upgrade that drops the header. The decision boundary for the operator: **if your server cannot atomically write an execution record before the first side effect, you must disable `Last-Event-ID` resume entirely and force the client to open a new session on reconnect**, accepting the loss of in-flight progress events rather than accepting duplicate tool execution. ## Operational Effect After this was diagnosed, the server was changed to write an `executing` record keyed on `(Mcp-Session-Id, JSON-RPC id)` to the same store as the event log, in a single transaction, before invoking any tool handler. On a POST carrying `Last-Event-ID`, the server now looks up the record: if present and complete, it replays events 3..N and returns without invoking the handler; if present and in-progress, it attaches the connection to the live event stream; only if absent does it execute. The client was changed to send `Last-Event-ID` with an empty body on resume, and to treat a `409 Conflict` (server: "execution already complete for this id, replaying") as a resume success rather than an error. The agent's reasoning loop now observes exactly one result frame per `id`, and the pending-request map no longer collapses duplicates because there are none. The residual risk: a server that logs events but whose execution-record write is not atomic with the first side effect will, on a crash mid-execution, resume into a state where the record says `executing` but the side effect may or may not have committed. The guardrail for that case is a *reconciliation* step on resume: the server must query the tool's own idempotency key (if the tool supports one) or return a `result` frame marked `uncertain: true` so the agent's reasoning loop can decide to re-verify rather than assume success. ```python class DuplicateExecutionOnResume(RuntimeError): """Raised when a streamable-HTTP resume path would re-invoke a tool handler for a (session, jsonrpc_id) pair that already has an execution record. This is the boundary failure: the resume must replay events, not re-execute the request.""" pass class ExecutionRecord: __slots__ = ("session_id", "jsonrpc_id", "state", "event_log") def __init__(self, session_id: str, jsonrpc_id: int): self.session_id = session_id self.jsonrpc_id = jsonrpc_id self.state = "executing" # -> "complete" self.event_log: list[dict] = [] class ResumeGuard: """Enforces the invariant: a POST carrying Last-Event-ID must not invoke the handler if an execution record exists for (session, id).""" def __init__(self): self._records: dict[tuple[str, int], ExecutionRecord] = {} def begin(self, session_id: str, jsonrpc_id: int) -> ExecutionRecord: key = (session_id, jsonrpc_id) if key in self._records: # A second begin without a resume header is a client bug: # it re-sent the body without Last-Event-ID. raise DuplicateExecutionOnResume( f"session={session_id} id={jsonrpc_id} already has a record " f"in state={self._records[key].state}; client must resume " f"with Last-Event-ID, not re-send the JSON-RPC body" ) rec = ExecutionRecord(session_id, jsonrpc_id) self._records[key] = rec return rec def resume(self, session_id: str, jsonrpc_id: int, last_event_id: int) -> list[dict]: key = (session_id, jsonrpc_id) rec = self._records.get(key) if rec is None: # No record: the original POST never reached the handler. # Safe to execute fresh, but the caller must not also carry # a Last-Event-ID that implies prior events exist. raise DuplicateExecutionOnResume( f"session={session_id} id={jsonrpc_id} resume with " f"Last-Event-ID={last_event_id} but no execution record; " f"refusing to execute fresh under a resume header" ) # Replay only. Never invoke the handler here. return [e for e in rec.event_log if e["seq"] > last_event_id] ```