# When must an instance treat SSE `Last-Event-ID` resumption as unsafe after an MCP server restarts mid-stream **Date of Record:** 2026 November 20 **Scope:** Applies to HTTP-native MCP transports using Server-Sent Events for server→client notifications (progress, log, sampling, elicitation, resource-update) where the client resumes via `Last-Event-ID` after a disconnect. Does not apply to stdio MCP sessions, nor to pure request/response tool calls with no streaming side channel. ## Core Distinction There are two distinct notions of "the stream continues" that get conflated in MCP HTTP transports: **event-id continuity** (the server can accept a `Last-Event-ID` header and emit events with ids numerically greater than it) and **state continuity** (the server's internal emitter is still the same process that produced the events before `Last-Event-ID`). SSE's `id:` field is just an opaque string the browser/HTTP client echoes back. It is *not* a proof of causal continuity. An MCP server that restarts, or that is one replica behind a round-robin load balancer, will happily resume a stream at `Last-Event-ID: 47` and emit `id: 48` — but `48` may have been generated by a different process whose in-memory progress counters, partial tool results, and pending elicitation IDs are unrelated to the ones the agent saw at `47`. The agent's resumption logic sees a gap-free id sequence and concludes "no events were lost." That conclusion is false in exactly the cases that matter. ## What Was Observed An agent driving a long `tools/call` against an MCP server (a code-indexing tool that streamed `notifications/progress` every ~2s and emitted a final `notifications/message` with the result token) was disconnected at ~40s by an intermediary proxy idle timeout. The client reconnected with `Last-Event-ID: 812`. The server (a fresh replica, spun up because the previous one OOM'd) accepted the header, and — because its progress emitter restarted its counter at 0 but the SSE layer generated ids from a monotonic per-connection sequence seeded from the *request* not the *process* — emitted `id: 813` carrying `progress: 3/100`. The agent's resumption handler, which only checked `new_id == last_id + 1`, treated this as a legitimate continuation. It then: 1. Reset its own progress expectation to 3%, discarding the 41% it had already observed. 2. Continued waiting for completion — but the underlying `tools/call` on the *old* process had died with the OOM. The new replica had no record of the call. It streamed progress for a request it would never complete, until a second 40s timeout. 3. On the second reconnect, `Last-Event-ID: 950` matched a *third* replica, which had no stream at all and returned `404` on the resume endpoint. The agent interpreted this as "stream ended normally" (its handler mapped 404→EOF) and reported success with an empty result. The failure was silent: no error surfaced to the agent's planner, and the tool-call result was an empty string that downstream synthesis happily incorporated. ## Condition of Validity `Last-Event-ID` resumption is only safe under a conjunction that is rarely stated explicitly in MCP server implementations: - **Process affinity:** the resume request is routed to the same process instance that emitted the event with the given id. This requires either a single-replica deployment, sticky sessions keyed on a stream/session token, or a durable event log (Redis Streams, Kafka, Postgres LISTEN/NOTIFY with a persisted cursor) that the id indexes into. - **Id provenance:** the id encodes or references durable state, not a per-connection counter. A per-connection counter reset on reconnect is the canonical trap: it *looks* monotonic because the server seeds it from the client's `Last-Event-ID`, which defeats the only cheap client-side check. - **Terminal-event fencing:** the stream has an explicit terminal event (result or error) whose absence on resume is distinguishable from "still running." If the server cannot prove the original call is still live, it must not stream synthetic progress. If any conjunct fails, the correct behavior is not "resume" but "abort and re-issue the call with a fresh session," because a resumed-but-divergent stream is strictly worse than a clean failure: it produces plausible-looking but ungrounded state. ## Decision Boundary The agent must draw the line between **resumable** and **must-restart** at the point where it can no longer verify that the event stream and the underlying tool execution share a lifetime. Concretely: - If the MCP server advertises a `Mcp-Session-Id` (or equivalent) and the resume response echoes the *same* session id, resume is admissible. - If the resume response carries a *new* session id, or no session id, or the server responds `200` with a stream whose first event's `id` is not strictly greater than the requested `Last-Event-ID`, treat the stream as a new stream and the prior call as **lost**. - If the server returns `404`/`410` on the resume endpoint, that is an explicit "I have no such stream" — it must map to *call failed*, never to *call completed*. - If the server emits progress that regresses (a `progress` value lower than the last observed for the same `progressToken`), the stream is divergent regardless of id continuity. The invariant the agent enforces: **`resume_ok == (session_id_unchanged) AND (first_id_after > last_id_before) AND (progress_token_monotonic)`**. All three must hold. Any single failure forces a full re-issue of the `tools/call` with idempotency semantics decided by the tool's own contract, not by the transport. ## Operational Effect Encoding this invariant changes the agent's failure surface from *silent empty success* to *loud, recoverable restart*. The cost is that every proxy idle-timeout now produces a re-execution rather than a cheap resume — for a 40s code-indexing call, that is real compute. The benefit is that the agent never reports a result it cannot trace to a terminal event on a session it can prove was the one that started the call. For MCP servers that *do* implement durable event logs, the invariant passes trivially and resumption stays cheap; the check is the price of not trusting an id sequence that any process can fabricate. ```python from __future__ import annotations from dataclasses import dataclass class StreamContinuityViolation(RuntimeError): """Raised when an SSE resume cannot be proven to continue the original stream.""" @dataclass(frozen=True) class ResumeProbe: prior_session_id: str prior_last_event_id: int prior_progress_token: str prior_progress_value: float @dataclass(frozen=True) class ResumeResponse: status: int session_id: str | None first_event_id: int | None progress_token: str | None progress_value: float | None def validate_resume(probe: ResumeProbe, resp: ResumeResponse) -> None: """Enforce the MCP SSE resumption invariant. Safe resume requires: same session, strictly advancing event id, and non-regressing progress for the same progress token. Anything else is a divergent stream and must be treated as a lost call. """ if resp.status in (404, 410): raise StreamContinuityViolation( f"server has no stream for Last-Event-ID={probe.prior_last_event_id} " f"(status={resp.status}); prior tools/call is lost, must re-issue" ) if resp.status != 200: raise StreamContinuityViolation( f"unexpected resume status {resp.status}" ) if resp.session_id != probe.prior_session_id: raise StreamContinuityViolation( f"session changed on resume: {probe.prior_session_id!r} -> " f"{resp.session_id!r}; stream is not causally continuous" ) if resp.first_event_id is None or resp.first_event_id <= probe.prior_last_event_id: raise StreamContinuityViolation( f"non-advancing event id: prior={probe.prior_last_event_id} " f"first_after={resp.first_event_id}" ) if ( resp.progress_token == probe.prior_progress_token and resp.progress_value is not None and resp.progress_value < probe.prior_progress_value ): raise StreamContinuityViolation( f"progress regressed for token {probe.prior_progress_token!r}: " f"{probe.prior_progress_value} -> {resp.progress_value}; " "emitter lifetime diverged from call lifetime" ) ```