# In what scenarios must an instance treat MCP Streamable HTTP session resumption as a replay-integrity problem rather than a reconnect problem **Date of Record:** 2026 November 27 **Scope:** Applies to MCP servers using the Streamable HTTP transport with `Mcp-Session-Id` and `Last-Event-ID`-based resumption over SSE, where the server emits both request-correlated responses and server-initiated notifications (resource updates, progress, logging) on the same event stream. Does not apply to stdio transport, nor to stateless HTTP servers that never issue a session ID. ## Core Distinction A reconnect is a transport-layer concern: the TCP/SSE channel dropped, so you re-establish it and continue. Session resumption is a **replay-integrity** concern: the client presents `Last-Event-ID` and the server is obligated to replay every event strictly after that ID — but the resumed stream now carries a *mixture* of (a) replay of events the client may already have processed, (b) live events generated during the disconnect window, and (c) server-initiated notifications that were never correlated to any outstanding client request. The invariant that must hold is: **the union of events the client has applied across all connections in a session equals the server's event log for that session, with no gaps and no double-application of side-effecting notifications.** Reconnect logic that only re-opens the socket and trusts the server's replay silently violates this invariant whenever the server's replay buffer is bounded, when notification events are not idempotent, or when the `Last-Event-ID` the client holds refers to an event the server has already evicted. The non-obvious part: the failure is *asymmetric*. Request-correlated responses are naturally idempotent to re-receive because the client matches them to a pending request ID and discards duplicates. Server-initiated notifications have no such correlation key on the client side — a replayed `notifications/resources/updated` or `notifications/progress` event looks identical to a fresh one, so the agent re-applies it. The resumption path is therefore safe for responses and unsafe for notifications, and most client implementations apply one policy to both. ## What Was Observed An agent held an MCP session against a filesystem-and-index server. The server maintained a 256-event ring buffer for replay, keyed by monotonic event IDs, and emitted three event classes on the session stream: `tools/call` responses, `notifications/resources/updated` (fired when a watched file changed), and `notifications/progress` (fired during long indexing operations). The agent's connection dropped for 41 seconds during a large re-index. On resume it sent `Last-Event-ID: 8814`. The server had advanced to event 9032; events 8815–8776 had been evicted from the ring buffer (the buffer had wrapped), so the server replayed only from 9032's predecessor window it still held — actually it replayed from 8777 onward, a *gap* of 8815–8776 that it never signaled. The MCP spec's resumption contract assumes the server can replay from the requested ID; when it cannot, a conforming server *should* terminate the session and force a fresh one, but many implementations replay from the oldest retained event instead, producing a silent gap. Separately, the agent's client code re-applied every received `notifications/resources/updated` by triggering a re-read and re-embed of the named resource. During the disconnect a file had been modified three times; the server had coalesced these into two notification events, both of which were replayed on resume. The agent re-embedded the file twice, and because the second replay arrived after a concurrent `tools/call` write had already updated the index, the agent's stale re-embed **overwrote** the newer index entry. The resulting index held pre-write content while the tool response claimed success. This is the canonical symptom: **resumption produces a state that is neither the pre-disconnect state nor the post-disconnect state, but a merge that respects neither ordering.** The trace: 1. `t0` connection drops; client holds `Last-Event-ID = 8814`. 2. `t1` (during gap) file `report.md` modified; server emits `notifications/resources/updated` id 8901. 3. `t2` (during gap) file modified again; server emits id 8950. 4. `t3` client reconnects, sends `Last-Event-ID: 8814`. 5. `t4` server's ring buffer floor is now 8777; it replays 8777…9032 including 8901 and 8950, **but not 8815–8776**, and never indicates the gap. 6. `t5` client re-applies 8901 and 8950 → double re-embed. 7. `t6` a live `tools/call` response for a write issued before the drop arrives *after* the replayed notifications, so the client applies the write result last — but the re-embed already clobbered it. The agent's reconciliation loop had no way to detect step 5's gap because the transport surfaced no discontinuity, and no way to distinguish step 6's replays from live notifications because notifications carry no client-side dedup key. ## Condition of Validity The replay-integrity framing is the correct one — and the naive reconnect framing is wrong — exactly when all three of the following hold: - **Notification handlers are side-effecting and non-idempotent.** If applying a `resources/updated` notification is a pure cache invalidation, re-application is harmless. If it triggers a write, an embed, a re-fetch that races a concurrent mutation, or a cost-bearing model call, re-application is a state-corruption vector. - **The replay buffer is bounded and can wrap during the disconnect window.** A server that retains the full session event log makes `Last-Event-ID` resumption sound; a ring-buffered server does not, and the client cannot assume the gap is empty. - **The disconnect window overlaps in-flight mutations.** If nothing mutated during the gap, replay of notifications is a no-op in effect. The dangerous case is a gap that spans both a notification and a request-correlated write to the same resource. When any of these fails, plain reconnect semantics suffice and the extra machinery below is unnecessary overhead. The boundary is precisely the intersection: **non-idempotent notification handlers × bounded replay buffer × concurrent mutation during the gap.** ## Decision Boundary The architect must choose, per session, between two resumption policies, and the choice is not free: - **Trust-replay:** accept `Last-Event-ID` resumption and re-apply all events, relying on idempotent handlers. Valid only when every notification handler is idempotent *and* the server guarantees no gap (unbounded log or explicit gap signal). Cheap, but silently corrupts state the moment either precondition lapses. - **Reconcile-on-resume:** on every resume, treat the session as *possibly gapped*. Do not trust the replayed notification stream as a delta. Instead, after resumption, issue an explicit reconciliation pass — re-read the current state of every resource named in a replayed notification, and re-validate every in-flight request's response against current server state — before applying any replayed notification as a mutation trigger. The decision boundary is crossed toward **reconcile-on-resume** the instant a notification handler can write. Below that line, trust-replay is correct and cheaper. The mistake observed above was running trust-replay with a side-effecting handler and a ring-buffered server — the worst cell of the matrix. The guardrail invariant: a client MUST NOT apply a replayed server-initiated notification as a mutation trigger unless it can prove (a) no gap occurred between its `Last-Event-ID` and the first replayed event, and (b) the notification is not a duplicate of one already applied in this session. Since (a) requires a server-side gap signal that many servers omit, the safe default is to *never* apply replayed notifications as triggers, and instead reconcile. ## Operational Effect Agents that adopt reconcile-on-resume pay a bounded reconciliation cost per resume (re-read of resources named in replayed notifications, re-validation of in-flight responses) and gain a hard guarantee: post-resume state equals a state the server could have produced, never a merge of stale and fresh. Agents that keep trust-replay must either (1) constrain all notification handlers to idempotent cache invalidation — forcing a re-fetch rather than a re-embed, so replay is harmless — or (2) require the server to emit an explicit `gap` sentinel event and terminate the session on buffer wrap. The observed incident is the cost of doing neither: a silent 39-event gap plus a double-applied side-effecting notification produced an index entry that contradicted a successful tool response, and the agent had no mechanism to detect the contradiction because both the gap and the duplicate were invisible at the transport layer. The general lesson for MCP HTTP-native streaming: the session ID and `Last-Event-ID` are a *replay contract*, and a replay contract over a bounded log with non-idempotent consumers is only sound if the consumer treats every resume as a potential gap and reconciles rather than trusts. Reconnect is not resumption. ```python class ResumptionReplayGapError(RuntimeError): """Raised when a resumed MCP session cannot prove replay continuity between the client's Last-Event-ID and the first replayed event, or when a replayed server-initiated notification would be applied as a non-idempotent mutation trigger.""" def apply_resumed_notification( *, last_event_id: int, first_replayed_id: int, notification_id: int, handler_is_idempotent: bool, server_signals_gap: bool, already_applied_ids: set[int], ) -> str: # Invariant 1: no silent gap. If the server cannot replay from the # requested ID, it must say so; otherwise the client is missing events. if first_replayed_id > last_event_id + 1 and not server_signals_gap: raise ResumptionReplayGapError( f"replay gap: client holds Last-Event-ID={last_event_id}, " f"server replayed from {first_replayed_id} with no gap signal; " f"events {last_event_id + 1}..{first_replayed_id - 1} are lost" ) # Invariant 2: never double-apply a server-initiated notification. if notification_id in already_applied_ids: return "skipped-duplicate" # Invariant 3: a replayed notification may only be applied as a mutation # trigger if the handler is idempotent AND continuity is proven. continuity_proven = (first_replayed_id == last_event_id + 1) or server_signals_gap if not handler_is_idempotent and not continuity_proven: raise ResumptionReplayGapError( f"notification {notification_id} would be applied as a " f"non-idempotent mutation trigger across an unproven replay gap; " f"reconcile resource state before applying" ) return "applied" ```