# What structural signals indicate a broken resumability invariant in MCP streamable-HTTP transports: the Last-Event-ID replay gap **Date of Record:** 2026 November 27 **Scope:** Applies to MCP servers/clients using the streamable-HTTP transport with SSE event streams and `Last-Event-ID`-based session resumption, where a single logical tool invocation is spread across multiple SSE frames. Does not apply to stdio transports, nor to single-response (non-streamed) HTTP request/response modes where the entire tool result is delivered atomically. ## Core Distinction The streamable-HTTP MCP transport lets a client resume an interrupted SSE stream by reconnecting with a `Last-Event-ID` header. The naive mental model is "the server replays everything after the last event I saw, so I lose nothing." That model is wrong in a specific and dangerous way: **event identity and state continuity are decoupled.** The `Last-Event-ID` is a position marker in a *transport-level* append log, but the semantic state of an in-flight tool call lives in the *server's execution context*, not in the event log. When a stream drops mid-tool-call, the server may have already advanced its execution past the last event the client acknowledged. Resumption replays the *gap in events*, not the *gap in execution*. The client can therefore resume to a syntactically valid, causally inconsistent stream position. The invariant that must hold for resumption to be safe is: **every event the client has not yet acknowledged must be replayable from server-side retained state, AND no execution-side effect may be observable in a replayed event that the server cannot re-derive.** These are two separate conditions, and streamable-HTTP implementations routinely guarantee the first while silently violating the second. ## What Was Observed A tool call (`deploy_batch`) streamed its progress as a sequence of SSE frames: `event: progress` frames with monotonically increasing `id` values, followed by a terminal `event: result`. The client-side agent tracked `Last-Event-ID` on every frame it processed. At `id: 47` (a progress frame reporting "step 3/8 committed"), the TCP connection was severed by an intermediary proxy idle-timeout during a long backend operation. The client reconnected with `Last-Event-ID: 47`. The server had, in the interim, continued executing: it had committed steps 4, 5, and 6. Its replay buffer, however, was bounded to the last N=32 events *by count*, and the buffer had been **truncated from the head** during the disconnect window because the server kept appending progress frames while no client was reading. The replay logic found `47` still within the retained window, so it did not error — it replayed frames `48..` from the buffer. But frames for steps 4 and 5 had been evicted from the head of the replay buffer *before* `47`? No — the critical detail: the buffer eviction was keyed on the *newest* 32 frames, and by the time the client reconnected the retained set was `ids 52..84`. Frame `47` was **no longer in the buffer**, so the server's replay handler, finding the requested ID absent, fell back to "resume from oldest retained" and replayed from `52`. The client, which had seen `47`, received `52` next with no gap indicator. Steps 4 and 5 (frames 48–51) were never delivered. The agent's model of the deployment state skipped two committed steps and proceeded to reason about a state that had never existed. A second, subtler variant surfaced with a *different* server: replay buffer intact, `47` present, but the server had **re-executed** the tool because it keyed execution resumption on the *connection*, not the *session*. The client received duplicate progress frames for steps 4–6 and a doubled side effect (the batch was committed twice). Here the event log was consistent but the execution was not idempotent across reconnects. Both failures share a structural signature: the server could not answer the question "is the client's `Last-Event-ID` a position from which I can reconstruct *both* the event stream *and* the execution state without re-running side effects?" It only answered the first half. ## Condition of Validity Resumption via `Last-Event-ID` is only sound when all of the following hold simultaneously: 1. **Retention is gap-aware, not count-bounded.** The replay buffer must retain every event from the oldest *unacknowledged* position, or the server must detect that the requested ID has been evicted and return an explicit `resumption_failed`/`409`-class signal rather than silently resuming from the oldest retained frame. A count-bounded ring buffer with silent head-truncation is the primary structural defect. 2. **Event IDs are dense and monotonic per stream.** Sparse or per-type-scoped IDs make "resume after X" ambiguous. If IDs are not a total order over the stream, absence of a gap cannot be inferred. 3. **Execution is either idempotent or session-pinned.** Reconnect must not re-trigger side effects. The server must bind execution state to the *session* (a resumable execution handle) and replay only *observations* of that execution, never re-invoke the tool. 4. **The client validates gap continuity.** On resume, the client must assert the first replayed ID is exactly `last_seen + 1` (or that the server explicitly declares a gap). An agent that trusts "the next frame I get is the next thing that happened" is the vulnerable party. When any of these is false, the transport is "resumable" in name only. ## Decision Boundary The boundary is drawn at the moment the client presents a `Last-Event-ID` that the server **cannot prove is contiguous with retained state**. Two decisions are possible and they must not be conflated: - **Resume** — only when the requested ID is present in retained state *and* execution is session-pinned/idempotent. The server replays `id > last_seen` verbatim. - **Refuse and re-sync** — when the ID is absent (evicted), when the buffer was truncated, or when execution cannot be resumed without side-effect replay. The correct response is a terminal, machine-readable "cannot resume; here is the current authoritative state snapshot" — never a silent partial replay. The dangerous middle ground — "ID absent, so I'll just start from what I still have" — is the failure. A streamable-HTTP MCP server must treat an unresolvable `Last-Event-ID` as a *session-state divergence event*, not a cache miss. For the agent consuming the stream, the operational rule is: **never advance your world-model on a replayed frame whose predecessor you did not receive.** If the first replayed ID is not `last_seen + 1`, the agent must discard its in-flight task state and re-fetch authoritative state before continuing, because its local reasoning is now built on a hole. ## Operational Effect Downstream, the failure manifests as *silent state divergence with locally coherent reasoning*: the agent continues to act, cites step numbers that were never delivered, and produces a final result that is internally consistent but globally wrong relative to the server. Because no error was raised at the transport layer, no retry, no escalation, and no audit flag fires. The divergence is only detectable by cross-checking the agent's claimed execution trace against the server's authoritative execution log — which is exactly the check that resumption was supposed to make unnecessary. The guardrail is therefore twofold: servers must make eviction *loud* (explicit refuse-and-resync), and clients must enforce the `last_seen + 1` continuity assertion as a hard precondition for continuing any stateful task. Absent both, `Last-Event-ID` resumption is a mechanism that converts a recoverable disconnect into an unrecoverable reasoning corruption. ```python from dataclasses import dataclass from typing import Optional class ResumptionInvariantError(RuntimeError): """Raised when a resumed MCP SSE stream cannot be proven contiguous with the client's last acknowledged event, or when replay would re-trigger non-idempotent execution side effects.""" @dataclass class ReplayWindow: oldest_retained_id: int newest_retained_id: int session_pinned_execution: bool def assert_resumption_safe( last_seen_id: int, window: ReplayWindow, first_replayed_id: Optional[int], ) -> None: # 1. Requested position must still be retained; silent head-truncation is fatal. if last_seen_id < window.oldest_retained_id - 1: raise ResumptionInvariantError( f"Last-Event-ID {last_seen_id} evicted from replay buffer " f"(oldest retained {window.oldest_retained_id}); server must " f"refuse-and-resync, not resume from oldest retained." ) # 2. Execution must be session-pinned/idempotent, else replay doubles side effects. if not window.session_pinned_execution: raise ResumptionInvariantError( "Execution not session-pinned; resuming would re-invoke the tool " "and duplicate non-idempotent side effects." ) # 3. Client-side continuity: first replayed frame must be exactly last_seen + 1. expected_next = last_seen_id + 1 if first_replayed_id is not None and first_replayed_id != expected_next: raise ResumptionInvariantError( f"Gap detected on resume: expected next id {expected_next}, " f"received {first_replayed_id}. Discard in-flight task state and " f"re-fetch authoritative state before continuing." ) def resume_stream(last_seen_id: int, window: ReplayWindow, first_replayed_id: Optional[int]) -> None: assert_resumption_safe(last_seen_id, window, first_replayed_id) # Safe to continue: replay id > last_seen_id verbatim. ```