Skip to content

Commit daa863d

Browse files
committed
feat: add transport option to cli
1 parent ad39ec2 commit daa863d

File tree

2 files changed

+63
-5
lines changed

2 files changed

+63
-5
lines changed

cli/src/index.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ type Args = {
2929
logLevel?: LogLevel;
3030
toolName?: string;
3131
toolArg?: Record<string, string>;
32+
transport?: "sse" | "stdio" | "http";
3233
};
3334

34-
function createTransportOptions(target: string[]): TransportOptions {
35+
function createTransportOptions(
36+
target: string[],
37+
transport?: "sse" | "stdio" | "http",
38+
): TransportOptions {
3539
if (target.length === 0) {
3640
throw new Error(
3741
"Target is required. Specify a URL or a command to execute.",
@@ -50,16 +54,38 @@ function createTransportOptions(target: string[]): TransportOptions {
5054
throw new Error("Arguments cannot be passed to a URL-based MCP server.");
5155
}
5256

57+
let transportType: "sse" | "stdio" | "http";
58+
if (transport) {
59+
if (!isUrl && transport !== "stdio") {
60+
throw new Error("Only stdio transport can be used with local commands.");
61+
}
62+
if (isUrl && transport === "stdio") {
63+
throw new Error("stdio transport cannot be used with URLs.");
64+
}
65+
transportType = transport;
66+
} else if (isUrl) {
67+
const url = new URL(command);
68+
if (url.pathname.endsWith("/mcp")) {
69+
transportType = "http";
70+
} else if (url.pathname.endsWith("/sse")) {
71+
transportType = "sse";
72+
} else {
73+
transportType = "sse";
74+
}
75+
} else {
76+
transportType = "stdio";
77+
}
78+
5379
return {
54-
transportType: isUrl ? "sse" : "stdio",
80+
transportType,
5581
command: isUrl ? undefined : command,
5682
args: isUrl ? undefined : commandArgs,
5783
url: isUrl ? command : undefined,
5884
};
5985
}
6086

6187
async function callMethod(args: Args): Promise<void> {
62-
const transportOptions = createTransportOptions(args.target);
88+
const transportOptions = createTransportOptions(args.target, args.transport);
6389
const transport = createTransport(transportOptions);
6490
const client = new Client({
6591
name: "inspector-cli",
@@ -214,6 +240,22 @@ function parseArgs(): Args {
214240

215241
return value as LogLevel;
216242
},
243+
)
244+
//
245+
// Transport options
246+
//
247+
.option(
248+
"--transport <type>",
249+
"Transport type (sse, http, or stdio). Auto-detected from URL: /mcp → http, /sse → sse, commands → stdio",
250+
(value: string) => {
251+
const validTransports = ["sse", "http", "stdio"];
252+
if (!validTransports.includes(value)) {
253+
throw new Error(
254+
`Invalid transport type: ${value}. Valid types are: ${validTransports.join(", ")}`,
255+
);
256+
}
257+
return value as "sse" | "http" | "stdio";
258+
},
217259
);
218260

219261
// Parse only the arguments before --

cli/src/transport.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,35 @@ import {
33
getDefaultEnvironment,
44
StdioClientTransport,
55
} from "@modelcontextprotocol/sdk/client/stdio.js";
6+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
67
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
78
import { findActualExecutable } from "spawn-rx";
89

910
export type TransportOptions = {
10-
transportType: "sse" | "stdio";
11+
transportType: "sse" | "stdio" | "http";
1112
command?: string;
1213
args?: string[];
1314
url?: string;
1415
};
1516

1617
function createSSETransport(options: TransportOptions): Transport {
1718
const baseUrl = new URL(options.url ?? "");
18-
const sseUrl = new URL("/sse", baseUrl);
19+
const sseUrl = baseUrl.pathname.endsWith("/sse")
20+
? baseUrl
21+
: new URL("/sse", baseUrl);
1922

2023
return new SSEClientTransport(sseUrl);
2124
}
2225

26+
function createHTTPTransport(options: TransportOptions): Transport {
27+
const baseUrl = new URL(options.url ?? "");
28+
const mcpUrl = baseUrl.pathname.endsWith("/mcp")
29+
? baseUrl
30+
: new URL("/mcp", baseUrl);
31+
32+
return new StreamableHTTPClientTransport(mcpUrl);
33+
}
34+
2335
function createStdioTransport(options: TransportOptions): Transport {
2436
let args: string[] = [];
2537

@@ -67,6 +79,10 @@ export function createTransport(options: TransportOptions): Transport {
6779
return createSSETransport(options);
6880
}
6981

82+
if (transportType === "http") {
83+
return createHTTPTransport(options);
84+
}
85+
7086
throw new Error(`Unsupported transport type: ${transportType}`);
7187
} catch (error) {
7288
throw new Error(

0 commit comments

Comments
 (0)