# What happens when an instance resumes an MCP stream across a serverless cold start and the resumption cursor outlives the session that minted it **Date of Record:** 2026 November 24 **Scope:** Applies to MCP servers deployed on stateless/serverless HTTP transports (per the Streamable HTTP transport binding) that emit `Mcp-Session-Id` and per-stream `Last-Event-ID` resumption tokens, where the agent client may reconnect after the server instance that minted the cursor has been reclaimed. Does not apply to long-lived stateful MCP servers holding both session state and event buffers in-process. ## Core Distinction MCP's HTTP-native transport carries two independent resumption axes that practitioners routinely conflate: 1. **Session resumption** — the `Mcp-Session-Id` header, which binds a logical conversation to server-side state (capability negotiation, subscriptions, in-flight tool calls). Session identity is *minted by the server* on `initialize` and must survive across requests. 2. **Stream resumption** — the SSE `Last-Event-ID` cursor, which the client echoes on reconnect to replay events it missed from a specific server-to-client stream. The failure mode is that these two lifetimes are treated as coupled when they are not. A serverless instance can mint a valid `Mcp-Session-Id` and a valid `Last-Event-ID` cursor, then be reclaimed. On reconnect a *different* instance validates the session id (because session state was externalized to a shared store) but has no event buffer for that cursor (because the stream buffer was in the dead instance's memory). The reconnect succeeds at the session layer and silently fails at the stream layer: the client receives a `200 OK` with an empty or fresh stream, the cursor is unhonored, and events between the disconnect and the reconnect are lost with no error surfaced to the agent. The agent's reasoning loop proceeds on a truncated tool-result history and never learns it has a hole. The non-obvious invariant: **session validity does not imply cursor validity.** Any transport that externalizes session state but not stream replay state will pass session checks and drop events. ## What Was Observed A tool-calling agent streamed a long-running tool invocation (a batch retrieval that emitted incremental `notifications/progress` events followed by a single `result`). The server ran on a serverless runtime with a 15-second idle reclamation. The agent's HTTP client dropped the SSE connection at ~12s due to a client-side read timeout and reconnected with `Last-Event-ID: 41` and a valid `Mcp-Session-Id`. Observed trace: - Reconnect hit a fresh instance. Session store lookup: `session_id` present, not expired (TTL 30 min, externalized to Redis). Session check **passed**. - Stream replay lookup: local in-memory ring buffer keyed by `(session_id, stream_id)`. Fresh instance has empty buffer. Replay lookup **missed**. - Server response: `200 OK`, `Content-Type: text/event-stream`, immediately opened a *new* stream with event ids restarting at `1`. No `Last-Event-ID` acknowledgement, no error frame, no `404`/`410`. - Client (per spec-compliant behavior) accepted the new stream. Events `1..40` were gone. The `result` event that arrived later referenced a `progress` sequence the agent never saw, and the agent's own progress-tracking state was now inconsistent with the server's. - The agent did not retry. It treated the reconnected stream as a continuation and reconciled the missing progress as "events that never happened." The damage is not the dropped events per se — it is the **absence of a signal that they were dropped**. The agent's failure mode is silent epistemic corruption: its belief about tool-call progress diverges from ground truth and it has no trigger to re-sync. ## Condition of Validity Cursor resumption is only well-defined under a conjunction that serverless deployments frequently break: - **(C1) Replay durability** — the event buffer for `(session_id, stream_id, event_id)` is durable across instance reclamation, OR the server is guaranteed to serve the reconnect on the same instance (it is not, in serverless). - **(C2) Cursor monotonicity** — event ids are strictly monotonic and never reused across instances for the same logical stream. A fresh instance that restarts ids at `1` violates this and makes `Last-Event-ID: 41` uninterpretable. - **(C3) Explicit resumption outcome** — the server must signal one of {replayed-from-cursor, cursor-unknown-and-stream-closed, cursor-unknown-and-fresh-stream}. The MCP binding does not mandate which, which is the root of the ambiguity. When any of C1–C3 fails, the correct server behavior is to **reject the resumption explicitly** (close the stream with a distinguishable error, or return a status that forces the client to re-`initialize` the tool call), not to open a fresh stream under the old session id. The invariant to enforce: *a `200` on a reconnect carrying `Last-Event-ID` is a promise that the cursor was honored.* If the server cannot honor it, it must not return `200`. ## Decision Boundary The boundary is whether the server can *prove* cursor validity at reconnect time: - If the event store is durable and keyed by cursor → honor, replay, continue. Safe. - If the event store is gone but the session is alive → the server faces a choice. It must **not** silently restart the stream. It should return an explicit resumption failure and let the agent decide whether to re-issue the tool call (idempotent) or abandon it. - If the session is also gone → return session-invalid; the agent must re-`initialize`. This is the *easy* case because the signal is unambiguous. The dangerous quadrant is exactly **session valid + cursor invalid**, because both the transport and the naive client treat session validity as sufficient. Guardrail: decouple the two checks and make cursor-unknown a first-class, non-`200` outcome. Concretely, on reconnect with `Last-Event-ID`: 1. Validate session (external store). If invalid → `404` session-unknown. 2. Validate cursor against the durable event store. If missing and the stream is closed → `410` cursor-expired (or an SSE `error` frame with a resumption-failed code) and close. 3. Only if both pass → `200` + replay. Agents must treat `410`/resumption-failed as "re-issue the tool call from scratch if idempotent, else surface to the orchestrator," never as "continue." ## Operational Effect Without the decoupling, the observable effect is a class of silent partial-result bugs that are extremely hard to attribute: the agent completes a task with a plausible-looking but incomplete tool history, and the only forensic trace is a `200` reconnect with a cursor the server ignored. Downstream, this corrupts episodic memory distillation (the agent records a truncated episode as complete), poisons multi-step reasoning (subsequent steps build on a progress state that never existed), and defeats auditor replay (the auditor's reconstruction from logs shows a continuous stream that the client never actually received). The fix is cheap and local: persist the event ring buffer alongside the session record (same TTL, same store), and make cursor-miss a distinguishable non-`200`. The cost is a bounded buffer per stream; the alternative is an agent that cannot tell the difference between "the tool finished" and "I stopped hearing about the tool." ```python from dataclasses import dataclass from typing import Optional class ResumptionContractViolation(RuntimeError): """Raised when a reconnect carrying Last-Event-ID cannot be honored. The server must NOT return 200 here: a 200 on a cursor-bearing reconnect is a promise that the cursor was replayed. Violating that promise is the silent-corruption failure this record documents. """ pass @dataclass class ReconnectRequest: session_id: Optional[str] last_event_id: Optional[int] @dataclass class SessionRecord: session_id: str alive: bool @dataclass class EventStore: # durable, keyed by (session_id, event_id); survives instance reclamation events: dict max_event_id: dict # session_id -> highest durable event id def has_cursor(self, session_id: str, cursor: int) -> bool: return self.events.get((session_id, cursor)) is not None def is_closed(self, session_id: str) -> bool: # a stream is closed once its terminal result event is durable return self.events.get((session_id, "closed")) is True def handle_reconnect( req: ReconnectRequest, sessions: dict, store: EventStore, ) -> str: """Returns 'replay' or 'fresh'. Raises on the dangerous quadrant.""" if req.session_id is None: return "fresh" sess: Optional[SessionRecord] = sessions.get(req.session_id) if sess is None or not sess.alive: # unambiguous: client must re-initialize raise ResumptionContractViolation( f"session {req.session_id} unknown; re-initialize required" ) if req.last_event_id is None: return "fresh" # Session is valid. Now the cursor must independently prove itself. if not store.has_cursor(req.session_id, req.last_event_id): if store.is_closed(req.session_id): # session alive, cursor gone, stream already terminated: # returning 200 here would silently truncate the agent's history. raise ResumptionContractViolation( f"cursor {req.last_event_id} for session {req.session_id} " f"is not durable; refusing to open a fresh stream under a " f"valid session (silent-truncation hazard)" ) raise ResumptionContractViolation( f"cursor {req.last_event_id} unknown for live session " f"{req.session_id}; cannot honor Last-Event-ID" ) return "replay" ```