Skip to content

Avoid MCP tool discovery after agent cancellation #450

Description

@coderabbitai

Summary

Prevent MCP tool discovery after an agent run is cancelled during MCP client initialization.

Rationale

The current cancellation handling rejects with signal.reason and closes created MCP clients. If cancellation occurs while createMCPClient() is pending, the flow can still execute one client.tools() call after client creation. This adds an unnecessary MCP listTools round trip after cancellation.

Affected areas

  • packages/runtime-core/src/agent-handler.ts
  • packages/runtime-core/src/execute-agent-block.test.ts

Required changes

  • Call context.signal?.throwIfAborted() after Promise.all() completes MCP client creation.
  • Call context.signal?.throwIfAborted() after MCP tool discovery completes and before agent execution continues.
  • Add a regression test that aborts while createMCPClient() is pending, resolves client creation, and verifies that client.tools() is not called.
  • Preserve MCP client cleanup for clients created before cancellation.

References

Acceptance criteria

  • A cancellation during MCP client creation rejects the agent run with signal.reason.
  • No client.tools() call occurs after that cancellation.
  • All created MCP clients are closed.
  • Existing abort behavior remains covered by tests.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions