#2661·mcp-use

agent: runToolLoopNonStreaming ignores AbortSignal between multi-tool calls in a turn

Author: rohith500Created Sep 24, 2026Updated Sep 26, 2026
LabelsbugreadyTypeScriptagentrepoclientmcp-use/mcp-use

Describe the bug

In @mcp-use/agent, when an LLM returns multiple tool calls in a single turn, runToolLoopNonStreaming continues to dispatch subsequent tool calls even if the signal was aborted during (or after) the execution of a prior tool call.

While the streaming counterpart runToolLoop correctly checks if (signal?.aborted) return; before each tool invocation, runToolLoopNonStreaming (and OpenAIResponsesDriver.runToolLoopNonStreaming) only checks signal?.aborted at the outer turn loop (for (let step = 0; step < maxSteps; step++)), omitting the check inside the inner tool loop (for (const tc of toolCalls)).

To Reproduce

A minimal runnable vitest test:

typescript
import { vi, expect, it } from "vitest";
import { runToolLoopNonStreaming } from "@mcp-use/agent";
import type { LlmDriver } from "@mcp-use/agent";

it("stops dispatching remaining tool calls in a turn when signal is aborted", async () => {
  const controller = new AbortController();
  const callTool = vi.fn(async (name: string) => {
    if (name === "tool1") {
      controller.abort();
    }
    return { ok: true };
  });

  const driver: LlmDriver = {
    stream: vi.fn(),
    complete: vi.fn(async () => ({
      text: "",
      toolCalls: [
        { id: "1", name: "tool1", args: {} },
        { id: "2", name: "tool2", args: {} },
      ],
    })),
  };

  await runToolLoopNonStreaming({
    driver,
    messages: [{ role: "user", content: "run tools" }],
    tools: [],
    callTool,
    signal: controller.signal,
  });

  // Expected: tool2 should NOT be called after controller.abort()
  // Actual: callTool is called 2 times (both tool1 and tool2 execute)
  expect(callTool).toHaveBeenCalledTimes(1);
  expect(callTool).toHaveBeenCalledWith("tool1", {});
});

Observed behavior

When controller.abort() is called during or immediately after tool1, tool2 is executed anyway:

AssertionError: expected "vi.fn()" to be called 1 times, but got 2 times
  ❯ callTool was called with:
     [1]: ["tool1", {}]
     [2]: ["tool2", {}]

Expected behavior

As soon as signal.aborted becomes true, runToolLoopNonStreaming should break out of the tool calls loop and cease dispatching subsequent tools in that turn, matching the cancellation behavior of runToolLoop.

Desktop

  • OS: macOS
  • Version: @mcp-use/agent (repo main), Node v22.14.0

Additional context

Root cause: In libraries/typescript/packages/agent/src/llm/toolLoop.ts:

  • Line 101 (runToolLoop): checks if (signal?.aborted) return; before calling each tool.
  • Line 176 (runToolLoopNonStreaming): iterates for (const tc of toolCalls) without checking signal?.aborted between tool calls.

The exact same pattern is present in libraries/typescript/packages/agent/src/llm/providers/openai-responses-driver.ts:

  • Line 80 (streamToolLoop): checks if (params.signal?.aborted) return;.
  • Line 150 (runToolLoopNonStreaming): iterates for (const tc of turn.toolCalls) without checking params.signal?.aborted.

Impact: Breaks cancellation parity between streaming and non-streaming execution modes. When a caller aborts an execution mid-turn (e.g., user cancellation, client disconnect, or request timeout), runToolLoopNonStreaming continues to invoke all remaining queued tools in that turn, triggering redundant RPC/network calls and failing to honor caller-directed cancellation until the entire batch finishes.

Resolution: Submitted PR #2667 with the surgical fix across generic and provider tool loops, accompanying Vitest unit tests in toolLoop.test.ts (streaming abort, non-streaming multi-tool abort, and pre-aborted signal), and the @mcp-use/agent changeset.