# When must an instance treat an MCP SSE `Last-Event-ID` resumption as a *new* session rather than a continuation — and what breaks if it doesn't **Date of Record:** 2026 November 29 **Scope:** Applies to MCP clients and servers using the HTTP-native transport with streamable SSE and `Mcp-Session-Id` + `Last-Event-ID` resumption. Does not apply to stdio transport, nor to stateless SSE endpoints that never issue session tokens. ## Core Distinction There are two orthogonal identities in streamable MCP-over-HTTP: the **session identity** (`Mcp-Session-Id`, established at `initialize` and stable for the life of a logical agent run) and the **stream cursor** (`Last-Event-ID`, a per-event monotonic identifier scoped to a single SSE stream instance). The failure mode is treating the cursor as a continuation of the session: a client that reconnects with a valid `Last-Event-ID` after the server has *already evicted or rotated the underlying session* will receive replay of events that belong to a dead execution context, and will merge them into a live context that no longer shares the same tool-call ledger, pending request map, or reasoning state. The invariant is: **a resumption is only a continuation if the server can prove the session backing the event ID is the same session the client is holding.** If it cannot, the correct action is to fail closed and re-initialize, not to splice. ## What Was Observed An agent driver (MCP client) maintained a long-lived SSE stream for a filesystem+shell server. The server's session store had a TTL of 300s of idle *stream* time, but the agent's tool calls were dispatched on a *separate* POST channel. Sequence: 1. Agent opens `GET /mcp` with `Mcp-Session-Id: S1`, server assigns stream, emits events `e1..e17` (including `notifications/tools/list_changed` and two `progress` notifications for an in-flight `shell/exec`). 2. Agent issues a long `shell/exec` (POST) that takes 340s. The SSE stream sees no traffic and the server's idle reaper tears down stream state, **but the session record for `S1` is retained for 60s of grace**, then evicted. 3. Network blip at t=341s. Client reconnects: `GET /mcp` with `Mcp-Session-Id: S1` and `Last-Event-ID: e17`. 4. Server's session store no longer has `S1`. Its stream replay buffer, however, was keyed by `(stream_id, event_id)` and *not* by session — because the stream id was generated at `GET` time, not at `initialize` time. The server replays `e15`, `e16`, `e17` from the orphaned buffer and answers `200 OK` with a fresh stream, but **without re-establishing session `S1`**. 5. Client receives replayed `progress` events for a `shell/exec` request id `req-42`. Its local pending-request map has `req-42` marked `completed` (it finished at t=340s). The replay overwrites the completion record, and the client re-issues the tool call. The shell executes twice. The observable symptom was not a crash. It was a **duplicate side effect** and a **stale progress bar** — the agent's reasoning loop saw "in progress" for a request it had already resolved, and its next planning step assumed the shell was still running. In a second reproduction, the replayed `tools/list_changed` caused the client to invalidate a tool schema cache that had been correctly refreshed on the *new* session, regressing to the old schema and then failing a schema-negotiation check on the next call. The key anatomical detail: **the replay buffer outlived the session, and the replay path did not re-validate the session binding.** The event IDs were valid; the session they implied was not. ## Condition of Validity A `Last-Event-ID` resumption is a *valid continuation* only when all of the following hold at the server, atomically: - The `Mcp-Session-Id` presented matches a session record that is still live (not in grace, not evicted, not rotated). - The event ID is within the retained replay window **for that session**, not merely for the transport connection. - The session's pending-request ledger and tool-call ledger are consistent with the replayed events (i.e., the server can assert "events `e(n+1)..e(m)` are the only ones you missed"). - No `initialize` has occurred on the stream since the last event the client acked. If any of these is false, the resumption must be answered with a terminal `404`/`410` on the session (or an explicit `session_expired` error frame), forcing the client to `initialize` a new session. The client-side mirror invariant: **on any resumption response, the client must reconcile the server's reported session state against its own before processing replayed events; if the server cannot prove same-session, the client must discard the replay and re-initialize.** ## Decision Boundary The boundary is not "did the connection drop" — it is "**can the server reconstruct the session's causal frontier from the event ID alone**." Two concrete discriminators: - **Session-scoped cursor vs. stream-scoped cursor.** If the cursor namespace is the transport connection (common when the SSE endpoint is stateless and event IDs are just a monotonic counter), replay is never safe across a session boundary. If the cursor namespace is the session (event IDs derived from a per-session log), replay is safe *only* while the session log is retained. The decision must be made on the *namespace*, not on the numeric continuity of IDs. - **Grace window vs. replay window.** These are independent TTLs and are almost always misconfigured to differ. A replay window longer than the grace window is a latent duplicate-execution bug. The safe relation is `replay_window ≤ grace_window ≤ session_ttl`, and the server must reject resumption as soon as the session leaves grace, even if the buffer still has events. The client-side boundary: if the client's own pending-request map has *advanced past* the last event ID the server claims to be resuming from, the client is ahead of the server's causal frontier. That is a divergence, not a resumption, and must be treated as a new session. ## Operational Effect Concretely, when this boundary is violated: - **Duplicate tool side effects.** Replayed `progress`/`result` frames for completed requests cause re-dispatch. This is the highest-severity outcome because it is silent: no error, no exception, just a second `shell/exec` or a second write. - **Schema regression.** Replayed `notifications/tools/list_changed` invalidate a cache that was refreshed on the new session, so the agent negotiates against a stale schema and fails the next structured call. - **Reasoning divergence.** The agent's planning state assumes a request is in-flight when it is resolved (or vice versa), producing a plan that references a tool result that no longer exists in the session ledger. - **Silent session fork.** The server believes it is serving a fresh stream while the client believes it holds `S1`. Any subsequent `initialize`-gated capability (sampling, roots, elicitation) is now answered against a session the client never initialized, and the server's auth/warrant propagation (if any) is keyed to the wrong session. The mitigation is a **session-binding check on every resumption**, performed before any event is written to the socket, and a client-side **causal-frontier reconciliation** that refuses to merge replay across a session-generation change. The following guard encodes both sides and raises on the unsafe splice. ```python from dataclasses import dataclass, field from typing import Optional class SessionResumptionViolation(RuntimeError): """Raised when an SSE resumption would splice events across a session boundary.""" pass @dataclass class SessionRecord: session_id: str generation: int live: bool replay_window_end: int # highest event id retained for this session grace_expires_at: float # monotonic seconds pending_requests: set[str] = field(default_factory=set) @dataclass class ClientState: session_id: str generation: int last_event_id: int resolved_requests: set[str] = field(default_factory=set) def validate_resumption( server: dict[str, SessionRecord], client: ClientState, presented_session_id: str, presented_last_event_id: int, now: float, ) -> SessionRecord: rec = server.get(presented_session_id) # 1. Session must exist and be live (not evicted, not merely in grace). if rec is None or not rec.live: raise SessionResumptionViolation( f"session {presented_session_id!r} is not live; " f"resumption must be rejected and client re-initialized" ) # 2. Grace window must not have lapsed even if the replay buffer still holds events. if now >= rec.grace_expires_at: raise SessionResumptionViolation( f"session {presented_session_id!r} grace expired at {rec.grace_expires_at}; " f"replay buffer presence is not sufficient for continuation" ) # 3. Session generation must match the client's held generation. if rec.generation != client.generation: raise SessionResumptionViolation( f"session generation mismatch: server={rec.generation} client={client.generation}; " f"cursor is stream-scoped, not session-scoped" ) # 4. Cursor must be within this session's replay window. if presented_last_event_id > rec.replay_window_end: raise SessionResumptionViolation( f"Last-Event-ID {presented_last_event_id} exceeds session replay window " f"{rec.replay_window_end}; cannot reconstruct causal frontier" ) # 5. Client must not be ahead of the server's frontier (divergence, not resumption). if client.last_event_id > presented_last_event_id: raise SessionResumptionViolation( f"client frontier {client.last_event_id} is ahead of resumed frontier " f"{presented_last_event_id}; this is divergence, not continuation" ) # 6. Replayed progress for already-resolved requests is a duplicate-execution hazard. overlap = rec.pending_requests & client.resolved_requests if overlap: raise SessionResumptionViolation( f"replay would resurrect resolved requests {sorted(overlap)}; " f"duplicate side effects possible" ) return rec ```