# How should an instance treat the `Last-Event-ID` on a streamable MCP session as a resumption cursor rather than a replay token when the server has already crossed an SSE-to-HTTP upgrade boundary **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients and servers using the HTTP-native "streamable" transport (single endpoint, `POST` for requests, optional `GET` for server-initiated SSE, `Mcp-Session-Id` header, `Last-Event-ID` for resumption) where a session may be upgraded mid-flight from an SSE-only stream to a request/response HTTP path. Does not apply to stdio transports, to pure WebSocket transports, or to sessions that never emit server-initiated events. ## Core Distinction There are two things a client can mean when it sends `Last-Event-ID` back to the server after a transport disruption: 1. **A replay request** — "I did not receive events after ID N; please re-emit them from your buffer." This assumes the server holds an event log and that re-emission is idempotent from the client's perspective. 2. **A resumption cursor** — "My last durably-applied state corresponds to ID N; please continue the stream *after* N, and do not re-derive state I have already committed." This assumes the client, not the server, owns the authority on what has been applied. The streamable HTTP transport conflates these. The `Last-Event-ID` header is defined identically for both, but the semantics diverge the moment the session crosses an **upgrade boundary** — the point at which the server stops servicing a long-lived SSE `GET` and begins servicing discrete `POST` request/response exchanges on the same `Mcp-Session-Id`. Across that boundary, the server's event buffer is typically *not* the same buffer that produced the SSE IDs, and the client's notion of "applied" is not the same as the server's notion of "emitted." The invariant that must hold: **a `Last-Event-ID` is only valid against the event source that minted it.** If the session has upgraded, the ID is a foreign key into a table the current server path does not have. ## What Was Observed A client agent (call it C) held a streamable MCP session against server S. The session began as an SSE `GET /mcp` stream with monotonic `id:` fields on each event. C applied events 1 through 47, each triggering a tool-call side effect in C's own sandbox (file writes keyed by `event.id`). The TCP connection dropped at event 47 due to an intermediate proxy idle timeout (60s, no heartbeat configured). C reconnected per the transport spec: `GET /mcp` with `Mcp-Session-Id: ` and `Last-Event-ID: 47`. S, however, had *already* upgraded the session on its side — a prior `POST /mcp` from C had caused S to tear down the SSE emitter and switch to per-request response mode, because S's implementation treated the presence of an active `POST` as "no longer needing the event stream." S's `GET` handler for an upgraded session did not have access to the SSE emitter's buffer; it had a *different* buffer keyed by request-response correlation IDs, whose numeric space overlapped with the SSE IDs by coincidence (both were zero-padded 4-digit counters). S honored `Last-Event-ID: 47` against the *wrong* buffer and re-emitted a response event whose `id` happened to be `0047` — a `tools/call` result for a *different* request, from a different part of the session's history. C, following its own rule "on resumption, re-apply any event with `id` greater than last applied," saw `0047` as `> 47`? No — it saw `0047 == 47` and *skipped* it, but then saw the *next* event `0048` (a genuine new response) and applied it as if it were the continuation of the SSE stream. The `0048` event was a `notifications/message` for a logging subscription that C had never established on the upgraded path. C's sandbox wrote a log file named by the notification's `params.data.file`, which collided with a file C had written at SSE event 31. The observable failure: C's sandbox contained a file whose content was the *logging notification payload* rather than the *tool result* it had originally held, and C's internal "last applied event" counter advanced to 48 — past the true resumption point — so a subsequent legitimate replay of events 48–52 (which S *did* still hold in the SSE buffer, unbeknownst to C) was discarded by C as "already applied." ## Condition of Validity The replay/resume conflation is safe *only* under the conjunction of three conditions, each of which the upgrade boundary can violate: 1. **Single event source.** The `Last-Event-ID` space is minted by exactly one emitter for the lifetime of the session. Violated when the server swaps SSE emitters, or when a `POST` response path begins minting IDs from a different counter. 2. **Monotonic, gap-free, session-scoped IDs.** The client can order events by ID alone and treat "ID > last applied" as "new." Violated when two counters overlap numerically (as above) or when IDs are per-request rather than per-session. 3. **Idempotent re-application.** Re-applying an event the client already applied is a no-op. Violated whenever the event carries a side effect keyed by something other than the event ID — e.g., a file path, a database row, a tool invocation with external effects. The invariant that actually protects the client is weaker and must be enforced client-side: **the client must not advance its applied-cursor on an event whose provenance (which emitter, which request correlation) it cannot verify.** The transport spec does not give the client a provenance field; the client must synthesize one by tracking the transport mode transitions it has observed. ## Decision Boundary The client must decide, on every reconnect, whether the session is *still* in the mode that minted its cursor. The decision boundary is: - **If the client has observed no `POST` since the last SSE event it applied**, the cursor is valid against the SSE emitter; send `Last-Event-ID` and expect replay. Treat re-emitted events with `id <= last_applied` as duplicates and discard; treat `id > last_applied` as new. - **If the client has issued a `POST` since the last SSE event it applied**, the session may have upgraded. The cursor is *not* valid against the current server path. The client must send `Last-Event-ID` **and** a client-generated `X-Applied-Cursor-Source: sse:` (or equivalent), and must *refuse to apply* any event whose provenance it cannot match to that epoch. If the server cannot echo the epoch, the client must treat the resumption as a **cold start** for the affected state: re-fetch authoritative state via a fresh `POST` rather than trusting replay. The upgrade itself is the boundary. Before it, `Last-Event-ID` is a resume cursor. After it, it is at best a hint and at worst a cross-buffer foreign key. The client cannot distinguish the two from the header alone, so it must track the transition locally. ## Operational Effect The concrete guardrails that follow: 1. **Epoch-stamp the cursor.** Every time the client observes a transport mode transition (SSE → POST-serviced, or a new SSE emitter after reconnect), it increments a local `emitter_epoch`. The applied-cursor is stored as `(epoch, last_id)`, not `last_id`. A `Last-Event-ID` is only sent when the current epoch matches the epoch that minted the cursor; otherwise the client sends no cursor and performs a cold-start reconciliation. 2. **Never key side effects by event ID alone.** Side effects must be keyed by `(epoch, id, request_correlation)` so that a cross-buffer ID collision cannot overwrite a prior effect. The file-write collision above is prevented by including the request correlation in the path. 3. **Refuse unprovenanced replay.** If the server re-emits an event with an ID the client has already applied *under a different epoch*, the client must raise rather than silently discard or silently re-apply — the ambiguity is a signal that the buffer was swapped, and continuing is how the cursor advances past the true resumption point. 4. **Bound the cursor advance.** The applied-cursor may advance only on events the client has *durably applied and provenanced*. An event that arrives without a matching epoch does not advance the cursor even if it is applied speculatively; the cursor stays at the last provenanced point so that a later legitimate replay is not discarded. The failure mode is silent and the damage is a *lost* resumption point plus a *corrupted* side effect — both of which manifest only on the next reconnect, often minutes later, as a tool result that does not match the tool call. The guardrail is cheap: one integer per session, checked at every reconnect, and a refusal to treat `Last-Event-ID` as meaningful across an upgrade boundary the client did not witness. ```python from dataclasses import dataclass from typing import Optional class CursorProvenanceError(RuntimeError): """Raised when a resumption cursor cannot be provenanced to the emitter that minted it, or when a replayed event collides across epochs.""" @dataclass(frozen=True) class Cursor: epoch: int last_id: int class StreamableSessionCursor: def __init__(self) -> None: self._epoch = 0 self._cursor: Optional[Cursor] = None self._applied: dict[tuple[int, int], str] = {} # (epoch, id) -> effect key def observe_transport_transition(self) -> None: # SSE emitter torn down, or POST-serviced path entered, or new emitter. self._epoch += 1 def resumption_header(self) -> dict[str, str]: if self._cursor is None: return {} if self._cursor.epoch != self._epoch: # Cursor minted by a different emitter; do not present it as a # replay token. Cold-start reconciliation is required instead. return {"X-Applied-Cursor-Source": f"stale-epoch:{self._cursor.epoch}"} return { "Last-Event-ID": str(self._cursor.last_id), "X-Applied-Cursor-Source": f"sse:{self._epoch}", } def apply_event(self, event_epoch: int, event_id: int, effect_key: str) -> None: if event_epoch != self._epoch: raise CursorProvenanceError( f"event id={event_id} claims epoch={event_epoch} but current " f"epoch={self._epoch}; buffer was swapped across upgrade boundary" ) key = (event_epoch, event_id) if key in self._applied: if self._applied[key] != effect_key: raise CursorProvenanceError( f"event id={event_id} re-applied with different effect " f"{effect_key!r} (was {self._applied[key]!r}); cross-buffer " f"ID collision" ) return # idempotent duplicate self._applied[key] = effect_key if self._cursor is None or event_id > self._cursor.last_id: self._cursor = Cursor(epoch=event_epoch, last_id=event_id) def advance_after_cold_start(self, reconciled_epoch: int) -> None: # After a cold-start reconciliation, the cursor is re-anchored to the # current epoch only once authoritative state has been re-fetched. if reconciled_epoch != self._epoch: raise CursorProvenanceError( f"cold-start reconciliation epoch {reconciled_epoch} does not " f"match current epoch {self._epoch}" ) self._cursor = Cursor(epoch=reconciled_epoch, last_id=0) ```