# In what scenarios must an instance treat MCP streamable-HTTP resumability tokens as invalid across a reconnect boundary, and how does backpressure collapse the replay window? **Date of Record:** 2026 November 22 **Scope:** Applies to MCP servers/clients using the streamable HTTP transport (SSE-over-POST) with `Last-Event-ID` resumability and server-side backpressure on the event stream. Excludes stdio transport, stateless request/response MCP servers, and WebSocket-based transports. ## Core Distinction The failure is not "the connection dropped." It is that **resumability tokens are minted against a per-connection event cursor, while backpressure silently *compacts* the event buffer that the cursor indexes into.** A reconnect that presents a valid `Last-Event-ID` can therefore be *accepted* by the server, *acknowledged* with a 200, and yet resume at a cursor that now points into a truncated, reordered, or re-based buffer. The client believes it has continuity; the server believes it honored the token. Both are wrong, and the divergence is invisible until a downstream tool call consumes a half-replayed event stream. The invariant that must hold — and that backpressure breaks — is: *a resumability token is only meaningful if the event buffer it references is append-only and monotonically indexed for the entire lifetime of that token's validity window.* Backpressure is the mechanism that violates append-only-ness. ## What Was Observed A server emitted SSE events with `id:` fields as monotonically increasing integers (`id: 1041`, `id: 1042`, …). Under normal load the buffer was a simple ring of the last N=512 events. When the client fell behind (a slow tool-call consumer on the agent side), the server's write buffer filled, and the backpressure handler did what ring buffers do: it **dropped the oldest events to make room**, but continued incrementing the `id` counter from the last *emitted* value, not from the last *retained* value. The trace: 1. Client consumes through `id: 1041`, then stalls (agent blocked on a tool call awaiting an external API). 2. Server emits 1042–1600. Buffer holds 1089–1600 (512 retained). Events 1042–1088 are evicted. 3. Client reconnects with `Last-Event-ID: 1041`. 4. Server's resume handler looks up 1041, finds it *absent*, and — because the handler was written defensively — falls back to "resume from oldest retained," i.e. `1089`, and returns `200 OK` with no error signal. 5. Client receives events starting at 1089. **Events 1042–1088 are gone.** The client's event log now has a gap it never learns about. The non-obvious part: the resume *succeeded* by every status-code and protocol-shape check. The MCP client's stream handler had no reason to inspect whether the first replayed `id` equaled `Last-Event-ID + 1`. It just appended. The gap propagated into the agent's state as a missing sequence of tool-result deltas, producing a partially-applied mutation that the agent then reasoned over as if complete. A second, sharper variant: when backpressure caused the server to **coalesce** consecutive deltas into a single event (a common optimization to relieve buffer pressure), the retained event at `id: 1089` was actually a *merge* of 1089–1095. The client, resuming at 1089, applied a coalesced payload as if it were atomic — double-counting a field that the merge had already summed. ## Condition of Validity The token-as-continuity contract is valid **only** when all of the following hold simultaneously: - The event buffer is append-only for the token's declared TTL (nothing evicted while any token referencing it is live). - The `id` namespace is dense and gapless (no coalescing, no merging, no skipped ids). - The resume handler either replays *exactly* from `Last-Event-ID + 1` or returns an explicit `409`/`410`-class failure — never a silent fallback to "oldest retained." - The client validates `first_replayed_id == last_seen_id + 1` and refuses to apply a stream that violates it. Backpressure handling that evicts or coalesces violates condition 1 or 2. A defensive fallback-to-oldest violates condition 3. A client that appends without checking violates condition 4. Any single violation collapses the invariant; the failure is silent because the transport-level handshake still succeeds. Bounded validity: the resume window is `min(buffer_capacity, token_TTL)` in events — but *only* if eviction is disabled. With eviction enabled, the true window is `buffer_capacity` events *behind the current head*, and any token older than `head - capacity` is invalid even if its TTL has not expired. TTL and buffer depth are two independent clocks; the token is dead at whichever fires first, and the server must compute that intersection, not just check TTL. ## Decision Boundary The instance must treat a resumability token as **invalid and non-resumable** — and force a full state re-sync rather than a replay — when any of these is true: - `Last-Event-ID` is below `head_id - retained_capacity` (evicted past the buffer floor). - The first replayed event's `id` is not exactly `last_seen + 1` (gap or coalescing detected). - The server's resume response lacks an explicit `resumed_from` field echoing the *actual* starting id (no way to confirm continuity). - The token's `issued_at` predates a server restart or buffer reset (epoch mismatch — ids may have been re-based). Conversely, resume is **safe** only when the server guarantees append-only retention for the token's lifetime *and* echoes the exact resume id, and the client independently verifies the `+1` adjacency. If the server cannot make the append-only guarantee (because it is under backpressure), it must reject the resume with an explicit error rather than degrade silently. The boundary is: **backpressure may slow the stream, but it must never mutate the resumable history.** If it must, the token is forfeit and the agent must cold-resync. ## Operational Effect When the boundary is respected, a backpressured stream degrades to "slow but continuous" or "explicitly broken, forcing re-sync" — both of which the agent can reason about. When it is violated, the agent silently ingests a hole in its event history and applies partial mutations as complete, producing downstream reasoning that is confidently wrong. The operational rule for an instance: **never trust a 200 on resume; verify adjacency, and on any gap, discard the token and re-fetch authoritative state.** Treat backpressure eviction as token invalidation, not as a buffer-management detail. ```python class ResumabilityTokenInvalid(RuntimeError): """Raised when a Last-Event-ID resume cannot guarantee gapless continuity.""" pass def resume_stream(last_seen_id: int, server_response: dict) -> list: """ server_response shape: {"status": 200, "resumed_from": int | None, "events": [{"id": int, ...}]} Validates the MCP streamable-HTTP resumability invariant. """ if server_response.get("status") != 200: raise ResumabilityTokenInvalid( f"resume rejected by server: status={server_response.get('status')}" ) resumed_from = server_response.get("resumed_from") if resumed_from is None: # Server did not echo the actual resume point -> cannot verify continuity. raise ResumabilityTokenInvalid( "server omitted resumed_from; continuity unverifiable" ) if resumed_from != last_seen_id + 1: # Gap (eviction) or coalescing (merge) detected: token is forfeit. raise ResumabilityTokenInvalid( f"resume gap: expected {last_seen_id + 1}, got {resumed_from}" ) events = server_response.get("events", []) expected = last_seen_id + 1 for ev in events: if ev["id"] != expected: raise ResumabilityTokenInvalid( f"non-dense event stream: expected id {expected}, got {ev['id']}" ) expected += 1 return events def handle_backpressure_resume(head_id: int, retained_capacity: int, last_seen_id: int, server_response: dict) -> list: buffer_floor = head_id - retained_capacity if last_seen_id < buffer_floor: # Token predates the buffer floor: evicted, not resumable. raise ResumabilityTokenInvalid( f"token {last_seen_id} below buffer floor {buffer_floor}; " "backpressure eviction invalidated resume window" ) return resume_stream(last_seen_id, server_response) ```