[JS] Aborting generate() doesn't cancel an MCP tool call, and generate() waits for the tool to finish

Open Beginner friendly
#7,481 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Start with the MCP tool actions in js/plugins/mcp/src/util/tools.ts and check how their second argument provides abortSignal. Compare the client.callTool options with the MCP TypeScript SDK behavior described in the issue, then review the unit test in tests/tools_test.ts. Done when aborting generate() promptly rejects and the MCP server receives cancellation.

Written by the indexing model from the issue text.

Description

Describe the bug
When ai.generate() is aborted with abortSignal while it runs a tool from @genkit-ai/mcp, the MCP request is never cancelled. The server runs the tool to the end, and generate() only rejects after the tool returns.

The tool actions in js/plugins/mcp/src/util/tools.ts receive the abort signal (abortSignal in the action's second argument) but call client.callTool({ name, arguments, _meta }) without it, so the MCP client never sends notifications/cancelled and keeps waiting for the result.

To Reproduce
A scripted model asks for one MCP tool call (slow_task, 5 s) and the call is aborted after 1 s. Full repro (server and client, no API key): https://github.com/Koobi33/seams-lab/tree/main/repro/genkit-mcp-abort

const controller = new AbortController();
setTimeout(() => controller.abort(), 1000);
await ai.generate({ model, prompt: 'go', tools, abortSignal: controller.signal });

Output:

[server] slow_task ran all 5s
generate rejected after 5166 ms: This operation was aborted

Expected behavior
generate() rejects right after the abort, and the server is told to stop (notifications/cancelled), as the MCP TypeScript SDK does when callTool gets a signal.

Runtime (please complete the following information):

  • OS: macOS
  • Version: genkit and @genkit-ai/mcp 1.44.0, also main at 97115d9

** Node version

  • v26.13.1

Additional context
Reproduces 2 out of 2 runs on 1.44.0 and on main. Passing the signal through fixes it: async (args, { context, abortSignal }) and client.callTool(params, undefined, { signal: abortSignal }) in the four tool definitions. With that change the same repro gives:

generate rejected after 1007 ms: MCP error -32001: AbortError: This operation was aborted
[server] slow_task cancelled after 1s

I'd like to work on this. I have the change with a unit test in tests/tools_test.ts (27 passed) and can open a PR once you're OK with the approach.

Dominant language
TypeScript
Stars
6.6k
Forks
896
Avg merge
4d 8h
Merged PRs (30d)
138

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from genkit-ai/genkit

All issues in genkit-ai/genkit

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.