Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/core/src/tools/mcp-client-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
39 changes: 18 additions & 21 deletions packages/core/src/tools/mcp-client-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,30 +218,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);
Expand Down
Loading