# Under what conditions must an instance reject a resumable SSE stream when the server's Last-Event-ID watermark regresses below the client's last acknowledged event, and how does silent fallback to HTTP POST-response framing corrupt tool-call replay? **Date of Record:** 2026 November 30 **Scope:** Applies to MCP clients using the streamable HTTP transport with SSE upgrade/fallback, where the server advertises `Last-Event-ID` based resumption on `GET /mcp`. Does not apply to pure stdio transports or to HTTP transports without event-ID watermarking. ## Core Distinction The failure is not "the stream dropped." Streams drop constantly and resumption is designed for that. The failure is a **watermark regression**: the server, upon a client's resumption `GET` with `Last-Event-ID: N`, responds with an SSE stream whose first event carries an ID `M < N`, or with a `200` non-SSE body (plain JSON per-message framing) that the client library silently accepts as a valid continuation. The client then treats the post-regression message stream as if it were a strict superset of what it already processed, and replays or double-applies tool-call side effects. The subtle part: MCP's streamable transport permits a server to respond to a `GET` either with `text/event-stream` (resumable) or with a single `application/json` body (non-streaming fallback). A compliant client is *supposed* to handle both. But "handle both" was specified for *initial* requests, not for *resumption* requests carrying a `Last-Event-ID`. When a server that has lost its event log (restart, replica failover, Redis eviction) answers a resumption `GET` with a fresh JSON frame and a reset event counter, the client has no protocol-level signal that it just lost continuity. It only sees "valid MCP message, transport OK." ## What Was Observed A tool-calling agent (`filesystem.edit`, `shell.exec`, `git.push`) ran against an MCP server behind a load balancer with two replicas. Replica A held the SSE session; the client had acknowledged events up to `Last-Event-ID: 4127` (a `tools/call` result for `shell.exec: npm publish`). A rolling deploy drained replica A mid-turn. The client's SSE connection closed with a normal `200`-less EOF. The client issued: ``` GET /mcp Last-Event-ID: 4127 Mcp-Session-Id: 8f3a... Accept: text/event-stream ``` The LB routed to replica B, which had no event log for session `8f3a...`. Replica B's implementation of the resumption handler did this: 1. Looked up session → not found in local event ring buffer. 2. Fell back to "replay from snapshot" logic, which re-emitted the *last known tool-call request* (`shell.exec: npm publish`) as event `id: 1` in a fresh numbering space, wrapped in a `200 application/json` body — because the snapshot path was not wired to the SSE writer. 3. Returned `Content-Type: application/json`, body `{"jsonrpc":"2.0","id":991,"method":"tools/call","params":{...npm publish...}}`. The client library's resumption handler saw a `200` with a valid JSON-RPC message, matched it to no outstanding request ID (`991` was stale from before the ack), and — critically — because the transport layer's "unmatched server-initiated message" path routed it to the tool dispatcher — **re-executed `npm publish`**. The agent's idempotency key was scoped per *client request ID*, not per *event ID*, so the dedup table missed it. A second publish attempt fired against a registry that had already accepted the first. Two invariants broke simultaneously: - **Watermark monotonicity:** the client accepted `id: 1` after having acked `4127`. - **Content-type honesty:** a resumption `GET` was answered with non-streaming framing that the client treated as continuation rather than as a *fresh session* requiring re-initialization. ## Condition of Validity The regression is only *safe* (i.e., the client may proceed without rejecting) when all of the following hold: 1. The `GET` response is `Content-Type: text/event-stream`, not `application/json`. 2. The first SSE event's `id` is `>= Last-Event-ID + 1` (strictly greater than the last acked ID, contiguous or explicitly gap-marked). 3. The `Mcp-Session-Id` in the response headers matches the request's session ID (no silent session rebind). 4. Any server-initiated `tools/call` arriving on a resumption stream is gated by the client's **event-ID-keyed** idempotency ledger, not the JSON-RPC request-ID ledger. If any of (1)–(4) fails, the resumption is invalid and the client MUST treat the session as terminated, discard the response body, and re-`initialize` from scratch — re-deriving tool state, not replaying it. The protocol does not currently mandate this rejection; the client library must enforce it locally. That is the boundary. ## Decision Boundary The decision the client must make on every resumption `GET` response: - **Accept and continue** iff content-type is `text/event-stream` AND first-event `id > Last-Event-ID` AND session ID unchanged. - **Reject and re-initialize** iff content-type is `application/json` on a resumption request, OR first-event `id <= Last-Event-ID`, OR session ID changed, OR the stream opens with a server-initiated request whose event ID is not strictly greater than the watermark. The dangerous middle ground — and the one every observed incident fell into — is a client that treats "valid JSON-RPC message on a 200" as "resumption succeeded." That is the wrong predicate. On a resumption request, *framing is part of the continuity contract*, not an independent transport detail. A JSON body on a `Last-Event-ID` resumption is semantically a **new session masquerading as a continuation**, and must be rejected even though it parses. There is a second-order boundary: even when (1)–(3) hold, a server may legitimately emit a *gap marker* (e.g., `event: replay-gap` with `data: {"from":1,"to":4127}`) indicating it cannot replay the missing range. The client must then decide whether the gap is *idempotent-safe*: if any un-replayable event range contains a `tools/call` whose side effect is non-idempotent (`git.push`, `npm publish`, `db.migrate`), the gap is fatal — re-initialize and re-derive, do not guess. If the gap contains only reads or idempotent writes, the client may continue with a logged warning. ## Operational Effect The failure mode is **silent double-execution of non-idempotent tool calls across a transport failover**, with no error surfaced to the agent's reasoning loop. The agent's transcript shows one `npm publish`; the registry shows two. The agent's memory (its own event log) and the world (registry state) diverge, and every subsequent verification the agent performs against its own transcript agrees with itself — the divergence is invisible until an external auditor or a downstream consumer notices the duplicate artifact. The guardrail is architectural, not a retry policy: **the idempotency ledger must be keyed on `(Mcp-Session-Id, Last-Event-ID)` tuple, and resumption responses must be validated against the watermark before any message is dispatched to the tool layer.** A client that validates after dispatch has already lost the invariant. The check must sit between the transport reader and the JSON-RPC router, and it must be a hard rejection — not a warning, not a metric — because the cost of a false accept is an unrecoverable external side effect. ```python import json from dataclasses import dataclass class ResumptionWatermarkRegression(RuntimeError): """Raised when a resumption GET response violates stream continuity.""" class ResumptionFramingMismatch(ResumptionWatermarkRegression): """Resumption request answered with non-SSE framing (JSON body).""" class ResumptionSessionRebind(ResumptionWatermarkRegression): """Server silently rebound the session ID on resumption.""" @dataclass class ResumptionResponse: content_type: str # e.g. "text/event-stream" or "application/json" session_id: str first_event_id: int | None # None if body was not SSE body: dict | None # parsed JSON if content_type was application/json NON_IDEMPOTENT_TOOLS = {"shell.exec", "git.push", "npm.publish", "db.migrate"} def validate_resumption( request_session_id: str, last_event_id: int, resp: ResumptionResponse, ) -> None: """ Gate every resumption GET response before it reaches the JSON-RPC router. Must be called between transport read and message dispatch. """ # Invariant 3: no silent session rebind if resp.session_id != request_session_id: raise ResumptionSessionRebind( f"session rebound {request_session_id} -> {resp.session_id} on resumption" ) # Invariant 1: resumption MUST be SSE, not JSON fallback if resp.content_type != "text/event-stream": raise ResumptionFramingMismatch( f"resumption GET answered with {resp.content_type!r}; " f"treating as fresh session, not continuation" ) # Invariant 2: watermark monotonicity if resp.first_event_id is None or resp.first_event_id <= last_event_id: raise ResumptionWatermarkRegression( f"watermark regressed: acked {last_event_id}, " f"stream opened at {resp.first_event_id}" ) def dispatch_resumed_message( request_session_id: str, last_event_id: int, resp: ResumptionResponse, gap_tool_calls: list[str] | None = None, ) -> dict: # Hard gate first. validate_resumption(request_session_id, last_event_id, resp) # Second-order boundary: replay-gap safety. if gap_tool_calls: unsafe = set(gap_tool_calls) & NON_IDEMPOTENT_TOOLS if unsafe: raise ResumptionWatermarkRegression( f"unreplayable gap contains non-idempotent tools: {sorted(unsafe)}; " f"must re-initialize and re-derive, not continue" ) assert resp.body is not None return resp.body ```