From a3b95414dee645d5de3dfeb324f222c2d8d8a08e Mon Sep 17 00:00:00 2001 From: tanzhenxin Date: Sun, 8 Feb 2026 11:11:02 +0800 Subject: [PATCH] feat(mcp): auto-detect transport type from URL in mcp add command - Automatically detect 'http' transport when commandOrUrl starts with http:// or https:// - Default to 'stdio' transport for non-URL commands - Explicit --transport flag still takes precedence over auto-detection - Update help text to indicate auto-detection capability Co-authored-by: Qwen-Coder --- packages/cli/src/commands/mcp/add.test.ts | 32 +++++++++++++++++++++++ packages/cli/src/commands/mcp/add.ts | 18 +++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands/mcp/add.test.ts b/packages/cli/src/commands/mcp/add.test.ts index e46a431b4..b1c98fb23 100644 --- a/packages/cli/src/commands/mcp/add.test.ts +++ b/packages/cli/src/commands/mcp/add.test.ts @@ -79,6 +79,38 @@ describe('mcp add command', () => { }); }); + it('should auto-detect http transport when commandOrUrl is an https URL', async () => { + await parser.parseAsync('add http-server https://example.com/mcp'); + + expect(mockSetValue).toHaveBeenCalledWith(SettingScope.User, 'mcpServers', { + 'http-server': { + httpUrl: 'https://example.com/mcp', + }, + }); + }); + + it('should auto-detect http transport when commandOrUrl is an http URL', async () => { + await parser.parseAsync('add http-server http://localhost:8080/mcp'); + + expect(mockSetValue).toHaveBeenCalledWith(SettingScope.User, 'mcpServers', { + 'http-server': { + httpUrl: 'http://localhost:8080/mcp', + }, + }); + }); + + it('should respect explicit transport even when commandOrUrl is a URL', async () => { + await parser.parseAsync( + 'add --transport sse sse-server https://example.com/sse-endpoint', + ); + + expect(mockSetValue).toHaveBeenCalledWith(SettingScope.User, 'mcpServers', { + 'sse-server': { + url: 'https://example.com/sse-endpoint', + }, + }); + }); + it('should add an sse server to user settings', async () => { await parser.parseAsync( 'add --transport sse sse-server https://example.com/sse-endpoint --scope user -H "X-API-Key: your-key"', diff --git a/packages/cli/src/commands/mcp/add.ts b/packages/cli/src/commands/mcp/add.ts index 65de64981..29fe25b88 100644 --- a/packages/cli/src/commands/mcp/add.ts +++ b/packages/cli/src/commands/mcp/add.ts @@ -164,9 +164,9 @@ export const addCommand: CommandModule = { }) .option('transport', { alias: 't', - describe: 'Transport type (stdio, sse, http)', + describe: + 'Transport type (stdio, sse, http). Auto-detected from URL if not specified.', type: 'string', - default: 'stdio', choices: ['stdio', 'sse', 'http'], }) .option('env', { @@ -211,6 +211,20 @@ export const addCommand: CommandModule = { const existingArgs = (argv['args'] as Array) || []; argv['args'] = [...existingArgs, ...(argv['--'] as string[])]; } + + // Auto-detect transport from URL if not explicitly specified + if (!argv['transport']) { + const commandOrUrl = argv['commandOrUrl'] as string; + if ( + commandOrUrl && + (commandOrUrl.startsWith('http://') || + commandOrUrl.startsWith('https://')) + ) { + argv['transport'] = 'http'; + } else { + argv['transport'] = 'stdio'; + } + } }), handler: async (argv) => { await addMcpServer(