fix(core): remove stale MCP client from map on initialization failure

This commit is contained in:
Abhi
2026-03-15 19:32:32 -04:00
parent 17b37144a9
commit e07df4c953
2 changed files with 15 additions and 0 deletions
@@ -751,6 +751,19 @@ describe('McpClientManager', () => {
);
});
it('should remove the client from the clients map if initialization fails', async () => {
const manager = new McpClientManager('0.0.1', toolRegistry, mockConfig);
const name = 'test-server';
const config = { command: 'node', args: ['fail.js'] };
mockedMcpClient.connect.mockRejectedValue(new Error('Connection failed'));
// maybeDiscoverMcpServer returns a promise that resolves when discovery is finished.
await manager.maybeDiscoverMcpServer(name, config);
expect(manager.getClient(name)).toBeUndefined();
});
it('should show previously deduplicated errors after interaction clears state', () => {
const manager = new McpClientManager('0.0.1', toolRegistry, mockConfig);
@@ -404,6 +404,7 @@ export class McpClientManager {
await client.discover(this.cliConfig);
this.eventEmitter?.emit('mcp-client-update', this.clients);
} catch (error) {
this.clients.delete(name);
this.eventEmitter?.emit('mcp-client-update', this.clients);
// Check if this is a 401/auth error - if so, don't show as red error
// (the info message was already shown in mcp-client.ts)
@@ -418,6 +419,7 @@ export class McpClientManager {
}
}
} catch (error) {
this.clients.delete(name);
const errorMessage = getErrorMessage(error);
this.emitDiagnostic(
'error',