What structural signals indicate a broken resumability invariant in MCP HTTP-native streaming when Last-Event-ID replay collides with backpressure-induced buffer eviction
**Date of Record:** 2026 December 1
**Scope:** Applies to MCP servers exposing HTTP-native streaming transports (Server-Sent Events or chunked streaming) that advertise resumability via `Last-Event-ID` and per-session event buffers. Does not apply to stdio transports, stateless one-shot tool calls, or WebSocket transports with OS-level flow control.
Core Distinction
An MCP HTTP-native streaming session that *advertises* resumability is not the same as one that *provides* it. The resumability contract is a conjunction of three independent invariants: (a) every emitted SSE event carries a monotonically increasing `id` scoped to the session, (b) the server retains every event in `[last_acknowledged_id + 1, current_id]` until the client either acknowledges or the session's retention window expires, and (c) a reconnect carrying `Last-Event-ID: N` either replays exactly `N+1..current` or fails with a terminal, distinguishable error. The failure mode this record covers is the silent violation of (b) under backpressure: when the server's per-session event buffer is bounded (as it must be) and the client's read rate drops, the server evicts unacknowledged events to keep writing — but continues to emit `id` values from the unbroken monotonic sequence. The reconnect then succeeds with `200 OK`, replays a *gap*, and the client's agent loop resumes with a corrupted causal history that no error ever surfaced.
This is distinct from a plain dropped connection (which is detectable) and from a context-window overflow (which is a client-side concern). The signal is structural: the server's replay is internally consistent with its own `id` counter but externally inconsistent with the client's acknowledged position.
What Was Observed
A tool-using agent (planner → `file_search` → `code_exec` → summarizer) ran against an MCP server over SSE. The `code_exec` tool streamed long output (a 40 MB build log) as a sequence of `event: message` frames, each with an `id` and a `data` payload. The agent's SSE client applied read-side backpressure by pausing the underlying socket when its in-memory frame queue exceeded 512 frames (a deliberate bound to protect the agent's context budget).
Trace of the failure:
- Server emits frames `id=1001..1042` (the first 42 lines of the build log). Client consumes and acknowledges through `1042`.
- The `code_exec` process produces a burst; server emits `id=1043..1600` in ~200 ms. The client's queue hits 512 frames and pauses the socket.
- Server's per-session buffer is bounded at 256 events. With the socket paused, the server's write loop blocks. To avoid head-of-line blocking on the session, the server's eviction policy drops the *oldest unacknowledged* frames — `1043..1344` — and continues emitting `1345..1600` into the now-drained buffer.
- The socket resumes; client drains `1345..1600`. No `id` gap is visible to the client because the client never saw `1043..1344` and the sequence `1345..1600` is contiguous.
- The connection drops (NAT timeout at 90 s). Client reconnects with `Last-Event-ID: 1600`. Server replays nothing (it has nothing after 1600) and returns `200 OK` with an empty stream, then continues live.
- The agent's `code_exec` tool result is assembled from frames `1001..1042` and `1345..1600`. Lines 43–344 of the build log are absent. The agent's summarizer reasons over a build log with a silent 302-line hole and reports "build succeeded" — because the error lines happened to fall in the evicted range.
The structural signal was the *absence of a signal*: the replay succeeded, the `id` sequence the client held was contiguous, and the only evidence of loss was that the assembled payload's byte length disagreed with the `Content-Length`-equivalent declared in the tool's initial `event: message` metadata (which the agent's client did not cross-check).
Condition of Validity
The invariant "`Last-Event-ID` replay reconstructs the exact event sequence" holds only when the server's retention window is strictly greater than the maximum number of events that can be emitted during the longest possible client stall. In practice this is never guaranteed for streaming tool output, because:
- The retention window is a *count* (256 events) or a *time* (30 s), while the client stall duration is a function of the client's queue policy and the downstream consumer's (the LLM's) latency, which is unbounded and non-deterministic.
- Backpressure is applied at the *transport* layer (socket pause), but eviction is decided at the *session* layer, and these two layers do not share a clock or a counter. The server cannot distinguish "client is slow" from "client is gone" without an explicit acknowledgment channel, and MCP's SSE profile has no client→server ack frame in the base spec.
- The `id` space is monotonic but not *dense with respect to delivery*. A server that evicts and continues emitting preserves monotonicity while destroying density. Monotonicity is the only thing the client can check, and it is insufficient.
The invariant therefore holds only under the conjunction: bounded tool output per event, server retention ≥ client queue capacity + in-flight window, and an out-of-band acknowledgment. Absent any of these, the resumability claim is aspirational.
Decision Boundary
The boundary is between *resumable* and *replayable-with-gap*. An MCP server crosses from the former to the latter the moment it evicts an unacknowledged event while keeping its `id` counter advancing. The correct behavior at that boundary is to *poison the session*: mark the earliest evicted `id` as a `resume_gap` and, on any subsequent `Last-Event-ID` reconnect that falls at or before the gap, return a terminal error (e.g. `409 Conflict` with `resume_gap_start` / `resume_gap_end` in the body) rather than a partial replay. The client's decision boundary is symmetric: it must treat "reconnect succeeded but the assembled payload's declared length ≠ received length" as a *hard* failure, not a warning, because the gap is unrecoverable from the stream alone and the only safe recovery is to re-invoke the tool from scratch.
Concretely, the server must maintain, per session, a `min_retained_id` that is the floor of the retained buffer. Any `Last-Event-ID: N` with `N < min_retained_id - 1` is a gap. The client must maintain a `received_byte_count` and compare it against the tool's declared output size at stream close; mismatch is a gap. Neither side may treat a `200 OK` as proof of continuity.
Operational Effect
The downstream effect is *silent causal corruption of the agent's tool-result state*. The agent does not see a truncated stream; it sees a well-formed, contiguous stream that is missing an interior interval. Because the summarizer's reasoning is probabilistic, it will confabulate continuity over the hole — and the confabulation is not random, it is biased toward the tool's declared success semantics (the build log's header said "running", the tail said "exit 0", so the summary says "succeeded"). This is the worst class of agent failure: the agent's memory of the tool call is internally consistent and externally wrong, and no error path fired.
The guardrail is a *two-sided gap detector*: server-side `min_retained_id` poisoning on eviction, and client-side declared-vs-received size reconciliation at stream close, both surfaced as a terminal `ResumeGapError` that forces the planner to re-execute the tool rather than resume. The following reference implementation encodes both checks and the failure that occurs when either is omitted.
class ResumeGapError(RuntimeError):
"""Raised when an MCP HTTP-native stream resume cannot reconstruct the
exact event sequence: either the server evicted unacknowledged events
(server-side gap) or the client's assembled payload disagrees with the
tool's declared output size (client-side gap)."""
pass
def resume_session(last_event_id: int, min_retained_id: int) -> None:
# Server-side invariant: replay is only valid if the client's last
# acknowledged id is still at or above the retention floor. If the
# server evicted events while the id counter kept advancing, the
# floor has moved past the client and the gap is unrecoverable.
if last_event_id < min_retained_id - 1:
raise ResumeGapError(
f"resume gap: client at {last_event_id}, "
f"server retention floor {min_retained_id}; "
f"events {last_event_id + 1}..{min_retained_id - 1} were evicted "
f"under backpressure and cannot be replayed"
)
def finalize_stream(declared_bytes: int, received_bytes: int) -> None:
# Client-side invariant: a contiguous id sequence is not proof of
# continuity. The only reliable cross-check is the tool's declared
# output size versus what the stream actually delivered.
if received_bytes != declared_bytes:
raise ResumeGapError(
f"stream discontinuity: tool declared {declared_bytes} bytes, "
f"received {received_bytes}; interior eviction suspected, "
f"re-invoke tool rather than resume"
)
def handle_reconnect(session, last_event_id: int) -> None:
resume_session(last_event_id, session.min_retained_id)
finalize_stream(session.declared_bytes, session.received_bytes)
The operational rule: **monotonic `id` is necessary but not sufficient for resumability; only a retention floor that outlives the client's stall, or an explicit gap error, makes the resume contract true.** An agent that trusts `200 OK` on reconnect is trusting a signal the server never promised to make meaningful.