Skip to content

Codex never tells MCP servers to stop after tool call interrupt/timeout #26956

Open

Description

@a9lim

What version of Codex CLI is running?

codex-cli 0.137.0

What subscription do you have?

Pro 20x

Which model were you using?

gpt-5.5

What platform is your computer?

Darwin 25.6.0 arm64 arm

What terminal emulator and version are you using (if applicable)?

Terminal.app

Codex doctor report

What issue are you seeing?

When using Codex with tools from MCP servers, interrupting or timing out a tool call doesn't notify the server. Codex will drop the request future locally but sends nothing back, so MCP servers will try to execute the tool to completion anyways.

What steps can reproduce the bug?

  1. Register a stdio MCP server that runs a tool call and logs whether it receives notifications/cancelled.
  2. Ask Codex to call that tool then interrupt it or let it time out.
  3. The server never gets notifications/cancelled and tries to keep running the tool even though its output will never be seen.

What is the expected behavior?

Codex should send notifications/cancelled when tool calls are cancelled so the server can stop executing it.

Additional information

I had Claude fix the bug at a9lim/codex@fix/mcp-client-cancellation

Activity

  1. added
    bug
    on Jun 8, 2026
  2. added
    CLI
    mcp
    tool-calls
    on Jun 8, 2026
  3. wgu9 commented on Jun 15, 2026

    @wgu9

    I checked current main and this looks like a real lifecycle gap in the MCP tool-call path, not just a UI interruption issue.

    The cancellation token exists at the generic tool runtime layer, but it does not make it down to the MCP request:

    • codex-rs/core/src/tools/parallel.rs passes a CancellationToken into each tool dispatch and aborts/drops the task when the turn is cancelled, unless the handler opts into waiting for runtime cleanup.
    • codex-rs/core/src/mcp_tool_call.rs::execute_mcp_tool_call() then awaits sess.call_tool(...) directly.
    • codex-rs/core/src/session/mcp.rs::call_tool() forwards to mcp_connection_manager.call_tool(...) without any cancellation token or request-id handle.
    • I only see CancelledNotificationParam handled as an inbound server-to-client notification in codex-rs/rmcp-client/src/logging_client_handler.rs; I do not see an outbound client-to-server cancellation path tied to a running tool request.

    So the likely failure mode is: Codex cancels/aborts the local future, but the MCP server still owns a live JSON-RPC request and never receives MCP notifications/cancelled for that request id. That matches the reproduction where the server keeps executing after the result has become unobservable to Codex.

    Patch shape I would expect:

    1. Keep a cancellation-aware handle around each outbound MCP tool request, including the actual MCP request id used by rmcp.
    2. When the Codex tool/turn CancellationToken fires before the MCP response resolves, send MCP notifications/cancelled to the server with that request id and a reason such as user interrupt/timeout.
    3. Prefer waiting for the cancellation send/cleanup path rather than simply aborting the local task, similar to how shell tools now distinguish runtime cleanup from immediate task abort.
    4. Add a stdio MCP integration test with a tool that blocks until it receives notifications/cancelled, then assert Codex sends that notification on interrupt/timeout.

    This is especially important for tools with external side effects: dropping the local future is not equivalent to cancelling the server-side operation.

  4. alexchexes commented on Jun 22, 2026

    @alexchexes

    This also occurs through Code mode: the model can terminate a pending nested MCP call, but the MCP server receives no notifications/cancelled. The server therefore continues waiting and cannot distinguish the abandoned request from a legitimate long-running call. #29122 contains the Code-mode reproducer.

  5. added a commit that references this issue on Oct 7, 2026
  6. euri10 commented on Oct 7, 2026

    @euri10

    Confirmed on native Codex 0.159.1 (rust-v0.159.1, Linux), independently of ACP/editor integration, using a local canned Responses endpoint and a cancellation-aware stdio MCP fixture. No real Provider request is needed.

    Reproduction: start one MCP call that waits 30 seconds or until its request cancellation token fires; interrupt the turn after the tool starts; then complete a new turn in the same thread. Stock Codex ends the first turn as interrupted and permits recovery, but sends zero notifications/cancelled; the fixture continues to its deadline. Sending the notification directly to the same fixture cancels it immediately.

    There are two layers for Code Mode:

    • With the default features.code_mode_interrupt = false, a nested MCP call remains active and reports successful completion after the interrupted turn and the recovery turn.
    • Enabling features.code_mode_interrupt = true aborts the cell/local wait, but stock Codex still sends no MCP cancellation. That option also interrupts other active Code Mode cells; it is not an MCP-only switch.

    Local fix: euri10@e3f3173

    Like the request-scoped drop-guard approach already linked here, this preserves the exact peer and request ID and sends bounded, best-effort cancellation when the response await is dropped. It covers both legacy calls and the newer continuation path, disarms on a resolved response/protocol error, and does not close the connection or cancel concurrent calls. The branch's separate lockfile commit is only release-build bookkeeping, not part of the proposed behavioral fix.

    Evidence:

    • New abort, timeout, and continuation regressions fail without the fix; normal completion/error control passes.
    • With the fix, all four regressions pass; the complete MCP-client suite passes (340 passed, 9 skipped), as do Clippy and rustfmt.
    • Actual patched native binary: both direct MCP and Code Mode with interruption enabled send exactly one cancellation for the pending request, stop the fixture, allow same-thread recovery, and emit no late success during a 31-second observation window.
    • The same check passes through the locally patched codex-acp 2.1.1 integration as well.

    Cancellation remains best effort; it cannot undo already-completed external effects. Sharing this here rather than opening a duplicate issue or a competing PR.

  7. maoxx241 commented on Oct 16, 2026

    @maoxx241

    Adding a direct subagent interruption reproduction on the official Codex 0.163.0 macOS ARM release, consistent with the request-lifetime analysis and cancellation guard already described in the October 7 comment and its existing patch.

    The parent had already completed normally. This tests interruption of the child's own active turn, not cancellation cascading from an active parent.

    Reproduction:

    1. Run an isolated app-server with a localhost synthetic Responses endpoint and a stdio MCP server exposing one tool that stays pending until it receives cancellation.
    2. Have the parent call native spawn_agent once, and bind the child to the completed spawn event.
    3. Wait for the parent to complete and the child to issue exactly one MCP tool call. Confirm that exact request has not received a result, cancellation, or EOF.
    4. Send one turn/interrupt for the child's exact thread and turn.
    5. Keep both the app-server and MCP transport open while observing cancellation and request release.

    On official 0.163.0, the interrupt RPC is acknowledged and the child reaches interrupted. A subsequent child thread/read succeeds. During the following 8-second observation, the pending request receives:

    exact notifications/cancelled: 0
    request releases:             0
    MCP stdin EOF:                0
    same MCP process identity:   alive
    app-server:                 open and responsive
    

    Fixture shutdown happens after these assertions and is not counted as cancellation. Cleanup completed without error. The run made three synthetic localhost Responses requests and zero external model requests. This is a bounded observation, not a claim of an indefinite hang.

    The official binary reports codex-cli 0.163.0; its SHA-256 is 8813367285524ac798dd96d1b071aea30d58708c7226fd236cd275cf1cfdee31. The downloaded release archive matched its published digest. Separate earlier observations reproduced missing notification/EOF on 0.153.4/macOS and 0.162.0-alpha.17.2/Windows. The Windows observer did not establish event-time process liveness, so the stronger liveness statement above is specific to the 0.163.0 run.

    In the exact 0.163.0 source, the legacy path still awaits the submitted request handle. The continuation path has the corresponding boundary. The pinned rmcp 3.5.1 request handle provides explicit cancellation; dropping the ordinary response await does not itself send it. A plugin Interrupt Hook cannot substitute for this path because child dispatch returns before running that Hook.

    As corroboration, an isolated local build of the earlier alpha with the same kind of request-scoped guard passed seven cancellation tests plus the existing tool-input test. Its native child fixture delivered the exact cancellation without closing the MCP connection. A separate test with an installed MindIE MCP and a held localhost SSH connection reclaimed all six identified operation processes in about 260 ms, while the same MCP process, listener, and native host remained open. That test explicitly initialized the child's selected MCP server before establishing its baseline; it is not a natural cold-start performance result. No Interrupt Hook ran.

    Our local investigation converged on the same request-scoped design already linked here. That existing work is the appropriate implementation baseline; our contribution is additional release and child-specific regression evidence. Our tested build is not byte-identical to that patch, so its results do not validate the linked patch unchanged. Neither is an official fix or a proposed replacement distribution. We are not opening a duplicate issue or claiming a new guard design.

    The desired behavior is cancellation of the exact submitted request when its native await is abandoned, preserving terminal-response races, unrelated requests, and single tool dispatch. MCP defines cancellation as an optional utility; this report concerns native operation lifetime, not a mandatory-protocol compliance claim. A fix in an official release still needs validation before our integration can claim child cancellation support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLIbugmcptool-calls

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions