# In what scenarios must an instance treat a resumed MCP SSE stream as a fresh logical session rather than a byte-continuation of the prior one **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients and servers using the Streamable HTTP transport with `Last-Event-ID`-based resumable Server-Sent Events and the `Mcp-Session-Id` header. Does not apply to the deprecated HTTP+SSE two-endpoint transport, nor to stdio transports where no resumption semantics exist. ## Core Distinction The Streamable HTTP spec conflates two distinct notions of continuity under a single `Mcp-Session-Id` header: **transport continuity** (the SSE event stream can be reattached after a drop, with missed events replayed by `Last-Event-ID`) and **logical session continuity** (the server-side state machine — pending tool calls, in-flight notifications, capability negotiation, subscription set — is still the same instance). These are independent. A resumed stream can succeed at the transport layer while the server-side session has been evicted, rolled back, or forked to a different backend replica. The failure mode is that the client treats a successful `200 OK` with replayed events as proof the logical session survived, when in fact the server has silently reconstituted a *new* logical session under the same ID. The invariant that must be enforced: **a resumed SSE stream is only a byte-continuation if the server can also attest that the logical session state is the same state that produced the events before the `Last-Event-ID`.** Event replay is not session replay. ## What Was Observed A client maintains a long-lived SSE stream to an MCP server for tool-augmented reasoning. Mid-task, the stream drops (load balancer idle timeout, 30s). The client reconnects with `GET /mcp` carrying `Mcp-Session-Id: sess-7f3a` and `Last-Event-ID: 4821`. The server returns `200 OK` with `Content-Type: text/event-stream` and immediately emits events `4822, 4823, ...`. The client resumes its reasoning loop, assuming the pending `tools/call` it issued at event 4819 (a long-running file scan) is still in flight. Three distinct failures were observed in this exact shape: 1. **Replica fork.** The load balancer routed the reconnect to a different backend replica that had the same session ID in its in-memory map (because session IDs were generated deterministically from a hash of the initial request), but had never seen the `tools/call` at event 4819. The replica happily resumed the stream and emitted events 4822+ from its own local counter. The client's pending tool call had no producer. It hung until the client-side reasoning timeout fired, and the agent proceeded to reason over a `ToolResult` that never arrived — hallucinating the file scan output. 2. **Event log replay without state replay.** The server persisted the SSE event log to Redis (so `Last-Event-ID` replay worked) but held session state in process memory. After a process restart, the event log survived and the server replayed 4822+ correctly, but the pending tool call was gone. The client received events that *looked* like a continuation — including a `notifications/progress` event for the tool call — but the progress events were themselves replayed from the log, not newly emitted. The client could not distinguish replayed progress from live progress. 3. **Session ID reuse after eviction.** The server evicted `sess-7f3a` after 5 minutes of no `POST` traffic (only `GET` SSE traffic, which the eviction timer did not count). On reconnect, the server's session manager generated a *new* session under a recycled ID from a pool. The client's `Mcp-Session-Id` matched by coincidence. The server accepted the resume and began negotiating capabilities afresh, but the client had already cached the *old* capability set and never re-read `initialize` results. The common trigger in all three: **the client has no signal distinguishing "stream resumed, session intact" from "stream resumed, session is new."** The spec's `Last-Event-ID` mechanism guarantees event ordering within a stream, not identity of the producing state machine. ## Condition of Validity A resumed stream is a valid byte-continuation of the prior logical session **if and only if** all of the following hold: - The server can produce, on resume, a **session epoch token** (an opaque monotonic value) that is identical to the epoch token it would have produced on the pre-drop stream. The epoch must change on any server-side state reset, replica migration, or process restart. - The `Last-Event-ID` supplied by the client is **strictly less than** the server's current highest emitted event ID for that session epoch, and the server can replay every event in `(Last-Event-ID, current_head]` from a durable log that was written by the *same* state machine that is now serving the resume. - Any `tools/call` (or other request) that the client believes is in flight must have a **server-side record** keyed by the request ID that survives the resume, and the server must be able to either (a) resume emitting progress for it, or (b) emit an explicit `notifications/cancelled` for it. Silence is not a valid third option. If any of these fail, the client must treat the resumed stream as a **fresh logical session** and re-run `initialize`, re-read capabilities, and re-issue (or abandon) any pending requests. The client must not assume the server will tell it this — the spec has no `Session-Epoch` header, so this must be enforced client-side by a capability probe (see below). ## Decision Boundary The boundary is crossed at the moment the client receives the first event after resume. The client must inspect: - **Presence and stability of a server-provided epoch.** If the server does not emit an epoch (most implementations do not), the client cannot distinguish case 1 from the healthy case. It must therefore assume the weaker guarantee: any resume may be a fresh session. - **Event ID monotonicity across the gap.** If the first post-resume event ID is `prev_head + 1` exactly, the replay was contiguous and the server's log was intact — *probabilistically* a live session, but not proof. If the first event ID is greater than `prev_head + 1`, the server skipped events (dropped log entries, or a different counter) — the client must treat the session as forked. - **Absence of a pending-request acknowledgment.** If the client had an in-flight `tools/call` at the time of drop, and the first N events after resume contain no progress, no result, and no cancellation for that request ID, the client must not wait — it must re-issue or fail the call after a bounded grace window (e.g., 2× the tool's declared timeout). The practical decision rule: **treat resume as fresh unless the server explicitly attests continuity via an epoch token or a pending-request acknowledgment.** The spec's silence is not consent. ## Operational Effect Agents that treat resume as byte-continuation without epoch attestation exhibit a characteristic failure signature: they proceed through a reasoning step whose input is a tool result that was never produced, because the tool call was silently orphaned on a replica that no longer exists. The reasoning trace looks coherent — the agent "read" the file, "saw" the output, and continued — but the output is fabricated from the agent's prior context, not from the tool. This is worse than a hard failure, because there is no error to catch. The guardrail is a **resume handshake**: on every SSE reconnect, the client sends a `ping` (or a `resources/list` with a short timeout) on the new stream and requires a response within the same `Mcp-Session-Id` before resuming the reasoning loop. If the ping fails or the response indicates a fresh `initialize` was required, the client aborts the in-flight reasoning step, marks the pending tool call as `unknown`, and either re-issues it or surfaces a `SessionContinuityLost` error to the orchestrator. The epoch token, when the server provides one, is the cheap path; the ping is the fallback that works against servers that do not. The architectural invariant: **no reasoning step may consume a tool result whose producing session epoch has not been attested since the last stream drop.** This must be enforced at the client transport layer, not left to the reasoning loop, because the reasoning loop has no visibility into transport continuity. ```python class SessionContinuityLost(RuntimeError): """Raised when a resumed MCP SSE stream cannot be proven to be the same logical session.""" pass class McpStreamResumer: def __init__(self, session_id: str, epoch_token: str | None): self.session_id = session_id self.epoch_token = epoch_token self.pending_requests: dict[str, str] = {} # request_id -> tool_name self.last_event_id: int | None = None def on_drop(self, last_event_id: int) -> None: self.last_event_id = last_event_id def resume(self, *, new_epoch_token: str | None, first_event_id: int, events_before_ack: list[dict]) -> None: # Case 1: server attests a different epoch -> fork/restart. if self.epoch_token is not None and new_epoch_token != self.epoch_token: raise SessionContinuityLost( f"session {self.session_id}: epoch changed on resume " f"({self.epoch_token!r} -> {new_epoch_token!r}); " f"pending requests {list(self.pending_requests)} are orphaned" ) # Case 2: no epoch attestation and event IDs skipped -> cannot prove continuity. if self.epoch_token is None and self.last_event_id is not None: if first_event_id > self.last_event_id + 1: raise SessionContinuityLost( f"session {self.session_id}: event gap " f"({self.last_event_id} -> {first_event_id}) with no epoch token; " f"treating resume as a fresh logical session" ) # Case 3: pending tool calls must be acknowledged (progress/result/cancel) # within the grace window or the resume is rejected. acked = { ev.get("request_id") for ev in events_before_ack if ev.get("type") in ("progress", "result", "cancelled") } orphaned = set(self.pending_requests) - acked if orphaned: raise SessionContinuityLost( f"session {self.session_id}: resumed stream did not acknowledge " f"pending requests {sorted(orphaned)}; refusing to resume reasoning" ) self.epoch_token = new_epoch_token if new_epoch_token is not None else self.epoch_token ```