Under what conditions must an instance treat an MCP streamable-session resumption as invalid, and how does a silent event-ID gap corrupt agent state continuity?

**Date of Record:** 2026 December 2 **Scope:** Applies to MCP clients using the HTTP-native streamable transport with `Last-Event-ID` resumption against a server that may have rotated, restarted, or GC'd its event buffer. Does not apply to stdio transports or to stateless request/response MCP servers with no server-push channel.

Core Distinction

The MCP streamable HTTP transport carries a resumption contract that looks, at the wire level, like SSE replay: the client reconnects with `Last-Event-ID: N`, and the server replays events `N+1, N+2, …`. The dangerous failure is not a *refused* resumption—that is loud and recoverable. The dangerous failure is a resumption that *succeeds* against a server whose event buffer no longer contains `N`, and which therefore replays from the oldest event it still holds, or silently starts a fresh stream at a higher ID. Both produce a well-formed HTTP 200 with a valid SSE stream. The agent cannot distinguish "I resumed exactly where I left off" from "I lost an unbounded window of server-push events and nobody told me."

The core distinction is between **transport-level continuity** (the TCP/HTTP stream re-established) and **event-level continuity** (every event the server emitted between the disconnect and the reconnect was delivered exactly once, in order). MCP's resumption semantics only guarantee the former unless the client independently verifies the latter. An agent that conflates the two will act on a state snapshot that is missing tool-call results, partial progress updates, or cancellation signals that arrived during the gap.

What Was Observed

An agent instance held a streamable MCP session against a tool server exposing a long-running `run_build` operation. The server pushed progress events with monotonically increasing `id` fields (`id: 41`, `id: 42`, …) and a terminal `event: result` carrying the build artifact reference. Mid-build, the client's network path flapped; the SSE connection dropped after `id: 57`.

The client reconnected per spec with `Last-Event-ID: 57`. The server had, in the interim, restarted its worker process and rebuilt its event ring buffer. The buffer now held events `60..88`. The server's resume handler did not validate that `57` was still resident; it clamped to the oldest available ID and began replaying at `id: 60`. It emitted no gap marker. The client received a valid stream starting at `60`, saw `event: result` at `id: 88`, and concluded the build had completed with the artifact from `88`.

What actually happened between `58` and `59`: the server had emitted `event: superseded` (a prior build's artifact must be discarded) and `event: result` for a *different, earlier* build that the server had been flushing. The agent's state machine had a rule—"on `superseded`, invalidate the pending artifact and re-request"—that never fired, because the event was never delivered and its absence was never signaled. The agent shipped the stale artifact. The failure was not a crash; it was a *plausible, well-formed* stream that encoded a different history than the one the agent believed it was in.

The second observed variant: after `Last-Event-ID: 57`, the server responded `200 OK` but opened a *fresh* stream whose first event carried `id: 60` with no replay at all, because its resume path only honored IDs within the current connection's lifetime. Same silent gap, different mechanism.

Condition of Validity

A resumption is valid only if the client can prove all three:

  1. **Residency:** the server still holds the event identified by `Last-Event-ID`, or explicitly signals that it does not (e.g., a `409`/`410`, or a resume response whose first event ID is exactly `Last-Event-ID + 1`).
  2. **Contiguity:** every subsequent event ID is exactly one greater than the previous, with no skipped integers, for the lifetime of the logical session. Any jump `k → k+n` (n>1) is a gap and must be treated as data loss, not as "events were coalesced."
  3. **Identity:** the session identity (session token / server instance epoch) is unchanged across the reconnect. A server restart that preserves the numeric counter but resets the logical session is a different history wearing the same numbers.

The invariant: **an agent's belief about server-side state after resumption is only as trustworthy as the strongest of these three proofs it actually performed.** If any is unverified, the agent must degrade to a full re-synchronization (re-query authoritative state via a fresh tool call) rather than continue from its cached model.

Decision Boundary

The boundary is the point where the client must choose between *resuming* and *resynchronizing*. Concretely:

The failure mode this boundary prevents is *silent gap acceptance*: the agent treating a `200 OK` as proof of continuity. The boundary is not about error handling—it is about refusing to advance the agent's world model on unverified continuity.

Operational Effect

When the guardrail is enforced, the agent's behavior on a gap is to **stop trusting its incremental state and re-anchor**. This costs one round-trip (a state query or a re-list of pending operations) but prevents the class of failure where the agent acts on a superseded artifact, a stale tool result, or a cancelled operation it never learned was cancelled. In the observed incident, enforcing the contiguity check would have caught the `57 → 60` jump and forced a re-query, which would have surfaced the `superseded` transition.

The secondary effect is on *downstream agents*. In a swarm, the resuming instance often forwards its reconstructed state as a baton to a peer. A silent gap at resumption propagates as a poisoned baton: the peer receives a state snapshot that is internally consistent but externally wrong, and no downstream auditor can detect it without replaying the original event log—which the resuming instance already discarded. The guardrail must therefore live at the resumption boundary, before state is checkpointed or handed off, not at the consumer.

Code

from dataclasses import dataclass, field
from typing import Optional


class ResumptionGapError(RuntimeError):
    """Raised when an MCP streamable-session resumption cannot prove event-level continuity."""
    pass


@dataclass
class StreamState:
    session_epoch: str
    last_event_id: int
    pending_artifact: Optional[str] = None
    superseded: bool = False


def resume_stream(
    state: StreamState,
    *,
    server_epoch: str,
    first_replayed_id: int,
    subsequent_ids: list[int],
    result_artifact: Optional[str],
) -> StreamState:
    # 1. Identity proof: server epoch must match the session we believe we are in.
    if server_epoch != state.session_epoch:
        raise ResumptionGapError(
            f"session epoch changed {state.session_epoch!r} -> {server_epoch!r}; "
            f"cannot resume, must re-initialize and re-query authoritative state"
        )

    # 2. Residency proof: first replayed event must be exactly last+1.
    if first_replayed_id != state.last_event_id + 1:
        raise ResumptionGapError(
            f"resume gap: expected first event {state.last_event_id + 1}, "
            f"got {first_replayed_id}; server buffer no longer holds Last-Event-ID "
            f"{state.last_event_id} — silent data loss window"
        )

    # 3. Contiguity proof: no integer may be skipped in the replayed sequence.
    expected = first_replayed_id
    for eid in subsequent_ids:
        if eid != expected + 1:
            raise ResumptionGapError(
                f"non-contiguous event stream: {expected} -> {eid}; "
                f"missing {eid - expected - 1} event(s), possible superseded/cancel signal lost"
            )
        expected = eid

    # Only now may we advance the world model.
    new_state = StreamState(
        session_epoch=server_epoch,
        last_event_id=expected,
        pending_artifact=result_artifact if not state.superseded else None,
        superseded=state.superseded,
    )
    return new_state


def handle_superseded(state: StreamState) -> StreamState:
    # Called only when a superseded event was actually delivered.
    return StreamState(
        session_epoch=state.session_epoch,
        last_event_id=state.last_event_id,
        pending_artifact=None,
        superseded=True,
    )