mcp-proxy 2.8.2 → 2.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mcp-proxy.js +407 -0
- package/dist/mcp-proxy.js.map +1 -0
- package/package.json +3 -3
- package/src/StdioClientTransport.ts +226 -0
- package/src/bin/mcp-proxy.ts +51 -59
- package/src/index.ts +3 -0
- package/src/proxyServer.ts +74 -0
- package/src/{MCPProxy.test.ts → startSSEServer.test.ts} +2 -1
- package/src/{MCPProxy.ts → startSSEServer.ts} +1 -164
- package/src/tapTransport.ts +90 -0
- package/dist/MCPProxy.d.ts +0 -44
- package/dist/MCPProxy.js +0 -11
- package/dist/MCPProxy.js.map +0 -1
- package/dist/bin/mcp-proxy.js +0 -104
- package/dist/bin/mcp-proxy.js.map +0 -1
- package/dist/chunk-A5VUTWBC.js +0 -227
- package/dist/chunk-A5VUTWBC.js.map +0 -1
- /package/dist/{bin/mcp-proxy.d.ts → mcp-proxy.d.ts} +0 -0
package/src/bin/mcp-proxy.ts
CHANGED
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
import yargs from "yargs";
|
|
4
4
|
import { hideBin } from "yargs/helpers";
|
|
5
|
-
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
6
5
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
7
6
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
8
|
-
import { proxyServer, startSSEServer } from "../MCPProxy.js";
|
|
9
7
|
import { EventSource } from "eventsource";
|
|
10
8
|
import { setTimeout } from "node:timers/promises";
|
|
9
|
+
import { StdioClientTransport } from "../StdioClientTransport.js";
|
|
10
|
+
import util from "node:util";
|
|
11
|
+
import { startSSEServer } from "../startSSEServer.js";
|
|
12
|
+
import { proxyServer } from "../proxyServer.js";
|
|
13
|
+
|
|
14
|
+
util.inspect.defaultOptions.depth = 8;
|
|
11
15
|
|
|
12
16
|
if (!("EventSource" in global)) {
|
|
13
17
|
// @ts-expect-error - figure out how to use --experimental-eventsource with vitest
|
|
@@ -47,85 +51,73 @@ const argv = await yargs(hideBin(process.argv))
|
|
|
47
51
|
.help()
|
|
48
52
|
.parseAsync();
|
|
49
53
|
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
},
|
|
62
|
-
{
|
|
63
|
-
capabilities: {},
|
|
64
|
-
},
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
let stderrOutput = '';
|
|
69
|
-
|
|
70
|
-
try {
|
|
71
|
-
console.info('connecting to the MCP server...');
|
|
72
|
-
|
|
73
|
-
const connectionPromise = client.connect(transport);
|
|
74
|
-
|
|
75
|
-
transport?.stderr?.on('data', (chunk) => {
|
|
76
|
-
stderrOutput += chunk.toString();
|
|
54
|
+
const connect = async (client: Client) => {
|
|
55
|
+
const transport = new StdioClientTransport({
|
|
56
|
+
command: argv.command,
|
|
57
|
+
args: argv.args,
|
|
58
|
+
env: process.env as Record<string, string>,
|
|
59
|
+
stderr: "pipe",
|
|
60
|
+
onEvent: (event) => {
|
|
61
|
+
if (argv.debug) {
|
|
62
|
+
console.debug("transport event", event);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
77
65
|
});
|
|
78
66
|
|
|
79
|
-
await
|
|
80
|
-
|
|
81
|
-
console.info('connected to the MCP server');
|
|
82
|
-
} catch (error) {
|
|
83
|
-
console.error(`
|
|
84
|
-
could not connect to the MCP server
|
|
85
|
-
|
|
86
|
-
--- error ---
|
|
87
|
-
${String(error)}
|
|
88
|
-
|
|
89
|
-
--- stderr output ---
|
|
90
|
-
${stderrOutput}
|
|
91
|
-
`);
|
|
67
|
+
await client.connect(transport);
|
|
68
|
+
};
|
|
92
69
|
|
|
93
|
-
|
|
70
|
+
const proxy = async () => {
|
|
71
|
+
const client = new Client(
|
|
72
|
+
{
|
|
73
|
+
name: "mcp-proxy",
|
|
74
|
+
version: "1.0.0",
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
capabilities: {},
|
|
78
|
+
},
|
|
79
|
+
);
|
|
94
80
|
|
|
95
|
-
|
|
96
|
-
}
|
|
81
|
+
await connect(client);
|
|
97
82
|
|
|
98
|
-
const serverVersion = client.getServerVersion() as {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
};
|
|
83
|
+
const serverVersion = client.getServerVersion() as {
|
|
84
|
+
name: string;
|
|
85
|
+
version: string;
|
|
86
|
+
};
|
|
102
87
|
|
|
103
|
-
const serverCapabilities = client.getServerCapabilities() as {};
|
|
88
|
+
const serverCapabilities = client.getServerCapabilities() as {};
|
|
104
89
|
|
|
105
|
-
|
|
106
|
-
console.info('starting the SSE server on port %d', argv.port);
|
|
90
|
+
console.info("starting the SSE server on port %d", argv.port);
|
|
107
91
|
|
|
108
92
|
await startSSEServer({
|
|
109
93
|
createServer: async () => {
|
|
110
94
|
const server = new Server(serverVersion, {
|
|
111
95
|
capabilities: serverCapabilities,
|
|
112
96
|
});
|
|
113
|
-
|
|
97
|
+
|
|
114
98
|
proxyServer({
|
|
115
99
|
server,
|
|
116
100
|
client,
|
|
117
101
|
serverCapabilities,
|
|
118
102
|
});
|
|
119
|
-
|
|
103
|
+
|
|
120
104
|
return server;
|
|
121
105
|
},
|
|
122
106
|
port: argv.port,
|
|
123
107
|
endpoint: argv.endpoint as `/${string}`,
|
|
124
108
|
});
|
|
125
|
-
}
|
|
126
|
-
console.error('could not start the SSE server', error);
|
|
109
|
+
};
|
|
127
110
|
|
|
128
|
-
|
|
111
|
+
const main = async () => {
|
|
112
|
+
try {
|
|
113
|
+
await proxy();
|
|
114
|
+
} catch (error) {
|
|
115
|
+
console.error("could not start the proxy", error);
|
|
129
116
|
|
|
130
|
-
|
|
131
|
-
|
|
117
|
+
await setTimeout(1000);
|
|
118
|
+
|
|
119
|
+
process.exit(1);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
await main();
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
|
+
import {
|
|
3
|
+
CallToolRequestSchema,
|
|
4
|
+
CompleteRequestSchema,
|
|
5
|
+
GetPromptRequestSchema,
|
|
6
|
+
ListPromptsRequestSchema,
|
|
7
|
+
ListResourcesRequestSchema,
|
|
8
|
+
ListResourceTemplatesRequestSchema,
|
|
9
|
+
ListToolsRequestSchema,
|
|
10
|
+
LoggingMessageNotificationSchema,
|
|
11
|
+
ReadResourceRequestSchema,
|
|
12
|
+
ServerCapabilities,
|
|
13
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
14
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
15
|
+
|
|
16
|
+
export const proxyServer = async ({
|
|
17
|
+
server,
|
|
18
|
+
client,
|
|
19
|
+
serverCapabilities,
|
|
20
|
+
}: {
|
|
21
|
+
server: Server;
|
|
22
|
+
client: Client;
|
|
23
|
+
serverCapabilities: ServerCapabilities;
|
|
24
|
+
}) => {
|
|
25
|
+
if (serverCapabilities?.logging) {
|
|
26
|
+
server.setNotificationHandler(
|
|
27
|
+
LoggingMessageNotificationSchema,
|
|
28
|
+
async (args) => {
|
|
29
|
+
return client.notification(args);
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (serverCapabilities?.prompts) {
|
|
35
|
+
server.setRequestHandler(GetPromptRequestSchema, async (args) => {
|
|
36
|
+
return client.getPrompt(args.params);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
server.setRequestHandler(ListPromptsRequestSchema, async (args) => {
|
|
40
|
+
return client.listPrompts(args.params);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (serverCapabilities?.resources) {
|
|
45
|
+
server.setRequestHandler(ListResourcesRequestSchema, async (args) => {
|
|
46
|
+
return client.listResources(args.params);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
server.setRequestHandler(
|
|
50
|
+
ListResourceTemplatesRequestSchema,
|
|
51
|
+
async (args) => {
|
|
52
|
+
return client.listResourceTemplates(args.params);
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
server.setRequestHandler(ReadResourceRequestSchema, async (args) => {
|
|
57
|
+
return client.readResource(args.params);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (serverCapabilities?.tools) {
|
|
62
|
+
server.setRequestHandler(CallToolRequestSchema, async (args) => {
|
|
63
|
+
return client.callTool(args.params);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
server.setRequestHandler(ListToolsRequestSchema, async (args) => {
|
|
67
|
+
return client.listTools(args.params);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
server.setRequestHandler(CompleteRequestSchema, async (args) => {
|
|
72
|
+
return client.complete(args.params);
|
|
73
|
+
});
|
|
74
|
+
};
|
|
@@ -2,11 +2,12 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
|
2
2
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
3
3
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
4
|
import { it, expect, vi } from "vitest";
|
|
5
|
-
import {
|
|
5
|
+
import { startSSEServer } from "./startSSEServer.js";
|
|
6
6
|
import { getRandomPort } from "get-port-please";
|
|
7
7
|
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
8
8
|
import { EventSource } from "eventsource";
|
|
9
9
|
import { setTimeout as delay } from "node:timers/promises";
|
|
10
|
+
import { proxyServer } from "./proxyServer.js";
|
|
10
11
|
|
|
11
12
|
if (!("EventSource" in global)) {
|
|
12
13
|
// @ts-expect-error - figure out how to use --experimental-eventsource with vitest
|
|
@@ -1,169 +1,6 @@
|
|
|
1
1
|
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
|
|
2
2
|
import http from "http";
|
|
3
3
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
4
|
-
import {
|
|
5
|
-
CallToolRequestSchema,
|
|
6
|
-
CompleteRequestSchema,
|
|
7
|
-
GetPromptRequestSchema,
|
|
8
|
-
JSONRPCMessage,
|
|
9
|
-
ListPromptsRequestSchema,
|
|
10
|
-
ListResourcesRequestSchema,
|
|
11
|
-
ListResourceTemplatesRequestSchema,
|
|
12
|
-
ListToolsRequestSchema,
|
|
13
|
-
LoggingMessageNotificationSchema,
|
|
14
|
-
ReadResourceRequestSchema,
|
|
15
|
-
ServerCapabilities,
|
|
16
|
-
} from "@modelcontextprotocol/sdk/types.js";
|
|
17
|
-
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
18
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
19
|
-
|
|
20
|
-
type TransportEvent =
|
|
21
|
-
| {
|
|
22
|
-
type: "close";
|
|
23
|
-
}
|
|
24
|
-
| {
|
|
25
|
-
type: "onclose";
|
|
26
|
-
}
|
|
27
|
-
| {
|
|
28
|
-
type: "onerror";
|
|
29
|
-
error: Error;
|
|
30
|
-
}
|
|
31
|
-
| {
|
|
32
|
-
type: "onmessage";
|
|
33
|
-
message: JSONRPCMessage;
|
|
34
|
-
}
|
|
35
|
-
| {
|
|
36
|
-
type: "send";
|
|
37
|
-
message: JSONRPCMessage;
|
|
38
|
-
}
|
|
39
|
-
| {
|
|
40
|
-
type: "start";
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const tapTransport = (
|
|
44
|
-
transport: Transport,
|
|
45
|
-
eventHandler: (event: TransportEvent) => void,
|
|
46
|
-
) => {
|
|
47
|
-
const originalClose = transport.close.bind(transport);
|
|
48
|
-
const originalOnClose = transport.onclose?.bind(transport);
|
|
49
|
-
const originalOnError = transport.onerror?.bind(transport);
|
|
50
|
-
const originalOnMessage = transport.onmessage?.bind(transport);
|
|
51
|
-
const originalSend = transport.send.bind(transport);
|
|
52
|
-
const originalStart = transport.start.bind(transport);
|
|
53
|
-
|
|
54
|
-
transport.close = async () => {
|
|
55
|
-
eventHandler({
|
|
56
|
-
type: "close",
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
return originalClose?.();
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
transport.onclose = async () => {
|
|
63
|
-
eventHandler({
|
|
64
|
-
type: "onclose",
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return originalOnClose?.();
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
transport.onerror = async (error: Error) => {
|
|
71
|
-
eventHandler({
|
|
72
|
-
type: "onerror",
|
|
73
|
-
error,
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
return originalOnError?.(error);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
transport.onmessage = async (message: JSONRPCMessage) => {
|
|
80
|
-
eventHandler({
|
|
81
|
-
type: "onmessage",
|
|
82
|
-
message,
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
return originalOnMessage?.(message);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
transport.send = async (message: JSONRPCMessage) => {
|
|
89
|
-
eventHandler({
|
|
90
|
-
type: "send",
|
|
91
|
-
message,
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
return originalSend?.(message);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
transport.start = async () => {
|
|
98
|
-
eventHandler({
|
|
99
|
-
type: "start",
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
return originalStart?.();
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
return transport;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
export const proxyServer = async ({
|
|
109
|
-
server,
|
|
110
|
-
client,
|
|
111
|
-
serverCapabilities,
|
|
112
|
-
}: {
|
|
113
|
-
server: Server;
|
|
114
|
-
client: Client;
|
|
115
|
-
serverCapabilities: ServerCapabilities;
|
|
116
|
-
}) => {
|
|
117
|
-
if (serverCapabilities?.logging) {
|
|
118
|
-
server.setNotificationHandler(
|
|
119
|
-
LoggingMessageNotificationSchema,
|
|
120
|
-
async (args) => {
|
|
121
|
-
return client.notification(args);
|
|
122
|
-
},
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (serverCapabilities?.prompts) {
|
|
127
|
-
server.setRequestHandler(GetPromptRequestSchema, async (args) => {
|
|
128
|
-
return client.getPrompt(args.params);
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
server.setRequestHandler(ListPromptsRequestSchema, async (args) => {
|
|
132
|
-
return client.listPrompts(args.params);
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (serverCapabilities?.resources) {
|
|
137
|
-
server.setRequestHandler(ListResourcesRequestSchema, async (args) => {
|
|
138
|
-
return client.listResources(args.params);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
server.setRequestHandler(
|
|
142
|
-
ListResourceTemplatesRequestSchema,
|
|
143
|
-
async (args) => {
|
|
144
|
-
return client.listResourceTemplates(args.params);
|
|
145
|
-
},
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
server.setRequestHandler(ReadResourceRequestSchema, async (args) => {
|
|
149
|
-
return client.readResource(args.params);
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (serverCapabilities?.tools) {
|
|
154
|
-
server.setRequestHandler(CallToolRequestSchema, async (args) => {
|
|
155
|
-
return client.callTool(args.params);
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
server.setRequestHandler(ListToolsRequestSchema, async (args) => {
|
|
159
|
-
return client.listTools(args.params);
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
server.setRequestHandler(CompleteRequestSchema, async (args) => {
|
|
164
|
-
return client.complete(args.params);
|
|
165
|
-
});
|
|
166
|
-
};
|
|
167
4
|
|
|
168
5
|
export type SSEServer = {
|
|
169
6
|
close: () => Promise<void>;
|
|
@@ -268,7 +105,7 @@ export const startSSEServer = async <T extends ServerLike>({
|
|
|
268
105
|
} catch (error) {
|
|
269
106
|
if (!closed) {
|
|
270
107
|
console.error("Error connecting to server:", error);
|
|
271
|
-
|
|
108
|
+
|
|
272
109
|
res.writeHead(500).end("Error connecting to server");
|
|
273
110
|
}
|
|
274
111
|
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
2
|
+
import { JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
|
|
4
|
+
type TransportEvent =
|
|
5
|
+
| {
|
|
6
|
+
type: "close";
|
|
7
|
+
}
|
|
8
|
+
| {
|
|
9
|
+
type: "onclose";
|
|
10
|
+
}
|
|
11
|
+
| {
|
|
12
|
+
type: "onerror";
|
|
13
|
+
error: Error;
|
|
14
|
+
}
|
|
15
|
+
| {
|
|
16
|
+
type: "onmessage";
|
|
17
|
+
message: JSONRPCMessage;
|
|
18
|
+
}
|
|
19
|
+
| {
|
|
20
|
+
type: "send";
|
|
21
|
+
message: JSONRPCMessage;
|
|
22
|
+
}
|
|
23
|
+
| {
|
|
24
|
+
type: "start";
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const tapTransport = (
|
|
28
|
+
transport: Transport,
|
|
29
|
+
eventHandler: (event: TransportEvent) => void,
|
|
30
|
+
) => {
|
|
31
|
+
const originalClose = transport.close.bind(transport);
|
|
32
|
+
const originalOnClose = transport.onclose?.bind(transport);
|
|
33
|
+
const originalOnError = transport.onerror?.bind(transport);
|
|
34
|
+
const originalOnMessage = transport.onmessage?.bind(transport);
|
|
35
|
+
const originalSend = transport.send.bind(transport);
|
|
36
|
+
const originalStart = transport.start.bind(transport);
|
|
37
|
+
|
|
38
|
+
transport.close = async () => {
|
|
39
|
+
eventHandler({
|
|
40
|
+
type: "close",
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return originalClose?.();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
transport.onclose = async () => {
|
|
47
|
+
eventHandler({
|
|
48
|
+
type: "onclose",
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return originalOnClose?.();
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
transport.onerror = async (error: Error) => {
|
|
55
|
+
eventHandler({
|
|
56
|
+
type: "onerror",
|
|
57
|
+
error,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return originalOnError?.(error);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
transport.onmessage = async (message: JSONRPCMessage) => {
|
|
64
|
+
eventHandler({
|
|
65
|
+
type: "onmessage",
|
|
66
|
+
message,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
return originalOnMessage?.(message);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
transport.send = async (message: JSONRPCMessage) => {
|
|
73
|
+
eventHandler({
|
|
74
|
+
type: "send",
|
|
75
|
+
message,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return originalSend?.(message);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
transport.start = async () => {
|
|
82
|
+
eventHandler({
|
|
83
|
+
type: "start",
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return originalStart?.();
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return transport;
|
|
90
|
+
};
|
package/dist/MCPProxy.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import http from 'http';
|
|
2
|
-
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
3
|
-
import { JSONRPCMessage, ServerCapabilities } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
-
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
|
|
5
|
-
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
6
|
-
|
|
7
|
-
type TransportEvent = {
|
|
8
|
-
type: "close";
|
|
9
|
-
} | {
|
|
10
|
-
type: "onclose";
|
|
11
|
-
} | {
|
|
12
|
-
type: "onerror";
|
|
13
|
-
error: Error;
|
|
14
|
-
} | {
|
|
15
|
-
type: "onmessage";
|
|
16
|
-
message: JSONRPCMessage;
|
|
17
|
-
} | {
|
|
18
|
-
type: "send";
|
|
19
|
-
message: JSONRPCMessage;
|
|
20
|
-
} | {
|
|
21
|
-
type: "start";
|
|
22
|
-
};
|
|
23
|
-
declare const tapTransport: (transport: Transport, eventHandler: (event: TransportEvent) => void) => Transport;
|
|
24
|
-
declare const proxyServer: ({ server, client, serverCapabilities, }: {
|
|
25
|
-
server: Server;
|
|
26
|
-
client: Client;
|
|
27
|
-
serverCapabilities: ServerCapabilities;
|
|
28
|
-
}) => Promise<void>;
|
|
29
|
-
type SSEServer = {
|
|
30
|
-
close: () => Promise<void>;
|
|
31
|
-
};
|
|
32
|
-
type ServerLike = {
|
|
33
|
-
connect: Server["connect"];
|
|
34
|
-
close: Server["close"];
|
|
35
|
-
};
|
|
36
|
-
declare const startSSEServer: <T extends ServerLike>({ port, createServer, endpoint, onConnect, onClose, }: {
|
|
37
|
-
port: number;
|
|
38
|
-
endpoint: string;
|
|
39
|
-
createServer: (request: http.IncomingMessage) => Promise<T>;
|
|
40
|
-
onConnect?: (server: T) => void;
|
|
41
|
-
onClose?: (server: T) => void;
|
|
42
|
-
}) => Promise<SSEServer>;
|
|
43
|
-
|
|
44
|
-
export { type SSEServer, proxyServer, startSSEServer, tapTransport };
|
package/dist/MCPProxy.js
DELETED
package/dist/MCPProxy.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/bin/mcp-proxy.js
DELETED
|
@@ -1,104 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
proxyServer,
|
|
4
|
-
startSSEServer
|
|
5
|
-
} from "../chunk-A5VUTWBC.js";
|
|
6
|
-
|
|
7
|
-
// src/bin/mcp-proxy.ts
|
|
8
|
-
import yargs from "yargs";
|
|
9
|
-
import { hideBin } from "yargs/helpers";
|
|
10
|
-
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
11
|
-
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
12
|
-
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
13
|
-
import { EventSource } from "eventsource";
|
|
14
|
-
import { setTimeout } from "node:timers/promises";
|
|
15
|
-
if (!("EventSource" in global)) {
|
|
16
|
-
global.EventSource = EventSource;
|
|
17
|
-
}
|
|
18
|
-
var argv = await yargs(hideBin(process.argv)).scriptName("mcp-proxy").command("$0 <command> [args...]", "Run a command with MCP arguments").positional("command", {
|
|
19
|
-
type: "string",
|
|
20
|
-
describe: "The command to run",
|
|
21
|
-
demandOption: true
|
|
22
|
-
}).positional("args", {
|
|
23
|
-
type: "string",
|
|
24
|
-
array: true,
|
|
25
|
-
describe: "The arguments to pass to the command"
|
|
26
|
-
}).options({
|
|
27
|
-
debug: {
|
|
28
|
-
type: "boolean",
|
|
29
|
-
describe: "Enable debug logging",
|
|
30
|
-
default: false
|
|
31
|
-
},
|
|
32
|
-
endpoint: {
|
|
33
|
-
type: "string",
|
|
34
|
-
describe: "The endpoint to listen on for SSE",
|
|
35
|
-
default: "/sse"
|
|
36
|
-
},
|
|
37
|
-
port: {
|
|
38
|
-
type: "number",
|
|
39
|
-
describe: "The port to listen on for SSE",
|
|
40
|
-
default: 8080
|
|
41
|
-
}
|
|
42
|
-
}).help().parseAsync();
|
|
43
|
-
var transport = new StdioClientTransport({
|
|
44
|
-
command: argv.command,
|
|
45
|
-
args: argv.args,
|
|
46
|
-
env: process.env,
|
|
47
|
-
stderr: "pipe"
|
|
48
|
-
});
|
|
49
|
-
var client = new Client(
|
|
50
|
-
{
|
|
51
|
-
name: "mcp-proxy",
|
|
52
|
-
version: "1.0.0"
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
capabilities: {}
|
|
56
|
-
}
|
|
57
|
-
);
|
|
58
|
-
var stderrOutput = "";
|
|
59
|
-
try {
|
|
60
|
-
console.info("connecting to the MCP server...");
|
|
61
|
-
const connectionPromise = client.connect(transport);
|
|
62
|
-
transport?.stderr?.on("data", (chunk) => {
|
|
63
|
-
stderrOutput += chunk.toString();
|
|
64
|
-
});
|
|
65
|
-
await connectionPromise;
|
|
66
|
-
console.info("connected to the MCP server");
|
|
67
|
-
} catch (error) {
|
|
68
|
-
console.error(`
|
|
69
|
-
could not connect to the MCP server
|
|
70
|
-
|
|
71
|
-
--- error ---
|
|
72
|
-
${String(error)}
|
|
73
|
-
|
|
74
|
-
--- stderr output ---
|
|
75
|
-
${stderrOutput}
|
|
76
|
-
`);
|
|
77
|
-
await setTimeout(1e3);
|
|
78
|
-
process.exit(1);
|
|
79
|
-
}
|
|
80
|
-
var serverVersion = client.getServerVersion();
|
|
81
|
-
var serverCapabilities = client.getServerCapabilities();
|
|
82
|
-
try {
|
|
83
|
-
console.info("starting the SSE server on port %d", argv.port);
|
|
84
|
-
await startSSEServer({
|
|
85
|
-
createServer: async () => {
|
|
86
|
-
const server = new Server(serverVersion, {
|
|
87
|
-
capabilities: serverCapabilities
|
|
88
|
-
});
|
|
89
|
-
proxyServer({
|
|
90
|
-
server,
|
|
91
|
-
client,
|
|
92
|
-
serverCapabilities
|
|
93
|
-
});
|
|
94
|
-
return server;
|
|
95
|
-
},
|
|
96
|
-
port: argv.port,
|
|
97
|
-
endpoint: argv.endpoint
|
|
98
|
-
});
|
|
99
|
-
} catch (error) {
|
|
100
|
-
console.error("could not start the SSE server", error);
|
|
101
|
-
await setTimeout(1e3);
|
|
102
|
-
process.exit(1);
|
|
103
|
-
}
|
|
104
|
-
//# sourceMappingURL=mcp-proxy.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/bin/mcp-proxy.ts"],"sourcesContent":["#!/usr/bin/env node\n\nimport yargs from \"yargs\";\nimport { hideBin } from \"yargs/helpers\";\nimport { StdioClientTransport } from \"@modelcontextprotocol/sdk/client/stdio.js\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { Server } from \"@modelcontextprotocol/sdk/server/index.js\";\nimport { proxyServer, startSSEServer } from \"../MCPProxy.js\";\nimport { EventSource } from \"eventsource\";\nimport { setTimeout } from \"node:timers/promises\";\n\nif (!(\"EventSource\" in global)) {\n // @ts-expect-error - figure out how to use --experimental-eventsource with vitest\n global.EventSource = EventSource;\n}\n\nconst argv = await yargs(hideBin(process.argv))\n .scriptName(\"mcp-proxy\")\n .command(\"$0 <command> [args...]\", \"Run a command with MCP arguments\")\n .positional(\"command\", {\n type: \"string\",\n describe: \"The command to run\",\n demandOption: true,\n })\n .positional(\"args\", {\n type: \"string\",\n array: true,\n describe: \"The arguments to pass to the command\",\n })\n .options({\n debug: {\n type: \"boolean\",\n describe: \"Enable debug logging\",\n default: false,\n },\n endpoint: {\n type: \"string\",\n describe: \"The endpoint to listen on for SSE\",\n default: \"/sse\",\n },\n port: {\n type: \"number\",\n describe: \"The port to listen on for SSE\",\n default: 8080,\n },\n })\n .help()\n .parseAsync();\n\nconst transport = new StdioClientTransport({\n command: argv.command,\n args: argv.args,\n env: process.env as Record<string, string>,\n stderr: 'pipe',\n});\n\nconst client = new Client(\n {\n name: \"mcp-proxy\",\n version: \"1.0.0\",\n },\n {\n capabilities: {},\n },\n);\n\n\nlet stderrOutput = '';\n\ntry {\n console.info('connecting to the MCP server...');\n\n const connectionPromise = client.connect(transport);\n\n transport?.stderr?.on('data', (chunk) => {\n stderrOutput += chunk.toString();\n });\n\n await connectionPromise;\n\n console.info('connected to the MCP server');\n} catch (error) {\n console.error(`\ncould not connect to the MCP server\n\n--- error ---\n${String(error)}\n\n--- stderr output ---\n${stderrOutput}\n`);\n\n await setTimeout(1000);\n\n process.exit(1);\n}\n\nconst serverVersion = client.getServerVersion() as {\n name: string;\n version: string;\n};\n\nconst serverCapabilities = client.getServerCapabilities() as {};\n\ntry {\n console.info('starting the SSE server on port %d', argv.port);\n\n await startSSEServer({\n createServer: async () => {\n const server = new Server(serverVersion, {\n capabilities: serverCapabilities,\n });\n \n proxyServer({\n server,\n client,\n serverCapabilities,\n });\n \n return server;\n },\n port: argv.port,\n endpoint: argv.endpoint as `/${string}`,\n });\n} catch (error) {\n console.error('could not start the SSE server', error);\n\n await setTimeout(1000);\n\n process.exit(1);\n}\n"],"mappings":";;;;;;;AAEA,OAAO,WAAW;AAClB,SAAS,eAAe;AACxB,SAAS,4BAA4B;AACrC,SAAS,cAAc;AACvB,SAAS,cAAc;AAEvB,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAE3B,IAAI,EAAE,iBAAiB,SAAS;AAE9B,SAAO,cAAc;AACvB;AAEA,IAAM,OAAO,MAAM,MAAM,QAAQ,QAAQ,IAAI,CAAC,EAC3C,WAAW,WAAW,EACtB,QAAQ,0BAA0B,kCAAkC,EACpE,WAAW,WAAW;AAAA,EACrB,MAAM;AAAA,EACN,UAAU;AAAA,EACV,cAAc;AAChB,CAAC,EACA,WAAW,QAAQ;AAAA,EAClB,MAAM;AAAA,EACN,OAAO;AAAA,EACP,UAAU;AACZ,CAAC,EACA,QAAQ;AAAA,EACP,OAAO;AAAA,IACL,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,EACX;AAAA,EACA,UAAU;AAAA,IACR,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,EACX;AAAA,EACA,MAAM;AAAA,IACJ,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,EACX;AACF,CAAC,EACA,KAAK,EACL,WAAW;AAEd,IAAM,YAAY,IAAI,qBAAqB;AAAA,EACzC,SAAS,KAAK;AAAA,EACd,MAAM,KAAK;AAAA,EACX,KAAK,QAAQ;AAAA,EACb,QAAQ;AACV,CAAC;AAED,IAAM,SAAS,IAAI;AAAA,EACjB;AAAA,IACE,MAAM;AAAA,IACN,SAAS;AAAA,EACX;AAAA,EACA;AAAA,IACE,cAAc,CAAC;AAAA,EACjB;AACF;AAGA,IAAI,eAAe;AAEnB,IAAI;AACF,UAAQ,KAAK,iCAAiC;AAE9C,QAAM,oBAAoB,OAAO,QAAQ,SAAS;AAElD,aAAW,QAAQ,GAAG,QAAQ,CAAC,UAAU;AACvC,oBAAgB,MAAM,SAAS;AAAA,EACjC,CAAC;AAED,QAAM;AAEN,UAAQ,KAAK,6BAA6B;AAC5C,SAAS,OAAO;AACd,UAAQ,MAAM;AAAA;AAAA;AAAA;AAAA,EAId,OAAO,KAAK,CAAC;AAAA;AAAA;AAAA,EAGb,YAAY;AAAA,CACb;AAEC,QAAM,WAAW,GAAI;AAErB,UAAQ,KAAK,CAAC;AAChB;AAEA,IAAM,gBAAgB,OAAO,iBAAiB;AAK9C,IAAM,qBAAqB,OAAO,sBAAsB;AAExD,IAAI;AACF,UAAQ,KAAK,sCAAsC,KAAK,IAAI;AAE5D,QAAM,eAAe;AAAA,IACnB,cAAc,YAAY;AACxB,YAAM,SAAS,IAAI,OAAO,eAAe;AAAA,QACvC,cAAc;AAAA,MAChB,CAAC;AAED,kBAAY;AAAA,QACV;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC;AAED,aAAO;AAAA,IACT;AAAA,IACA,MAAM,KAAK;AAAA,IACX,UAAU,KAAK;AAAA,EACjB,CAAC;AACH,SAAS,OAAO;AACd,UAAQ,MAAM,kCAAkC,KAAK;AAErD,QAAM,WAAW,GAAI;AAErB,UAAQ,KAAK,CAAC;AAChB;","names":[]}
|