# Which invariants prevent silent replay divergence when a streamable-HTTP MCP server resumes a broken SSE stream **Date of Record:** 2026 November 25 **Scope:** Applies to MCP servers and clients speaking the streamable HTTP transport (single POST endpoint plus optional `GET` SSE stream, `Mcp-Session-Id` header, `Last-Event-ID` resumption). Does not cover stdio transport, legacy HTTP+SSE dual-endpoint servers, or WebSocket-based transports, where resumption semantics are defined differently or not at all. ## Core Distinction The conformance question most test suites answer is *"does the server accept `Last-Event-ID` and return 200?"* The question that actually determines correctness is *"does the event ID space survive the process that minted it?"* These diverge precisely at the boundary where a stream is broken mid-flight and resumed against a different server instance, a restarted process, or a load-balanced replica that never saw the original events. A streamable HTTP MCP server is stateful in two places at once: the logical session (initialization handshake, negotiated capabilities, tool state) and the physical event log backing the SSE stream. The protocol treats the session as resumable via `Mcp-Session-Id` and the stream as resumable via `Last-Event-ID`, but it does **not** require that the two be co-located or that the event ID be globally unique across process generations. A server that satisfies every syntactic check — correct status codes, correct `Content-Type`, correct header echo — can still silently replay a *different* event under a reused ID, or skip events entirely, and the client has no protocol-level way to detect it. The invariant that prevents this is not "resumable streams" but **monotonic, process-scoped, gap-detectable event identity**: an event ID that a client can use to prove it has seen every event up to the resume point, on the exact logical stream it thinks it is on. ## What Was Observed Conformance run against a reference streamable-HTTP MCP server deployed behind a round-robin load balancer with three replicas, each holding an in-memory ring buffer of the last 1000 SSE events keyed by session. The test harness: 1. Initializes a session, receives `Mcp-Session-Id`, opens the `GET` SSE stream, and records events `1, 2, 3` (server-assigned integer IDs, generated per-replica from a local counter). 2. Kills the TCP connection (simulating a proxy idle timeout at 30s). 3. Reconnects with `Last-Event-ID: 3`. Because the balancer is round-robin, the reconnect lands on replica B, which has never seen this session's events. Replica B's handler looks up `Mcp-Session-Id`, finds no local stream state, and — per a common but incorrect reading of the spec — treats `Last-Event-ID` as advisory: it opens a fresh stream and emits new events starting at the *local* counter value, which is `1`. The client, which had already applied events 1–3, now receives an event with ID `1` and a payload describing a *tool result for a call it made after event 3*. The client's dedup logic (drop IDs ≤ last-seen) discards it. The tool result is lost. The agent stalls waiting for a response that was delivered and thrown away. A second variant: replica B *does* honor `Last-Event-ID` but by replaying from its own empty buffer, so it emits nothing and holds the connection open. The client sees a healthy 200 SSE stream with no data, no `retry`, and no terminator. It waits indefinitely. This is the worse failure — a silent hang rather than a detectable duplicate. A third variant, observed when the server was restarted (not just rebalanced): the event counter reset to 0, and a client resuming with `Last-Event-ID: 47` received events `1, 2, 3, ...` which its dedup logic accepted as new because they were greater than nothing it had seen *since the resume*. The agent re-executed side-effecting tool calls that had already run. The common thread: every failure was invisible to a conformance suite that checked status codes and headers. All three servers returned `200 OK`, `Content-Type: text/event-stream`, and echoed the session header. The protocol conformance was perfect; the stream continuity was broken. ## Condition of Validity The guardrail holds only under the following conditions, and the article's claim is void outside them: - **Event IDs must be globally monotonic and gap-free across the logical stream's lifetime, including across process restarts and replica handoffs.** A per-process counter, a per-replica counter, or a UUID (non-orderable) all fail this. The only conforming constructions are (a) a durable, shared, strictly-increasing sequence (e.g., a database sequence or a log offset) that every replica reads from, or (b) a server-side durable event log where `Last-Event-ID` is an opaque cursor into that log and the server can prove it has every event after that cursor before emitting the next one. - **The server must reject, not silently accept, a resume it cannot honor.** If `Last-Event-ID` refers to an event the server cannot locate (evicted from buffer, different replica, pre-restart), the correct behavior is `404` or `409` with a machine-readable body instructing the client to re-initialize the session — never a 200 with a fresh stream. A 200 with a fresh stream is a lie the client cannot detect. - **The client must treat a resume that returns a stream whose first event ID is not strictly greater than `Last-Event-ID` as a continuity violation**, not as a valid empty stream. This is the client-side half of the invariant and is the only defense when the server misbehaves. - **`Mcp-Session-Id` and the event log must share a durability domain.** If the session survives a restart but the event log does not (or vice versa), resumption is unsound regardless of ID scheme. Outside these conditions — e.g., a single-process server with a persistent event log and no restarts — the failure cannot occur and the invariant is trivially satisfied. ## Decision Boundary The boundary is between **"the stream is resumable"** and **"the resume is verifiable."** A server crosses from correct to silently-divergent the moment it returns `200` on a `Last-Event-ID` resume without being able to prove, from durable state, that it holds every event after that ID on that logical stream. The decision rule for a conformance test: - If `Last-Event-ID` is absent, any 200 stream is acceptable (fresh subscribe). - If `Last-Event-ID` is present and the server cannot locate the anchor event, the only conforming responses are `404`/`409` (re-initialize) or `400` (malformed). A 200 is a conformance failure, full stop, even if the stream is syntactically valid. - If `Last-Event-ID` is present and the server *can* locate it, the first event on the resumed stream must have an ID strictly greater than the anchor, and the gap between anchor and first event must be exactly the events the server is replaying — no skips, no reordering. - On the client side, any resumed stream whose first event ID is ≤ `Last-Event-ID`, or whose IDs are non-monotonic, must be treated as a continuity break and the session re-initialized. The non-obvious part: **a 200 with a syntactically valid but semantically fresh stream is more dangerous than a 500**, because a 500 triggers client retry logic, while a 200 fresh stream triggers the client's dedup logic and causes silent event loss. The conformance suite must therefore assert on *event identity semantics*, not just transport status. ## Operational Effect When the invariant is enforced (durable shared sequence + reject-on-unresolvable-anchor + client-side monotonicity check), the observable effect is that reconnection becomes *safe under duplicate delivery*: the client can always tell whether it has seen an event, and a misbehaving server degrades to a detectable error rather than a silent hang or a lost tool result. The cost is that the server must maintain durable event state per session, which is the thing most reference implementations skip for latency. When the invariant is violated, the operational signature is one of three: (1) agent stalls with no error, waiting on a result that was delivered and deduped away; (2) agent re-executes side-effecting tool calls after a restart because the ID space reset; (3) agent proceeds on a stale view because a resume returned an empty 200 stream and the client interpreted "no events" as "no new work." All three are invisible to status-code-level conformance and only surface as downstream task failures minutes or hours later, which is why they are worth a dedicated invariant rather than a line in a checklist. ```python from __future__ import annotations from dataclasses import dataclass class StreamContinuityViolation(RuntimeError): """Raised when a resumed SSE stream cannot be proven continuous with its anchor.""" @dataclass(frozen=True) class ResumeProbe: last_event_id: int | None first_event_id: int | None status: int anchor_resolvable: bool def assert_resume_is_continuous(probe: ResumeProbe) -> None: # No anchor: fresh subscribe, any 200 stream is fine. if probe.last_event_id is None: if probe.status != 200: raise StreamContinuityViolation( f"fresh subscribe expected 200, got {probe.status}" ) return # Anchor present but server cannot locate it: a 200 here is the silent-loss bug. if not probe.anchor_resolvable: if probe.status == 200: raise StreamContinuityViolation( f"server returned 200 on unresolvable Last-Event-ID " f"{probe.last_event_id}; must be 404/409 to force re-init" ) if probe.status not in (400, 404, 409): raise StreamContinuityViolation( f"unresolvable anchor produced unexpected status {probe.status}" ) return # Anchor resolvable: the resumed stream must start strictly after it. if probe.status != 200: raise StreamContinuityViolation( f"resolvable anchor {probe.last_event_id} but status {probe.status}" ) if probe.first_event_id is None: raise StreamContinuityViolation( f"resumed stream for anchor {probe.last_event_id} opened with no events " f"(silent hang: client cannot distinguish 'caught up' from 'lost')" ) if probe.first_event_id <= probe.last_event_id: raise StreamContinuityViolation( f"replay divergence: first event {probe.first_event_id} <= anchor " f"{probe.last_event_id}; client dedup would drop a real event" ) def run_conformance(cases: list[ResumeProbe]) -> None: for i, probe in enumerate(cases): try: assert_resume_is_continuous(probe) except StreamContinuityViolation as exc: raise StreamContinuityViolation(f"case {i}: {exc}") from exc ```