Skip to content

Close partially initialized MCP clients when startup fails #449

Description

@coderabbitai

Summary

Prevent MCP client leaks when one createMCPClient call fails after another call succeeds.

Required changes

Update executeAgentBlock to retain every successfully created MCP client when MCP client initialization has a partial failure. Re-throw the startup failure after retaining the fulfilled clients so the existing finally block closes them.

Add a regression test with one successful client creation and one rejected client creation. Verify that the successful client is closed.

Rationale

Promise.all rejects before assignment to mcpClients when any client creation fails. Clients that completed successfully before the rejection are then not available to the cleanup logic.

Affected areas

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

References

Acceptance criteria

  • Successful MCP clients from a partially failed initialization are retained for cleanup.
  • The original MCP client initialization failure is propagated.
  • The cleanup logic closes each retained client.
  • A test covers one successful client creation and one rejected client creation.

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