Merge pull request #7520 from uinstinct/cli-sse-streamable-mcp

feat(cli): support sse and streamable http for mcp
This commit is contained in:
Ting-Wai 2025-09-02 21:54:21 -07:00 • committed by GitHub
commit f0bc32a6d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 130 additions and 22 deletions

View file

@ -1,5 +1,5 @@
import { AssistantConfig } from "@continuedev/sdk";
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { MCPService } from "./MCPService.js";
@ -21,6 +21,14 @@ vi.mock("@modelcontextprotocol/sdk/client/stdio.js", () => ({
StdioClientTransport: vi.fn(),
}));
vi.mock("@modelcontextprotocol/sdk/client/sse.js", () => ({
SSEClientTransport: vi.fn(),
}));
vi.mock("@modelcontextprotocol/sdk/client/streamableHttp.js", () => ({
StreamableHTTPClientTransport: vi.fn(),
}));
describe("MCPService", () => {
let mcpService: MCPService;
let mockAssistant: AssistantConfig;
@ -152,4 +160,56 @@ describe("MCPService", () => {
await expect(mcpService.cleanup()).resolves.not.toThrow();
});
});
describe("transport types", () => {
it("should support SSE transport", async () => {
const sseAssistant: AssistantConfig = {
name: "sse-assistant",
version: "1.0.0",
mcpServers: [
{
name: "sse-server",
type: "sse",
url: "https://example.com/sse",
},
],
} as AssistantConfig;
await expect(mcpService.initialize(sseAssistant)).resolves.not.toThrow();
});
it("should support streamable-http transport", async () => {
const httpAssistant: AssistantConfig = {
name: "http-assistant",
version: "1.0.0",
mcpServers: [
{
name: "http-server",
type: "streamable-http",
url: "https://example.com/http",
},
],
} as AssistantConfig;
await expect(mcpService.initialize(httpAssistant)).resolves.not.toThrow();
});
it("should default to stdio transport when type is not specified", async () => {
const defaultAssistant: AssistantConfig = {
name: "default-assistant",
version: "1.0.0",
mcpServers: [
{
name: "default-server",
command: "npx",
args: ["-v"],
},
],
} as AssistantConfig;
await expect(
mcpService.initialize(defaultAssistant),
).resolves.not.toThrow();
});
});
});

View file

@ -1,6 +1,9 @@
import { type AssistantConfig } from "@continuedev/sdk";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import { getErrorString } from "../util/error.js";
import { logger } from "../util/logger.js";
@ -9,9 +12,9 @@ import { BaseService, ServiceWithDependencies } from "./BaseService.js";
import { serviceContainer } from "./ServiceContainer.js";
import {
MCPConnectionInfo,
MCPServerConfig,
MCPServiceState,
SERVICE_NAMES,
MCPServerConfig,
} from "./types.js";
interface ServerConnection extends MCPConnectionInfo {
@ -214,31 +217,12 @@ export class MCPService
this.updateState();
try {
if (serverConfig.type && serverConfig.type !== "stdio") {
throw new Error(
`${serverConfig.type} MCP servers are not yet supported in the Continue CLI`,
);
}
if (!serverConfig.command) {
throw new Error("MCP server command is not specified");
}
const client = new Client(
{ name: "continue-cli-client", version: "1.0.0" },
{ capabilities: {} },
);
const env: Record<string, string> = serverConfig.env || {};
if (process.env.PATH !== undefined) {
env.PATH = process.env.PATH;
}
const transport = new StdioClientTransport({
command: serverConfig.command,
args: serverConfig.args,
env,
stderr: "ignore",
});
const transport = await this.constructTransport(serverConfig);
logger.debug("Connecting to MCP server", {
name: serverName,
@ -363,4 +347,68 @@ export class MCPService
logger.debug("Shutting down MCPService");
await this.shutdownConnections();
}
/**
* Construct transport based on server configuration
*/
private async constructTransport(
serverConfig: MCPServerConfig,
): Promise<Transport> {
const transportType = serverConfig.type || "stdio";
switch (transportType) {
case "stdio":
if (!serverConfig.command) {
throw new Error(
"MCP server command is not specified for stdio transport",
);
}
const env: Record<string, string> = serverConfig.env || {};
if (process.env.PATH !== undefined) {
env.PATH = process.env.PATH;
}
return new StdioClientTransport({
command: serverConfig.command,
args: serverConfig.args || [],
env,
cwd: serverConfig.cwd,
stderr: "ignore",
});
case "sse":
if (!serverConfig.url) {
throw new Error("MCP server URL is not specified for SSE transport");
}
return new SSEClientTransport(new URL(serverConfig.url), {
eventSourceInit: {
fetch: (input, init) =>
fetch(input, {
...init,
headers: {
...init?.headers,
...(serverConfig.requestOptions?.headers as
| Record<string, string>
| undefined),
},
}),
},
requestInit: { headers: serverConfig.requestOptions?.headers },
});
case "streamable-http":
if (!serverConfig.url) {
throw new Error(
"MCP server URL is not specified for streamable-http transport",
);
}
return new StreamableHTTPClientTransport(new URL(serverConfig.url), {
requestInit: { headers: serverConfig.requestOptions?.headers },
});
default:
throw new Error(`Unsupported transport type: ${transportType}`);
}
}
}