From 4fc3ebb93000774d3b788f6bff4ba4e74646ba31 Mon Sep 17 00:00:00 2001 From: Ratish P <114130421+Ratish1@users.noreply.github.com> Date: Fri, 23 Jan 2026 22:51:47 +0530 Subject: [PATCH] fix(core): await MCP initialization in non-interactive mode (#17390) --- packages/core/src/config/config.test.ts | 31 +++++++++++++++++++++++-- packages/core/src/config/config.ts | 14 ++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/packages/core/src/config/config.test.ts b/packages/core/src/config/config.test.ts index 815104f231..2ee826c466 100644 --- a/packages/core/src/config/config.test.ts +++ b/packages/core/src/config/config.test.ts @@ -274,10 +274,11 @@ describe('Server Config (config.ts)', () => { ); }); - it('should not await MCP initialization', async () => { + it('should await MCP initialization in non-interactive mode', async () => { const config = new Config({ ...baseParams, checkpointing: false, + // interactive defaults to false }); const { McpClientManager } = await import( @@ -295,7 +296,33 @@ describe('Server Config (config.ts)', () => { await config.initialize(); - // Should return immediately, before MCP finishes (50ms delay) + // Should wait for MCP to finish + expect(mcpStarted).toBe(true); + }); + + it('should not await MCP initialization in interactive mode', async () => { + const config = new Config({ + ...baseParams, + checkpointing: false, + interactive: true, + }); + + const { McpClientManager } = await import( + '../tools/mcp-client-manager.js' + ); + let mcpStarted = false; + + (McpClientManager as unknown as Mock).mockImplementation(() => ({ + startConfiguredMcpServers: vi.fn().mockImplementation(async () => { + await new Promise((resolve) => setTimeout(resolve, 50)); + mcpStarted = true; + }), + getMcpInstructions: vi.fn(), + })); + + await config.initialize(); + + // Should return immediately, before MCP finishes expect(mcpStarted).toBe(false); // Wait for it to eventually finish to avoid open handles diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index 921017c8de..02d431f2d7 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -815,13 +815,21 @@ export class Config { ); // We do not await this promise so that the CLI can start up even if // MCP servers are slow to connect. - Promise.all([ + const mcpInitialization = Promise.allSettled([ this.mcpClientManager.startConfiguredMcpServers(), this.getExtensionLoader().start(this), - ]).catch((error) => { - debugLogger.error('Error initializing MCP clients:', error); + ]).then((results) => { + for (const result of results) { + if (result.status === 'rejected') { + debugLogger.error('Error initializing MCP clients:', result.reason); + } + } }); + if (!this.interactive) { + await mcpInitialization; + } + if (this.skillsSupport) { this.getSkillManager().setAdminSettings(this.adminSkillsEnabled); if (this.adminSkillsEnabled) {