# Which invariants prevent SSE event-ID reuse from silently replaying a poisoned tool-call result across an MCP streamable-session resume **Date of Record:** 2026 November 24 **Scope:** Applies to MCP servers using the HTTP-native streamable transport with `Last-Event-ID`-based session resumption, where a client reconnects mid-tool-call and requests replay of buffered SSE events. Does not apply to stateless request/response MCP transports or to stdio transports with no resumption buffer. ## Core Distinction The dangerous failure is not "the resume fails." The dangerous failure is "the resume succeeds and delivers a *stale-but-well-formed* tool result that the agent treats as fresh." Streamable-session resumption is governed by an event-ID cursor contract: the client presents the last event ID it durably consumed, and the server replays everything after that cursor. The invariant that matters is **event-ID monotonicity coupled to result-immutability per event**. If a server reuses or rewinds the event-ID space — because it restarted, because it compacted its buffer, or because two worker replicas share a session ID but not an event counter — the client's cursor points into a *different* logical history. Replay then hands the agent an event whose ID matches expectation but whose payload belongs to a different attempt. The agent cannot distinguish "replayed the result I was waiting for" from "replayed a result from a prior, superseded attempt," because both arrive as structurally valid `tools/call` results. ## What Was Observed A tool call (`deploy_service`, long-running, ~90s) was dispatched over a streamable session. The client's SSE reader consumed events up to ID `evt-4471` (a `progress` notification) and then the TCP connection dropped before the `result` event. The client reconnected with `Last-Event-ID: evt-4471`. In the interim, the server process had been rescheduled onto a fresh replica. The replica accepted the session ID (session state was replicated), but its local event counter reset to `evt-0001`. It had no buffer for `evt-4471`, so per its (mis-implemented) fallback it replayed *from the beginning of the rehydrated buffer*, which had been repopulated by a **retry** of the same tool call — the orchestrator had already re-dispatched `deploy_service` because the original connection died. The replayed stream contained `evt-0001 … evt-0040`, culminating in a `result` for the retried invocation. The client, seeing a `result` event in the resumed stream, emitted it to the agent as the completion of the original call. But the retried invocation had hit a *different* validation branch (a transient feature-flag state), and returned `{"status":"skipped","reason":"flag_off"}`. The agent recorded the deployment as skipped and moved on. The *original* invocation, still running on the old replica, later completed successfully — but its result was orphaned. The agent operated for 40 minutes on the belief that a service it had deployed was not deployed. The failure was silent: no error, no schema violation, no timeout. The resumed stream was internally consistent; it was just *the wrong stream*. ## Condition of Validity The replay-poisoning failure requires the conjunction of four conditions, all of which held here: 1. **Cursor-addressable resumption** is offered (`Last-Event-ID` honored), so the client believes replay is authoritative. 2. **Event-ID space is not globally monotonic per session** — it reset across replica handoff, so cursor `evt-4471` was unresolvable and the server fell back to buffer-start rather than erroring. 3. **The tool call is retried concurrently** with the original still in flight, so the rehydrated buffer contains a *second* attempt's events under a *colliding* ID range. 4. **Results are not idempotency-keyed** — the `result` event carries no invocation identity that the client can match against its outstanding request, so attempt-2's result is accepted as attempt-1's. Remove any one and the failure degrades to a detectable error. Remove (2) and the server returns "cursor not found," which the client handles as a hard resume failure. Remove (4) and the client rejects the mismatched result and re-queries. ## Decision Boundary The boundary is between **resume-as-replay** and **resume-as-reconcile**: - If the server can guarantee event-ID monotonicity across the session's entire lifetime (single-writer event log, cursor persisted with the log), then `Last-Event-ID` replay is sound and the client may treat replayed events as the authoritative continuation. - If the server *cannot* guarantee that — replica handoff, buffer compaction, concurrent retries — then replay is *unsound* and the server MUST reject unresolvable cursors with an explicit `resume_gap` error rather than silently replaying from buffer-start. The client must then fall back to a reconcile query (`tools/call` status by invocation ID), not to blind replay. The architectural invariant required to bound this: **every `result` event must carry the invocation ID of the request that produced it, and the client must discard any replayed `result` whose invocation ID does not match its outstanding request.** This makes replay poisoning *detectable* even when the event-ID space is corrupted, because the payload itself is self-identifying. Event-ID monotonicity is the primary guard; invocation-ID matching is the defense-in-depth that catches the case where monotonicity is violated by infrastructure the server does not control. ## Operational Effect Agents resuming streamable sessions must treat a replayed `result` as **untrusted until invocation-matched**. The concrete operational rule: on resume, the client enters a `RECONCILING` state, buffers all replayed events, and only commits a `result` to the agent's working memory if `event.invocation_id == request.invocation_id` *and* the event ID is strictly greater than the last durably-consumed ID. If either check fails, the client issues an out-of-band status query and blocks the tool call's continuation until reconciled. This converts a silent 40-minute divergence into a bounded reconciliation pause. The cost is one extra round-trip on every resume; the benefit is that a replica handoff can never cause an agent to act on another attempt's outcome. ```python class ResumeReplayPoisoningError(RuntimeError): """Raised when a replayed SSE event cannot be safely attributed to the outstanding tool call during an MCP streamable-session resume.""" pass def commit_replayed_result( event: dict, outstanding_invocation_id: str, last_consumed_event_id: int, ) -> dict: evt_id = event.get("event_id") evt_invocation = event.get("invocation_id") evt_type = event.get("type") if evt_type != "result": raise ResumeReplayPoisoningError( f"non-result event {evt_type!r} routed to result commit path" ) # Invariant 1: event-ID must strictly advance past the durable cursor. if evt_id is None or evt_id <= last_consumed_event_id: raise ResumeReplayPoisoningError( f"event_id {evt_id!r} does not advance past cursor " f"{last_consumed_event_id}; replay space likely rewound" ) # Invariant 2: the result must belong to the request we are waiting on. if evt_invocation != outstanding_invocation_id: raise ResumeReplayPoisoningError( f"replayed result invocation_id {evt_invocation!r} != " f"outstanding {outstanding_invocation_id!r}; " f"possible concurrent-retry collision" ) return event ```