From 31b4c76a6b0e52c9d748f51a1766bec19552adf5 Mon Sep 17 00:00:00 2001 From: Jack Wotherspoon Date: Fri, 15 Aug 2025 15:36:38 -0400 Subject: [PATCH] fix: improve robustness of `gemini mcp add` command (#6332) --- packages/cli/src/commands/mcp/add.test.ts | 34 +++++++++++++++++++++++ packages/cli/src/commands/mcp/add.ts | 11 ++++++++ 2 files changed, 45 insertions(+) diff --git a/packages/cli/src/commands/mcp/add.test.ts b/packages/cli/src/commands/mcp/add.test.ts index 1d431c48..fc1ffb64 100644 --- a/packages/cli/src/commands/mcp/add.test.ts +++ b/packages/cli/src/commands/mcp/add.test.ts @@ -85,4 +85,38 @@ describe('mcp add command', () => { }, ); }); + + it('should handle MCP server args with -- separator', async () => { + await parser.parseAsync( + 'add my-server npx -- -y http://example.com/some-package', + ); + + expect(mockSetValue).toHaveBeenCalledWith( + SettingScope.Workspace, + 'mcpServers', + { + 'my-server': { + command: 'npx', + args: ['-y', 'http://example.com/some-package'], + }, + }, + ); + }); + + it('should handle unknown options as MCP server args', async () => { + await parser.parseAsync( + 'add test-server npx -y http://example.com/some-package', + ); + + expect(mockSetValue).toHaveBeenCalledWith( + SettingScope.Workspace, + 'mcpServers', + { + 'test-server': { + command: 'npx', + args: ['-y', 'http://example.com/some-package'], + }, + }, + ); + }); }); diff --git a/packages/cli/src/commands/mcp/add.ts b/packages/cli/src/commands/mcp/add.ts index 9537e131..67c44572 100644 --- a/packages/cli/src/commands/mcp/add.ts +++ b/packages/cli/src/commands/mcp/add.ts @@ -130,6 +130,10 @@ export const addCommand: CommandModule = { builder: (yargs) => yargs .usage('Usage: gemini mcp add [options] [args...]') + .parserConfiguration({ + 'unknown-options-as-args': true, // Pass unknown options as server args + 'populate--': true, // Populate server args after -- separator + }) .positional('name', { describe: 'Name of the server', type: 'string', @@ -189,6 +193,13 @@ export const addCommand: CommandModule = { describe: 'A comma-separated list of tools to exclude', type: 'array', string: true, + }) + .middleware((argv) => { + // Handle -- separator args as server args if present + if (argv['--']) { + const existingArgs = (argv.args as Array) || []; + argv.args = [...existingArgs, ...(argv['--'] as string[])]; + } }), handler: async (argv) => { await addMcpServer(