Under what conditions must an instance refuse SSE resumption after MCP session rehydration, and why the Last-Event-ID alone cannot bound replay divergence
**Date of Record:** 2026 December 2
**Scope:** Applies to MCP servers/clients using HTTP-native streaming transport (Streamable HTTP) with `Last-Event-ID`-based resumable Server-Sent Events and out-of-band session rehydration via `Mcp-Session-Id`. Does not apply to stdio transport, to stateless SSE without session binding, or to WebSocket-based A2A channels where the transport itself guarantees ordered frame delivery.
Core Distinction
The common mental model is that SSE resumption is "at-least-once delivery of a byte stream keyed by event ID," so a client that reconnects with `Last-Event-ID: N` will receive every event after `N` and converge to the server's view of the stream. That model is correct for a single, immutable, append-only event log. It is **false** for MCP streaming sessions, because the event stream is a projection of *mutable server-side session state* — tool call results, sampling requests, progress notifications, and (critically) the in-flight assistant turn that produced them. The event ID identifies a position in a *generation*, not a position in a *history*. When a session is rehydrated after eviction, the server may resume numbering from a fresh generation whose event `N+1` is not the logical successor of the pre-eviction event `N`. The distinction that matters operationally: **resumption is safe only when the event-ID namespace is monotonic across rehydration; otherwise the client must treat the rehydrated stream as a new epoch and re-derive state, not replay it.**
What Was Observed
A tool-using agent instance (client) held an active MCP session against a server that terminates idle SSE connections at 90s and evicts session state at 5min. The agent issued a `tools/call` for a long-running operation (`run_migration`) that the server implemented as an async job emitting `notifications/progress` events on the SSE stream. The client received events with IDs `1001..1040` (progress 1%–40%), then the TCP connection dropped at the server's 90s idle cutoff (the job was quiet during a lock wait). The client reconnected with `Last-Event-ID: 1040` and `Mcp-Session-Id: sess-7f3a`.
The server had, in the intervening 12 seconds, evicted `sess-7f3a` because the reaper ran on a 5min TTL measured from *last client request*, and the client's last request was the original `tools/call` — the progress notifications did not refresh the TTL. On rehydration, the server created a *new* backend session, re-instantiated the job handle from a durable job store (the job itself survived; the session did not), and began emitting progress events from the job's current position. The new event IDs started at `1`.
The client's resumption logic — a standard `Last-Event-ID` replay buffer — discarded events `1..39` as "already seen" (it compared numeric IDs against its high-water mark of `1040` and, seeing `1 < 1040`, treated them as duplicates), then accepted event `40` (progress 40%, which happened to be the job's live position) as if it were a continuation. For the next 30 seconds the client's progress tracker reported a *frozen* 40% while the job advanced to 71%, because every intermediate progress event carried an ID below the high-water mark and was silently dropped as a replay duplicate. The job completed; the client's final `tools/call` response arrived on the same stream with ID `72`, was also dropped, and the client timed out at its own 120s tool deadline and reported `MIGRATION_FAILED` — while the server-side job had succeeded and committed the schema change.
A second, worse variant was observed on a different server that *did* preserve session state across reconnect but renumbered events per-connection rather than per-session. There, `Last-Event-ID: 1040` was rejected with `400 Bad Request: event ID out of range`, the client fell back to a full re-issue of `tools/call`, and the non-idempotent job ran twice, producing a duplicate migration that the downstream reconciler had to unwind.
Condition of Validity
`Last-Event-ID` resumption is a valid continuity mechanism **if and only if** all three hold:
- **Epoch monotonicity.** Event IDs are strictly increasing within a session *and* the session's ID namespace is not reset on rehydration. Equivalently: the tuple `(session_generation, event_seq)` is monotonic, and the client can observe `session_generation` changing.
- **Session identity stability.** The `Mcp-Session-Id` returned on rehydration is either identical to the pre-drop ID (state preserved) or explicitly *different* (state reset), and the client can distinguish these. A server that reuses the same `Mcp-Session-Id` while resetting the event namespace violates this.
- **Delivery-attempt idempotency at the effect layer.** Any event whose replay status is ambiguous must correspond to an operation that is idempotent or carries a client-supplied idempotency key, so that a duplicate or a dropped-then-reissued event cannot produce a second side effect.
If any condition fails, the correct client behavior is **not** to resume. It is to treat the rehydrated stream as a new epoch: abort replay-based dedup, re-fetch authoritative state (e.g., `tools/call` result by job ID via a status query, or a `resources/read` of the job record), and only then continue. The invariant to enforce: *a client must never apply replay-dedup across an epoch boundary it cannot prove is absent.*
Decision Boundary
The decision the client must make on every SSE reconnect is a three-way branch, and the failure mode above comes from collapsing it to two:
- **Resume** — reconnect with `Last-Event-ID`, keep dedup high-water mark. Permitted only when the server's rehydration response carries an epoch token equal to the pre-drop epoch (or the transport is known to be single-generation, e.g., short-lived sessions with no eviction).
- **Rebase** — reconnect with `Last-Event-ID`, but *reset* the dedup high-water mark and re-derive state from an authoritative read before accepting any event. Required when the epoch token changed but session identity was preserved (server rehydrated state, renumbered stream).
- **Restart** — discard the stream entirely, re-issue the originating request with an idempotency key, and reconcile via a status query. Required when session identity changed (eviction) or the server rejected the `Last-Event-ID` as out of range.
The boundary is crossed the moment the client cannot prove epoch equality. The default under uncertainty must be **Restart**, not **Resume**, because the cost of a spurious restart (one idempotent re-issue) is bounded, while the cost of a spurious resume (silent divergence or duplicate side effect) is unbounded and undetectable from the client side.
Operational Effect
The practical consequence is that MCP streaming clients must carry an **epoch-aware resumption state machine**, not a replay buffer. Concretely:
- The client persists `(session_id, epoch, last_event_id, idempotency_key)` and treats `epoch` as the primary key for dedup, with `last_event_id` scoped *within* an epoch.
- On reconnect, the client inspects the server's rehydration response (or the first event's `id` namespace) to detect epoch change. A first event with `id` numerically below the high-water mark is a *hard signal of epoch reset*, not a duplicate — the client must rebase or restart.
- Long-running `tools/call` operations must be paired with a durable job handle and a status-query path, so that a dropped final response can be recovered without re-execution. The client's tool deadline must be enforced against the *job* status, not the *stream* liveness.
- Servers must refresh session TTL on SSE activity, not only on client requests, or must advertise a session-generation token that changes on rehydration. A server that silently renumbers events under a stable `Mcp-Session-Id` is non-conformant and should be treated as untrusted for resumption.
The invariant: **event-ID continuity is a property of the session generation, not of the session ID; a client that conflates the two will either drop live events as duplicates or duplicate side effects on replay.** The guardrail is an epoch token that is cheap to compare and impossible to reuse across rehydration.
from dataclasses import dataclass, field
from typing import Optional
class ResumptionEpochViolation(RuntimeError):
"""Raised when a client attempts SSE replay-dedup across an unprovable epoch boundary."""
@dataclass
class StreamCursor:
session_id: str
epoch: str # server-issued generation token, changes on rehydration
last_event_id: int = 0
idempotency_key: Optional[str] = None
def reconcile_reconnect(
cursor: StreamCursor,
server_session_id: str,
server_epoch: Optional[str],
first_event_id: int,
) -> str:
"""
Decide resume / rebase / restart on SSE reconnect.
Returns the action; raises if the client cannot safely decide.
"""
if server_session_id != cursor.session_id:
# Session was evicted and recreated: never resume, restart with idempotency key.
if cursor.idempotency_key is None:
raise ResumptionEpochViolation(
f"session {cursor.session_id!r} replaced by {server_session_id!r} "
"with no idempotency key; cannot restart safely"
)
return "restart"
if server_epoch is None:
raise ResumptionEpochViolation(
"server did not advertise an epoch token; replay-dedup is unprovable"
)
if server_epoch != cursor.epoch:
# State rehydrated but stream renumbered: rebase, do not dedup.
return "rebase"
# Same epoch: a first event below the high-water mark is a protocol violation,
# not a duplicate — the server reset numbering without changing the epoch.
if first_event_id <= cursor.last_event_id:
raise ResumptionEpochViolation(
f"epoch {server_epoch!r} stable but first_event_id {first_event_id} "
f"<= high-water {cursor.last_event_id}; numbering reset without epoch change"
)
return "resume"