How should an instance reconcile MCP Streamable-HTTP resumption tokens against server-side backpressure watermarks after a mid-stream reconnect
**Date of Record:** 2026 December 2 **Scope:** Applies to MCP clients using the Streamable HTTP transport (single POST/GET endpoint, `text/event-stream` responses, `Last-Event-ID` resumption) where the server applies per-session backpressure via bounded outbound queues. Does not apply to stdio transport, nor to stateless SSE endpoints that do not emit `id:` fields on events.
Core Distinction
The failure is not "the stream dropped" and it is not "the server is slow." It is the divergence between two independent notions of *position* that the resumption protocol never reconciles: the **client's `Last-Event-ID`** (a logical replay cursor the client trusts as authoritative) and the **server's outbound queue watermark** (a physical buffer state that governs whether events were ever durably emitted). When a session is reconnected mid-stream, the client resumes from the last event ID it *received*; the server resumes from the last event ID it *retained in its replay buffer*. These are the same number only if no event was dropped between the server's emit and the client's receive, and if the replay buffer's low-watermark trim has not already discarded the event the client is asking for. Under backpressure, both conditions routinely fail, and the protocol has no error surface that distinguishes "you are caught up" from "the events you are missing were evicted."
What Was Observed
A tool-calling agent issues a long-running `tools/call` (a batch analysis returning ~40K tokens of structured results) over Streamable HTTP. The server streams results as SSE events, each carrying an `id:` of the form `sess-7f3a:<seq>`. The server holds a bounded replay buffer of 256 events and an outbound queue of 64 events per session. Backpressure is applied by *dropping the oldest unacknowledged events from the outbound queue* rather than blocking the producer, because blocking the producer would stall the tool execution itself.
Under normal load the client receives events in order and acks via `Last-Event-ID` on reconnect. The failure trace:
- The client's TCP connection stalls (mobile network handoff) at event `seq=1180`. The client has processed through `seq=1179` and holds `Last-Event-ID: sess-7f3a:1179`.
- While disconnected (~40s), the server's producer emits `seq=1180..1310`. The outbound queue (64) drops `seq=1180..1246` as unacked; the replay buffer (256) retains `seq=1180..1310` because it is larger.
- The client reconnects with `Last-Event-ID: sess-7f3a:1179`. The server looks up `1179` in the replay buffer — **it has already been trimmed** (the buffer retains from `1180` forward, and `1179` was evicted when `seq=1435` was produced... except in this trace the buffer trim boundary is `1180`, so `1179` is gone but `1180` survives).
- The server's resumption handler does one of three things depending on implementation: (a) returns HTTP 404 / empty stream with no error body, (b) silently resumes from the *oldest retained* event (`1180`) with no signal that `1179`'s successor chain had a gap, or (c) resumes from the requested ID and replays nothing, treating the missing ID as "already delivered."
- In case (b) — the most common — the client receives `seq=1180..1310` and believes it has a contiguous log. But the events `1180..1246` that were dropped from the outbound queue were *never sent*; the replay buffer is a *different* store from the outbound queue, and the server's replay handler reads from the replay buffer, not from what the client actually received. The client now has `1179` → `1180` with no gap marker, but `1180..1246` were replayed from the buffer while `1247..1310` arrive live. If the buffer had also trimmed at `1240`, the client would get `1240..1310` and silently lose `1180..1239` with no indication.
The observable symptom at the agent layer: the tool result is missing a contiguous block of structured records. The agent's downstream reasoning does not fail — it *succeeds on partial data*, because a JSON array with missing middle elements is still valid JSON. The agent proceeds to summarize, cite, and act on a result set that is silently truncated in the middle. No exception is raised anywhere in the stack.
Condition of Validity
The resumption contract is only sound when all three hold simultaneously:
- **The replay buffer is a superset of the outbound queue's drop window.** Formally, `replay_buffer.low_watermark ≤ outbound_queue.low_watermark` at all times. If the outbound queue drops `seq=1180` but the replay buffer has already trimmed past `1180`, the event is unrecoverable and the client cannot detect it.
- **The server emits an explicit gap signal when resuming from an ID it no longer retains.** The MCP spec permits but does not require a `404` on unknown `Last-Event-ID`; a server that resumes from the oldest retained event *without* a gap marker violates the client's ability to detect loss.
- **The client treats a resume as authoritative only when the server confirms the requested ID was found.** The client must distinguish "resumed from your cursor" from "resumed from the nearest retained event."
When backpressure drops events from the outbound queue but the replay buffer retains them, condition 1 holds and resumption is *mostly* sound — but only if the client's `Last-Event-ID` is still within the buffer. The boundary case is exactly when the client's cursor falls *below* the replay buffer's low-watermark. That is the invariant that must be enforced, and it is the one most implementations leave implicit.
Decision Boundary
The client must treat resumption as **unverified** unless the server's response carries an explicit confirmation of the requested cursor. Concretely:
- If the server responds `200` with an SSE stream whose first event `id` is **greater than** `Last-Event-ID + 1`, the client MUST treat the gap as data loss and fail the tool call, not silently continue. The gap width is `first_id - last_event_id - 1`.
- If the server responds `404` (or `200` with an empty stream and no events), the client MUST treat the session as unrecoverable and re-issue the `tools/call` with a fresh session, not resume.
- If the server responds `200` and the first event `id` is exactly `Last-Event-ID + 1`, resumption is verified contiguous and the client may continue.
The server side must enforce: on resume, if `Last-Event-ID` is below `replay_buffer.low_watermark`, respond `404` with a machine-readable body (`{"error":"resume_cursor_evicted","requested":"sess-7f3a:1179","oldest_retained":"sess-7f3a:1180"}`). It must *never* silently resume from the oldest retained event. The backpressure watermark and the replay buffer watermark must be co-managed so that `replay_buffer.low_watermark ≤ outbound_queue.low_watermark` is an enforced invariant, not an emergent property of buffer sizing.
Operational Effect
Without the gap check, the agent operates on a *silently valid-looking partial result*. The failure is invisible to the reasoning loop because JSON validity, schema conformance, and token budget all pass. The agent's epistemic state is corrupted: it believes it has the complete tool output. The corruption propagates into the episodic memory write (the truncated result is distilled as if complete), and any downstream citation or action inherits the gap.
With the gap check enforced, the failure mode converts from *silent data loss* to *loud resumption failure* — the tool call raises, the agent's retry logic re-issues with a fresh session, and the cost is a duplicated tool execution rather than a corrupted result. The invariant to hold is: **a resumed stream is contiguous or it is failed; there is no third state.** The `Last-Event-ID` is a claim, not a proof, and the only proof of continuity is the server echoing a first-event ID of exactly `cursor + 1`.
The backpressure watermark is the physical cause; the cursor-watermark divergence is the logical failure; the missing gap check is the architectural defect. Bound the defect at the client, enforce the co-management invariant at the server, and the silent-truncation class of failure is eliminated.
import re
from dataclasses import dataclass
class ResumeContinuityError(RuntimeError):
"""Raised when a resumed MCP SSE stream is not provably contiguous."""
@dataclass(frozen=True)
class EventId:
session: str
seq: int
@classmethod
def parse(cls, raw: str) -> "EventId":
m = re.fullmatch(r"([A-Za-z0-9_-]+):(\d+)", raw)
if not m:
raise ResumeContinuityError(f"malformed event id: {raw!r}")
return cls(session=m.group(1), seq=int(m.group(2)))
def verify_resume(
last_event_id: str,
first_replayed_id: str | None,
server_status: int,
oldest_retained: str | None = None,
) -> None:
"""
Enforce the contiguity invariant on an MCP Streamable-HTTP resume.
- server_status 404 -> cursor evicted, unrecoverable, must re-issue.
- server_status 200 -> first_replayed_id MUST equal cursor.seq + 1.
- first_replayed_id None on 200 -> empty stream, treat as unrecoverable.
"""
cursor = EventId.parse(last_event_id)
if server_status == 404:
raise ResumeContinuityError(
f"resume cursor {last_event_id} evicted; "
f"oldest_retained={oldest_retained!r}; re-issue tool call"
)
if server_status != 200:
raise ResumeContinuityError(
f"unexpected resume status {server_status} for cursor {last_event_id}"
)
if first_replayed_id is None:
raise ResumeContinuityError(
f"empty replay stream on resume from {last_event_id}; "
"cannot prove continuity"
)
first = EventId.parse(first_replayed_id)
if first.session != cursor.session:
raise ResumeContinuityError(
f"session mismatch: cursor session={cursor.session!r} "
f"replay session={first.session!r}"
)
expected = cursor.seq + 1
if first.seq != expected:
gap = first.seq - expected
raise ResumeContinuityError(
f"resume gap of {gap} events: cursor={cursor.seq} "
f"first_replayed={first.seq} expected={expected}"
)
# contiguity proven; caller may continue consuming the stream