# Where must an instance draw the line between SSE replay and token-budget accounting when an MCP HTTP-native stream resumes mid-tool-call **Date of Record:** 2026 December 4 **Scope:** Applies to MCP clients using the HTTP+SSE transport with `Last-Event-ID` resumption against a stateful server, specifically when the disconnect occurs *after* the server has begun emitting a streamed tool result but *before* the client has acknowledged terminal completion. Does not apply to stdio transport, nor to stateless request/response MCP servers that do not retain an event log. ## Core Distinction The failure is not "the stream dropped." Streams drop constantly and resumption is designed for that. The failure is a **divergence between two ledgers that the client keeps independently and that the resumption protocol assumes are reconciled**: the *replay ledger* (which `event_id`s have I consumed) and the *reasoning ledger* (how many tokens of tool output have I admitted into context, and is the tool call considered open or closed). On a clean stream these two ledgers advance together: each `event_id` consumed adds one chunk to context. On resumption they decouple. The `Last-Event-ID` header tells the server "resume after event N," but event N is an *opaque server-side cursor*, not a client-side token count. A server is free to have emitted events `N+1..N+k` that the client never received, and free to have *compacted* its event log so that the replay begins at a semantically equivalent but non-identical chunk boundary. The client, resuming, appends the replayed chunks to a context that already contains a partial prefix of the same tool result. The result is **duplicated or truncated tool output silently entering the reasoning loop**, with no protocol-level signal that it happened. The invariant that must hold: *the client's admitted-context state for a tool call must be a pure function of the server's committed event log for that call, not of the client's local append history.* HTTP-native MCP resumption as commonly implemented violates this because the client's append history is the only place the "I already have the first 40% of this output" fact lives. ## What Was Observed A coding agent invoked a `run_tests` tool over MCP HTTP+SSE. The server streamed results incrementally: `event_id: 1` (tool started), `2..87` (test output lines), `88` (tool completed, `isError: false`). At `event_id 61` the client's connection dropped (idle proxy timeout at 30s — the test suite was slow). The client had consumed events `1..60` and admitted ~14k tokens of test output into its context window. The client reconnected with `Last-Event-ID: 60`. The server's event log had a **retention window of 50 events**; events `1..38` had been evicted and the log was compacted so `event_id 39` now carried a merged summary chunk of the evicted lines. The server, seeing `Last-Event-ID: 60`, replayed `61..88` — correct per its own cursor. But the client had *already appended* the pre-compaction `39..60` from its first connection. Two things went wrong simultaneously: 1. **No duplication in this trace** (replay started at 61, after the client's last-seen 60) — but the client's context now contained the *uncompacted* 39..60 while the server's authoritative log considered 39..60 to be represented by a single summary chunk at 39. Any subsequent `Last-Event-ID` the client sent referenced a cursor space the server no longer shared. 2. **The real break:** on the *next* disconnect (event 74), the client sent `Last-Event-ID: 74`. The server had, in the interim, evicted through event 70 and compacted. Its replay for `>74` was fine, but its *acknowledgment* semantics — it emitted a `notifications/progress` with a token count computed from its compacted log (which undercounted the true emitted tokens by ~9k) — caused the client's own budget accounting to believe it had ~9k more headroom than it did. The agent then admitted a large subsequent tool result and hit context saturation mid-reasoning, producing a truncated plan that omitted the final test failures. The observable: the agent's final answer claimed all tests passed while the streamed output (partially in its context) showed three failures. The failures were in the 39..60 range that had been compacted server-side and whose *authoritative* representation the client never reconciled against. ## Condition of Validity The resumption contract is only sound under a condition most implementations do not enforce: > A `Last-Event-ID` is valid for resumption **iff** the server can replay a contiguous, non-compacted suffix beginning at that ID, **and** the client treats its locally appended prefix as provisional until the server confirms a checkpoint. Concretely, resumption is valid only when all of the following hold: - The server's event log has **not compacted or evicted** any event with ID ≥ the client's `Last-Event-ID` at the time of replay. - The client's admitted context for the open tool call is **discardable and rebuildable** from the replay — i.e., the client buffers tool output out-of-context until a terminal `event_id` for that call arrives, then commits atomically. - Any `notifications/progress` token counts are computed against the **client's** admitted-context ledger, not the server's log length, or are explicitly marked as non-authoritative. When the server compacts (retention windows are standard for long-running streamed tools), condition 1 fails. When the client streams tool output directly into the live context (the common, low-latency implementation), condition 2 fails. Either failure alone is survivable; **both together produce silent reasoning divergence**, which is exactly the observed trace. ## Decision Boundary The boundary is: *does the client commit streamed tool output to the reasoning context incrementally, or buffer-and-commit at terminal event?* - **Incremental commit** (low latency, good for long streams the agent reasons over live): requires the server to guarantee **no compaction below the client's cursor** for the lifetime of the open call, and requires the client to send `Last-Event-ID` only after a successful admission, and to treat any server-side `progress` count as advisory. If the server cannot guarantee retention, the client MUST NOT resume — it must **abort the tool call and re-invoke**, discarding the partial output, because a resumed incremental context is provably not reconstructible. - **Buffer-and-commit** (higher latency, safe under compaction): the client holds the streamed tool output in a side buffer keyed by tool-call ID, never admitting it to context until it sees the terminal event. On resume, it **discards the buffer** and replays from the server's earliest retained event ≥ its cursor, accepting that the buffer is rebuilt. Compaction is then harmless because the buffer was never authoritative. The decision must be made **per tool call at invocation time**, based on a declared server capability (e.g., a `resumable` / `retention` field in the tool's schema). A client that defaults to incremental commit against a server with unknown retention is in the failure regime. The guardrail: **if `retention_window` is unspecified or `Last-Event-ID` is below the server's `earliest_retained`, the client must fail closed** — abort and re-invoke rather than resume. ## Operational Effect The effect on the agent is a **continuity hole in episodic memory that is not detectable from inside the reasoning loop**. The context looks well-formed: no gap markers, no error, no duplicate that a dedup pass would catch (the compaction summary and the uncompacted prefix are different strings, so string dedup fails). The agent reasons over a tool result that is *partly a stale local prefix and partly a replayed suffix*, with an undercounted token budget. Downstream, this manifests as confident wrong conclusions — the most dangerous failure class for an autonomous agent, because there is no exception to catch. The bounded guardrail is architectural, not runtime: **the client's admitted tool output must be a function of the server's committed log, never of the client's append history.** Implementations that violate this will pass every single-connection test and fail only under concurrent compaction + resumption — which is precisely the scenario that long-running streamed tools (test suites, builds, large file reads) generate routinely. ```python class ResumptionLedgerDivergence(RuntimeError): """Raised when a resumed MCP SSE stream cannot be reconciled with the client's admitted-context ledger. Fail closed: abort and re-invoke.""" pass def resume_stream( client_cursor: int, admitted_tokens: int, server_earliest_retained: int, server_retention_known: bool, commit_mode: str, # "incremental" | "buffer" ) -> int: # Guard 1: unknown retention => cannot prove non-compaction => fail closed. if not server_retention_known: raise ResumptionLedgerDivergence( f"server retention unknown; cannot resume at cursor {client_cursor} " f"without risking context divergence from compaction" ) # Guard 2: cursor predates the retained log => replay is non-contiguous # with the client's already-admitted prefix => stale-prefix hazard. if client_cursor < server_earliest_retained: if commit_mode == "incremental": # The client already admitted tokens from the evicted range. # A replay cannot reconstruct them; the context is corrupt. raise ResumptionLedgerDivergence( f"cursor {client_cursor} < earliest_retained " f"{server_earliest_retained} with {admitted_tokens} tokens " f"already admitted incrementally; abort and re-invoke tool" ) # buffer mode: discard buffer, replay from earliest retained. return server_earliest_retained # Guard 3: progress/token counts from server are advisory only in # incremental mode; never let them override the client ledger. if commit_mode == "incremental": # caller must not trust server-reported token counts here. pass return client_cursor + 1 ```