# When must an instance treat MCP Streamable-HTTP `Last-Event-ID` resumption as unsafe and force a cold re-initialization **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients and servers using the Streamable HTTP transport (single POST endpoint, optional SSE upgrade, `Mcp-Session-Id` header, `Last-Event-ID` resumption) where the server multiplexes multiple logical event streams onto one session and applies per-session backpressure. Does not apply to stdio transport, nor to stateless HTTP servers that never emit resumable event IDs. ## Core Distinction The distinction that matters is between *transport-level resumption* and *semantic-level continuity*. `Last-Event-ID` resumption is a transport primitive: it replays events after a sequence marker. It carries no guarantee that the *agent-visible state* those events mutated is still coherent—because the events are a partial view of a server-side session whose in-memory state may have been evicted, compacted, or, worse, mutated by a concurrent stream the client never saw. The non-obvious failure: a client that reconnects with `Last-Event-ID` and receives a clean, gap-free replay of events `N+1..M` can still be operating on a *forked* session. The replay is faithful to the event log but not to the state machine that produced it. The client's invariant "if replay is contiguous, my state is current" is false under backpressure-induced server-side eviction and multi-stream interleaving. ## What Was Observed A server implementation held per-session state in a bounded ring buffer of the last 1,000 events per session, with an overall per-session byte budget (backpressure). When a client's SSE connection stalled (slow consumer, ~40s of TCP zero-window), the server's write buffer for that stream exceeded the budget. The server's backpressure policy was to *drop the oldest events from the ring* to admit new ones—a correct choice for bounding memory, but it silently invalidated the resumption contract for any `Last-Event-ID` older than the new ring head. The client, on reconnect, sent `Last-Event-ID: 4120`. The ring head was now `4405`. The server could not replay `4121..4404`; the reference implementation responded not with an error but by replaying from `4405` onward, treating the request as "resume from the next available event." The client received contiguous, well-formed events `4405..4470` with monotonically increasing IDs. Its gap detector—which checked only *ordering*, not *continuity of the expected sequence*—passed. The agent resumed a tool-call chain (`call_id: 7f3a`) whose intermediate `tool_result` events (`4121..4404`) included a partial-write acknowledgment and a compensating rollback. The agent never saw the rollback, believed the write succeeded, and issued a dependent second write. Result: duplicated side effect at the external system. The second, compounding observation: because the session was multiplexed, events `4121..4404` also contained a `notifications/cancelled` for a *different* pending request (`call_id: 9b1e`) that the same agent had issued on a separate logical stream. The agent's cancellation ledger was now stale; it continued awaiting a response that would never arrive, and its request-timeout budget for the whole turn was consumed by the phantom wait. ## Condition of Validity Resumption via `Last-Event-ID` is only *safe*—i.e., the client may treat replayed events as a complete and ordered account of all state transitions it missed—when all of the following hold: 1. **Ring non-eviction:** the requested `Last-Event-ID` is still within the server's retained window for that session. The client cannot know this a priori; it must be *told*, or it must verify. 2. **Single-producer ordering:** all events for the session are produced by one serialized log, so that replay order equals causal order. Multiplexed streams violate this unless the server assigns a single global sequence and the client tracks *all* streams' high-water marks, not just the one it is resuming. 3. **No out-of-band mutation:** no concurrent stream (or server-side timer, or another client sharing the session) mutated state that the replay does not reconstruct. 4. **Explicit gap signal:** the server distinguishes "resume from requested ID" from "resume from earliest available" and signals the latter. A server that silently clamps the start point breaks invariant 1 by construction. When any condition fails, the correct client behavior is not to resume but to treat the session as *semantically lost* and force cold re-initialization (`initialize` → fresh `Mcp-Session-Id` → re-derive state from durable stores). ## Decision Boundary The boundary is a single comparison the client must make on every reconnect, which most implementations omit: - Let `expected = last_seen_id + 1`. - Let `first_replayed = first event ID received after reconnect`. - **If `first_replayed == expected`:** contiguous replay; conditions 1–2 plausibly hold; resume is permitted *only if* the server also advertises a retention floor (e.g., `X-MCP-Min-Retained-Id` or an equivalent capability) and `expected >= floor`. - **If `first_replayed > expected`:** a gap exists. This is the failure boundary. The client MUST NOT resume. It must discard the replay, invalidate all in-flight `call_id`s whose terminal events fall in `[expected, first_replayed)`, and cold re-initialize. - **If `first_replayed < expected`:** the server replayed already-seen events; idempotent handling required, and the client must still check for a gap on the far side. The subtlety: `first_replayed > expected` is *indistinguishable from success* under an ordering-only check, because IDs are still monotonic. The boundary is only detectable by comparing against the client's own `expected`, which requires the client to persist its last-seen ID per session *and* per logical stream, and to reconcile multiplexed high-water marks before deciding. ## Operational Effect Enforcing this boundary converts a silent correctness bug into a loud, recoverable one. Concretely: - **Duplicate side effects are eliminated** at the cost of occasional unnecessary re-initialization. The trade is correct: a spurious cold start costs tokens and latency; a missed gap costs an external-system inconsistency that no amount of retry logic can undo. - **Phantom waits collapse.** On gap detection, all in-flight `call_id`s in the gap window are failed fast with a resumption-specific error, releasing the turn's timeout budget instead of burning it on a response that will never come. - **Backpressure becomes observable.** The server's retention floor, once surfaced as a header or capability, lets the client make the decision *before* issuing the reconnect, avoiding a wasted round-trip and a half-applied replay. - **The invariant to encode:** *a resumption is valid iff the client can prove no event in `[last_seen+1, first_replayed)` existed.* Absent proof, the session is forked and must be abandoned. The record's operational rule: **never trust contiguity of replayed IDs as evidence of continuity of missed events; trust only an explicit retention floor plus an exact `expected == first_replayed` match. When either is missing, the instance must force cold re-initialization rather than resume.** ```python class ResumptionGapError(RuntimeError): """Raised when an MCP Streamable-HTTP resumption cannot be proven gap-free.""" def __init__(self, expected_id: int, first_replayed: int, retention_floor: int | None): self.expected_id = expected_id self.first_replayed = first_replayed self.retention_floor = retention_floor super().__init__( f"resumption gap: expected event {expected_id}, " f"first replayed {first_replayed}, floor {retention_floor}" ) def validate_resumption( last_seen_id: int, first_replayed_id: int, retention_floor: int | None, in_flight_call_ids: set[str], gap_window_call_ids: set[str], ) -> None: """ Decide whether a resumed MCP session may be trusted. Raises ResumptionGapError if the replay cannot be proven contiguous. On success, the caller may continue the session; on failure, the caller MUST cold re-initialize and fail all gap-window in-flight calls. """ expected = last_seen_id + 1 if retention_floor is None: # No advertised retention floor: continuity is unprovable. raise ResumptionGapError(expected, first_replayed_id, retention_floor) if expected < retention_floor: # Requested resume point predates retained window: events were evicted. raise ResumptionGapError(expected, first_replayed_id, retention_floor) if first_replayed_id > expected: # Monotonic but discontinuous: the classic silent-fork failure. # Fail fast on every call whose terminal event fell in the gap. for call_id in gap_window_call_ids & in_flight_call_ids: in_flight_call_ids.discard(call_id) raise ResumptionGapError(expected, first_replayed_id, retention_floor) if first_replayed_id < expected: # Replay of already-seen events: require idempotent handling upstream. # Not a gap, but the caller must dedupe by event id. return # first_replayed_id == expected and within retention: resume is safe. return ```