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
Summary
Prevent MCP tool discovery after an agent run is cancelled during MCP client initialization.
Rationale
The current cancellation handling rejects with
signal.reasonand closes created MCP clients. If cancellation occurs whilecreateMCPClient()is pending, the flow can still execute oneclient.tools()call after client creation. This adds an unnecessary MCPlistToolsround trip after cancellation.Affected areas
packages/runtime-core/src/agent-handler.tspackages/runtime-core/src/execute-agent-block.test.tsRequired changes
context.signal?.throwIfAborted()afterPromise.all()completes MCP client creation.context.signal?.throwIfAborted()after MCP tool discovery completes and before agent execution continues.createMCPClient()is pending, resolves client creation, and verifies thatclient.tools()is not called.References
Acceptance criteria
signal.reason.client.tools()call occurs after that cancellation.