Skip to content

Commit 4e123c9

Browse files
committed
feat: add WebSocket transport type
1 parent 23d48e0 commit 4e123c9

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/commands/mcp.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function createMCPCommand(): Command {
2121
.argument("<name>", "Server name")
2222
.option(
2323
"-t, --transport <type>",
24-
"Transport type: stdio | sse | streamable-http (alias: http)",
24+
"Transport type: stdio | sse | ws | streamable-http (alias: http)",
2525
"stdio",
2626
)
2727
.option(
@@ -33,7 +33,7 @@ export function createMCPCommand(): Command {
3333
"Arguments for the server command (for stdio transport)",
3434
[],
3535
)
36-
.option("-u, --url <url>", "URL for SSE/HTTP transport")
36+
.option("-u, --url <url>", "URL for SSE/HTTP/WS transport")
3737
.option("-e, --env [envs...]", "Environment variables (KEY=VALUE)")
3838
.option("-H, --header [headers...]", "HTTP headers (KEY=VALUE)")
3939
.option("--timeout <ms>", "Operation timeout in ms", (v) => parseInt(v, 10))
@@ -60,6 +60,14 @@ export function createMCPCommand(): Command {
6060
enabled: opts.enabled,
6161
};
6262
break;
63+
case TRANSPORT_TYPES.WS:
64+
config = {
65+
type: TRANSPORT_TYPES.WS,
66+
url: opts.url,
67+
timeout: opts.timeout,
68+
enabled: opts.enabled,
69+
};
70+
break;
6371
case TRANSPORT_TYPES.HTTP:
6472
case TRANSPORT_TYPES.STREAMABLE_HTTP:
6573
config = {

src/mcp/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const TRANSPORT_TYPES = {
33
STDIO: "stdio",
44
SSE: "sse",
55
HTTP: "http",
6+
WS: "ws",
67
STREAMABLE_HTTP: "streamable-http",
78
} as const;
89

src/mcp/transports.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import type {
99
SSEServerConfig,
1010
StdioServerConfig,
1111
StreamableHttpServerConfig,
12+
WebSocketServerConfig,
1213
} from "./types.js";
14+
import { WebSocketClientTransport } from "@modelcontextprotocol/sdk/client/websocket.js";
1315

1416
interface TransportFactory {
1517
create(config: MCPServerConfig): Promise<Transport>;
@@ -51,11 +53,18 @@ class StreamableHttpTransportFactory implements TransportFactory {
5153
);
5254
}
5355
}
56+
class WebSocketTransportFactory implements TransportFactory {
57+
async create(config: WebSocketServerConfig): Promise<Transport> {
58+
if (!config.url) throw new Error("URL is required for WebSocket transport");
59+
return new WebSocketClientTransport(new URL(config.url));
60+
}
61+
}
5462

5563
const factories: Record<string, TransportFactory> = {
5664
[TRANSPORT_TYPES.STDIO]: new StdioTransportFactory(),
5765
[TRANSPORT_TYPES.SSE]: new SSETransportFactory(),
5866
[TRANSPORT_TYPES.STREAMABLE_HTTP]: new StreamableHttpTransportFactory(),
67+
[TRANSPORT_TYPES.WS]: new WebSocketTransportFactory(),
5968
};
6069

6170
export async function createTransport(

src/mcp/types.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
* List of all MCP client transport types.
1212
* Which define the communication protocol used to connect MCP servers.
1313
*/
14-
export type TransportType = "stdio" | "sse" | "http" | "streamable-http";
14+
export type TransportType = "stdio" | "sse" | "http" | "streamable-http" | "ws";
1515

1616
export interface BaseServerConfig {
1717
type: TransportType;
@@ -37,11 +37,16 @@ export interface StreamableHttpServerConfig extends BaseServerConfig {
3737
url: string;
3838
headers?: Record<string, string>;
3939
}
40+
export interface WebSocketServerConfig extends BaseServerConfig {
41+
type: "ws";
42+
url: string;
43+
}
4044

4145
export type MCPServerConfig =
4246
| StdioServerConfig
4347
| SSEServerConfig
44-
| StreamableHttpServerConfig;
48+
| StreamableHttpServerConfig
49+
| WebSocketServerConfig;
4550

4651
export type ServerConfigs = Record<string, MCPServerConfig>;
4752

0 commit comments

Comments
 (0)