fix(core): handle unhandled promise rejection in mcp-client-manager
- Add outer try-catch block to catch initialization errors
- Connect async IIFE rejection to Promise reject handler
- Add tests for error scenarios during client initialization
- Prevents unhandled promise rejection when McpClient constructor throws
- Ensures one server failure doesn't stop other servers from starting
Fixes #14700
Summary
Fixes a structural issue in McpClientManager.addOrUpdateServer() that causes unhandled promise rejections during MCP server initialization. The Promise constructor received an unused _reject parameter, and the async IIFE lacked proper error handling, causing crashes when errors occurred in the McpClient constructor or disconnect method.
Details
Root cause: The current implementation creates a Promise with _reject parameter but never uses it. The async IIFE has no .catch() handler, so errors thrown before the try block become unhandled rejections.
Changes made:
- Changed
_rejecttorejectand properly connected it - Added outer try-catch to catch initialization errors (constructor, disconnect)
- Added
.catch(reject)to async IIFE to handle any uncaught errors - Added proper error logging via
coreEvents.emitFeedback
Files modified:
-
packages/core/src/tools/mcp-client-manager.ts(lines 162-220) -
packages/core/src/tools/mcp-client-manager.test.ts(added 2 test cases)
Note: This is NOT a duplicate of #8365. That issue was about MCP timeout errors in gemini.mjs and is already closed. This PR fixes a different structural problem in mcp-client-manager.ts that could cause unhandled rejections during initialization.
Related Issues
Fixes #14700
How to Validate
- Run unit tests:
npm run test mcp-client-manager.test.ts
## Pre-Merge Checklist
<!-- Check all that apply before requesting review or merging. -->
- [ ] Updated relevant documentation and README (if needed)
- [O] Added/updated tests (if needed)
- [ ] Noted breaking changes (if any)
- [ ] Validated on required platforms/methods:
- [ ] MacOS
- [ ] npm run
- [ ] npx
- [ ] Docker
- [ ] Podman
- [ ] Seatbelt
- [ ] Windows
- [ ] npm run
- [ ] npx
- [ ] Docker
- [ ] Linux
- [O] npm run
- [ ] npx
- [ ] Docker