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 <qwen-coder@alibabacloud.com>
This commit is contained in:
tanzhenxin 2026-02-08 11:11:02 +08:00
parent 21e711469d
commit a3b95414de
2 changed files with 48 additions and 2 deletions

View file

@ -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"',

View file

@ -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<string | number>) || [];
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(