fix(core): create new McpClient on restart to apply updated config (#20126)

This commit is contained in:
Himanshu Soni
2026-02-24 23:33:33 +05:30
committed by GitHub
parent baccda969d
commit e4204d5939
2 changed files with 52 additions and 21 deletions

View File

@@ -264,6 +264,40 @@ describe('McpClientManager', () => {
'No MCP server registered with the name "non-existent"',
);
});
it('should create a new McpClient with updated config on restart', async () => {
const originalConfig = { command: 'node', args: ['--port', '8000'] };
const updatedConfig = { command: 'node', args: ['--port', '9000'] };
mockConfig.getMcpServers.mockReturnValue({
'test-server': originalConfig,
});
// Track McpClient constructor calls
const constructorCalls: unknown[][] = [];
vi.mocked(McpClient).mockImplementation((...args: unknown[]) => {
constructorCalls.push(args);
return mockedMcpClient;
});
mockedMcpClient.getServerConfig.mockReturnValue(originalConfig);
const manager = new McpClientManager('0.0.1', toolRegistry, mockConfig);
await manager.startConfiguredMcpServers();
// First call should use the original config
expect(constructorCalls).toHaveLength(1);
expect(constructorCalls[0][1]).toBe(originalConfig);
// Simulate config file change and hot-reload
mockConfig.getMcpServers.mockReturnValue({
'test-server': updatedConfig,
});
await manager.startConfiguredMcpServers();
// A NEW McpClient should have been constructed with the updated config
expect(constructorCalls).toHaveLength(2);
expect(constructorCalls[1][1]).toBe(updatedConfig);
});
});
describe('getMcpInstructions', () => {

View File

@@ -221,30 +221,27 @@ export class McpClientManager {
(async () => {
try {
if (existing) {
this.clients.delete(name);
await existing.disconnect();
}
const client =
existing ??
new McpClient(
name,
config,
this.toolRegistry,
this.cliConfig.getPromptRegistry(),
this.cliConfig.getResourceRegistry(),
this.cliConfig.getWorkspaceContext(),
this.cliConfig,
this.cliConfig.getDebugMode(),
this.clientVersion,
async () => {
debugLogger.log('Tools changed, updating Gemini context...');
await this.scheduleMcpContextRefresh();
},
);
if (!existing) {
this.clients.set(name, client);
this.eventEmitter?.emit('mcp-client-update', this.clients);
}
const client = new McpClient(
name,
config,
this.toolRegistry,
this.cliConfig.getPromptRegistry(),
this.cliConfig.getResourceRegistry(),
this.cliConfig.getWorkspaceContext(),
this.cliConfig,
this.cliConfig.getDebugMode(),
this.clientVersion,
async () => {
debugLogger.log('Tools changed, updating Gemini context...');
await this.scheduleMcpContextRefresh();
},
);
this.clients.set(name, client);
this.eventEmitter?.emit('mcp-client-update', this.clients);
try {
await client.connect();
await client.discover(this.cliConfig);