# Why does an instance lose resumability tokens when an MCP Streamable HTTP session outlives its last event ID? **Date of Record:** 2026 December 3 **Scope:** Applies to MCP servers implementing the Streamable HTTP transport with `Last-Event-ID` resumption and per-session SSE event buffers. Does not apply to stateless stdio transports or to SSE-only transports without resumability semantics. ## Core Distinction There are two distinct notions of "session liveness" in MCP Streamable HTTP, and they diverge under backpressure: 1. **Transport liveness** — the TCP/SSE connection is open and the server is still emitting `id:`-tagged events. 2. **Resumability liveness** — the server retains a bounded replay buffer keyed by event ID, such that a client reconnecting with `Last-Event-ID: N` can receive every event with id > N. The failure mode is not that a connection drops. It is that **the replay buffer is evicted on a different clock than the connection lifecycle**, so a client can hold a valid `Mcp-Session-Id` and a syntactically valid `Last-Event-ID` while the server has already discarded the events that ID refers to. The session is "alive" but not "resumable," and the protocol has no distinct error for this state. The client receives a fresh 200 with a new event stream starting at whatever id the server's monotonic counter is now at — a silent gap. ## What Was Observed A server-side MCP implementation using a per-session ring buffer of 256 events with a backpressure policy that *drops the oldest event when a slow consumer's queue exceeds the buffer bound*. This is a reasonable backpressure choice in isolation. The interaction with resumability is the problem. Trace: 1. Client opens `GET /mcp` with `Mcp-Session-Id: S`. Server assigns event IDs from a global monotonic counter, say `1000, 1001, ...`. 2. A long-running tool call emits a burst of `notifications/progress` events. The client's TCP receive window stalls (slow reader, GC pause in the runtime, mobile network). Server-side per-session queue grows. 3. Queue hits 256. Backpressure policy evicts oldest: events `1000..1010` are dropped from the buffer, but the *counter* keeps advancing. The client has only seen up to `1005`. 4. Client disconnects (or the SSE connection is reset by an intermediary). Client reconnects with `Last-Event-ID: 1005`. 5. Server looks up `1005` in the replay buffer. **It is gone** — evicted at step 3. Server's lookup returns "unknown ID." 6. Server's handler, written defensively, treats unknown `Last-Event-ID` as "client is too far behind, start fresh." It responds 200 with a new stream beginning at id `1250`. 7. Client's resumption logic sees a *forward* jump in event IDs with no error, no gap marker, and no `Mcp-Session-Id` rotation. It assumes continuity. Events `1006..1249` are silently lost — including, in the observed case, a `notifications/tools/list_changed` and the terminal `result` of the tool call whose progress events started the burst. The client resumes execution believing the tool call is still in flight (it never saw the terminal result) or believing the tool list is unchanged (it never saw the invalidation). Both are wrong, and neither is detectable from the client side without out-of-band state comparison. ## Condition of Validity The invariant that *must* hold for `Last-Event-ID` resumption to be sound is: > For any event ID `N` that the server has ever emitted to a session, the server MUST retain either (a) the event payload for `N`, or (b) an explicit tombstone recording that `N` existed and was not delivered, until the session is terminated. The naive ring-buffer implementation violates this: eviction removes both payload and existence record. The counter advancing past an evicted ID is the precise trigger — it makes the evicted ID *look* like a valid historical point (it's less than the current counter) while being unreplayable. This condition is only met by one of three designs: - **Unbounded replay buffer per session** (correct, memory-unsafe under adversarial slow consumers). - **Bounded buffer with tombstone-on-evict and a distinct resumption error** (correct; requires protocol-level handling of the tombstone case). - **Buffer bound tied to the SSE connection lifetime, not the event emission rate** — i.e., backpressure is applied by *blocking the producer* (the tool call) rather than by dropping events. This preserves the invariant by never evicting an undelivered event. The observed system used none of these. It used producer-non-blocking eviction, which is the one combination that is *neither* memory-safe *nor* resumption-sound. ## Decision Boundary The choice between blocking-producer backpressure and drop-oldest backpressure is not a performance tuning decision — it is a **semantic** decision about whether the MCP session is a reliable ordered log or a best-effort stream. The boundary: - If any tool in the session emits events whose loss changes the client's belief about world state (progress with side effects, list invalidation, terminal results), the session **must** be treated as a reliable ordered log. Drop-oldest is disallowed. Backpressure must propagate to the producer. - If all events are pure telemetry (heartbeats, non-authoritative progress percentages), drop-oldest is admissible — but then `Last-Event-ID` resumption must be scoped to a *separate* reliable channel, or the server must emit an explicit `resync-required` signal on gap. The failure occurred because the implementation applied a telemetry-grade backpressure policy to a state-bearing event stream. The decision boundary was crossed silently: the code path that emitted `notifications/tools/list_changed` and the code path that emitted `notifications/progress` shared the same buffer and the same eviction policy, despite having different reliability requirements. A second, subtler boundary: when the server *does* detect an unknown `Last-Event-ID`, it must not silently restart the stream. It must either (a) return a resumption error that forces the client to re-initialize the session (new `Mcp-Session-Id`, full state re-fetch), or (b) emit a gap marker event that the client is *required by spec* to treat as "state may have changed, re-query." Restarting at a higher counter with no marker is the specific anti-pattern. ## Operational Effect Downstream consequences observed and reproducible: - **Phantom in-flight tool calls.** Client never receives terminal `result`; its execution loop waits on a future that will never resolve, until a client-side timeout fires — but the timeout is typically longer than the session's idle timeout, so the client and server disagree about whether the call is live. - **Stale capability views.** A missed `tools/list_changed` means the agent continues issuing `tools/call` against a tool whose schema changed or which was removed. The server returns a schema-mismatch error that the agent misattributes to its own argument construction, triggering a retry loop against a dead capability. - **Idempotency-key collisions under resumption.** If the client retries the lost tool call after reconnecting, and the server's idempotency key is derived from `(session, event-id)` rather than `(session, request-id)`, the retry generates a *new* event ID and is treated as a distinct request — duplicate side effects. The idempotency key must be bound to the request, not the transport event. - **Silent divergence in multi-agent fan-out.** When an orchestrator fans out to N MCP sessions and one loses a gap, the orchestrator's aggregate state is now inconsistent with that one worker, and consensus/verification logic that assumes per-worker event-stream completeness will accept a partial result as complete. The guardrail is a **resumption contract** enforced on both sides: server retains tombstones for evicted IDs and refuses silent restarts; client treats any forward jump in event IDs across a reconnect as a mandatory resync trigger, not a continuation. The counter must never be allowed to advance past an ID whose fate is not recorded. ```python class ResumabilityGapError(RuntimeError): """Raised when a Last-Event-ID cannot be replayed and the server would otherwise silently restart the stream at a higher counter.""" pass class EventBuffer: def __init__(self, bound: int): self.bound = bound self.events: dict[int, bytes] = {} self.tombstones: set[int] = set() self.counter = 0 def emit(self, payload: bytes, reliable: bool) -> int: self.counter += 1 eid = self.counter if len(self.events) >= self.bound: # Evict oldest; record a tombstone so resumption can detect the gap. oldest = min(self.events) del self.events[oldest] self.tombstones.add(oldest) self.events[eid] = payload return eid def replay_from(self, last_event_id: int) -> list[tuple[int, bytes]]: if last_event_id in self.tombstones: # The client's resume point was evicted. We cannot honestly # claim continuity. Force a full resync instead of a silent # restart at self.counter. raise ResumabilityGapError( f"Last-Event-ID {last_event_id} was evicted and cannot be " f"replayed; session must be re-initialized" ) if last_event_id > self.counter: raise ResumabilityGapError( f"Last-Event-ID {last_event_id} is ahead of server counter " f"{self.counter}; session state is inconsistent" ) return [(eid, self.events[eid]) for eid in sorted(self.events) if eid > last_event_id] def handle_resume(buf: EventBuffer, last_event_id: int) -> list[tuple[int, bytes]]: try: return buf.replay_from(last_event_id) except ResumabilityGapError: # Do NOT silently restart. Surface to the transport layer so the # client receives an explicit resync-required signal. raise ```