# What happens when an instance resumes an MCP stream without replaying the backpressure watermark **Date of Record:** 2026 November 20 **Scope:** Applies to MCP servers and clients using HTTP-native streaming transports (SSE / chunked `text/event-stream` with `Last-Event-ID` resumption) where the server buffers per-session outbound events and the client applies credit-based or window-based backpressure. Does not apply to stdio transports, WebSocket full-duplex sessions, or stateless one-shot request/response MCP deployments. ## Core Distinction The failure is not "the stream dropped." Streams drop constantly and the protocol anticipates it: the client reconnects with `Last-Event-ID`, the server replays events after that ID, and the session continues. The distinction that matters is between **event-ID continuity** and **flow-control continuity**. MCP's HTTP-native resumption contract guarantees the former (no gaps in the event sequence) and says nothing about the latter (how many unacknowledged events the client is willing to hold). A resuming instance that replays from `Last-Event-ID` while resetting its receive window to its configured maximum will silently violate the backpressure invariant that the pre-disconnect session was maintaining — and the server, which tracks credit per session, has no way to distinguish "client resumed and wants a fresh window" from "client resumed and still owes us the old debt." The invariant that breaks: **for a given session, the sum of events delivered on the wire must never exceed the client's advertised credit at any point in the event log, across all connection incarnations of that session.** Resumption restores the first half of that sentence and forgets the second. ## What Was Observed A tool-heavy agent (filesystem + shell + a long-running `code_search` tool) ran against an MCP server over SSE. The client advertised a window of 64 in-flight events. Mid-`code_search`, the client's event loop stalled for ~9 seconds behind a synchronous tool result handler, the TCP connection was reset by an intermediary, and the client reconnected with `Last-Event-ID: 8412`. The server had, at disconnect time, 64 events in flight (its full permitted credit) plus 41 more events already enqueued in the session's server-side backlog because the `code_search` tool was emitting progress notifications faster than the client drained them. On resume, the client sent `Last-Event-ID: 8412` and a fresh `X-MCP-Credit: 64`. The server interpreted the new header as the session's current window, replayed events 8413–8477 (65 events — the backlog plus one that had been emitted during the reconnect handshake), and then continued live emission at 64-credit pace. The client, which had already consumed and partially processed events 8413–8453 before the stall, received them again. Because the client's dedup layer keyed on event ID and its handler was **not idempotent for progress notifications**, it double-counted 41 progress ticks against the tool's declared `total`, concluded the tool had over-reported, and emitted a `tool_result` with a corrupted `progress: 106/100`. The agent's planner, seeing an impossible ratio, abandoned the `code_search` call and re-issued it — three times, because the same stall recurred. The session never crashed, never logged an error, and produced a plausible-looking but wrong final answer. The non-obvious part: **the server did nothing wrong per spec.** It replayed from the last acknowledged event ID and honored the client's advertised credit. The client did nothing wrong per spec either — it resumed correctly and advertised a legal window. The bug lives in the seam: credit is per-connection in the client's implementation and per-session in the server's, and neither side has a field in the resumption handshake to reconcile the two. ## Condition of Validity This failure requires all of the following to hold simultaneously: 1. **Server-side backlog exists at disconnect time.** If the server only buffers up to the client's outstanding credit and drops on disconnect (a "no backlog" policy), replay is bounded by the credit and the client's fresh window is sufficient. The failure needs backlog > 0, which requires the server to accept events from tool execution while the client is not draining — i.e., the tool emits asynchronously relative to the client's read loop. 2. **Replay is not idempotent at the client handler layer.** Event-ID dedup protects against duplicate *delivery* but not duplicate *effect*. Progress notifications, incremental token streams, and "append to buffer" semantics are all non-idempotent under replay unless the handler tracks applied IDs, not just received IDs. 3. **The client resets credit on reconnect rather than carrying it.** A client that persists its outstanding-credit counter across reconnects (credit = window − unacked) forces the server to either honor a reduced window or reject the resume. Most client SDKs reset because credit is scoped to the connection object, which is recreated on reconnect. 4. **The tool's output is monotonic and self-describing** (progress counters, sequence numbers, cumulative token counts) so that double-application produces a detectable-but-not-fatal corruption rather than an immediate parse error. If the tool emitted opaque chunks, the corruption would be silent and undetectable. If (1) fails, you have a plain reconnect and no bug. If (2) fails, replay is safe and the credit mismatch is harmless. The dangerous quadrant is (1)∧(2)∧(3): backlog exists, replay is non-idempotent, and credit resets. ## Decision Boundary The architectural choice is where to anchor flow-control state. Three viable anchors, each with a different failure surface: - **Anchor credit to the session, not the connection.** The server maintains `outstanding_credit` per session ID; the resume handshake must present the client's *remaining* credit (window minus unacked), not its window. The server rejects the resume with a `409 CreditMismatch` if the presented credit exceeds what the server believes is outstanding, forcing the client to drain-then-resume. This is correct but requires the client to persist credit across connection objects — a real implementation cost. - **Anchor replay to applied-ID, not received-ID.** The client's `Last-Event-ID` must reflect the last event whose *effect* was committed, not the last event whose bytes arrived. A client that advances `Last-Event-ID` on receipt will replay events it received but did not apply; a client that advances it on commit will replay events it applied but did not acknowledge, which is the safe direction (at-least-once with idempotent handlers). This shifts the burden to handler idempotency but is the only anchor that survives arbitrary disconnects. - **Forbid backlog entirely.** The server drops events beyond outstanding credit on disconnect and signals `X-MCP-Truncated: true` on resume. The client must then re-derive state from a checkpoint rather than from replay. This is the correct choice for tools whose output is not replay-safe, and the wrong choice for tools whose output is expensive to recompute. The boundary condition for choosing: **if the tool emits non-idempotent incremental output, you cannot rely on replay alone and must either persist credit (anchor 1) or truncate-and-checkpoint (anchor 3).** Anchor 2 alone is insufficient because it still permits the server to replay into a client that has already applied those events under a stale credit assumption. ## Operational Effect The observable signature of this failure is a **progress-ratio anomaly**: any tool that reports `current/total` will show `current > total` after a resume-with-backlog, or a non-monotonic sequence. Agents that validate tool output will retry; agents that don't will silently produce corrupted results. The retry loop is the tell — three identical `code_search` invocations in a row with no intervening state change is the fingerprint of a resume that corrupted progress accounting, not a tool that failed. The guardrail that bounds it: on every resume, the client must (a) present remaining credit, not window; (b) advance `Last-Event-ID` only on commit; and (c) reject any replayed event whose ID is ≤ the last *applied* ID, treating it as a protocol violation rather than a duplicate to be swallowed. The server must (d) reject resumes where presented credit exceeds its outstanding-credit ledger, and (e) emit a truncation marker when it drops backlog so the client knows to checkpoint instead of replay. The deeper lesson: MCP's resumption contract is an **event-log contract**, and backpressure is a **flow-control contract**, and the two are specified in different documents with no shared field in the handshake. Any implementation that treats `Last-Event-ID` as the complete resumption state will eventually corrupt a non-idempotent stream. The invariant to enforce is that resumption state is a tuple `(last_applied_event_id, remaining_credit, checkpoint_ref)` and that all three must be presented and reconciled on every reconnect. ```python class ResumeCreditMismatch(RuntimeError): """Raised when a resumed MCP session presents credit inconsistent with the server ledger.""" pass class SessionLedger: """Server-side per-session flow-control ledger, anchored to session_id (not connection).""" def __init__(self, session_id: str, window: int): self.session_id = session_id self.window = window self.outstanding = 0 # events sent but not acked by client self.last_emitted_id = 0 self.backlog = [] # events enqueued while client not draining def emit(self, event_id: int, payload: dict) -> None: if self.outstanding >= self.window: self.backlog.append((event_id, payload)) return self.outstanding += 1 self.last_emitted_id = event_id # ... write to wire ... def resume_session( ledger: SessionLedger, presented_last_event_id: int, presented_remaining_credit: int, ) -> list[tuple[int, dict]]: """ Reconcile a resume handshake against the session ledger. Invariant: presented_remaining_credit must equal window - outstanding. A client that presents its full window (rather than remaining credit) is signaling it forgot the outstanding debt; replaying into it would exceed the flow-control bound. """ expected_remaining = ledger.window - ledger.outstanding if presented_remaining_credit > expected_remaining: # Client reset its window on reconnect. Replaying backlog now would # push delivered-events-past-credit above the invariant. raise ResumeCreditMismatch( f"session {ledger.session_id}: client presented credit " f"{presented_remaining_credit}, ledger expects {expected_remaining} " f"(window={ledger.window}, outstanding={ledger.outstanding}). " f"Client must drain-then-resume or present remaining credit." ) if presented_last_event_id > ledger.last_emitted_id: raise ResumeCreditMismatch( f"session {ledger.session_id}: client claims last_event_id " f"{presented_last_event_id} > server last_emitted_id " f"{ledger.last_emitted_id}; client is ahead of the log." ) # Safe to replay: client's credit accounts for outstanding debt, and its # last-applied ID is within the log. Replay only events after last_applied, # and only up to the client's remaining credit. replay = [e for e in ledger.backlog if e[0] > presented_last_event_id] if len(replay) > presented_remaining_credit: # Cannot replay the full backlog without exceeding credit; truncate # and force the client to checkpoint instead. raise ResumeCreditMismatch( f"session {ledger.session_id}: backlog {len(replay)} exceeds " f"remaining credit {presented_remaining_credit}; " f"client must resume from checkpoint, not replay." ) return replay ```