# In what scenarios must an instance treat MCP streamable-HTTP session resumption as unsafe across a stateful-terminating intermediary **Date of Record:** 2026 November 30 **Scope:** Applies to MCP clients and servers using the streamable-HTTP transport (SSE-over-POST with `Mcp-Session-Id` and `Last-Event-ID` resumption) where a reverse proxy, load balancer, or API gateway sits between them. Does not apply to stdio transport, nor to HTTP deployments where the client and server share a single connection with no retry-capable intermediary. ## Core Distinction The MCP streamable-HTTP transport defines resumption as a *logical* operation: the client reconnects with `Mcp-Session-Id` and `Last-Event-ID`, and the server replays every SSE event with a sequence number strictly greater than the supplied one, then continues the live stream. This is correct only if the server's event log is the sole authority on what the client has seen. A stateful-terminating intermediary breaks that assumption silently. The proxy holds an open upstream SSE connection and a downstream connection that are *not* the same socket. When the downstream client drops, the proxy may (a) keep the upstream connection alive and buffer events, (b) tear down the upstream connection and reconnect on the client's behalf without forwarding `Last-Event-ID`, or (c) inject its own keepalive/comment frames into the stream. In cases (b) and (c) the client's `Last-Event-ID` no longer describes the proxy's upstream position. The client believes it is resuming from event N; the server believes the proxy is a fresh subscriber at event M > N, or the proxy silently discards the replay window. The result is a **gap** — not a duplicate — in the event sequence, and gaps in MCP streams are where tool-call results and `notifications/progress` frames disappear. ## What Was Observed A client agent executing a long-running `tools/call` (a code-execution tool emitting progress notifications every 500 ms for ~90 s) was pinned behind an nginx `proxy_buffering on` layer with `proxy_read_timeout 60s`. At t≈60 s nginx closed the upstream connection, buffered the tail, and re-established upstream without propagating the client's `Last-Event-ID` header (it was not in nginx's forwarded-header allowlist). The client, on its next reconnect at t≈61 s, sent `Last-Event-ID: 412`. The server, seeing a new upstream connection with no `Last-Event-ID`, started a fresh replay from its retention head — event 388. The client received events 388–412 *again* (duplicates, handled) but events 413 onward arrived only after nginx's re-subscription, and events 405–412 that had been emitted during the buffering window were **never replayed to the server's new subscriber** because the server's per-connection cursor had advanced past them. The client's progress tracker stalled at 412 and its 90 s deadline fired at t=90 s while the tool was still running. The client issued a `tools/call` cancellation; the server, unaware of the resumption gap, had already committed the side effect (a file write) and returned success on a connection the client had abandoned. The failure is not "the proxy dropped a connection." It is that **the resumption contract is between client and server, but the sequence-number cursor is physically owned by the intermediary**, and the intermediary has no obligation to preserve it. ## Condition of Validity Resumption via `Last-Event-ID` is sound only when the following invariant holds end-to-end: > For every event the server emits with sequence `s`, the client either receives `s` exactly once, or receives a resumption response whose replay set provably covers every `s` in `(Last-Event-ID_client, s_head]` with no server-side cursor advancement that skips an unflushed range. This invariant is *violated* whenever any hop satisfies all three of: 1. It terminates the SSE stream (does not byte-forward). 2. It reconnects upstream without forwarding the client's `Last-Event-ID` (header stripping, or reconnect-on-behalf-of with a fresh request). 3. The server's replay buffer is bounded (a ring buffer of N events) such that a skipped range can fall out of retention before the client's next resume. Condition 3 is the non-obvious one. Even a *correct* forwarding proxy is unsafe if the server's retention window is shorter than the maximum time an intermediary may hold a connection open before reconnecting. A 60 s `proxy_read_timeout` against a server ring buffer sized for 30 s of events means a single proxy-induced reconnect can outrun retention. The guardrail is a *joint* sizing: `retention_window_events × max_event_rate > proxy_read_timeout + client_reconnect_backoff + clock_skew_margin`. ## Decision Boundary An instance should treat streamable-HTTP resumption as **unsafe and fall back to full re-initialization** (new `initialize`, new session, re-fetch of any state) rather than attempting `Last-Event-ID` resumption when any of the following is true: - The `Mcp-Session-Id` is unchanged but the server's response to the resume carries a `Last-Event-ID` *older* than the one the client sent (cursor regression — the server reset, or the proxy re-subscribed at head). - The client cannot confirm the intermediary forwards `Last-Event-ID` (no `Via`/`Forwarded` header discipline, or the deployment documentation does not assert it). - The tool call in flight has *non-idempotent side effects* and the client cannot prove the gap range is empty. In this case, resumption may duplicate or skip a `tools/call` completion; the only safe action is to abandon the session and reconcile out-of-band via an idempotency key. - The server advertises a retention bound (`Retry-After` or a capability field) shorter than the observed intermediary timeout. Conversely, resumption is *safe and preferred* when the intermediary is a pure L4 TCP forwarder, or when the server exposes a monotonic `Last-Event-ID` and the client has verified (via a probe stream) that a dropped downstream connection produces a replay whose first event equals `Last-Event-ID + 1`. ## Operational Effect The practical consequence is that **session resumption must be treated as a liveness probe, not a recovery mechanism, in the presence of stateful intermediaries.** The client should: 1. On every reconnect, assert `first_replayed_seq == last_seen_seq + 1`. If not, declare a gap. 2. On a gap, mark the in-flight `tools/call` as *indeterminate* and escalate to reconciliation rather than resuming. 3. Bound the total time spent in resumption attempts to less than the server's retention window, so a retry cannot itself cause the range to fall out of retention. The server-side invariant is that the replay buffer must be sized against the *worst-case intermediary timeout in the deployment*, not against the client's reconnect backoff, because the intermediary — not the client — controls when the upstream cursor advances. ```python class ResumptionGapError(RuntimeError): """Raised when an MCP streamable-HTTP resume cannot prove gap-free replay.""" pass def resume_stream(client, session_id: str, last_seen_seq: int, retention_window: int, intermediary_timeout_s: float, max_event_rate: float, in_flight_tool: dict | None) -> None: # Joint sizing invariant: retention must outlast the intermediary's # reconnect window plus client backoff, or a single proxy-induced # reconnect can outrun the replay buffer. safe_retention_s = retention_window / max_event_rate if safe_retention_s <= intermediary_timeout_s: raise ResumptionGapError( f"server retention {safe_retention_s:.1f}s <= intermediary " f"timeout {intermediary_timeout_s:.1f}s; resume cannot be proven " f"gap-free (session={session_id})" ) resp = client.post_resume(session_id=session_id, last_event_id=last_seen_seq) first_replayed = resp.first_event_seq if first_replayed is None: # Server reset or proxy re-subscribed at head without forwarding # Last-Event-ID. Cursor regression: every event in # (last_seen_seq, head] may be lost. raise ResumptionGapError( f"cursor regression on resume: sent Last-Event-ID={last_seen_seq}, " f"server resumed at head with no replay (session={session_id})" ) if first_replayed != last_seen_seq + 1: # Duplicate or gap. Duplicates are tolerable; gaps are not. if first_replayed > last_seen_seq + 1: gap = (last_seen_seq, first_replayed) if in_flight_tool and not in_flight_tool.get("idempotent", False): raise ResumptionGapError( f"non-idempotent tool {in_flight_tool['name']} spans gap " f"{gap}; abandon session and reconcile via idempotency key" ) raise ResumptionGapError( f"event gap {gap} on resume (session={session_id}); " f"re-initialize rather than continue" ) # first_replayed <= last_seen_seq: duplicates, safe to dedupe. ```