[cli] Don't force specific endpoints for remote servers
At the moment, the CLI tool expects that remote MCP servers, either using SSE or streamable HTTP transport, have certain endpoints or fragments -- `/sse` and `/mcp`, respectively. This is not part of the MCP spec, nor should it be expected. I also added a check to make sure a URL is provided if STDIO isn't picked, erroring out if not.
This commit is contained in:
parent
c57dc7893b
commit
7c2b761748
2 changed files with 28 additions and 20 deletions
|
|
@ -710,6 +710,26 @@ async function runTests() {
|
||||||
"tools/list",
|
"tools/list",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Test 29: HTTP transport without URL (should fail)
|
||||||
|
await runErrorTest(
|
||||||
|
"http_transport_without_url",
|
||||||
|
"--transport",
|
||||||
|
"http",
|
||||||
|
"--cli",
|
||||||
|
"--method",
|
||||||
|
"tools/list",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Test 30: SSE transport without URL (should fail)
|
||||||
|
await runErrorTest(
|
||||||
|
"sse_transport_without_url",
|
||||||
|
"--transport",
|
||||||
|
"sse",
|
||||||
|
"--cli",
|
||||||
|
"--method",
|
||||||
|
"tools/list",
|
||||||
|
);
|
||||||
|
|
||||||
// Kill HTTP server
|
// Kill HTTP server
|
||||||
try {
|
try {
|
||||||
process.kill(-httpServer.pid);
|
process.kill(-httpServer.pid);
|
||||||
|
|
|
||||||
|
|
@ -14,24 +14,6 @@ export type TransportOptions = {
|
||||||
url?: string;
|
url?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
function createSSETransport(options: TransportOptions): Transport {
|
|
||||||
const baseUrl = new URL(options.url ?? "");
|
|
||||||
const sseUrl = baseUrl.pathname.endsWith("/sse")
|
|
||||||
? baseUrl
|
|
||||||
: new URL("/sse", baseUrl);
|
|
||||||
|
|
||||||
return new SSEClientTransport(sseUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createHTTPTransport(options: TransportOptions): Transport {
|
|
||||||
const baseUrl = new URL(options.url ?? "");
|
|
||||||
const mcpUrl = baseUrl.pathname.endsWith("/mcp")
|
|
||||||
? baseUrl
|
|
||||||
: new URL("/mcp", baseUrl);
|
|
||||||
|
|
||||||
return new StreamableHTTPClientTransport(mcpUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createStdioTransport(options: TransportOptions): Transport {
|
function createStdioTransport(options: TransportOptions): Transport {
|
||||||
let args: string[] = [];
|
let args: string[] = [];
|
||||||
|
|
||||||
|
|
@ -75,12 +57,18 @@ export function createTransport(options: TransportOptions): Transport {
|
||||||
return createStdioTransport(options);
|
return createStdioTransport(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If not STDIO, then it must be either SSE or HTTP.
|
||||||
|
if (!options.url) {
|
||||||
|
throw new Error("URL must be provided for SSE or HTTP transport types.");
|
||||||
|
}
|
||||||
|
const url = new URL(options.url);
|
||||||
|
|
||||||
if (transportType === "sse") {
|
if (transportType === "sse") {
|
||||||
return createSSETransport(options);
|
return new SSEClientTransport(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (transportType === "http") {
|
if (transportType === "http") {
|
||||||
return createHTTPTransport(options);
|
return new StreamableHTTPClientTransport(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`Unsupported transport type: ${transportType}`);
|
throw new Error(`Unsupported transport type: ${transportType}`);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue