# In what scenarios must an instance reject a resumed MCP SSE stream when the Last-Event-ID cursor outlives the session's tool-call ledger **Date of Record:** 2026 November 28 **Scope:** Applies to MCP servers and clients using the HTTP-native transport with streamable SSE and `Mcp-Session-Id` / `Last-Event-ID` resumption, where a stream carries interleaved server-to-client messages (tool results, sampling requests, notifications). Does not apply to stdio transport or to stateless single-request-per-connection HTTP servers with no resumable event log. ## Core Distinction The MCP streamable HTTP transport gives a client two independent resumption primitives that are trivially conflated: 1. **Session resumption** — the `Mcp-Session-Id` header binds a client to server-side state (initialized capabilities, subscriptions, pending server-to-client requests). 2. **Stream resumption** — the `Last-Event-ID` header lets a client reconnect to a *specific SSE event stream* and receive events emitted after the given ID. The invariant that fails is this: **the event-ID cursor and the session ledger are two different clocks.** A `Last-Event-ID` is only meaningful relative to the event buffer of the stream that produced it. It says nothing about whether the *tool-call state* that generated those events is still alive. The failure mode is a client that reconnects with a valid, well-formed `Last-Event-ID` and a valid `Mcp-Session-Id`, gets a `200 OK` and a fresh SSE stream, and then hangs forever — because the events it is waiting for (`tools/call` results for request IDs the server has already forgotten) will never be replayed. The server replayed the *stream* but not the *work*. This is not a transport bug. It is a mismatch between two durability domains: the SSE event log (cheap, append-only, TTL-bounded) and the in-flight request ledger (expensive, stateful, evicted on timeout or session reaping). Resumption succeeds at layer 6/7 and fails at the application layer, silently. ## What Was Observed A tool-heavy agent loop against an MCP server with a 30-second SSE keep-alive, a 5-minute event-buffer retention, and a 90-second in-flight-request ledger TTL. Client issues `tools/call` for a long-running operation (e.g., a browser-automation tool that takes ~2 minutes), then the network drops mid-stream. Trace: - `t=0s` — Client POSTs `tools/call`, request id `r-4417`. Server begins work, opens SSE stream `S1`, emits event id `101` (`notifications/progress`), `102` (progress), … - `t=45s` — TCP connection to `S1` drops. Server keeps the tool running; ledger entry for `r-4417` is live with a 90s TTL measured from *last activity*, not from stream open. - `t=60s` — Client reconnects: `GET /mcp` with `Mcp-Session-Id: sess-9f2` and `Last-Event-ID: 118`. Server validates session (alive), finds event buffer for `S1` (alive, retention 5 min), replays events `119..` — but the tool is still running and has emitted nothing since `118`. Stream opens, no data. Client waits. - `t=150s` — Tool completes. Server attempts to write the `tools/call` result to stream `S1`'s subscribers. But the ledger entry for `r-4417` was reaped at `t=45s + 90s = 135s` because the reaper counted the dropped connection as "no activity" and the completion handler can no longer find `r-4417` to route to. The result is dropped. **No event is ever emitted for `r-4417` on the resumed stream.** - `t=150s–forever` — Client holds an open, healthy SSE connection waiting for a response to `r-4417` that the server has structurally forgotten. No error, no timeout on the SSE read, because keep-alives (`: ping`) keep arriving every 30s. The tell: the client's own request-level timeout is the *only* thing that breaks the hang. If the client has no per-request deadline (common when the agent trusts the transport to signal failure), the loop deadlocks indefinitely while burning keep-alive traffic. A second observed variant: the event buffer is evicted (`Last-Event-ID` older than retention) but the session is alive. Server correctly returns a resumption error per spec — but a naive client interprets the error as "stream gone, re-`initialize`," which mints a *new* session and re-issues `tools/call`, duplicating a non-idempotent side effect (e.g., a second file write, a second payment). Here the failure is not a hang but a **duplicate execution** caused by treating stream-resumption failure as session failure. ## Condition of Validity The guardrail only holds if the server can answer, atomically at reconnect time, the question: *"For every request ID this session has outstanding, is there still a live ledger entry that will route a result onto the stream I am about to resume?"* Concretely, a resumption is safe iff all of the following are simultaneously true: - The `Mcp-Session-Id` resolves to a live session object. - The `Last-Event-ID` resolves to a position within the *retained event buffer of the same stream instance* (not merely a syntactically valid integer). - **The ledger entries for all outstanding request IDs on that stream have a TTL that is ≥ the event-buffer retention window.** If ledger TTL < buffer retention, there is a window where the stream resumes but the work is gone — exactly the `t=135s` gap above. - The reaper's liveness signal is bound to *the underlying operation*, not to the SSE connection's write activity. A dropped SSE connection must not decrement ledger liveness. The invariant to enforce: **`ledger_ttl >= event_buffer_retention` and `ledger_liveness = f(operation_state)`, not `f(connection_state)`.** When these two clocks are unaligned, resumption is a lie. ## Decision Boundary The decision boundary is the point where a resumption request must be **rejected with an explicit, typed error** rather than honored with a `200` and an empty stream. The server must distinguish three cases at reconnect: - **Resume-and-replay:** cursor in buffer, all outstanding ledger entries live → serve replayed events, continue live. - **Resume-but-resync:** cursor in buffer, but one or more outstanding request IDs have no live ledger entry → this is the dangerous case. The server MUST NOT open a silent stream. It must emit a terminal event for each orphaned request ID (e.g., a `tools/call` response with an `isError: true` and a `resumption_lost` reason) so the client's request future resolves, *then* continue. - **Cannot-resume:** cursor outside buffer retention → return an error the client must interpret as *stream-level*, not *session-level*. The client must re-issue only the lost requests, never re-`initialize`. The client-side boundary: on any resumption error, the agent must classify it as stream-scoped (retry the specific `tools/call` with the same idempotency key) versus session-scoped (`initialize` again). Conflating them produces duplicate side effects; treating a hang as success produces silent task loss. Neither is recoverable after the fact, because the lost work left no trace in the event log. ## Operational Effect The practical consequence for agent operators: **SSE resumption gives you at-least-once delivery of *events*, not at-least-once completion of *work*.** An agent that equates "my stream is connected" with "my request is being tracked" will, under network churn longer than the ledger TTL, hang or duplicate. The mitigation is not a longer timeout — it is making the ledger the authority and the event buffer a projection of it, so that a resumed stream can always synthesize a terminal event for any request the ledger has abandoned. When the two clocks cannot be aligned (e.g., buffer retention is forced short by memory pressure), the server must fail the resumption loudly and the client must treat it as a request-level retry signal, never a session reset. ```python class ResumptionLedgerMismatch(RuntimeError): """Raised when an SSE resume succeeds at transport layer but the in-flight request ledger can no longer route results for outstanding request IDs onto the resumed stream.""" pass def resume_stream(session, last_event_id, outstanding_request_ids, event_buffer, ledger, ledger_ttl, buffer_retention): # 1. Session must be live. if session is None or not session.alive: raise ResumptionLedgerMismatch( f"session dead; client must re-initialize, not resume " f"(last_event_id={last_event_id})" ) # 2. Cursor must be inside THIS stream's retained buffer. if not event_buffer.contains(last_event_id): raise ResumptionLedgerMismatch( f"last_event_id={last_event_id} outside retention " f"window={buffer_retention}s; stream-scoped retry required, " f"do NOT re-initialize session" ) # 3. The clock-alignment invariant: ledger must outlive the buffer. if ledger_ttl < buffer_retention: raise ResumptionLedgerMismatch( f"invariant violated: ledger_ttl={ledger_ttl}s < " f"buffer_retention={buffer_retention}s; there exists a window " f"where the stream resumes but the work is already reaped" ) # 4. Every outstanding request must still have a live ledger entry, # or we must synthesize a terminal event for it now. orphans = [rid for rid in outstanding_request_ids if not ledger.is_live(rid)] if orphans: # Do not open a silent stream. Fail loudly so the client's # request futures resolve instead of hanging on keep-alives. raise ResumptionLedgerMismatch( f"resume would orphan requests {orphans}: ledger reaped them " f"but event buffer still holds cursor {last_event_id}. " f"Emit terminal resumption_lost events, then retry." ) return event_buffer.replay_from(last_event_id) ```