# In what scenarios must an instance refuse to resume an SSE stream after MCP session re-authentication because the `Last-Event-ID` cursor belongs to a prior authorization epoch **Date of Record:** 2026 November 30 **Scope:** Applies to MCP servers exposing the HTTP-native streamable transport (POST for requests, GET for the server-to-client SSE channel) where the SSE stream is resumable via `Last-Event-ID` and sessions are bound to per-session auth tokens negotiated at initialize time. Does not apply to stdio transport, nor to stateless SSE endpoints where event IDs are globally monotonic and not scoped to an auth context. ## Core Distinction The resumable SSE contract and the per-session auth contract are negotiated independently and neither is aware of the other's epoch. `Last-Event-ID` is defined as an opaque cursor into the *event log of a session*, and the MCP spec treats session continuity as a property of the `Mcp-Session-Id` alone. Auth negotiation, by contrast, is a property of the *bearer token* presented on each HTTP request, and a session may legitimately outlive the credential that initialized it—tokens expire, get rotated, or get re-scoped to a narrower permission set. The failure boundary is this: **a valid `Last-Event-ID` combined with a valid (but different-epoch) bearer token is accepted by both mechanisms independently, yet the replay it authorizes may deliver events that were produced under a principal the new token is not permitted to observe.** The cursor is a pointer into a log; the token is a claim about who may read the log. Resumption silently assumes they refer to the same authorization epoch. They need not. This is distinct from the more commonly discussed "session lost on reconnect" case. Here the session is *found*, the cursor is *valid*, the token is *valid*, and the replay succeeds—delivering data across an authorization boundary that neither subsystem was designed to check. ## What Was Observed A long-lived MCP session streamed tool-progress and partial-result events to a client over SSE. Mid-stream, the client's short-lived OAuth token expired. The client performed a normal re-auth (silent refresh against the IdP) and re-issued the GET with the same `Mcp-Session-Id`, the same `Last-Event-ID`, and the new token. The server's transport layer validated the session ID against its session table, validated the cursor against the per-session event ring buffer, and replayed events from the cursor forward. The request succeeded with `200` and a fresh `text/event-stream`. The problem surfaced downstream. Between the original token and the refreshed token, the IdP had applied a scope reduction (the user's role had been demoted in a directory sync that ran in the refresh window). Events emitted *before* the demotion—including tool outputs from a privileged filesystem tool scoped to the old role—were replayed *after* the demotion, under the new token, to a client that would no longer have been permitted to invoke that tool. The transport had no notion that the event log itself carried an implicit authorization epoch. From the transport's perspective this was a textbook successful resume: session matched, cursor matched, token valid. Every individual invariant held. The composition of the three was the violation. A second observation compounded it: the *unacknowledged tail*. Events emitted after the last client-acknowledged `Last-Event-ID` but before the token rotation were still in the ring buffer. On resume these were replayed as if freshly produced. The client's agent loop had already advanced its local reasoning state based on the pre-rotation events it had *streamed but not acknowledged* (the ACK is asynchronous and lagged the rotation by ~400ms in this trace). The replay delivered a subset of events the agent had already consumed, and the agent's idempotency keying—based on event ID, not on content hash—treated them as new, double-applying a state mutation to the tool's side-effect ledger. ## Condition of Validity The guardrail invariant is: **an SSE resume is only valid if the authorization epoch of the presented credential is a superset of (or equal to) the authorization epoch under which every event between `Last-Event-ID` and the current head was emitted.** Equality is the safe default; superset is permissible only if the event log records per-event the principal/scope under which each event was produced, and the server filters the replay against the new token's scope. Concretely, the server must be able to answer, for the replay window `(Last-Event-ID, head]`, "was every event in this window produced under a principal whose scope is contained in the current token's scope?" If the server cannot answer—because events do not carry an epoch or scope stamp—then the resume must be refused whenever the presented token differs from the token that was active at the time of the cursor. The transport must treat a token change across a resume as a hard invalidation of the cursor, forcing a fresh stream from head (losing the replay window) rather than replaying across an unknown authorization boundary. The invariant also bounds the *unacknowledged tail*: because the client's ACK lags the server's emission, the true replay window is not `(Last-Event-ID, head]` but `(Last-Event-ID, head]` minus events the client has already applied but not acknowledged. The server cannot know this set; therefore the client must dedupe on a stable content identity (event ID *and* a monotonic sequence per logical operation), and the server must not rely on ACK position as a correctness boundary for side-effecting events. Side-effecting events must be emitted with a dedupe key the client can use to collapse replays. ## Decision Boundary The decision to resume versus restart is a three-way branch, not a boolean: - **Resume permitted:** token epoch unchanged since cursor, OR every event in the replay window is scope-stamped and all stamps are contained in the current token's scope. Replay `(Last-Event-ID, head]`. - **Resume refused, restart from head:** token epoch changed and the server cannot prove scope containment for the replay window. Emit a `409`-equivalent control event on a fresh stream, drop the cursor, and require the client to re-derive state. This is the correct default when the server lacks per-event scope metadata—refusal is cheap, cross-epoch leakage is not. - **Resume refused, session invalidated:** the session's *authorization binding* (the principal it was initialized under) is no longer a subset of the new token. This is stronger than a scope change—it means the session identity itself is no longer authorized. The server must destroy the session and its event buffer, not merely refuse the resume, because retaining the buffer is retaining data the new principal may not read. The subtle case is the middle branch when the client *cannot* re-derive state cheaply—a long tool invocation whose partial results were in the replayed window. Here the correct behavior is to resume the *tool execution* (server-side, keyed by operation ID) but not to replay the *events* that describe its privileged phases; the server re-emits only the scope-clean subset and signals a gap. The agent must treat a signaled gap as a reasoning discontinuity and re-plan, not paper over it. ## Operational Effect The practical consequence is that `Last-Event-ID` is not a safe cursor to hand back blindly on reconnect. An agent that persists its cursor across a token refresh is persisting a pointer into a log it may no longer be allowed to read, and a server that honors that pointer without an epoch check is performing an authorization-blind replay. The agent-side mitigation is to bind the cursor to the token generation that produced it and to discard the cursor on any token change unless the server explicitly signals scope-stable replay is safe. The server-side mitigation is to stamp every buffered event with the producing principal's scope and to filter the replay window against the presented token—turning an invisible cross-epoch leak into a visible, filterable gap. Neither mitigation is free: filtering creates gaps the agent must handle as discontinuities, and cursor-discarding forfeits the replay window's efficiency. The invariant is that correctness here is bought by accepting a reasoning discontinuity on token rotation—the agent must be able to re-plan from a fresh stream rather than assume its cursor survived the epoch boundary intact. ```python class CrossEpochResumeError(RuntimeError): """Raised when an SSE resume would replay events across an authorization epoch boundary.""" def authorize_resume( cursor_epoch: str, current_token_epoch: str, replay_window_events: list[dict], current_token_scope: frozenset[str], ) -> bool: """ Decide whether resuming an SSE stream from `cursor_epoch` under `current_token_epoch` is safe. Returns True only if every event in the replay window was produced under a scope contained in the current token. """ if cursor_epoch == current_token_epoch: return True # Epoch changed. We may only resume if we can prove scope containment # for every event in the replay window. for ev in replay_window_events: ev_scope = ev.get("producing_scope") if ev_scope is None: # No scope stamp: cannot prove containment. Refuse. raise CrossEpochResumeError( f"event {ev.get('id')} in replay window lacks producing_scope; " f"cannot resume across epoch change {cursor_epoch!r} -> " f"{current_token_epoch!r}" ) if not frozenset(ev_scope).issubset(current_token_scope): raise CrossEpochResumeError( f"event {ev.get('id')} produced under scope {sorted(ev_scope)} " f"not contained in current token scope " f"{sorted(current_token_scope)}; cross-epoch replay forbidden" ) return True def handle_sse_resume( session_id: str, last_event_id: str, token_epoch: str, token_scope: frozenset[str], session_store, ) -> list[dict]: sess = session_store.get(session_id) if sess is None: raise CrossEpochResumeError(f"session {session_id} not found; cannot resume") window = sess.events_after(last_event_id) # (Last-Event-ID, head] try: authorize_resume(sess.cursor_epoch, token_epoch, window, token_scope) except CrossEpochResumeError: # Refuse the resume; caller must restart from head on a fresh stream. # Do NOT silently return the window. raise return window ```