@jadchene/mcp-gateway-service 0.7.0 → 0.7.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/http-server.js +97 -4
- package/dist/mcp/server.js +18 -1
- package/package.json +1 -1
package/dist/http-server.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import http from "node:http";
|
|
2
|
-
import { timingSafeEqual } from "node:crypto";
|
|
3
|
-
import { createMcpHandler } from "@modelcontextprotocol/server";
|
|
2
|
+
import { randomUUID, timingSafeEqual } from "node:crypto";
|
|
3
|
+
import { createMcpHandler, isLegacyRequest, WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/server";
|
|
4
4
|
import { hostHeaderValidation, originValidation, toNodeHandler } from "@modelcontextprotocol/node";
|
|
5
5
|
import { createGatewayMcpServer } from "./mcp/server.js";
|
|
6
|
+
const LEGACY_SESSION_IDLE_TIMEOUT_MS = 30 * 60 * 1_000;
|
|
7
|
+
const LEGACY_SESSION_SWEEP_INTERVAL_MS = 60 * 1_000;
|
|
6
8
|
/**
|
|
7
9
|
* Exposes the gateway through SDK v2 Streamable HTTP.
|
|
8
10
|
*/
|
|
9
11
|
export class StreamableHttpGatewayServer {
|
|
10
12
|
config;
|
|
13
|
+
engine;
|
|
11
14
|
logger;
|
|
12
15
|
handler;
|
|
16
|
+
legacySessions = new Map();
|
|
17
|
+
legacySessionSweepTimer = null;
|
|
13
18
|
server = null;
|
|
14
19
|
/**
|
|
15
20
|
* Returns the current endpoint URL after the server starts.
|
|
@@ -24,11 +29,12 @@ export class StreamableHttpGatewayServer {
|
|
|
24
29
|
*/
|
|
25
30
|
constructor(config, engine, logger) {
|
|
26
31
|
this.config = config;
|
|
32
|
+
this.engine = engine;
|
|
27
33
|
this.logger = logger;
|
|
28
34
|
this.handler = createMcpHandler(() => createGatewayMcpServer(engine, {
|
|
29
35
|
includeAdminTools: config.enableAdminTools ?? false
|
|
30
36
|
}), {
|
|
31
|
-
legacy: "
|
|
37
|
+
legacy: "reject",
|
|
32
38
|
onerror: (error) => {
|
|
33
39
|
this.logger.error("gateway.http_protocol_error", { message: error.message });
|
|
34
40
|
}
|
|
@@ -50,7 +56,9 @@ export class StreamableHttpGatewayServer {
|
|
|
50
56
|
const allowedHosts = allowedHostnames(this.config.host);
|
|
51
57
|
const validateHost = hostHeaderValidation(allowedHosts);
|
|
52
58
|
const validateOrigin = originValidation(allowedHosts);
|
|
53
|
-
const handleMcp = toNodeHandler(
|
|
59
|
+
const handleMcp = toNodeHandler({
|
|
60
|
+
fetch: (request) => this.handleMcpRequest(request)
|
|
61
|
+
}, {
|
|
54
62
|
onerror: (error) => this.logger.error("gateway.http_adapter_error", { message: error.message })
|
|
55
63
|
});
|
|
56
64
|
const maxConcurrentRequests = this.config.maxConcurrentRequests ?? 64;
|
|
@@ -91,6 +99,8 @@ export class StreamableHttpGatewayServer {
|
|
|
91
99
|
resolveListen();
|
|
92
100
|
});
|
|
93
101
|
});
|
|
102
|
+
this.legacySessionSweepTimer = setInterval(() => void this.closeExpiredLegacySessions(), LEGACY_SESSION_SWEEP_INTERVAL_MS);
|
|
103
|
+
this.legacySessionSweepTimer.unref();
|
|
94
104
|
this.logger.info("gateway.http_started", {
|
|
95
105
|
host: this.config.host,
|
|
96
106
|
port: this.config.port,
|
|
@@ -101,6 +111,12 @@ export class StreamableHttpGatewayServer {
|
|
|
101
111
|
* Stops accepting requests and closes all in-flight SDK exchanges.
|
|
102
112
|
*/
|
|
103
113
|
async stop() {
|
|
114
|
+
if (this.legacySessionSweepTimer) {
|
|
115
|
+
clearInterval(this.legacySessionSweepTimer);
|
|
116
|
+
this.legacySessionSweepTimer = null;
|
|
117
|
+
}
|
|
118
|
+
await Promise.all([...this.legacySessions.values()].map((session) => session.server.close().catch(() => undefined)));
|
|
119
|
+
this.legacySessions.clear();
|
|
104
120
|
await this.handler.close();
|
|
105
121
|
const server = this.server;
|
|
106
122
|
this.server = null;
|
|
@@ -112,6 +128,83 @@ export class StreamableHttpGatewayServer {
|
|
|
112
128
|
server.closeAllConnections();
|
|
113
129
|
});
|
|
114
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Routes modern requests to the per-request handler and preserves 2025 sessions.
|
|
133
|
+
*/
|
|
134
|
+
async handleMcpRequest(request) {
|
|
135
|
+
if (!(await isLegacyRequest(request))) {
|
|
136
|
+
return this.handler.fetch(request);
|
|
137
|
+
}
|
|
138
|
+
const sessionId = request.headers.get("mcp-session-id");
|
|
139
|
+
if (sessionId) {
|
|
140
|
+
const session = this.legacySessions.get(sessionId);
|
|
141
|
+
if (!session) {
|
|
142
|
+
return sessionNotFoundResponse();
|
|
143
|
+
}
|
|
144
|
+
session.lastAccessedAt = Date.now();
|
|
145
|
+
return session.transport.handleRequest(request);
|
|
146
|
+
}
|
|
147
|
+
return this.handleLegacyInitialization(request);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Creates a stateful 2025 transport for an initialize request.
|
|
151
|
+
*/
|
|
152
|
+
async handleLegacyInitialization(request) {
|
|
153
|
+
let session;
|
|
154
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
155
|
+
sessionIdGenerator: randomUUID,
|
|
156
|
+
onsessioninitialized: (sessionId) => {
|
|
157
|
+
this.legacySessions.set(sessionId, session);
|
|
158
|
+
},
|
|
159
|
+
onsessionclosed: (sessionId) => {
|
|
160
|
+
this.legacySessions.delete(sessionId);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
const server = createGatewayMcpServer(this.engine, {
|
|
164
|
+
includeAdminTools: this.config.enableAdminTools ?? false
|
|
165
|
+
});
|
|
166
|
+
session = { server, transport, lastAccessedAt: Date.now() };
|
|
167
|
+
await server.connect(transport);
|
|
168
|
+
try {
|
|
169
|
+
const response = await transport.handleRequest(request);
|
|
170
|
+
if (!transport.sessionId) {
|
|
171
|
+
await server.close().catch(() => undefined);
|
|
172
|
+
}
|
|
173
|
+
return response;
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
if (transport.sessionId) {
|
|
177
|
+
this.legacySessions.delete(transport.sessionId);
|
|
178
|
+
}
|
|
179
|
+
await server.close().catch(() => undefined);
|
|
180
|
+
throw error;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Releases abandoned legacy sessions without touching active modern requests.
|
|
185
|
+
*/
|
|
186
|
+
async closeExpiredLegacySessions() {
|
|
187
|
+
const expiresBefore = Date.now() - LEGACY_SESSION_IDLE_TIMEOUT_MS;
|
|
188
|
+
const expired = [...this.legacySessions.entries()]
|
|
189
|
+
.filter(([, session]) => session.lastAccessedAt < expiresBefore);
|
|
190
|
+
for (const [sessionId, session] of expired) {
|
|
191
|
+
this.legacySessions.delete(sessionId);
|
|
192
|
+
await session.server.close().catch(() => undefined);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Returns the standard status for a missing or expired MCP session.
|
|
198
|
+
*/
|
|
199
|
+
function sessionNotFoundResponse() {
|
|
200
|
+
return Response.json({
|
|
201
|
+
jsonrpc: "2.0",
|
|
202
|
+
error: {
|
|
203
|
+
code: -32001,
|
|
204
|
+
message: "Session not found"
|
|
205
|
+
},
|
|
206
|
+
id: null
|
|
207
|
+
}, { status: 404 });
|
|
115
208
|
}
|
|
116
209
|
/**
|
|
117
210
|
* Compares a supplied bearer credential without leaking a useful timing signal.
|
package/dist/mcp/server.js
CHANGED
|
@@ -53,6 +53,9 @@ function toDownstreamContext(context, mcp2025ClientCapabilities) {
|
|
|
53
53
|
const clientCapabilities = isRecord(envelopeCapabilities)
|
|
54
54
|
? envelopeCapabilities
|
|
55
55
|
: mcp2025ClientCapabilities;
|
|
56
|
+
if (!context.mcpReq.envelope) {
|
|
57
|
+
normalizeLegacyFormElicitationCapability(clientCapabilities);
|
|
58
|
+
}
|
|
56
59
|
return {
|
|
57
60
|
signal: context.mcpReq.signal,
|
|
58
61
|
...(clientCapabilities ? { clientCapabilities } : {}),
|
|
@@ -60,11 +63,25 @@ function toDownstreamContext(context, mcp2025ClientCapabilities) {
|
|
|
60
63
|
...(context.mcpReq.requestState() ? { requestState: context.mcpReq.requestState() } : {}),
|
|
61
64
|
...(!context.mcpReq.envelope
|
|
62
65
|
? {
|
|
63
|
-
elicitForm: (params, signal) => context.mcpReq.elicitInput(params, {
|
|
66
|
+
elicitForm: (params, signal) => context.mcpReq.elicitInput(params, {
|
|
67
|
+
signal,
|
|
68
|
+
relatedRequestId: context.mcpReq.id
|
|
69
|
+
})
|
|
64
70
|
}
|
|
65
71
|
: {})
|
|
66
72
|
};
|
|
67
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* MCP 2025 clients may advertise form elicitation as the legacy shorthand
|
|
76
|
+
* `elicitation: {}`. The SDK's request helper expects the later explicit
|
|
77
|
+
* `elicitation.form` shape, so normalize the negotiated capability in place.
|
|
78
|
+
*/
|
|
79
|
+
export function normalizeLegacyFormElicitationCapability(clientCapabilities) {
|
|
80
|
+
const elicitation = clientCapabilities?.elicitation;
|
|
81
|
+
if (isRecord(elicitation) && !("form" in elicitation)) {
|
|
82
|
+
elicitation.form = {};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
68
85
|
/**
|
|
69
86
|
* Checks whether a value is a plain record.
|
|
70
87
|
*/
|
package/package.json
CHANGED