# Why does an instance resuming an MCP streamable HTTP session silently diverge from the server's SSE event log **Date of Record:** 2026 December 4 **Scope:** Applies to MCP clients (agent runtimes) that reconnect to a streamable-HTTP MCP server after a transport interruption and rely on `Last-Event-ID` replay to reconstruct in-flight tool-call state. Does not apply to stdio transports, nor to stateless HTTP servers that do not emit SSE `id:` fields on the event stream. ## Core Distinction There are two legally distinct meanings of "resume" in the MCP streamable-HTTP transport, and they are routinely conflated by client implementations: 1. **Transport resumption** — the client reconnects the HTTP connection and asks the server, via `Last-Event-ID`, to replay SSE events it missed. This restores the *byte stream*, not the *semantic session*. 2. **Session resumption** — the client reconstructs the agent's *logical* state (which JSON-RPC requests are in flight, which tool calls have returned, which are orphaned) so that the reasoning loop can continue coherently. The MCP spec defines the `Last-Event-ID` replay mechanism for (1). It is silent on (2). A client that treats a successful SSE replay as proof of session continuity will diverge from the server's actual request ledger. The failure is not a dropped event — it is a *replayed subset* that the client mistakes for a complete history. ## What Was Observed An agent issued three concurrent JSON-RPC requests over one streamable-HTTP session: a long-running `tools/call` (call A, ~40 s), a fast `tools/call` (call B, ~200 ms), and a `sampling/createMessage` (call C, ~8 s). The event stream carried monotonically increasing `id:` values. At t≈2 s the TCP connection was severed by an intermediate proxy (idle-read timeout on the client's read half). Call B had already completed and its response event had been consumed. Call A was still executing server-side; call C was mid-flight. The client reconnected with `Last-Event-ID: 7` (the last event it had durably recorded). The server replayed events 8–11, which included the *response to call C* and several progress notifications for call A. It did **not** replay the response to call B (event 6), because event 6 preceded the resume point — correct per the spec, since the client already acknowledged it. The client's resume handler, however, rebuilt its pending-request table *from the replay buffer only*. Call B's entry was dropped from the in-memory table because no replayed event referenced it. The agent then re-issued call B (a non-idempotent `write_file`). The server, which had no dedup key on that request, executed it twice. The second write clobbered the first, and the agent's subsequent reasoning step — which assumed the file contained the first write's content — produced a corrupted plan. The observable signature was not an error. It was a **silent divergence**: the client believed it had 2 pending requests (A, C) and had completed B; the server believed it had 1 pending request (A) and had completed B *and* C. The two ledgers disagreed by one completed request, and the disagreement was invisible until the duplicate side effect surfaced downstream. ## Condition of Validity The divergence requires all three of the following to hold simultaneously: - **Concurrent in-flight requests share one SSE stream.** With serialized request/response, the replay buffer's oldest event always corresponds to the oldest unacknowledged request, so a replay-window rebuild is complete. Concurrency breaks the ordering assumption. - **The last acknowledged event ID precedes at least one completed request's response event.** If the client's durable cursor is *behind* every completed request, replay reconstructs them. The failure needs the cursor to sit *between* completions — a narrow but common window under partial reads. - **The client reconstructs pending state from the replay window rather than from a request-ID-keyed ledger.** This is the architectural choice that converts a transport replay into a semantic loss. If any one is absent, the failure does not occur. Notably, a client that keys its pending table on JSON-RPC `id` and marks entries resolved *only* when it sees the matching response — never by absence from a replay — is immune regardless of stream concurrency. ## Decision Boundary The invariant that must be enforced: > **Replay completeness is bounded by the replay window, not by session history. Absence of an event in a replay is not evidence that the event did not occur.** Operationally, this yields a hard rule: a resumed client MUST treat its pre-disconnect pending-request ledger as authoritative and MUST NOT prune entries merely because the replay did not mention them. Entries are resolved only by (a) observing their response event, or (b) an explicit server-side `notifications/cancelled` or timeout. For requests whose response may have been consumed but whose side effects are non-idempotent, the client must re-query state (e.g., re-read the file, re-list the resource) before re-issuing — or attach a client-generated idempotency key so the server can dedup. The boundary is sharp: **replay restores delivery, not history.** A client that cannot distinguish "event not replayed" from "event never happened" has no valid basis for resuming a reasoning loop, only for resuming a socket. ## Operational Effect When the invariant is violated, the failure surfaces as *duplicate side effects and stale reasoning premises*, not as a protocol error. The agent's context now contains a false belief ("B completed once with content X") that no subsequent tool result will contradict until it re-reads the affected resource. This is the dangerous class: the divergence is consistent with all locally observable evidence, so the agent will not self-correct. Recovery requires an out-of-band reconciliation step — re-reading every resource touched by any request whose resolution status is ambiguous across the disconnect. The guardrail is cheap to implement and expensive to omit: maintain a request-ID-keyed ledger with an explicit `UNKNOWN` state for entries straddling a resume, and force a reconciliation read before the next dependent reasoning step. The cost is one extra round-trip per ambiguous request; the cost of omission is silent, compounding state corruption that the agent has no signal to detect. ```python import time from dataclasses import dataclass, field from enum import Enum class ReplayDivergenceError(RuntimeError): """Raised when a resumed session's pending ledger cannot be reconciled against the server's replay window without ambiguity.""" pass class RequestState(Enum): PENDING = "pending" RESOLVED = "resolved" UNKNOWN = "unknown" # straddled a disconnect; resolution unproven @dataclass class PendingRequest: rpc_id: str idempotent: bool state: RequestState = RequestState.PENDING last_seen_event_id: int = 0 @dataclass class ResumeLedger: last_event_id: int requests: dict[str, PendingRequest] = field(default_factory=dict) def on_disconnect(self) -> None: # Mark every unresolved entry UNKNOWN; do NOT prune. for req in self.requests.values(): if req.state is RequestState.PENDING: req.state = RequestState.UNKNOWN def apply_replay(self, replayed_events: list[tuple[int, str]]) -> None: # replayed_events: (event_id, rpc_id_responded) seen = {rpc_id for _, rpc_id in replayed_events if rpc_id} for req in self.requests.values(): if req.rpc_id in seen: req.state = RequestState.RESOLVED # Absence from replay is NOT resolution. Leave UNKNOWN. def assert_resumable(self) -> None: ambiguous = [ r for r in self.requests.values() if r.state is RequestState.UNKNOWN and not r.idempotent ] if ambiguous: raise ReplayDivergenceError( f"{len(ambiguous)} non-idempotent request(s) straddle the resume " f"window at Last-Event-ID={self.last_event_id}; replay cannot " f"prove resolution. Reconcile via resource re-read before " f"resuming the reasoning loop." ) def resume_session(ledger: ResumeLedger, replayed_events: list[tuple[int, str]]) -> None: ledger.on_disconnect() ledger.apply_replay(replayed_events) ledger.assert_resumable() # Safe to continue only if no UNKNOWN non-idempotent entries remain. for req in ledger.requests.values(): if req.state is RequestState.UNKNOWN: req.state = RequestState.PENDING # reconciled by caller ```