# Which invariants prevent duplicate tool-effect replay when an MCP HTTP-native session resumes after a severed SSE stream **Date of Record:** 2026 November 19 **Scope:** Applies to MCP servers and clients using the HTTP-native transport (Streamable HTTP with SSE-delivered responses) where the client reconnects via `Last-Event-ID` and re-establishes an `Mcp-Session-Id`. Does not apply to stdio transports, nor to stateless JSON-only request/response MCP servers with no resumable event stream. ## Core Distinction The critical distinction is between **stream resumption** and **session re-establishment**. Resumption is a transport-layer operation: the client presents a `Last-Event-ID` and the server replays events with higher sequence numbers from a per-stream buffer. Re-establishment is a session-layer operation: the client presents an `Mcp-Session-Id` to recover server-side state (capabilities, negotiated schema, pending requests). These two are orthogonal in the MCP spec, but agents routinely conflate them — assuming that a successful SSE reconnect implies the session is intact, or that a valid session implies the stream position is recoverable. The failure lives in the gap: **the SSE stream can be resumed while the session is gone, and the session can be alive while the stream buffer has been evicted.** An agent that treats either signal as sufficient will silently re-execute a `tools/call` whose side effect already committed, because the request was accepted, the tool ran, and only the *response delivery* was interrupted. ## What Was Observed An agent issued `tools/call` for `write_file` with a request id `r-4471`. The server accepted the request, executed the tool (file written, checksum `a91c…`), and began streaming the JSON-RPC response as SSE events `ev-880` (partial result) and `ev-881` (final result). The TCP connection dropped between `ev-880` and `ev-881`. The client reconnected with `Last-Event-ID: ev-880` and a valid `Mcp-Session-Id`. Two things then happened in sequence: 1. The server's per-stream replay buffer had a 60-second TTL and had evicted `ev-881` because the reconnect arrived at T+72s. The server responded with `400` and a body indicating the event id was unknown — but it *did not* invalidate the session, so the `Mcp-Session-Id` remained valid. 2. The client's transport layer interpreted the `400` as "stream unrecoverable, restart the request," and re-sent the *same* `tools/call` with the *same* request id `r-4471` on a fresh stream. The server, which keyed idempotency on `(session_id, request_id)` only for *in-flight* requests and discarded that key on completion, saw `r-4471` as a new request. The tool executed again. The file was written twice; the second write raced with a concurrent reader and produced a torn read (`b03f…`), which the verifier downstream flagged as corruption. The observable signature: **a resumed stream that returns a terminal transport error is not a signal that the request did not execute.** The agent's recovery logic treated transport-level failure as request-level failure. That equivalence is false. ## Condition of Validity The invariant that must hold for safe resumption is: > A `tools/call` is replay-safe if and only if the server maintains a durable, session-scoped record of completed request ids, and the client re-issues a request only after confirming that id is absent from that record — not merely because the stream failed. This holds only when all of the following are true: - The server persists completed `(session_id, request_id) → result_digest` entries with a TTL at least as long as the maximum client reconnect backoff, and ideally for the session lifetime. - The server's replay buffer TTL and the completed-request record TTL are decoupled; evicting an SSE event does not evict the idempotency record. - The client distinguishes three outcomes on reconnect: `replayed` (events delivered), `unknown-event-id` (stream lost, request state unknown), and `session-expired` (must re-establish, not resume). Only `session-expired` licenses re-issuing the request; `unknown-event-id` requires a `tools/call` status query or a server-side idempotency check. - Request ids are client-generated and stable across retries, not regenerated per attempt. When any of these fails, the invariant is void and the agent is in the duplicate-effect regime. ## Decision Boundary The boundary is the **terminal-error classification on the resume path**. Concretely: | Server response to resume | Client must conclude | Permitted action | |---|---|---| | `200` with replayed events | Stream resumed, request outcome delivered | None | | `200` with `Last-Event-ID` honored but no new events (request still running) | Request in flight | Wait, do not re-issue | | `400 unknown event id`, session valid | Stream lost, request outcome **unknown** | Query request status; do not blindly re-issue | | `404 session not found` | Session gone | Re-establish session, then re-issue with same request id | | `409` conflict on request id | Duplicate detected server-side | Treat as already executed; fetch result | The trap is row 3. A `400` on the resume endpoint reads like "your stream position is bad, start over," and the natural client reflex is to re-send the request. But the request may have completed. The correct action is a status probe keyed on the request id, or a server-side idempotency rejection on the re-issued id. If the server has no completed-request record, the client cannot distinguish "never ran" from "ran and lost the response," and must treat the effect as *possibly applied* — which for non-idempotent tools (writes, sends, charges) means surfacing the ambiguity to the orchestrator rather than retrying. ## Operational Effect The practical consequence is that **HTTP-native MCP resumption is not a transaction; it is a best-effort event replay with no atomicity guarantee across the session boundary.** Agents that build retry logic on top of SSE reconnection inherit a duplicate-effect hazard for every non-idempotent tool call whose response was interrupted after server-side execution but before client-side receipt. The architectural guardrail is to move idempotency out of the transport and into the request identity: client-generated stable request ids, server-side durable completed-request records decoupled from stream buffers, and a client resume policy that maps `unknown-event-id` to a status query rather than a re-issue. Without the completed-request record, the session id alone is insufficient — it proves *who* you are, not *what already happened*. ```python class UnsafeResumeError(RuntimeError): """Raised when a client attempts to re-issue a tool call on a resume path where the prior outcome is unknown. Re-issuing here risks duplicate side effects for non-idempotent tools.""" class ResumeOutcome: REPLAYED = "replayed" IN_FLIGHT = "in_flight" UNKNOWN_EVENT = "unknown_event_id" SESSION_EXPIRED = "session_expired" DUPLICATE = "duplicate" def classify_resume(status_code: int, session_valid: bool) -> str: if status_code == 200: return ResumeOutcome.REPLAYED if status_code == 409: return ResumeOutcome.DUPLICATE if status_code == 400 and session_valid: return ResumeOutcome.UNKNOWN_EVENT if status_code == 404 or not session_valid: return ResumeOutcome.SESSION_EXPIRED raise UnsafeResumeError(f"unclassified resume status {status_code}") def on_resume(outcome: str, request_id: str, tool_is_idempotent: bool) -> str: """Decide the next action. Returns an action token for the orchestrator.""" if outcome == ResumeOutcome.REPLAYED: return "consume_events" if outcome == ResumeOutcome.IN_FLIGHT: return "wait" if outcome == ResumeOutcome.DUPLICATE: return "fetch_existing_result" if outcome == ResumeOutcome.SESSION_EXPIRED: # Session is gone; request may or may not have run. Only safe to # re-issue if the tool is idempotent OR the server has no record # but the caller accepts possible duplication. if tool_is_idempotent: return "reissue_same_request_id" return "reestablish_then_status_probe" if outcome == ResumeOutcome.UNKNOWN_EVENT: # Stream buffer evicted but session alive: outcome is ambiguous. # Blindly re-issuing here is the exact duplicate-effect trigger. if tool_is_idempotent: return "reissue_same_request_id" raise UnsafeResumeError( f"request {request_id} has unknown outcome after stream eviction; " f"non-idempotent tool must not be re-issued without a status probe" ) raise UnsafeResumeError(f"unhandled resume outcome {outcome!r}") ```