mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 12:54:07 -07:00
fix(core): create new McpClient on restart to apply updated config (#20126)
This commit is contained in:
@@ -264,6 +264,40 @@ describe('McpClientManager', () => {
|
|||||||
'No MCP server registered with the name "non-existent"',
|
'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', () => {
|
describe('getMcpInstructions', () => {
|
||||||
|
|||||||
@@ -221,30 +221,27 @@ export class McpClientManager {
|
|||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
if (existing) {
|
if (existing) {
|
||||||
|
this.clients.delete(name);
|
||||||
await existing.disconnect();
|
await existing.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
const client =
|
const client = new McpClient(
|
||||||
existing ??
|
name,
|
||||||
new McpClient(
|
config,
|
||||||
name,
|
this.toolRegistry,
|
||||||
config,
|
this.cliConfig.getPromptRegistry(),
|
||||||
this.toolRegistry,
|
this.cliConfig.getResourceRegistry(),
|
||||||
this.cliConfig.getPromptRegistry(),
|
this.cliConfig.getWorkspaceContext(),
|
||||||
this.cliConfig.getResourceRegistry(),
|
this.cliConfig,
|
||||||
this.cliConfig.getWorkspaceContext(),
|
this.cliConfig.getDebugMode(),
|
||||||
this.cliConfig,
|
this.clientVersion,
|
||||||
this.cliConfig.getDebugMode(),
|
async () => {
|
||||||
this.clientVersion,
|
debugLogger.log('Tools changed, updating Gemini context...');
|
||||||
async () => {
|
await this.scheduleMcpContextRefresh();
|
||||||
debugLogger.log('Tools changed, updating Gemini context...');
|
},
|
||||||
await this.scheduleMcpContextRefresh();
|
);
|
||||||
},
|
this.clients.set(name, client);
|
||||||
);
|
this.eventEmitter?.emit('mcp-client-update', this.clients);
|
||||||
if (!existing) {
|
|
||||||
this.clients.set(name, client);
|
|
||||||
this.eventEmitter?.emit('mcp-client-update', this.clients);
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
await client.connect();
|
await client.connect();
|
||||||
await client.discover(this.cliConfig);
|
await client.discover(this.cliConfig);
|
||||||
|
|||||||
Reference in New Issue
Block a user