fix: Prevent duplicate or inactive tools/prompts after server refresh (#5850)
This commit is contained in:
parent
c0c0e9b7a0
commit
8fae227e8d
|
@ -881,9 +881,14 @@ describe('mcpCommand', () => {
|
||||||
}),
|
}),
|
||||||
getToolRegistry: vi.fn().mockResolvedValue(mockToolRegistry),
|
getToolRegistry: vi.fn().mockResolvedValue(mockToolRegistry),
|
||||||
getGeminiClient: vi.fn().mockReturnValue(mockGeminiClient),
|
getGeminiClient: vi.fn().mockReturnValue(mockGeminiClient),
|
||||||
|
getPromptRegistry: vi.fn().mockResolvedValue({
|
||||||
|
removePromptsByServer: vi.fn(),
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
// Mock the reloadCommands function
|
||||||
|
context.ui.reloadCommands = vi.fn();
|
||||||
|
|
||||||
const { MCPOAuthProvider } = await import('@google/gemini-cli-core');
|
const { MCPOAuthProvider } = await import('@google/gemini-cli-core');
|
||||||
|
|
||||||
|
@ -901,6 +906,7 @@ describe('mcpCommand', () => {
|
||||||
'test-server',
|
'test-server',
|
||||||
);
|
);
|
||||||
expect(mockGeminiClient.setTools).toHaveBeenCalled();
|
expect(mockGeminiClient.setTools).toHaveBeenCalled();
|
||||||
|
expect(context.ui.reloadCommands).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
expect(isMessageAction(result)).toBe(true);
|
expect(isMessageAction(result)).toBe(true);
|
||||||
if (isMessageAction(result)) {
|
if (isMessageAction(result)) {
|
||||||
|
@ -985,6 +991,8 @@ describe('mcpCommand', () => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
// Mock the reloadCommands function, which is new logic.
|
||||||
|
context.ui.reloadCommands = vi.fn();
|
||||||
|
|
||||||
const refreshCommand = mcpCommand.subCommands?.find(
|
const refreshCommand = mcpCommand.subCommands?.find(
|
||||||
(cmd) => cmd.name === 'refresh',
|
(cmd) => cmd.name === 'refresh',
|
||||||
|
@ -1002,6 +1010,7 @@ describe('mcpCommand', () => {
|
||||||
);
|
);
|
||||||
expect(mockToolRegistry.discoverMcpTools).toHaveBeenCalled();
|
expect(mockToolRegistry.discoverMcpTools).toHaveBeenCalled();
|
||||||
expect(mockGeminiClient.setTools).toHaveBeenCalled();
|
expect(mockGeminiClient.setTools).toHaveBeenCalled();
|
||||||
|
expect(context.ui.reloadCommands).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
expect(isMessageAction(result)).toBe(true);
|
expect(isMessageAction(result)).toBe(true);
|
||||||
if (isMessageAction(result)) {
|
if (isMessageAction(result)) {
|
||||||
|
|
|
@ -417,6 +417,9 @@ const authCommand: SlashCommand = {
|
||||||
await geminiClient.setTools();
|
await geminiClient.setTools();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reload the slash commands to reflect the changes.
|
||||||
|
context.ui.reloadCommands();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
type: 'message',
|
type: 'message',
|
||||||
messageType: 'info',
|
messageType: 'info',
|
||||||
|
@ -507,6 +510,9 @@ const refreshCommand: SlashCommand = {
|
||||||
await geminiClient.setTools();
|
await geminiClient.setTools();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reload the slash commands to reflect the changes.
|
||||||
|
context.ui.reloadCommands();
|
||||||
|
|
||||||
return getMcpStatus(context, false, false, false);
|
return getMcpStatus(context, false, false, false);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -61,6 +61,7 @@ export interface CommandContext {
|
||||||
toggleCorgiMode: () => void;
|
toggleCorgiMode: () => void;
|
||||||
toggleVimEnabled: () => Promise<boolean>;
|
toggleVimEnabled: () => Promise<boolean>;
|
||||||
setGeminiMdFileCount: (count: number) => void;
|
setGeminiMdFileCount: (count: number) => void;
|
||||||
|
reloadCommands: () => void;
|
||||||
};
|
};
|
||||||
// Session-specific data
|
// Session-specific data
|
||||||
session: {
|
session: {
|
||||||
|
|
|
@ -57,6 +57,11 @@ export const useSlashCommandProcessor = (
|
||||||
) => {
|
) => {
|
||||||
const session = useSessionStats();
|
const session = useSessionStats();
|
||||||
const [commands, setCommands] = useState<readonly SlashCommand[]>([]);
|
const [commands, setCommands] = useState<readonly SlashCommand[]>([]);
|
||||||
|
const [reloadTrigger, setReloadTrigger] = useState(0);
|
||||||
|
|
||||||
|
const reloadCommands = useCallback(() => {
|
||||||
|
setReloadTrigger((v) => v + 1);
|
||||||
|
}, []);
|
||||||
const [shellConfirmationRequest, setShellConfirmationRequest] =
|
const [shellConfirmationRequest, setShellConfirmationRequest] =
|
||||||
useState<null | {
|
useState<null | {
|
||||||
commands: string[];
|
commands: string[];
|
||||||
|
@ -172,6 +177,7 @@ export const useSlashCommandProcessor = (
|
||||||
toggleCorgiMode,
|
toggleCorgiMode,
|
||||||
toggleVimEnabled,
|
toggleVimEnabled,
|
||||||
setGeminiMdFileCount,
|
setGeminiMdFileCount,
|
||||||
|
reloadCommands,
|
||||||
},
|
},
|
||||||
session: {
|
session: {
|
||||||
stats: session.stats,
|
stats: session.stats,
|
||||||
|
@ -195,6 +201,7 @@ export const useSlashCommandProcessor = (
|
||||||
toggleVimEnabled,
|
toggleVimEnabled,
|
||||||
sessionShellAllowlist,
|
sessionShellAllowlist,
|
||||||
setGeminiMdFileCount,
|
setGeminiMdFileCount,
|
||||||
|
reloadCommands,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -220,7 +227,7 @@ export const useSlashCommandProcessor = (
|
||||||
return () => {
|
return () => {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
};
|
};
|
||||||
}, [config, ideMode]);
|
}, [config, ideMode, reloadTrigger]);
|
||||||
|
|
||||||
const handleSlashCommand = useCallback(
|
const handleSlashCommand = useCallback(
|
||||||
async (
|
async (
|
||||||
|
|
|
@ -158,6 +158,18 @@ export class ToolRegistry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all tools from a specific MCP server.
|
||||||
|
* @param serverName The name of the server to remove tools from.
|
||||||
|
*/
|
||||||
|
removeMcpToolsByServer(serverName: string): void {
|
||||||
|
for (const [name, tool] of this.tools.entries()) {
|
||||||
|
if (tool instanceof DiscoveredMCPTool && tool.serverName === serverName) {
|
||||||
|
this.tools.delete(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Discovers tools from project (if available and configured).
|
* Discovers tools from project (if available and configured).
|
||||||
* Can be called multiple times to update discovered tools.
|
* Can be called multiple times to update discovered tools.
|
||||||
|
|
Loading…
Reference in New Issue