feat(cli): Adds the ability to run MCP prompt commands in non-interactive mode (#10194)

Co-authored-by: Allen Hutchison <adh@google.com>
This commit is contained in:
James
2025-10-21 23:40:13 +00:00
committed by GitHub
parent 5b750f519b
commit ed9f714fbb
2 changed files with 45 additions and 2 deletions
@@ -59,6 +59,9 @@ vi.mock('./services/CommandService.js', () => ({
}, },
})); }));
vi.mock('./services/FileCommandLoader.js');
vi.mock('./services/McpPromptLoader.js');
describe('runNonInteractive', () => { describe('runNonInteractive', () => {
let mockConfig: Config; let mockConfig: Config;
let mockSettings: LoadedSettings; let mockSettings: LoadedSettings;
@@ -938,6 +941,46 @@ describe('runNonInteractive', () => {
expect(processStdoutSpy).toHaveBeenCalledWith('Acknowledged'); expect(processStdoutSpy).toHaveBeenCalledWith('Acknowledged');
}); });
it('should instantiate CommandService with correct loaders for slash commands', async () => {
// This test indirectly checks that handleSlashCommand is using the right loaders.
const { FileCommandLoader } = await import(
'./services/FileCommandLoader.js'
);
const { McpPromptLoader } = await import('./services/McpPromptLoader.js');
mockGetCommands.mockReturnValue([]); // No commands found, so it will fall through
const events: ServerGeminiStreamEvent[] = [
{ type: GeminiEventType.Content, value: 'Acknowledged' },
{
type: GeminiEventType.Finished,
value: { reason: undefined, usageMetadata: { totalTokenCount: 1 } },
},
];
mockGeminiClient.sendMessageStream.mockReturnValue(
createStreamFromEvents(events),
);
await runNonInteractive(
mockConfig,
mockSettings,
'/mycommand',
'prompt-id-loaders',
);
// Check that loaders were instantiated with the config
expect(FileCommandLoader).toHaveBeenCalledTimes(1);
expect(FileCommandLoader).toHaveBeenCalledWith(mockConfig);
expect(McpPromptLoader).toHaveBeenCalledTimes(1);
expect(McpPromptLoader).toHaveBeenCalledWith(mockConfig);
// Check that instances were passed to CommandService.create
expect(mockCommandServiceCreate).toHaveBeenCalledTimes(1);
const loadersArg = mockCommandServiceCreate.mock.calls[0][0];
expect(loadersArg).toHaveLength(2);
expect(loadersArg[0]).toBe(vi.mocked(McpPromptLoader).mock.instances[0]);
expect(loadersArg[1]).toBe(vi.mocked(FileCommandLoader).mock.instances[0]);
});
it('should allow a normally-excluded tool when --allowed-tools is set', async () => { it('should allow a normally-excluded tool when --allowed-tools is set', async () => {
// By default, ShellTool is excluded in non-interactive mode. // By default, ShellTool is excluded in non-interactive mode.
// This test ensures that --allowed-tools overrides this exclusion. // This test ensures that --allowed-tools overrides this exclusion.
@@ -14,6 +14,7 @@ import {
} from '@google/gemini-cli-core'; } from '@google/gemini-cli-core';
import { CommandService } from './services/CommandService.js'; import { CommandService } from './services/CommandService.js';
import { FileCommandLoader } from './services/FileCommandLoader.js'; import { FileCommandLoader } from './services/FileCommandLoader.js';
import { McpPromptLoader } from './services/McpPromptLoader.js';
import type { CommandContext } from './ui/commands/types.js'; import type { CommandContext } from './ui/commands/types.js';
import { createNonInteractiveUI } from './ui/noninteractive/nonInteractiveUi.js'; import { createNonInteractiveUI } from './ui/noninteractive/nonInteractiveUi.js';
import type { LoadedSettings } from './config/settings.js'; import type { LoadedSettings } from './config/settings.js';
@@ -38,9 +39,8 @@ export const handleSlashCommand = async (
return; return;
} }
// Only custom commands are supported for now.
const commandService = await CommandService.create( const commandService = await CommandService.create(
[new FileCommandLoader(config)], [new McpPromptLoader(config), new FileCommandLoader(config)],
abortController.signal, abortController.signal,
); );
const commands = commandService.getCommands(); const commands = commandService.getCommands();