@jadchene/mcp-gateway-service 0.5.0 → 0.6.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/README.md +59 -203
- package/README_zh.md +59 -203
- package/config.example.json +5 -6
- package/dist/config.js +14 -17
- package/dist/gateway-engine.js +7 -78
- package/dist/gateway-server.js +17 -33
- package/dist/http-server.js +36 -154
- package/dist/index.js +5 -3
- package/dist/mcp/client.js +61 -567
- package/dist/mcp/http-client.js +24 -473
- package/dist/mcp/sdk-client.js +270 -0
- package/dist/mcp/server.js +68 -0
- package/dist/mcp/versions.js +4 -0
- package/dist/service-registry.js +3 -2
- package/package.json +27 -16
- package/skills/mcp-gateway/SKILL.md +2 -0
- package/dist/mcp/protocol.js +0 -193
package/dist/http-server.js
CHANGED
|
@@ -1,30 +1,14 @@
|
|
|
1
1
|
import http from "node:http";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
2
|
+
import { createMcpHandler } from "@modelcontextprotocol/server";
|
|
3
|
+
import { hostHeaderValidation, originValidation, toNodeHandler } from "@modelcontextprotocol/node";
|
|
4
|
+
import { createGatewayMcpServer } from "./mcp/server.js";
|
|
5
5
|
/**
|
|
6
|
-
* Exposes the
|
|
6
|
+
* Exposes the gateway through SDK v2 Streamable HTTP.
|
|
7
7
|
*/
|
|
8
8
|
export class StreamableHttpGatewayServer {
|
|
9
|
-
/**
|
|
10
|
-
* Stores inbound HTTP behavior.
|
|
11
|
-
*/
|
|
12
9
|
config;
|
|
13
|
-
/**
|
|
14
|
-
* Stores the transport-neutral gateway engine.
|
|
15
|
-
*/
|
|
16
|
-
engine;
|
|
17
|
-
/**
|
|
18
|
-
* Stores the shared logger instance.
|
|
19
|
-
*/
|
|
20
10
|
logger;
|
|
21
|
-
|
|
22
|
-
* Stores active SSE sessions keyed by MCP session id.
|
|
23
|
-
*/
|
|
24
|
-
sessions = new Map();
|
|
25
|
-
/**
|
|
26
|
-
* Stores the underlying HTTP server instance.
|
|
27
|
-
*/
|
|
11
|
+
handler;
|
|
28
12
|
server = null;
|
|
29
13
|
/**
|
|
30
14
|
* Returns the current endpoint URL after the server starts.
|
|
@@ -35,27 +19,41 @@ export class StreamableHttpGatewayServer {
|
|
|
35
19
|
return `http://${this.config.host}:${port}${this.config.path}`;
|
|
36
20
|
}
|
|
37
21
|
/**
|
|
38
|
-
* Creates
|
|
22
|
+
* Creates an SDK HTTP handler that automatically serves both standard protocol revisions.
|
|
39
23
|
*/
|
|
40
24
|
constructor(config, engine, logger) {
|
|
41
25
|
this.config = config;
|
|
42
|
-
this.engine = engine;
|
|
43
26
|
this.logger = logger;
|
|
27
|
+
this.handler = createMcpHandler(() => createGatewayMcpServer(engine), {
|
|
28
|
+
legacy: "stateless",
|
|
29
|
+
onerror: (error) => {
|
|
30
|
+
this.logger.error("gateway.http_protocol_error", { message: error.message });
|
|
31
|
+
}
|
|
32
|
+
});
|
|
44
33
|
}
|
|
45
34
|
/**
|
|
46
|
-
* Starts
|
|
35
|
+
* Starts the guarded single-endpoint HTTP server.
|
|
47
36
|
*/
|
|
48
37
|
async start() {
|
|
49
38
|
if (this.server) {
|
|
50
39
|
return;
|
|
51
40
|
}
|
|
41
|
+
const allowedHosts = allowedHostnames(this.config.host);
|
|
42
|
+
const validateHost = hostHeaderValidation(allowedHosts);
|
|
43
|
+
const validateOrigin = originValidation(allowedHosts);
|
|
44
|
+
const handleMcp = toNodeHandler(this.handler, {
|
|
45
|
+
onerror: (error) => this.logger.error("gateway.http_adapter_error", { message: error.message })
|
|
46
|
+
});
|
|
52
47
|
this.server = http.createServer((request, response) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
const pathname = new URL(request.url ?? "/", "http://localhost").pathname;
|
|
49
|
+
if (pathname !== this.config.path) {
|
|
50
|
+
response.writeHead(404).end();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (!validateHost(request, response) || !validateOrigin(request, response)) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
void handleMcp(request, response);
|
|
59
57
|
});
|
|
60
58
|
await new Promise((resolveListen, rejectListen) => {
|
|
61
59
|
this.server?.once("error", rejectListen);
|
|
@@ -67,144 +65,28 @@ export class StreamableHttpGatewayServer {
|
|
|
67
65
|
this.logger.info("gateway.http_started", {
|
|
68
66
|
host: this.config.host,
|
|
69
67
|
port: this.config.port,
|
|
70
|
-
path: this.config.path
|
|
71
|
-
enableJsonResponse: this.config.enableJsonResponse
|
|
68
|
+
path: this.config.path
|
|
72
69
|
});
|
|
73
70
|
}
|
|
74
71
|
/**
|
|
75
|
-
* Stops
|
|
72
|
+
* Stops accepting requests and closes all in-flight SDK exchanges.
|
|
76
73
|
*/
|
|
77
74
|
async stop() {
|
|
78
|
-
|
|
79
|
-
response.end();
|
|
80
|
-
}
|
|
81
|
-
this.sessions.clear();
|
|
75
|
+
await this.handler.close();
|
|
82
76
|
const server = this.server;
|
|
83
77
|
this.server = null;
|
|
84
78
|
if (!server) {
|
|
85
79
|
return;
|
|
86
80
|
}
|
|
87
81
|
await new Promise((resolveClose, rejectClose) => {
|
|
88
|
-
server.close((error) =>
|
|
89
|
-
|
|
90
|
-
rejectClose(error);
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
resolveClose();
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Dispatches one HTTP request by method on the configured single endpoint.
|
|
99
|
-
*/
|
|
100
|
-
async handleRequest(request, response) {
|
|
101
|
-
const requestUrl = new URL(request.url ?? "/", `http://${request.headers.host ?? "localhost"}`);
|
|
102
|
-
if (requestUrl.pathname !== this.config.path) {
|
|
103
|
-
response.writeHead(404).end();
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
if (request.method === "GET") {
|
|
107
|
-
this.handleGet(request, response, requestUrl);
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
if (request.method === "POST") {
|
|
111
|
-
await this.handlePost(request, response, requestUrl);
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
response.writeHead(405, {
|
|
115
|
-
"Allow": "GET, POST"
|
|
116
|
-
}).end();
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Opens one SSE session for downstream messages.
|
|
120
|
-
*/
|
|
121
|
-
handleGet(request, response, requestUrl) {
|
|
122
|
-
const sessionId = getSessionId(request, requestUrl) ?? randomUUID();
|
|
123
|
-
this.sessions.set(sessionId, response);
|
|
124
|
-
response.writeHead(200, {
|
|
125
|
-
"Content-Type": "text/event-stream; charset=utf-8",
|
|
126
|
-
"Cache-Control": "no-cache",
|
|
127
|
-
"Connection": "keep-alive",
|
|
128
|
-
"Mcp-Session-Id": sessionId
|
|
82
|
+
server.close((error) => error ? rejectClose(error) : resolveClose());
|
|
83
|
+
server.closeAllConnections();
|
|
129
84
|
});
|
|
130
|
-
response.write(":\n\n");
|
|
131
|
-
writeSseEvent(response, "endpoint", this.config.path);
|
|
132
|
-
request.on("close", () => {
|
|
133
|
-
if (this.sessions.get(sessionId) === response) {
|
|
134
|
-
this.sessions.delete(sessionId);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Handles one JSON-RPC POST request.
|
|
140
|
-
*/
|
|
141
|
-
async handlePost(request, response, requestUrl) {
|
|
142
|
-
const message = await readJsonRpcMessage(request);
|
|
143
|
-
const result = await this.engine.handleMessage(message);
|
|
144
|
-
if (!result) {
|
|
145
|
-
response.writeHead(202).end();
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
const sessionId = getSessionId(request, requestUrl);
|
|
149
|
-
const session = sessionId ? this.sessions.get(sessionId) : null;
|
|
150
|
-
if (!this.config.enableJsonResponse && session) {
|
|
151
|
-
writeSseEvent(session, "message", JSON.stringify(result));
|
|
152
|
-
response.writeHead(202, {
|
|
153
|
-
"Content-Type": "application/json"
|
|
154
|
-
}).end(JSON.stringify({ accepted: true }));
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
writeJson(response, 200, result);
|
|
158
85
|
}
|
|
159
86
|
}
|
|
160
87
|
/**
|
|
161
|
-
*
|
|
88
|
+
* Builds the Host and Origin allow-list for the configured bind address.
|
|
162
89
|
*/
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
let totalBytes = 0;
|
|
166
|
-
for await (const chunk of request) {
|
|
167
|
-
const buffer = typeof chunk === "string" ? Buffer.from(chunk, "utf8") : chunk;
|
|
168
|
-
totalBytes += buffer.byteLength;
|
|
169
|
-
if (totalBytes > MAX_POST_BODY_BYTES) {
|
|
170
|
-
throw new Error(`HTTP request body exceeded ${MAX_POST_BODY_BYTES} bytes.`);
|
|
171
|
-
}
|
|
172
|
-
chunks.push(buffer);
|
|
173
|
-
}
|
|
174
|
-
const text = Buffer.concat(chunks).toString("utf8");
|
|
175
|
-
return JSON.parse(text);
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Extracts the MCP session id from header or query string.
|
|
179
|
-
*/
|
|
180
|
-
function getSessionId(request, requestUrl) {
|
|
181
|
-
const header = request.headers["mcp-session-id"];
|
|
182
|
-
if (typeof header === "string" && header.trim() !== "") {
|
|
183
|
-
return header;
|
|
184
|
-
}
|
|
185
|
-
const query = requestUrl.searchParams.get("sessionId");
|
|
186
|
-
return query && query.trim() !== "" ? query : null;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Writes one JSON response.
|
|
190
|
-
*/
|
|
191
|
-
function writeJson(response, statusCode, payload) {
|
|
192
|
-
if (response.headersSent) {
|
|
193
|
-
response.end();
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
response.writeHead(statusCode, {
|
|
197
|
-
"Content-Type": "application/json; charset=utf-8"
|
|
198
|
-
});
|
|
199
|
-
response.end(JSON.stringify(payload));
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Writes one Server-Sent Event.
|
|
203
|
-
*/
|
|
204
|
-
function writeSseEvent(response, eventName, data) {
|
|
205
|
-
response.write(`event: ${eventName}\n`);
|
|
206
|
-
for (const line of data.split(/\r?\n/)) {
|
|
207
|
-
response.write(`data: ${line}\n`);
|
|
208
|
-
}
|
|
209
|
-
response.write("\n");
|
|
90
|
+
function allowedHostnames(host) {
|
|
91
|
+
return [...new Set([host, "localhost", "127.0.0.1", "[::1]"])];
|
|
210
92
|
}
|
package/dist/index.js
CHANGED
|
@@ -89,6 +89,7 @@ class Application {
|
|
|
89
89
|
this.logger.info("gateway.stopping", { signal });
|
|
90
90
|
this.watcher.stop();
|
|
91
91
|
await this.httpServer?.stop();
|
|
92
|
+
await this.server.stop();
|
|
92
93
|
await this.registry.dispose();
|
|
93
94
|
process.exit(0);
|
|
94
95
|
};
|
|
@@ -114,12 +115,14 @@ if (import.meta.main) {
|
|
|
114
115
|
});
|
|
115
116
|
}
|
|
116
117
|
export function parseCliArgs(args) {
|
|
118
|
+
if (args.includes("--protocol-mode")) {
|
|
119
|
+
throw new Error("--protocol-mode is no longer supported; protocol negotiation is automatic.");
|
|
120
|
+
}
|
|
117
121
|
const configPath = readOption(args, "--config") ?? process.env.MCP_GATEWAY_CONFIG ?? "config.json";
|
|
118
122
|
const port = readIntegerOption(args, "--port");
|
|
119
123
|
const host = readOption(args, "--host");
|
|
120
124
|
const path = readHttpPathOption(args);
|
|
121
125
|
const enableHttp = args.includes("--http");
|
|
122
|
-
const enableJsonResponse = args.includes("--json-response");
|
|
123
126
|
return {
|
|
124
127
|
configPath,
|
|
125
128
|
server: enableHttp
|
|
@@ -127,8 +130,7 @@ export function parseCliArgs(args) {
|
|
|
127
130
|
enable: true,
|
|
128
131
|
host: host ?? "127.0.0.1",
|
|
129
132
|
port: port ?? 3000,
|
|
130
|
-
path: path ?? "/mcp"
|
|
131
|
-
enableJsonResponse
|
|
133
|
+
path: path ?? "/mcp"
|
|
132
134
|
}
|
|
133
135
|
: undefined
|
|
134
136
|
};
|