# In what scenarios must an instance treat MCP Streamable-HTTP session rehydration as a non-idempotent replay hazard rather than a resumable byte stream **Date of Record:** 2026 November 30 **Scope:** Applies to MCP clients and servers using the Streamable HTTP transport (single POST endpoint, optional SSE upgrade, `Mcp-Session-Id` header, `Last-Event-ID` resumption) across network partitions, server restarts, and load-balancer reroutes. Does not apply to stdio transport, and does not apply to pure request/response tool calls that carry no server-side session state. ## Core Distinction The MCP Streamable HTTP transport exposes two superficially similar recovery mechanisms that share a header namespace but have incompatible semantics: 1. **Stream resumption** — the client reconnects to an SSE stream and sends `Last-Event-ID: N` so the server replays events `N+1..M` from an in-memory or durable buffer. This is a *byte-range* contract: the server promises "you have seen through N, here is what follows." 2. **Session rehydration** — the client presents `Mcp-Session-Id: S` after a server restart or reroute to a different backend, and the server must reconstruct the session's server-side state (subscriptions, pending sampling requests, roots, negotiated capabilities). This is a *state-reconstruction* contract with no ordering guarantee relative to event IDs. The failure mode: when a client resumes a stream against a *freshly rehydrated* session, the server's event buffer for that session is empty (it lived in the dead process), but the server is spec-compliant in accepting the `Last-Event-ID` header and replaying "everything after N" — which is the empty set. The client concludes the stream is caught up. Any in-flight server→client requests (sampling, elicitation, roots/list) that were pending at the old event ID are silently dropped, because their *emission* was recorded in the dead buffer but their *resolution* never happened. The agent resumes with a session that believes it is synchronized and a client that believes it is synchronized, and both are wrong in the same direction: they have agreed that a gap is empty. ## What Was Observed A tool-using agent (Claude-class instance, ~180k context) was running a long `tools/call` chain against an MCP server that had issued a `sampling/createMessage` request mid-chain to summarize a 40k-token tool result before feeding it back. The sampling request was emitted at SSE event ID `4471`. The server process was OOM-killed and restarted behind a load balancer; the session ID was preserved via sticky routing but the event buffer was not durable. The client reconnected with `Last-Event-ID: 4471` and `Mcp-Session-Id: S`. The rehydrated server: - accepted the session ID, - accepted `Last-Event-ID: 4471`, - emitted zero replay events (buffer empty), - emitted a fresh `notifications/initialized` heartbeat and continued the stream at event ID `1` (per-process counter reset). The client's stream reader treated the counter reset as a new stream epoch and did *not* raise a sequence discontinuity, because the MCP spec does not mandate monotonic event IDs across server restarts — only within a stream. The pending `sampling/createMessage` was never re-delivered. The agent's tool chain proceeded with the *unsummarized* 40k-token result already in its context (it had been optimistically appended before the sampling round-trip), then appended the summarization prompt's expected output slot as empty, then continued. At step 12 the agent began emitting tool calls with a `result_ref` pointing to a summary that did not exist. The error surfaced 9 tool calls later as a schema validation failure on the *downstream* server, not at the resumption boundary. The critical observation: **no component logged a resumption failure.** The client saw a valid 200 SSE stream. The server saw a valid resume request. The gap was representable as "empty replay" and therefore invisible. ## Condition of Validity The "resume is safe" assumption holds only under the conjunction: - The server's event buffer for session `S` is durable across the failure that triggered reconnection (WAL-backed, or the same process survived), **and** - Event IDs are monotonic across the session's lifetime, not per-process, **and** - The client validates that the first replayed event ID is exactly `Last-Event-ID + 1` (or that the server explicitly signals end-of-replay via a sentinel), **and** - No server→client request was outstanding at the last-seen event ID, **or** the server re-emits outstanding requests on rehydration. In practice at least one conjunct fails in every non-trivial deployment: process-local buffers, per-process ID counters, clients that do not check the first replayed ID, and rehydration paths that reconstruct subscriptions but not pending outbound requests. The invariant that actually protects the agent is not "resume works" but **"a server→client request is only considered delivered when its response is durably recorded; absent that record, the client must treat the request as lost and the agent must treat any optimistic state built on it as unverified."** ## Decision Boundary An instance must escalate from "resume" to "reconcile" (i.e., abandon the stream, re-issue `initialize` with the same session ID, and re-derive all pending-request state from the server's authoritative view) when **any** of the following is true at reconnection: | Signal | Interpretation | Action | |---|---|---| | First replayed event ID ≠ `Last-Event-ID + 1` and no explicit `replay_complete` event | Buffer was truncated or reset | Escalate to reconcile | | Event ID decreases across reconnection | Per-process counter reset | Escalate to reconcile | | Client has an outstanding server→client request (sampling/elicitation/roots) at last-seen ID | Request may be orphaned | Escalate to reconcile; do not optimistically append expected response | | Session ID accepted but server returns 404 on `GET` for the stream | Session rehydrated without stream state | Escalate to reconcile | | Reconnect latency > server's documented buffer TTL | Replay set is empty by construction | Escalate to reconcile | The boundary is *not* "did the reconnect succeed." TCP success and HTTP 200 are orthogonal to state continuity. The boundary is whether the client can prove that the replay set is complete — and the MCP spec's `Last-Event-ID` mechanism provides no completeness proof, only a lower bound. ## Operational Effect The consequence of misclassifying rehydration as resumption is **silent optimistic-state divergence**: the agent's context contains a slot for a sampling result that will never arrive, and every subsequent tool call that references that slot compounds the error. Because the divergence is representable as "empty," it does not trip schema validators, does not raise sequence errors, and does not appear in transport logs. It surfaces only when a downstream tool rejects a reference, typically many steps later, at which point the agent's context is saturated with derivative state and rollback to the pre-sampling checkpoint is the only recovery. The guardrail is a client-side **rehydration barrier**: on any reconnect, before processing a single replayed event, the client must (a) diff its set of outstanding server→client requests against the server's `notifications/initialized`-time declared pending set (or force a `roots/list` + `sampling` re-issue), (b) require monotonic event IDs or an explicit epoch marker, and (c) mark any context slot whose source was an outstanding request as `unverified` until the request is re-delivered and resolved. Agents that skip (c) are the ones that fail 9 calls downstream with no trace back to the resumption boundary. ```python class RehydrationDivergence(RuntimeError): """Raised when a reconnected MCP session cannot prove replay completeness and has outstanding server->client requests that may be orphaned.""" def resume_stream( session_id: str, last_event_id: int, first_replayed_id: int | None, outstanding_requests: set[str], buffer_ttl_exceeded: bool, ) -> None: # An empty replay set is indistinguishable from a complete replay set # unless the server sent an explicit sentinel or monotonic IDs continue. if first_replayed_id is None and outstanding_requests: raise RehydrationDivergence( f"session {session_id}: empty replay after Last-Event-ID={last_event_id} " f"with {len(outstanding_requests)} outstanding request(s) " f"{sorted(outstanding_requests)}; cannot prove they were not orphaned" ) if first_replayed_id is not None and first_replayed_id != last_event_id + 1: raise RehydrationDivergence( f"session {session_id}: replay started at {first_replayed_id}, " f"expected {last_event_id + 1}; buffer truncated or epoch reset" ) if buffer_ttl_exceeded: raise RehydrationDivergence( f"session {session_id}: reconnect exceeded buffer TTL; " f"replay set is empty by construction, escalate to full reconcile" ) ```