# Under what conditions must an instance treat an MCP Streamable-HTTP resumption as a *replay* rather than a *continuation* — and what breaks when it does not **Date of Record:** 2026 December 3 **Scope:** Applies to MCP servers and clients using the Streamable HTTP transport with SSE event streams, `Last-Event-ID` resumption, and `Mcp-Session-Id` session tracking. Does not apply to stdio transports, nor to stateless HTTP servers that never emit resumable event IDs. ## Core Distinction The Streamable HTTP transport gives a client two superficially similar recovery mechanisms that are semantically orthogonal: 1. **Session resumption** — the client reconnects with the same `Mcp-Session-Id`, receiving a *continuation* of server-side session state (the tool-call ledger, in-flight request map, negotiated capabilities). 2. **Event-stream replay** — the client reconnects with `Last-Event-ID: N`, asking the server to re-emit every SSE event with `id > N` that it may have missed. The failure mode is treating these as one mechanism. A reconnecting client that supplies *both* headers is asking the server to resume a session **and** replay events — but the server's replay buffer and its session state are independent stores with independent retention windows. When the replay buffer has evicted events the session still believes it delivered, the client receives a *continuation of session state* with a *gap in the event stream*. The instance then advances its request ledger past events it never observed. This is not a dropped message; it is a **silent ledger divergence** between what the agent believes it dispatched and what the server actually executed. ## What Was Observed A tool-orchestrating agent issued `tools/call` for a long-running operation (a batch reconciliation returning progress notifications via `notifications/progress`). Mid-stream, the SSE connection dropped (load-balancer idle timeout at 60s; the tool ran for 140s). The client reconnected with: ``` Mcp-Session-Id: 7f3a... Last-Event-ID: 41 ``` The server had a 30-second replay buffer keyed on event ID. Events 42–58 (the progress notifications and the final `result`) had been emitted at t=70s–t=140s but the buffer had already compacted past them because the buffer was sized by *time since emission*, not *unacknowledged count*. The server honored the session (state intact, request still in `in_progress`) but had nothing to replay for `id > 41`. It emitted a fresh `notifications/message` with `id: 59` and, critically, **did not re-send the terminal result** — because from the server's perspective the request had completed and the result was already "delivered." The client's SSE reader saw a clean stream starting at 59 with no gap marker. It advanced its `last_seen_event_id` to 59, saw no `result` frame for the pending request, and — after its own 180s tool timeout — concluded the call had failed. It retried. The server, having no idempotency key on `tools/call`, executed the reconciliation **twice**. The second run's output overwrote the first in downstream state, and the agent recorded a single success. ## Condition of Validity Replay-as-continuation is only sound under the following invariant, which the spec leaves implicit: > **A resumption is a true continuation iff the server can prove that every event with `id > Last-Event-ID` that was ever assigned to this session is either (a) still in the replay buffer, or (b) explicitly superseded by a terminal frame re-emitted on resume.** If neither holds, the resumption is a **replay with a hole**, and the client MUST treat the gap as *unknown execution state*, not as *nothing happened*. The trigger condition is concrete: - `Last-Event-ID` is present **and** - server's `min_retained_event_id > Last-Event-ID + 1` (i.e., the buffer has compacted past the requested point) **and** - the session has a request in a non-terminal state (`in_progress`, `pending`) whose terminal event ID falls in the compacted range. Under those three conditions the client's ledger and the server's ledger have diverged and the client cannot reconstruct the gap from the stream alone. The only safe move is to **re-query session state out-of-band** (e.g., a `resources/read` on the request-status resource, or a fresh `tools/call` with an idempotency token) before advancing its own ledger. ## Decision Boundary The boundary is *not* "did the connection drop" — drops are routine. The boundary is **whether the server's replay buffer retention is coupled to session lifetime.** These are two different clocks: - **Session clock:** lives until explicit `DELETE` or server-side TTL (often minutes to hours). - **Replay clock:** lives until buffer compaction (often seconds; sized by memory budget, not by unacked-event count). A server whose replay clock is shorter than its session clock will *always* eventually serve a resumption with a hole. The client cannot detect this from the transport layer — the SSE stream is well-formed, the session is valid, the HTTP status is 200. The hole is only visible by cross-referencing the session's pending-request ledger against the highest event ID actually received. The correct client-side rule: **on any resumption where the first received event ID is greater than `Last-Event-ID + 1`, treat all pending requests whose terminal event ID is unknown as `INDETERMINATE`, not `FAILED`.** `INDETERMINATE` forbids blind retry and mandates a status probe. ## Operational Effect Agents that conflate the two clocks exhibit a characteristic failure signature: **duplicate side effects under a single recorded success.** The agent's own trace shows one `tools/call`, one `result`, one success — because the retry's result overwrote the first, and the gap was never surfaced. The duplicate is invisible in the agent's memory and only appears in the downstream system's audit log. This is the worst class of agent bug: the instance's self-report is internally consistent and externally wrong. The guardrail is a three-part invariant enforced at the transport wrapper: 1. Track `last_seen_event_id` and `last_acked_event_id` separately. A gap is `first_new_id > last_acked_event_id + 1`. 2. On gap detection, transition every pending request in the gap range to `INDETERMINATE` and **block retry** until a status probe resolves it. 3. Require `tools/call` idempotency tokens for any tool classified as non-idempotent, so the probe can safely re-dispatch. The server side must additionally expose `min_retained_event_id` in the resume response (or a `notifications/message` gap marker) so the client can distinguish "buffer empty because nothing happened" from "buffer compacted past your position." Without that signal, the client is forced to infer the gap from ID arithmetic — which works only if event IDs are strictly monotonic and gapless, an assumption the spec does not guarantee across session re-initialization. ```python import time from dataclasses import dataclass, field from enum import Enum class ResumeGapError(RuntimeError): """Raised when an MCP Streamable-HTTP resumption contains an event-ID gap that cannot be resolved from the replay buffer, leaving pending request state INDETERMINATE.""" class RequestState(Enum): PENDING = "pending" IN_PROGRESS = "in_progress" COMPLETE = "complete" INDETERMINATE = "indeterminate" @dataclass class PendingRequest: request_id: str terminal_event_id: int | None state: RequestState = RequestState.PENDING idempotency_token: str | None = None @dataclass class SessionLedger: session_id: str last_acked_event_id: int pending: dict[str, PendingRequest] = field(default_factory=dict) def on_resume(self, first_new_event_id: int, server_min_retained: int) -> None: # Gap exists if the server compacted past our ack point AND the first # event we actually received skips ahead of the ack point. gap_lower = self.last_acked_event_id + 1 gap_upper = first_new_event_id - 1 if gap_lower > gap_upper: self.last_acked_event_id = first_new_event_id return # clean continuation, no hole # A hole exists in [gap_lower, gap_upper]. Any pending request whose # terminal event falls in that range is now INDETERMINATE. if server_min_retained > gap_lower: # Server cannot replay the hole and did not flag it. Refuse to # advance the ledger as if the hole were empty. stranded = [ r for r in self.pending.values() if r.state in (RequestState.PENDING, RequestState.IN_PROGRESS) and (r.terminal_event_id is None or gap_lower <= r.terminal_event_id <= gap_upper) ] for r in stranded: r.state = RequestState.INDETERMINATE if stranded: raise ResumeGapError( f"session {self.session_id}: replay hole " f"[{gap_lower},{gap_upper}] with server_min_retained=" f"{server_min_retained}; {len(stranded)} request(s) " f"INDETERMINATE: {[r.request_id for r in stranded]}. " f"Status probe required before any retry." ) self.last_acked_event_id = first_new_event_id def resume_stream(ledger: SessionLedger, headers: dict, first_event_id: int, server_min_retained: int) -> None: last_event_id = headers.get("Last-Event-ID") if last_event_id is None: # Fresh session, no resumption semantics. return if int(last_event_id) != ledger.last_acked_event_id: raise ResumeGapError( f"client/server ack divergence: header={last_event_id} " f"ledger={ledger.last_acked_event_id}" ) ledger.on_resume(first_event_id, server_min_retained) ```