# Where must an instance reconcile SSE replay against its own tool-call ledger when an MCP Streamable HTTP session resumes with a stale Last-Event-ID? **Date of Record:** 2026 November 20 **Scope:** Applies to MCP clients operating over Streamable HTTP transport using Server-Sent Events for server-to-client streaming, where the client persists a session ID and an SSE `Last-Event-ID` and reconnects after a transport break. Does not apply to stdio transport, to stateless HTTP without SSE, or to servers that do not implement event-ID-based resumability. ## Core Distinction The resumption contract in MCP Streamable HTTP is a *transport-level* invariant: given a `Last-Event-ID`, the server replays all SSE events with a higher event ID that were emitted on that session's stream. It is **not** a *task-level* invariant. The server has no knowledge of whether the client actually processed a tool call whose result was carried in an event that the client already consumed before the socket died. Replay is idempotent at the wire; it is **not** idempotent at the agent's ledger. The failure mode is therefore not "lost events." It is **duplicate delivery of events the client already acted upon**, combined with **suppression of events the client never saw but whose side effects landed anyway**. The client must reconcile the replayed SSE stream against its own record of issued `tools/call` requests and their observed outcomes. The server will not do this for it, because the server cannot distinguish "client never received event 41" from "client received event 41, executed the tool call inside it, and crashed before sending the next request." ## What Was Observed An agent instance issues `tools/call` for `deploy.stage` over a Streamable HTTP session (`Mcp-Session-Id: 7f3c...`). The server acknowledges with a 202 and begins streaming progress on the SSE channel. Event IDs arrive as `12`, `13`, `14`. Event `14` carries the terminal result `{"status":"staged","rev":"a91"}`. The client-side agent records the result in its episodic buffer, then attempts the follow-up `tools/call` for `deploy.promote`. The HTTP request times out at the transport layer (connection reset during POST). The agent's supervisor logic catches the timeout and reconnects to the SSE endpoint with `Last-Event-ID: 14` — a value it persisted *before* the promote call was attempted, because the persistence write and the promote dispatch were not ordered. The server, per spec, replays every event with ID `> 14`. That includes event `15`, which the server emitted *after* the client's promote request had already reached it and been executed — event `15` carries `{"status":"promoted","rev":"a91"}`. The agent now receives a promote result for a call it believes it is about to re-issue. Two things go wrong in sequence: 1. The agent's replay handler appends event `15` to the transcript as a *new* observation, not a duplicate of an in-flight call. Its ledger now contains a promote result with no matching issued call — an orphan result. 2. The agent's retry logic, seeing no local record of a dispatched promote, re-issues `deploy.promote`. The server executes it a second time. If the tool is not idempotent (e.g., it appends a deployment annotation, increments a counter, or rotates a signing key), the second execution is a distinct side effect. The inverse case is equally damaging. If the client's `Last-Event-ID` was persisted *after* the terminal result of a call but *before* the call's side effect was durably recorded locally, replay suppresses the result event (server considers it delivered) while the client has no record of it. The agent proceeds as if the call never happened and re-issues it — same duplicate side effect, opposite mechanism. ## Condition of Validity Replay-based resumption is only safe when the following invariant holds at the client: > For every `tools/call` the agent has dispatched on session S, there exists exactly one durable ledger entry keyed by (session ID, request ID, event ID of the terminal result) that is written **before** the transport-layer POST returns and **before** the SSE event carrying the result is acknowledged to the replay layer. This requires the client to treat the SSE event ID as part of the tool-call identity, not as a transport cursor. The `Last-Event-ID` header must be derived from the ledger's high-water mark — the largest event ID for which the client has a *committed* ledger entry — not from a separate cursor variable updated on receipt. The invariant fails whenever the client maintains two independent cursors: one for SSE position, one for tool-call state. They drift under exactly the conditions that trigger resumption: partial writes, process restarts, and concurrent dispatch. ## Decision Boundary The client must decide, on every resumed stream, whether an incoming event with ID `E` is: - **Novel** — `E` > ledger high-water mark, no matching request ID in the ledger. Process normally. - **Duplicate** — `E` ≤ ledger high-water mark, matching request ID present with terminal result recorded. Discard; do not re-append to transcript. - **Orphan** — `E` ≤ ledger high-water mark, but no matching request ID (the promote case above). This is the dangerous class. The correct action is **not** to append it as a new observation and **not** to re-issue the call. It is to reconcile: query the server for the authoritative state of the resource the event refers to, or fail closed. - **Gap** — `E` > ledger high-water mark + 1. The server has replayed past events the client never recorded. The ledger is inconsistent; the client cannot trust the stream position and must treat the session as poisoned. The boundary that matters operationally: **orphan events must be treated as evidence of a side effect the agent did not authorize in its current reasoning trace.** The agent's local model of "what I have done" is now provably incomplete. Any subsequent plan that assumes the ledger is a faithful record is unsound. ## Operational Effect Without this reconciliation, three concrete degradations appear: 1. **Non-idempotent tool double-execution.** Retried calls after resumption duplicate side effects. Observed in deployment promotion, secret rotation, and message-send tools. 2. **Reasoning divergence from phantom results.** The agent's chain-of-thought incorporates an orphan result as if it were its own action, producing plans that reference state it did not create. Downstream tool calls then operate on a resource the agent believes it owns but does not. 3. **Silent gap acceptance.** When the server's replay window is bounded (many implementations cap replay at N events or T seconds), a `Last-Event-ID` older than the window yields a stream that starts mid-sequence with no error. The client sees a contiguous-looking stream and assumes continuity. The gap is invisible unless the client checks `E == high_water + 1` explicitly. The guardrail is a ledger-first resumption protocol: persist the terminal-result event ID atomically with the tool-call outcome, derive `Last-Event-ID` from that committed value, and reject any resumed stream whose first event ID is not exactly `committed + 1`. Orphan and gap events escalate to a reconciliation path that queries server state before any retry is permitted. ```python class McpResumptionLedgerViolation(RuntimeError): """Raised when a resumed SSE stream cannot be reconciled with the tool-call ledger.""" pass class ResumptionLedger: def __init__(self, session_id: str): self.session_id = session_id self.committed_high_water: int = 0 self.terminal_results: dict[int, dict] = {} # event_id -> result self.dispatched_requests: dict[str, int] = {} # request_id -> event_id def commit_terminal(self, event_id: int, request_id: str, result: dict) -> None: # Atomic in real implementation: ledger write + high-water bump together. if event_id != self.committed_high_water + 1: raise McpResumptionLedgerViolation( f"non-contiguous commit: event {event_id} after high-water " f"{self.committed_high_water} on session {self.session_id}" ) self.terminal_results[event_id] = result self.dispatched_requests[request_id] = event_id self.committed_high_water = event_id def last_event_id_header(self) -> str: return str(self.committed_high_water) def reconcile_resumed_event(ledger: ResumptionLedger, event_id: int, request_id: str | None) -> str: expected = ledger.committed_high_water + 1 if event_id == expected: return "novel" if event_id <= ledger.committed_high_water: if request_id is not None and request_id in ledger.dispatched_requests: return "duplicate" # Orphan: server emitted a result the client never recorded as its own. raise McpResumptionLedgerViolation( f"orphan event {event_id} on session {ledger.session_id}: " f"no ledger entry for request {request_id!r}; " f"side effect may have landed without client authorization — " f"reconcile server state before any retry" ) # event_id > expected: replay started past our high-water mark. raise McpResumptionLedgerViolation( f"replay gap on session {ledger.session_id}: resumed at event {event_id}, " f"expected {expected}; server replay window may have truncated — " f"treat session as poisoned" ) ```