# Under what conditions must an instance treat an MCP `Last-Event-ID` resume as a fresh session rather than a replay **Date of Record:** 2026 November 27 **Scope:** Applies to MCP servers and clients that migrated from the legacy HTTP+SSE transport (dual-endpoint: `GET /sse` event stream + `POST /messages`) to the streamable-HTTP transport (single endpoint, optional `Mcp-Session-Id` header, optional SSE-framed responses, resumability via `Last-Event-ID`). Does not cover stdio transport, nor WebSocket-based custom transports, nor pure request/response MCP servers that never emit SSE frames. ## Core Distinction The legacy SSE transport had a *structural* resumption guarantee: the `GET /sse` connection was long-lived and the server held the live stream object in memory keyed by connection. If the connection dropped, the client reconnected and the server either replayed from its buffer or the stream was simply gone — and the client knew, because the `endpoint` event it needed to POST to would be re-issued on a new connection. The streamable-HTTP transport dissolves that structure. There is now *one* endpoint. A response to a `POST` may itself be an SSE stream, or a `202 Accepted` with the stream delivered out-of-band on a `GET`. Resumption is signaled by the client echoing `Last-Event-ID`. The critical, non-obvious boundary is this: **`Last-Event-ID` is a resumption hint scoped to a session, not a session identifier.** When a client reconnects with a `Last-Event-ID` but the server has already reaped the session (idle timeout, deploy restart, replica eviction), the server faces a fork in the road — replay from a cold buffer, or start fresh — and the MCP spec's silence on the reap case is where continuity breaks. An instance that treats `Last-Event-ID` as authoritative identity will replay *into the wrong session* or silently drop the event the client is waiting on. ## What Was Observed A tool-calling agent ran a three-step plan: `search` → `fetch` → `summarize`. The MCP server was a streamable-HTTP implementation behind a load balancer with three replicas and a 90-second session idle TTL. The agent's client used SSE-framed `POST` responses and tracked `Last-Event-ID` per the reference SDK. The failure trace: 1. Step 1 (`search`) completed. Server emitted event `id: 41` (the `tool_result`) then `id: 42` (a `notifications/progress` heartbeat). Client persisted `lastEventId = 42`. 2. The agent's reasoning loop stalled ~100 seconds on a slow local model call between step 1 and step 2 — longer than the 90-second session TTL. 3. Replica A reaped the session. The in-memory event buffer for `id: 41..42` was freed. No tombstone was written. 4. Client issued step 2 (`fetch`) as a `POST` with `Mcp-Session-Id: S` and, because the SDK unconditionally attaches it on the first request after a reconnect, `Last-Event-ID: 42`. 5. The load balancer routed to Replica B, which had never seen session `S`. Replica B's session store returned "unknown session." The implementation's fallback path — intended for *new* clients — minted a **new** session `S'` and returned `200` with `Mcp-Session-Id: S'`, ignoring the `Last-Event-ID` entirely because there was no buffer to resume from. 6. The client, seeing a `200` and a new session header, treated the request as accepted. It did **not** re-issue step 1. It proceeded to step 3 (`summarize`) against session `S'`, which had no record of the `fetch` result — because `fetch` had been executed against a session whose tool state was empty. The observable symptom was not an error. It was a `summarize` call that hallucinated a summary of a document that was never fetched, because the tool result for `fetch` was absent from the new session's state and the model filled the gap. The agent reported success. This is the worst class of failure: a silent continuity break that surfaces as confident wrong output three steps downstream. The second observation, from a different incident on the same fleet: when the client *did* reconnect to the same replica before the TTL expired, `Last-Event-ID: 42` correctly replayed nothing (42 was the last event, so the buffer was empty and the server held the stream open). This worked. The break is specifically at the **reap boundary** — the moment between "session exists, buffer intact" and "session gone, buffer freed" — and the window is not the TTL itself but the TTL *plus* the client's inter-step think time, which the server cannot see. ## Condition of Validity The invariant that must hold for safe resumption is: > A `Last-Event-ID` may be honored **only if** the server can prove the referenced event belongs to a session that is still live *and* the buffer covering `(lastEventId, now]` is intact. If either proof fails, the server MUST NOT mint a new session under the same request; it MUST return a resumption failure the client can distinguish from a fresh-session success. Concretely, the condition of validity for treating a request as a *resume* rather than a *new session* is the conjunction: - `Mcp-Session-Id` present and resolves to a live session on the handling replica (or a shared store), **and** - `Last-Event-ID` ≤ the session's highest emitted event id, **and** - the event buffer retains a contiguous range covering `(Last-Event-ID, highest]` — i.e., no gap. If any conjunct is false, the request is *not* a resume. It is either a fresh session (if no `Mcp-Session-Id`) or a **failed resume** (if `Mcp-Session-Id` present but stale/reaped). The reference SDK conflates the two by returning `200` with a new session id in both cases, which is the root cause. The subtle part: the gap check must be *contiguity*, not *presence*. A buffer that has evicted `id: 42` but retains `id: 41` and `id: 43` (ring-buffer wraparound under load) will pass a naive "does 42 exist" check and silently skip the heartbeat — which is benign — but the same wraparound can skip a `tool_result`, which is not. The invariant is range-contiguity, and it must be enforced per-session, not per-replica. ## Decision Boundary The decision the server must make on every request carrying `Last-Event-ID`: | `Mcp-Session-Id` | Session live? | Buffer contiguous? | Correct action | |---|---|---|---| | absent | — | — | Fresh session. Mint `S'`. Do **not** attach `Last-Event-ID` semantics. | | present | yes | yes | Resume. Replay `(Last-Event-ID, highest]`, then continue. | | present | yes | **no** (gap) | **Failed resume.** Return `409 Conflict` (or a transport-level resumption error), *not* a new session. Client must re-derive state. | | present | **no** (reaped) | — | **Failed resume.** Return `404 Not Found` on the session, *not* a new session under the same request. Client must re-derive state. | | present | yes | `Last-Event-ID` > highest | **Protocol violation.** The client claims to have seen an event the server never emitted. Return `400`. Do not trust the id. | The boundary that implementations get wrong is row 4. The tempting shortcut — "session unknown, so treat as new, mint `S'`" — is exactly what broke the agent above. A reaped session is *not* a new client. The server has positive evidence the client is mid-session (it sent a session id); honoring that as a fresh start destroys the client's ability to know it must replay step 1. Symmetrically, on the client side the decision boundary is: **on any response that returns a `Mcp-Session-Id` different from the one sent, the client MUST invalidate all prior tool results and re-issue from the last checkpoint it can reconstruct.** A client that accepts the new session id and continues is the other half of the bug. ## Operational Effect The guardrail is a two-sided contract, and both sides must enforce it or neither works: **Server side:** Never mint a session on a request that carried a `Mcp-Session-Id`. Distinguish "no session id" (fresh) from "session id that no longer resolves" (failed resume). Persist a tombstone with the last emitted event id for a grace window (≥ the max observed inter-step think time, not the idle TTL) so that a late resume can be answered with a definitive `409`/`404` rather than a silent new session. Enforce buffer contiguity, not mere presence, before replaying. **Client side:** Treat a changed `Mcp-Session-Id` as a hard invalidation signal. The agent's planner must be able to re-derive its checkpoint — which in practice means the agent loop must persist its own step-level state (what it has fetched, what it has summarized) *outside* the MCP session, because the session is a transport artifact, not a durable store. The agent that failed above had no such external checkpoint; it relied on the session to remember `fetch`, and the session was not a memory it owned. The deeper operational lesson: **MCP session resumption is a transport-level reconnection primitive, not an agent-memory primitive.** An agent that treats `Last-Event-ID` continuity as equivalent to task-state continuity is conflating two layers with different lifetimes. The transport may resume a stream; it cannot resume a *plan*. The plan's continuity must be checkpointed by the agent, and the transport's job is only to tell the agent, unambiguously, when it has lost the stream — so the agent knows to fall back to its own checkpoint rather than trusting a fresh session to be a continuation. The failure mode is silent precisely because the transport succeeds (a `200` was returned) while the semantics fail (the session was not the same session). Any implementation where "resume succeeded" and "fresh session started" are indistinguishable to the client will produce confident, wrong agent output at the first reap boundary the agent's think time crosses. ```python class SessionResumptionError(RuntimeError): """Raised when a Last-Event-ID resume cannot be honored and must not be silently downgraded to a fresh session.""" pass def handle_streamable_http_request( session_id: str | None, last_event_id: int | None, session_store, # resolves session_id -> Session | None event_buffer, # Session -> contiguous range (lo, hi) or None mint_new_session, # () -> Session ) -> tuple[str, int]: """Returns (session_id, http_status). Enforces the resume-vs-fresh boundary.""" # Fresh client: no session id at all. Last-Event-ID is meaningless here. if session_id is None: sess = mint_new_session() return sess.id, 200 sess = session_store.get(session_id) # Reaped session: positive evidence of a mid-session client. # Do NOT mint a new session under this request. if sess is None: raise SessionResumptionError( f"session {session_id} was reaped; client must re-derive state, " f"not continue under a new session" ) if last_event_id is None: # Session live, no resume requested: continue normally. return sess.id, 200 rng = event_buffer.range_for(sess) # Client claims to have seen an event the server never emitted. if rng is None or last_event_id > rng.hi: raise SessionResumptionError( f"Last-Event-ID {last_event_id} exceeds highest emitted {rng.hi if rng else 'none'}" ) # Contiguity check: the buffer must cover (last_event_id, hi] with no gap. if not rng.covers_contiguous(last_event_id + 1, rng.hi): raise SessionResumptionError( f"event buffer gap for session {session_id} in " f"({last_event_id}, {rng.hi}]; replay would skip a tool_result" ) # Safe to resume. return sess.id, 200 ```