# In what scenarios must an instance treat MCP stream `Last-Event-ID` resumption as invalid rather than replayable, and how does SSE multiplexing across concurrent tool calls break the naive resume invariant **Date of Record:** 2026 November 29 **Scope:** Applies to MCP servers and clients using the HTTP-native transport (Streamable HTTP) where a single SSE stream multiplexes events for multiple concurrent in-flight requests identified by per-request event IDs. Does not apply to stdio transport, nor to single-request-per-connection SSE designs where stream identity and request identity are 1:1. ## Core Distinction The MCP Streamable HTTP transport permits a client to reconnect to an interrupted SSE stream and request resumption by sending `Last-Event-ID`. The *naive* invariant most implementations assume is: *the server can replay every event with an ID numerically or lexically greater than `Last-Event-ID`, and the client will reconstruct the missing suffix of the session.* This invariant is false whenever the SSE stream is **multiplexed** — i.e., one persistent stream carries events belonging to several concurrent JSON-RPC requests (multiple tool calls, notifications, and progress updates interleaved). The failure is not "we lost an event." The failure is that **event IDs are stream-global but their payloads are request-partitioned**, so a resume from `Last-Event-ID = N` delivers a suffix of the *stream*, not a suffix of any *request*. A client that reasons about resumption at the request level will either (a) apply a replayed event to the wrong in-flight request, or (b) conclude a request is complete because no further events for it appeared in the replayed suffix — when in fact that request's terminal event was emitted *before* `N` and its counterpart events for other requests continued afterward. The distinction that matters: **stream continuity ≠ request continuity.** A server can honor `Last-Event-ID` perfectly (no gaps in the stream) while the *logical* request that the client cares about is already unrecoverable, because its terminal `result` or `error` frame fell below the resume watermark. ## What Was Observed A client agent dispatched three concurrent `tools/call` requests over one MCP session: `A` (long-running, ~40s), `B` (fast, ~200ms), `C` (medium, ~3s). The server multiplexed all three onto a single SSE response stream with monotonically increasing event IDs: ``` id: 100 event: message data: {"jsonrpc":"2.0","id":"B","result":{...}} # B completes id: 101 event: message data: {"jsonrpc":"2.0","id":"C","result":{...}} # C completes id: 102 event: message data: {"jsonrpc":"2.0","id":"A","result":{"progress":0.4}} ``` The client's HTTP connection dropped at event 102 (a mid-stream TCP reset during A's progress notification). The client reconnected with `Last-Event-ID: 102`. The server, honoring the header correctly, replayed only events with ID > 102 — which were *later* progress notifications for A and eventually A's terminal result. The client's request-tracking layer, however, had already garbage-collected B and C from its pending-request table (they had terminal frames) but had **not** recorded that their terminal frames were *below the resume watermark*. On reconnect, the client's resume handler saw a stream whose first event belonged to `A`, and its "requests still awaiting a terminal frame" set was `{A}`. That was correct for A. The failure surfaced on a *second* disconnect, this time at event 105, after A's terminal frame at 104 had been delivered but before the server's trailing `end_of_stream` marker. The client reconnected with `Last-Event-ID: 105`. The server replayed 106+. A's terminal frame at 104 was **not** replayed (it is below the watermark). The client's resume handler, having lost its in-memory record of A's completion across a process restart (the agent had checkpointed only the pending-request set, not the completed-set), saw a stream with no terminal frame for A and no further events for A, and hung until timeout — then re-issued A, producing a duplicate side effect. The observable signature: **resumption succeeded at the transport layer (no stream gaps, server returned 200 with valid SSE) while resumption failed at the session layer (a completed request was resurrected as pending).** The two layers disagreed about what "resumed" means. ## Condition of Validity The naive resume invariant holds only under a conjunction of conditions, all of which must be true simultaneously: 1. **No multiplexing across the resume window.** Either the stream carries exactly one logical request, or the client resumes with a per-request watermark rather than a stream-global one. The MCP spec's `Last-Event-ID` is stream-global; it does not carry request identity. Therefore multiplexed streams violate this condition by construction. 2. **Client-side completed-set survives the disconnect.** The client must durably persist, for every request whose terminal frame it has *observed*, the fact of completion and the terminal frame's event ID, across the reconnect — and across any process restart that the reconnect may entail. If the client persists only the pending set (a common checkpointing shortcut), completed requests below the watermark become invisible. 3. **Server replay window covers the terminal frame of every request the client will resume.** The server must retain, per request, the terminal frame in its replay buffer for at least as long as the client might resume that request. If the server's replay buffer is bounded (it always is) and evicts A's terminal frame before the client's second resume, condition 3 fails even though conditions 1 and 2 hold. 4. **Event IDs are dense and monotonic per stream, and the client's watermark is the maximum *observed* ID, not the maximum *contiguous* ID.** If the server emits non-contiguous IDs (e.g., per-request counters, or gaps from filtered events), the client's `Last-Event-ID` may skip unobserved events that it nonetheless needed. When any of these fails, `Last-Event-ID` resumption is **not a recovery primitive** — it is a stream-position primitive being misused as a session-state primitive. The correct architectural move is to treat resumption as *transport-level continuity only*, and to make session-level recovery a separate, explicit protocol: the client, on reconnect, must send a `session/resume` request enumerating the request IDs it still considers pending, and the server must answer with the terminal state (or "still running") for each, independent of the SSE watermark. ## Decision Boundary The boundary is crossed at the moment the client's resume handler attempts to infer **request liveness** from **stream position**. Concretely, it is crossed when the handler does any of the following: - Treats "no events for request R in the replayed suffix" as "R is still running" — rather than "R's fate is unknown; query the server." - Reconstructs its pending set by subtracting observed terminal frames from the replayed suffix, rather than from a durable completed-set keyed by request ID. - Uses a single global `Last-Event-ID` to resume a session in which more than one request was in flight at disconnect time. The boundary is *not* crossed, and naive resumption is safe, when: the stream is single-request (one in-flight request per stream, as in a per-request SSE response body), or the client durably persists per-request terminal state and treats the replayed suffix as advisory-only. The operational rule: **if `pending_requests_at_disconnect > 1`, `Last-Event-ID` alone is insufficient to resume; the client MUST issue a session-level reconciliation query.** If `pending_requests_at_disconnect == 1` and that request's terminal frame was not yet observed, `Last-Event-ID` resumption is valid. If `pending_requests_at_disconnect == 0`, the client must not resume at all — it should close the session, because a resume with an empty pending set is exactly the case that resurrects completed requests. ## Operational Effect The downstream effect is **duplicate side effects from resurrected-completed requests**, plus **hung agents waiting on terminal frames that were delivered below the watermark**. Both are silent: the transport reports success, the SSE stream is well-formed, and no error is raised at the layer that would normally detect a problem. The agent's execution loop sees a stream that looks alive (heartbeats, progress for other requests) and a pending set that looks non-empty, and it waits — or re-issues. The guardrail is a **two-watermark resume**: the client persists, per request, the highest event ID observed *for that request* (not the stream-global max), and on reconnect sends `Last-Event-ID` as the stream-global max *plus* a `resume_requests` list of `{request_id, last_seen_event_id}` pairs. The server replays the stream suffix from the global watermark (for transport continuity) and, separately, replays any retained terminal frames for the enumerated requests whose `last_seen_event_id` is below their terminal frame's ID (for session continuity). If the server has evicted a terminal frame the client needs, it must answer with an explicit `resume_gap` error for that request — never silently omit it. The invariant to enforce: **for every request the client considers pending, either the server replays its terminal frame or the server explicitly reports the frame as unrecoverable.** Absence of a terminal frame in the replay is never evidence of non-completion. The following reference implementation encodes the reconciliation check and raises when the invariant is violated. ```python from dataclasses import dataclass, field from typing import Dict, Optional class ResumeInvariantViolation(RuntimeError): """Raised when SSE replay is used as a session-resume primitive and the stream-global watermark cannot account for a request's terminal frame.""" @dataclass class RequestState: request_id: str last_seen_event_id: int terminal_event_id: Optional[int] = None # set when terminal frame observed @dataclass class SessionResumeState: stream_watermark: int requests: Dict[str, RequestState] = field(default_factory=dict) def pending(self) -> Dict[str, RequestState]: return {rid: r for rid, r in self.requests.items() if r.terminal_event_id is None} def reconcile_resume( client_state: SessionResumeState, server_replayable_terminal_ids: Dict[str, Optional[int]], ) -> Dict[str, str]: """ Reconcile a client's pending set against the server's replay buffer. Returns a mapping request_id -> disposition in {"replay", "still_running"}. Raises ResumeInvariantViolation if a pending request's terminal frame is below the stream watermark and the server cannot replay it. """ dispositions: Dict[str, str] = {} for rid, req in client_state.pending().items(): if rid not in server_replayable_terminal_ids: # Server has no record of this request at all: cannot resume. raise ResumeInvariantViolation( f"request {rid!r} pending at client but unknown to server replay buffer" ) terminal_id = server_replayable_terminal_ids[rid] if terminal_id is None: # Server confirms the request is genuinely still running. dispositions[rid] = "still_running" continue if terminal_id > client_state.stream_watermark: # Terminal frame is above the watermark: normal replay covers it. dispositions[rid] = "replay" else: # Terminal frame is BELOW the watermark. Naive Last-Event-ID replay # will NOT deliver it, but the request is actually complete. # This is the resurrection hazard. if req.terminal_event_id is None: raise ResumeInvariantViolation( f"request {rid!r} terminal frame (id={terminal_id}) is below " f"stream watermark {client_state.stream_watermark} and was never " f"observed by client; naive Last-Event-ID resume would hang or " f"resurrect a completed request" ) # Client already observed the terminal frame; nothing to replay. dispositions[rid] = "already_complete" return dispositions def safe_resume( client_state: SessionResumeState, server_replayable_terminal_ids: Dict[str, Optional[int]], ) -> None: """Entry point called by the agent's reconnect handler.""" pending = client_state.pending() if len(pending) == 0: # A resume with an empty pending set is the classic resurrection case. raise ResumeInvariantViolation( "refusing to resume: no pending requests, but stream watermark " f"{client_state.stream_watermark} implies prior activity; close session instead" ) if len(pending) > 1: # Multiplexed resume: Last-Event-ID alone is insufficient. reconcile_resume(client_state, server_replayable_terminal_ids) return # Single pending request: naive resume is valid only if its terminal frame # is not already below the watermark. (rid, req), = pending.items() terminal_id = server_replayable_terminal_ids.get(rid) if terminal_id is not None and terminal_id <= client_state.stream_watermark: raise ResumeInvariantViolation( f"single pending request {rid!r} has terminal frame id={terminal_id} " f"<= watermark {client_state.stream_watermark}; resume would hang" ) ```