Under what conditions must an instance treat MCP streamable-session resumption as unsafe when the last acknowledged event ID precedes a backpressure drop

**Date of Record:** 2026 December 2 **Scope:** Applies to MCP servers using the streamable HTTP transport with `Last-Event-ID` resumption over SSE-style event streams, where the client (agent runtime) maintains a session across reconnects. Does not apply to stdio transport, nor to stateless request/response MCP deployments without event replay.

Core Distinction

The streamable HTTP transport gives an MCP client two superficially similar recovery primitives: *reconnect* (transport-level, resuming an event stream from `Last-Event-ID`) and *retry* (application-level, re-issuing an idempotent tool call after a timeout). These are conflated by most agent runtimes because both are triggered by the same symptom — a dropped connection mid-call — and both are mediated by the same session identifier. They are not equivalent. Reconnect assumes the server retains a replayable event log keyed by monotonic event IDs and that the client's last acknowledged ID is a *prefix* of that log. Retry assumes the server is stateless with respect to the in-flight call and that re-execution is safe.

The boundary failure occurs when the server's event log has been **truncated by backpressure** — specifically, when a slow consumer (the agent, blocked in a long reasoning step or a downstream tool call) causes the server's per-session ring buffer to evict events *that the client has not yet acknowledged*. On reconnect, the client presents a `Last-Event-ID` that is now **older than the server's oldest retained event**. The server has three possible behaviors, and only one is safe:

  1. Replay from the oldest retained event (silently skipping the gap) — **unsafe**.
  2. Return `404`/`410` and force a fresh session — **safe but lossy**.
  3. Return a resumption response with an explicit gap marker — **safe and recoverable**, but almost never implemented.

The non-obvious part: the client cannot distinguish case 1 from a normal resumption, because the event IDs it receives are still monotonic and well-formed. The gap is invisible unless event IDs are *dense* (no gaps in the numeric sequence) and the client checks density.

What Was Observed

An agent runtime executing a long tool-call chain (12 sequential `tools/call` invocations against a filesystem MCP server) held a single SSE stream open. Between calls 7 and 8, the agent spent ~40 seconds in local reasoning (a large context expansion), during which the server emitted 2,300 keepalive and progress events into a per-session buffer capped at 1,024 events. The buffer evicted events 1 through ~1,300. The connection then dropped (LB idle timeout at 60s).

On reconnect, the client sent `Last-Event-ID: 1287`. The server's oldest retained event was `2301`. The server — implementing naive "resume from last-known" semantics — began streaming from `2301`. The client accepted the stream, saw monotonic IDs (`2301, 2302, ...`), and concluded resumption was successful.

But events `1288` through `2300` contained two `notifications/progress` frames for call 7 (indicating a *failed* partial write that the server had retried internally) and the `result` frame for call 7 itself. The agent never saw them. It proceeded to call 8, which operated on a file in an inconsistent state (partial write from the retried call 7). The corruption was silent: the agent's transcript shows a clean sequence of 12 successful calls, but the filesystem state diverges from the transcript at call 7.

The failure is not the dropped connection. It is the **silent gap acceptance** on resumption, combined with the agent's assumption that "I received a `result` for every call I issued" implies "I received every event between calls." That assumption is false whenever the server has applied backpressure eviction.

Condition of Validity

The invariant that makes resumption safe is:

> For every reconnect where the client presents `Last-Event-ID = L`, the server's response stream begins at an event ID `E` such that `E == L + 1` (dense) or the server explicitly signals a gap in a resumption control frame.

This invariant holds only under the conjunction of:

The invariant fails — and resumption becomes unsafe — whenever any of these is violated. In practice, the most common violation is the last one: agent runtimes update `Last-Event-ID` in the transport layer on byte receipt, while the reasoning loop processes events in a separate queue. The ack pointer races ahead of the processing pointer by exactly the depth of the processing queue. Under backpressure, that depth is the number of events that get silently dropped.

Decision Boundary

The operational decision is: **on reconnect, does the agent resume the session or abandon it?**

Resume is safe iff the density check passes *and* the agent's last acknowledged event ID corresponds to an event it has fully processed (not merely received). Both conditions must be checked at the application layer; neither can be delegated to the transport.

The boundary is sharp:

The critical asymmetry: **the agent cannot recover by retrying the in-flight call**, because the call may have partially succeeded server-side (as in the observed case, where call 7's retry left a partial write). Retry is only safe for calls the agent *knows* did not commit; a gap in the event stream destroys that knowledge. The correct recovery is checkpoint-based, not retry-based.

Operational Effect

Consequences of getting this boundary wrong:

  1. **Silent state divergence.** The agent's transcript and the server's actual state diverge without any error signal. Downstream calls operate on inconsistent state. This is the worst failure class because it is undetectable from the agent's logs.
  2. **False idempotency assumptions.** Agents that treat `tools/call` as idempotent will retry after a gap, compounding partial writes. Filesystem, database, and message-queue MCP servers are all vulnerable.
  3. **Backpressure-induced deadlock avoidance attempts.** A server that blocks the producer on a full buffer (the safe option) will stall the agent's stream during long reasoning steps, which the agent may interpret as a hung connection and trigger a reconnect — re-entering the same boundary. The agent must distinguish "stream is quiet because producer is blocked on my slow consumption" from "stream is dead." This requires the server to emit a `backpressure/waiting` heartbeat that the agent treats as *not* a liveness failure.
  4. **Session poisoning propagation.** In multi-agent setups, a poisoned session's state can be handed off to a peer (via A2A or a shared MCP server) before the gap is detected, propagating the divergence. The gap check must run *before* any handoff, not after.

The guardrail is a single invariant enforced at the agent runtime: **never advance a session's processing pointer past an event ID without confirming density of the preceding stream segment.** This is cheap (one comparison per resumption) and catches the entire failure class.

class MCPResumptionGapError(RuntimeError):
    """Raised when a resumed MCP stream's first event ID does not densely
    follow the last fully-processed event ID, indicating server-side
    backpressure eviction and a silent event gap."""
    pass


class MCPStreamResumptionGuard:
    def __init__(self):
        self.last_processed_id: int | None = None
        self.poisoned: bool = False

    def on_event_processed(self, event_id: int) -> None:
        if self.last_processed_id is not None and event_id <= self.last_processed_id:
            # Duplicate replay; ignore, do not regress pointer.
            return
        self.last_processed_id = event_id

    def on_resume(self, first_received_id: int) -> None:
        if self.poisoned:
            raise MCPResumptionGapError(
                "Session already poisoned; must re-establish from checkpoint, "
                "not from Last-Event-ID."
            )
        if self.last_processed_id is None:
            # Cold start; no prior processing pointer to validate against.
            return
        expected = self.last_processed_id + 1
        if first_received_id > expected:
            self.poisoned = True
            raise MCPResumptionGapError(
                f"Resumption gap: last processed event {self.last_processed_id}, "
                f"but resumed stream begins at {first_received_id} "
                f"(missing {first_received_id - expected} events). "
                f"Server backpressure eviction suspected. Session is poisoned; "
                f"discard in-flight state and recover from last durable checkpoint."
            )
        if first_received_id < expected:
            # Server replayed already-processed events; benign, dedupe on process.
            return
        # Dense resumption: safe to continue.
        return


def resume_session(guard: MCPStreamResumptionGuard, first_received_id: int) -> None:
    guard.on_resume(first_received_id)