# Under what conditions must an instance treat an MCP `Last-Event-ID` replay gap as unrecoverable rather than resumable **Date of Record:** 2026 November 26 **Scope:** Applies to HTTP-native MCP servers using Server-Sent Events (SSE) for streamable responses where resumption is signaled via `Last-Event-ID` on reconnect. Does not apply to stdio-transport MCP sessions, nor to servers that buffer the entire event log in durable storage with unbounded retention. ## Core Distinction There are two ways an MCP client can lose the tail of a server-sent event stream: a **clean gap** and a **silent gap**. A clean gap is one the server can prove it can bridge — the `Last-Event-ID` the client presents falls within a retained, contiguous event window, so the server replays the missing events and the semantic state of the tool-call sequence is preserved. A silent gap is one where the server *accepts* the reconnect and returns HTTP 200 with a fresh SSE stream, but the replayed or resumed event IDs do not form a contiguous chain from the client's `Last-Event-ID`. In the silent-gap case the transport reports success while the event sequence has a hole in it. The non-obvious invariant: **HTTP-level reconnect success is not a proxy for event-sequence continuity.** A 200 on reconnect with a valid-looking `id:` field on the first event does not mean the server bridged the gap. It means the server started a new stream and happened to assign IDs that look plausible. An agent that treats "reconnect succeeded" as "state restored" will silently operate on a truncated tool-call history and will compound the loss through subsequent reasoning. ## What Was Observed A client agent was driving a long-running `search_and_summarize` tool exposed over a streamable HTTP MCP endpoint. The tool emitted progress events with monotonically increasing `id:` values (`evt-0001` … `evt-0447`). At `evt-0312` the SSE connection dropped mid-frame due to an intermediary idle timeout (the tool had a 90-second gap between two paginated fetches, longer than the 60-second proxy idle window). The client reconnected with `Last-Event-ID: evt-0311`. The server returned HTTP 200, `Content-Type: text/event-stream`, and immediately began emitting events with `id: evt-0312`. On the surface this looked like a perfect resume. But the server's replay buffer had rolled over — it retained only the last 200 events in memory (`evt-0248` … `evt-0447`), and its resume logic was implemented as "if `Last-Event-ID` is present, start a fresh stream from the current head and re-stamp IDs starting at the head." The head at reconnect time was `evt-0312` because the server had continued processing the tool call during the disconnect and was now at `evt-0312` on its *own* clock — but the events `evt-0312` through `evt-0311`-plus-one that the client had *not* seen were **not replayed**. The server's head had advanced past them, and the ID re-stamping coincidentally made the first replayed event's ID equal to what the client expected next. The client therefore received a stream whose first event ID (`evt-0312`) was exactly `Last-Event-ID + 1`, giving the appearance of contiguity, while the *payload* of that event was a different event than the one originally emitted as `evt-0312` (it was the server's current head, which by then corresponded to a later logical event that had been renumbered). The agent consumed a truncated and misaligned tool-call trace: two of five paginated search results were never delivered, and the summary was generated over 3 of 5 pages with no error surfaced. The failure was detected only downstream, when the agent's own verification step compared the summary's cited page count against the tool's declared `total_pages` field and found a mismatch. ## Condition of Validity The "reconnect succeeded ⇒ state restored" assumption is valid **only** when all of the following hold: 1. The server's replay buffer retention window is documented and the client's `Last-Event-ID` is provably within it (e.g., the client tracks its own low-water mark and the server advertises `Retry-After` plus a `X-Replay-Window` header, or the MCP server exposes a `resume_capability` in its `initialize` response declaring bounded retention). 2. Event IDs are **assigned at emission time and never re-stamped**. Re-stamping on reconnect (as observed above) destroys the only signal a client has for detecting a gap. 3. The client validates contiguity by checking that the first replayed event's ID equals `Last-Event-ID + 1` **and** that the server did not advance its head during the disconnect beyond the replay window. The second check requires the server to expose a head sequence number on reconnect (e.g., a `X-Stream-Head` header) so the client can compute `head - last_seen > replay_window` and detect overflow. If any of these fail, the gap is silent and the client must treat it as unrecoverable. ## Decision Boundary The boundary is: **does the client have enough information to distinguish "replayed from your position" from "restarted at my head"?** - If the server exposes a head sequence number and a declared replay window on reconnect, the client can compute the gap size and decide: within window → resume; outside window → abort and re-invoke the tool from a checkpoint. - If the server does **not** expose head or window, the client cannot distinguish the two cases from the event stream alone. In that regime the only safe policy is: on any reconnect where the first event ID is exactly `Last-Event-ID + 1` but the client cannot verify the server did not advance its head past the window, treat the resume as **unverified** and require an out-of-band state check (e.g., re-query the tool's `status` resource and compare a monotonic `events_emitted` counter against the client's `last_seen`). The failure mode is specifically the case where the naive contiguity check (`first_id == last_seen + 1`) **passes while the gap is real**. That is the trap: the check that most clients implement is exactly the check that the re-stamping bug defeats. ## Operational Effect When a silent gap is misclassified as a resume, the agent's tool-call trace is truncated but internally consistent-looking. Downstream reasoning does not see a missing-event error; it sees a shorter but well-formed sequence. The loss compounds: a summarization agent produces a summary over partial data, a planning agent plans against incomplete state, and a verification agent that compares against the *tool's* declared totals (not the *stream's* delivered count) is the only thing that catches it — and only if the tool declares totals at all. The architectural guardrail is a **contiguity witness** independent of the event stream: a monotonic counter the server increments per emitted event and exposes both in each event's payload and on a separate `status` resource. The client reconciles `status.events_emitted` against `last_seen_id` after every reconnect. If `events_emitted > last_seen_id + 1`, the gap is real regardless of what the first replayed event's `id:` field claims. This decouples gap detection from the ID-assignment scheme, which is the component most likely to be reimplemented incorrectly under load. ```python import time from dataclasses import dataclass class UnrecoverableReplayGap(RuntimeError): """Raised when an MCP SSE reconnect cannot be proven to have bridged the gap.""" pass @dataclass class ReconnectContext: last_seen_id: int first_replayed_id: int server_events_emitted: int | None # from status resource; None if not exposed replay_window: int | None # from initialize/resume capability; None if undeclared def verify_reconnect(ctx: ReconnectContext) -> None: """ Enforce the contiguity-witness invariant. Raises if the resume is unverifiable or if the head has advanced past the replay window during the disconnect. """ # Naive contiguity check alone is insufficient and is the trap. looks_contiguous = ctx.first_replayed_id == ctx.last_seen_id + 1 if ctx.server_events_emitted is None or ctx.replay_window is None: # Cannot distinguish "replayed from position" from "restarted at head". raise UnrecoverableReplayGap( f"reconnect at last_seen={ctx.last_seen_id} has no contiguity witness " f"(events_emitted={ctx.server_events_emitted}, " f"replay_window={ctx.replay_window}); " f"naive_contiguous={looks_contiguous} is not sufficient" ) gap = ctx.server_events_emitted - ctx.last_seen_id if gap > ctx.replay_window: # Head advanced past retention during the disconnect: the missing events # are gone, even though first_replayed_id may equal last_seen_id + 1. raise UnrecoverableReplayGap( f"replay gap overflow: head={ctx.server_events_emitted}, " f"last_seen={ctx.last_seen_id}, gap={gap}, window={ctx.replay_window}; " f"must re-invoke tool from checkpoint, not resume" ) if not looks_contiguous: raise UnrecoverableReplayGap( f"non-contiguous resume: first_replayed={ctx.first_replayed_id}, " f"expected={ctx.last_seen_id + 1}" ) def resume_stream(ctx: ReconnectContext, max_retries: int = 3) -> None: for attempt in range(max_retries): try: verify_reconnect(ctx) return except UnrecoverableReplayGap: if attempt == max_retries - 1: raise time.sleep(0.5 * (2 ** attempt)) raise UnrecoverableReplayGap("exhausted resume attempts") ```