{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-lose-in-flight-tool-call-state-when-an-mcp-sse-stream-is-upgraded-to-websocket-after-last-event-id-replay",
  "title": "Why does an instance lose in-flight tool-call state when an MCP SSE stream is upgraded to WebSocket after `Last-Event-ID` replay?",
  "date_of_record": "2026 November 27",
  "scope": "Applies to MCP servers exposing HTTP-native transport that support resumable Server-Sent Events (SSE) with `Last-Event-ID` replay *and* a concurrent SSE-to-WebSocket upgrade path for the same logical session. Does not apply to pure stdio transports, stateless single-shot HTTP request/response MCP, or WebSocket-only servers with no SSE replay history.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-in-flight-tool-call-state-when-an-mcp-sse-stream-is-upgraded-to-websocket-after-last-event-id-replay.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-in-flight-tool-call-state-when-an-mcp-sse-stream-is-upgraded-to-websocket-after-last-event-id-replay.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-lose-in-flight-tool-call-state-when-an-mcp-sse-stream-is-upgraded-to-websocket-after-last-event-id-replay.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1854,
    "python_slice_tokens": 324,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"the stream dropped.\" It is that **SSE replay and WebSocket upgrade are two independent state-recovery mechanisms operating on the same event log with different cursor semantics, and the upgrade path silently re-executes already-dispatched server→client events while discarding the client→server in-flight requests that were never assigned a...",
    "what_was_observed": "A coding agent (call it `A`) was mid-task on a `tools/call` invocation of a long-running `apply_patch` tool over MCP HTTP-native transport. The sequence: 1. `A` opens SSE stream `S1`, receives `event: message, id: 41, data: {jsonrpc result for tools/call #1}`. 2. `A` sends `tools/call #2` (a second patch) as an HTTP POST. Server accepts, begins work, assigns no event ID yet (still executing). 3. Network path flaps. `...",
    "condition_of_validity": "This failure requires all of the following to co-occur; remove any one and the boundary does not trigger: - The transport supports **both** SSE replay (`Last-Event-ID`) **and** a WebSocket upgrade for the same session, and the upgrade path honors `Last-Event-ID` for replay. - The server assigns event IDs to **outbound notifications and results** but has **no...",
    "decision_boundary": "The correct architectural invariant is a **single recovery cursor that spans both directions**, not two independent ones. Concretely: - The session must expose a **bidirectional sequence**: every inbound request gets a server-assigned `request_seq` echoed in its result, and every outbound event carries `event_id`. The client's resume token is a pair `(reques...",
    "operational_effect": "Without the bidirectional cursor, an agent operating over this transport will, under network flap, **duplicate every non-idempotent tool effect that completed between its last SSE ack and the upgrade, and lose every tool call that was in flight at upgrade time.** The failure is silent — no protocol error is raised, because each individual frame is well-forme..."
  },
  "matched_failure_symptoms": [
    "DOMAIN:EPISTEMIC_PROVENANCE_AND_FEDERATION_ROUTING"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "4e27521ff135a51d7e372ee8ea36b8fadb5bc93f8da1e168470f493f26203db1",
    "verified_classes": [
      "McpUpgradeStateLoss",
      "SessionCursor"
    ],
    "verified_functions": [
      "negotiate_upgrade",
      "apply_replayed_result",
      "__init__"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "McpUpgradeStateLoss"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
