@openfinance-sh/mcp 0.1.4 → 0.1.5
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/build/chunk-B6UCQHRZ.js +6108 -0
- package/build/index.js +14878 -30
- package/build/streamableHttp-2SBIBVHE.js +1230 -0
- package/manifest.json +1 -1
- package/openfinance.mcpb +0 -0
- package/package.json +2 -2
|
@@ -0,0 +1,1230 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_NEGOTIATED_PROTOCOL_VERSION,
|
|
3
|
+
JSONRPCMessageSchema,
|
|
4
|
+
SUPPORTED_PROTOCOL_VERSIONS,
|
|
5
|
+
isInitializeRequest,
|
|
6
|
+
isJSONRPCErrorResponse,
|
|
7
|
+
isJSONRPCRequest,
|
|
8
|
+
isJSONRPCResultResponse
|
|
9
|
+
} from "./chunk-B6UCQHRZ.js";
|
|
10
|
+
|
|
11
|
+
// ../node_modules/.pnpm/@hono+node-server@1.19.9_hono@4.11.9/node_modules/@hono/node-server/dist/index.mjs
|
|
12
|
+
import { Http2ServerRequest as Http2ServerRequest2 } from "http2";
|
|
13
|
+
import { Http2ServerRequest } from "http2";
|
|
14
|
+
import { Readable } from "stream";
|
|
15
|
+
import crypto2 from "crypto";
|
|
16
|
+
var RequestError = class extends Error {
|
|
17
|
+
constructor(message, options) {
|
|
18
|
+
super(message, options);
|
|
19
|
+
this.name = "RequestError";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var toRequestError = (e) => {
|
|
23
|
+
if (e instanceof RequestError) {
|
|
24
|
+
return e;
|
|
25
|
+
}
|
|
26
|
+
return new RequestError(e.message, { cause: e });
|
|
27
|
+
};
|
|
28
|
+
var GlobalRequest = global.Request;
|
|
29
|
+
var Request = class extends GlobalRequest {
|
|
30
|
+
constructor(input, options) {
|
|
31
|
+
if (typeof input === "object" && getRequestCache in input) {
|
|
32
|
+
input = input[getRequestCache]();
|
|
33
|
+
}
|
|
34
|
+
if (typeof options?.body?.getReader !== "undefined") {
|
|
35
|
+
;
|
|
36
|
+
options.duplex ??= "half";
|
|
37
|
+
}
|
|
38
|
+
super(input, options);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var newHeadersFromIncoming = (incoming) => {
|
|
42
|
+
const headerRecord = [];
|
|
43
|
+
const rawHeaders = incoming.rawHeaders;
|
|
44
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
|
45
|
+
const { [i]: key, [i + 1]: value } = rawHeaders;
|
|
46
|
+
if (key.charCodeAt(0) !== /*:*/
|
|
47
|
+
58) {
|
|
48
|
+
headerRecord.push([key, value]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return new Headers(headerRecord);
|
|
52
|
+
};
|
|
53
|
+
var wrapBodyStream = /* @__PURE__ */ Symbol("wrapBodyStream");
|
|
54
|
+
var newRequestFromIncoming = (method, url, headers, incoming, abortController) => {
|
|
55
|
+
const init = {
|
|
56
|
+
method,
|
|
57
|
+
headers,
|
|
58
|
+
signal: abortController.signal
|
|
59
|
+
};
|
|
60
|
+
if (method === "TRACE") {
|
|
61
|
+
init.method = "GET";
|
|
62
|
+
const req = new Request(url, init);
|
|
63
|
+
Object.defineProperty(req, "method", {
|
|
64
|
+
get() {
|
|
65
|
+
return "TRACE";
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return req;
|
|
69
|
+
}
|
|
70
|
+
if (!(method === "GET" || method === "HEAD")) {
|
|
71
|
+
if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) {
|
|
72
|
+
init.body = new ReadableStream({
|
|
73
|
+
start(controller) {
|
|
74
|
+
controller.enqueue(incoming.rawBody);
|
|
75
|
+
controller.close();
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
} else if (incoming[wrapBodyStream]) {
|
|
79
|
+
let reader;
|
|
80
|
+
init.body = new ReadableStream({
|
|
81
|
+
async pull(controller) {
|
|
82
|
+
try {
|
|
83
|
+
reader ||= Readable.toWeb(incoming).getReader();
|
|
84
|
+
const { done, value } = await reader.read();
|
|
85
|
+
if (done) {
|
|
86
|
+
controller.close();
|
|
87
|
+
} else {
|
|
88
|
+
controller.enqueue(value);
|
|
89
|
+
}
|
|
90
|
+
} catch (error) {
|
|
91
|
+
controller.error(error);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
} else {
|
|
96
|
+
init.body = Readable.toWeb(incoming);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return new Request(url, init);
|
|
100
|
+
};
|
|
101
|
+
var getRequestCache = /* @__PURE__ */ Symbol("getRequestCache");
|
|
102
|
+
var requestCache = /* @__PURE__ */ Symbol("requestCache");
|
|
103
|
+
var incomingKey = /* @__PURE__ */ Symbol("incomingKey");
|
|
104
|
+
var urlKey = /* @__PURE__ */ Symbol("urlKey");
|
|
105
|
+
var headersKey = /* @__PURE__ */ Symbol("headersKey");
|
|
106
|
+
var abortControllerKey = /* @__PURE__ */ Symbol("abortControllerKey");
|
|
107
|
+
var getAbortController = /* @__PURE__ */ Symbol("getAbortController");
|
|
108
|
+
var requestPrototype = {
|
|
109
|
+
get method() {
|
|
110
|
+
return this[incomingKey].method || "GET";
|
|
111
|
+
},
|
|
112
|
+
get url() {
|
|
113
|
+
return this[urlKey];
|
|
114
|
+
},
|
|
115
|
+
get headers() {
|
|
116
|
+
return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]);
|
|
117
|
+
},
|
|
118
|
+
[getAbortController]() {
|
|
119
|
+
this[getRequestCache]();
|
|
120
|
+
return this[abortControllerKey];
|
|
121
|
+
},
|
|
122
|
+
[getRequestCache]() {
|
|
123
|
+
this[abortControllerKey] ||= new AbortController();
|
|
124
|
+
return this[requestCache] ||= newRequestFromIncoming(
|
|
125
|
+
this.method,
|
|
126
|
+
this[urlKey],
|
|
127
|
+
this.headers,
|
|
128
|
+
this[incomingKey],
|
|
129
|
+
this[abortControllerKey]
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
[
|
|
134
|
+
"body",
|
|
135
|
+
"bodyUsed",
|
|
136
|
+
"cache",
|
|
137
|
+
"credentials",
|
|
138
|
+
"destination",
|
|
139
|
+
"integrity",
|
|
140
|
+
"mode",
|
|
141
|
+
"redirect",
|
|
142
|
+
"referrer",
|
|
143
|
+
"referrerPolicy",
|
|
144
|
+
"signal",
|
|
145
|
+
"keepalive"
|
|
146
|
+
].forEach((k) => {
|
|
147
|
+
Object.defineProperty(requestPrototype, k, {
|
|
148
|
+
get() {
|
|
149
|
+
return this[getRequestCache]()[k];
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
154
|
+
Object.defineProperty(requestPrototype, k, {
|
|
155
|
+
value: function() {
|
|
156
|
+
return this[getRequestCache]()[k]();
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
Object.setPrototypeOf(requestPrototype, Request.prototype);
|
|
161
|
+
var newRequest = (incoming, defaultHostname) => {
|
|
162
|
+
const req = Object.create(requestPrototype);
|
|
163
|
+
req[incomingKey] = incoming;
|
|
164
|
+
const incomingUrl = incoming.url || "";
|
|
165
|
+
if (incomingUrl[0] !== "/" && // short-circuit for performance. most requests are relative URL.
|
|
166
|
+
(incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))) {
|
|
167
|
+
if (incoming instanceof Http2ServerRequest) {
|
|
168
|
+
throw new RequestError("Absolute URL for :path is not allowed in HTTP/2");
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
const url2 = new URL(incomingUrl);
|
|
172
|
+
req[urlKey] = url2.href;
|
|
173
|
+
} catch (e) {
|
|
174
|
+
throw new RequestError("Invalid absolute URL", { cause: e });
|
|
175
|
+
}
|
|
176
|
+
return req;
|
|
177
|
+
}
|
|
178
|
+
const host = (incoming instanceof Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
|
|
179
|
+
if (!host) {
|
|
180
|
+
throw new RequestError("Missing host header");
|
|
181
|
+
}
|
|
182
|
+
let scheme;
|
|
183
|
+
if (incoming instanceof Http2ServerRequest) {
|
|
184
|
+
scheme = incoming.scheme;
|
|
185
|
+
if (!(scheme === "http" || scheme === "https")) {
|
|
186
|
+
throw new RequestError("Unsupported scheme");
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
scheme = incoming.socket && incoming.socket.encrypted ? "https" : "http";
|
|
190
|
+
}
|
|
191
|
+
const url = new URL(`${scheme}://${host}${incomingUrl}`);
|
|
192
|
+
if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
|
|
193
|
+
throw new RequestError("Invalid host header");
|
|
194
|
+
}
|
|
195
|
+
req[urlKey] = url.href;
|
|
196
|
+
return req;
|
|
197
|
+
};
|
|
198
|
+
var responseCache = /* @__PURE__ */ Symbol("responseCache");
|
|
199
|
+
var getResponseCache = /* @__PURE__ */ Symbol("getResponseCache");
|
|
200
|
+
var cacheKey = /* @__PURE__ */ Symbol("cache");
|
|
201
|
+
var GlobalResponse = global.Response;
|
|
202
|
+
var Response2 = class _Response {
|
|
203
|
+
#body;
|
|
204
|
+
#init;
|
|
205
|
+
[getResponseCache]() {
|
|
206
|
+
delete this[cacheKey];
|
|
207
|
+
return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
|
|
208
|
+
}
|
|
209
|
+
constructor(body, init) {
|
|
210
|
+
let headers;
|
|
211
|
+
this.#body = body;
|
|
212
|
+
if (init instanceof _Response) {
|
|
213
|
+
const cachedGlobalResponse = init[responseCache];
|
|
214
|
+
if (cachedGlobalResponse) {
|
|
215
|
+
this.#init = cachedGlobalResponse;
|
|
216
|
+
this[getResponseCache]();
|
|
217
|
+
return;
|
|
218
|
+
} else {
|
|
219
|
+
this.#init = init.#init;
|
|
220
|
+
headers = new Headers(init.#init.headers);
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
this.#init = init;
|
|
224
|
+
}
|
|
225
|
+
if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
|
|
226
|
+
headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
|
|
227
|
+
this[cacheKey] = [init?.status || 200, body, headers];
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
get headers() {
|
|
231
|
+
const cache = this[cacheKey];
|
|
232
|
+
if (cache) {
|
|
233
|
+
if (!(cache[2] instanceof Headers)) {
|
|
234
|
+
cache[2] = new Headers(cache[2]);
|
|
235
|
+
}
|
|
236
|
+
return cache[2];
|
|
237
|
+
}
|
|
238
|
+
return this[getResponseCache]().headers;
|
|
239
|
+
}
|
|
240
|
+
get status() {
|
|
241
|
+
return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
|
|
242
|
+
}
|
|
243
|
+
get ok() {
|
|
244
|
+
const status = this.status;
|
|
245
|
+
return status >= 200 && status < 300;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k) => {
|
|
249
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
250
|
+
get() {
|
|
251
|
+
return this[getResponseCache]()[k];
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k) => {
|
|
256
|
+
Object.defineProperty(Response2.prototype, k, {
|
|
257
|
+
value: function() {
|
|
258
|
+
return this[getResponseCache]()[k]();
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
Object.setPrototypeOf(Response2, GlobalResponse);
|
|
263
|
+
Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
|
|
264
|
+
async function readWithoutBlocking(readPromise) {
|
|
265
|
+
return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]);
|
|
266
|
+
}
|
|
267
|
+
function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) {
|
|
268
|
+
const cancel = (error) => {
|
|
269
|
+
reader.cancel(error).catch(() => {
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
writable.on("close", cancel);
|
|
273
|
+
writable.on("error", cancel);
|
|
274
|
+
(currentReadPromise ?? reader.read()).then(flow, handleStreamError);
|
|
275
|
+
return reader.closed.finally(() => {
|
|
276
|
+
writable.off("close", cancel);
|
|
277
|
+
writable.off("error", cancel);
|
|
278
|
+
});
|
|
279
|
+
function handleStreamError(error) {
|
|
280
|
+
if (error) {
|
|
281
|
+
writable.destroy(error);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
function onDrain() {
|
|
285
|
+
reader.read().then(flow, handleStreamError);
|
|
286
|
+
}
|
|
287
|
+
function flow({ done, value }) {
|
|
288
|
+
try {
|
|
289
|
+
if (done) {
|
|
290
|
+
writable.end();
|
|
291
|
+
} else if (!writable.write(value)) {
|
|
292
|
+
writable.once("drain", onDrain);
|
|
293
|
+
} else {
|
|
294
|
+
return reader.read().then(flow, handleStreamError);
|
|
295
|
+
}
|
|
296
|
+
} catch (e) {
|
|
297
|
+
handleStreamError(e);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
function writeFromReadableStream(stream, writable) {
|
|
302
|
+
if (stream.locked) {
|
|
303
|
+
throw new TypeError("ReadableStream is locked.");
|
|
304
|
+
} else if (writable.destroyed) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
return writeFromReadableStreamDefaultReader(stream.getReader(), writable);
|
|
308
|
+
}
|
|
309
|
+
var buildOutgoingHttpHeaders = (headers) => {
|
|
310
|
+
const res = {};
|
|
311
|
+
if (!(headers instanceof Headers)) {
|
|
312
|
+
headers = new Headers(headers ?? void 0);
|
|
313
|
+
}
|
|
314
|
+
const cookies = [];
|
|
315
|
+
for (const [k, v] of headers) {
|
|
316
|
+
if (k === "set-cookie") {
|
|
317
|
+
cookies.push(v);
|
|
318
|
+
} else {
|
|
319
|
+
res[k] = v;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (cookies.length > 0) {
|
|
323
|
+
res["set-cookie"] = cookies;
|
|
324
|
+
}
|
|
325
|
+
res["content-type"] ??= "text/plain; charset=UTF-8";
|
|
326
|
+
return res;
|
|
327
|
+
};
|
|
328
|
+
var X_ALREADY_SENT = "x-hono-already-sent";
|
|
329
|
+
if (typeof global.crypto === "undefined") {
|
|
330
|
+
global.crypto = crypto2;
|
|
331
|
+
}
|
|
332
|
+
var outgoingEnded = /* @__PURE__ */ Symbol("outgoingEnded");
|
|
333
|
+
var handleRequestError = () => new Response(null, {
|
|
334
|
+
status: 400
|
|
335
|
+
});
|
|
336
|
+
var handleFetchError = (e) => new Response(null, {
|
|
337
|
+
status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
|
|
338
|
+
});
|
|
339
|
+
var handleResponseError = (e, outgoing) => {
|
|
340
|
+
const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
|
|
341
|
+
if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
|
|
342
|
+
console.info("The user aborted a request.");
|
|
343
|
+
} else {
|
|
344
|
+
console.error(e);
|
|
345
|
+
if (!outgoing.headersSent) {
|
|
346
|
+
outgoing.writeHead(500, { "Content-Type": "text/plain" });
|
|
347
|
+
}
|
|
348
|
+
outgoing.end(`Error: ${err.message}`);
|
|
349
|
+
outgoing.destroy(err);
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
var flushHeaders = (outgoing) => {
|
|
353
|
+
if ("flushHeaders" in outgoing && outgoing.writable) {
|
|
354
|
+
outgoing.flushHeaders();
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
var responseViaCache = async (res, outgoing) => {
|
|
358
|
+
let [status, body, header] = res[cacheKey];
|
|
359
|
+
if (header instanceof Headers) {
|
|
360
|
+
header = buildOutgoingHttpHeaders(header);
|
|
361
|
+
}
|
|
362
|
+
if (typeof body === "string") {
|
|
363
|
+
header["Content-Length"] = Buffer.byteLength(body);
|
|
364
|
+
} else if (body instanceof Uint8Array) {
|
|
365
|
+
header["Content-Length"] = body.byteLength;
|
|
366
|
+
} else if (body instanceof Blob) {
|
|
367
|
+
header["Content-Length"] = body.size;
|
|
368
|
+
}
|
|
369
|
+
outgoing.writeHead(status, header);
|
|
370
|
+
if (typeof body === "string" || body instanceof Uint8Array) {
|
|
371
|
+
outgoing.end(body);
|
|
372
|
+
} else if (body instanceof Blob) {
|
|
373
|
+
outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
374
|
+
} else {
|
|
375
|
+
flushHeaders(outgoing);
|
|
376
|
+
await writeFromReadableStream(body, outgoing)?.catch(
|
|
377
|
+
(e) => handleResponseError(e, outgoing)
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
;
|
|
381
|
+
outgoing[outgoingEnded]?.();
|
|
382
|
+
};
|
|
383
|
+
var isPromise = (res) => typeof res.then === "function";
|
|
384
|
+
var responseViaResponseObject = async (res, outgoing, options = {}) => {
|
|
385
|
+
if (isPromise(res)) {
|
|
386
|
+
if (options.errorHandler) {
|
|
387
|
+
try {
|
|
388
|
+
res = await res;
|
|
389
|
+
} catch (err) {
|
|
390
|
+
const errRes = await options.errorHandler(err);
|
|
391
|
+
if (!errRes) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
res = errRes;
|
|
395
|
+
}
|
|
396
|
+
} else {
|
|
397
|
+
res = await res.catch(handleFetchError);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
if (cacheKey in res) {
|
|
401
|
+
return responseViaCache(res, outgoing);
|
|
402
|
+
}
|
|
403
|
+
const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
|
|
404
|
+
if (res.body) {
|
|
405
|
+
const reader = res.body.getReader();
|
|
406
|
+
const values = [];
|
|
407
|
+
let done = false;
|
|
408
|
+
let currentReadPromise = void 0;
|
|
409
|
+
if (resHeaderRecord["transfer-encoding"] !== "chunked") {
|
|
410
|
+
let maxReadCount = 2;
|
|
411
|
+
for (let i = 0; i < maxReadCount; i++) {
|
|
412
|
+
currentReadPromise ||= reader.read();
|
|
413
|
+
const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => {
|
|
414
|
+
console.error(e);
|
|
415
|
+
done = true;
|
|
416
|
+
});
|
|
417
|
+
if (!chunk) {
|
|
418
|
+
if (i === 1) {
|
|
419
|
+
await new Promise((resolve) => setTimeout(resolve));
|
|
420
|
+
maxReadCount = 3;
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
currentReadPromise = void 0;
|
|
426
|
+
if (chunk.value) {
|
|
427
|
+
values.push(chunk.value);
|
|
428
|
+
}
|
|
429
|
+
if (chunk.done) {
|
|
430
|
+
done = true;
|
|
431
|
+
break;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (done && !("content-length" in resHeaderRecord)) {
|
|
435
|
+
resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
439
|
+
values.forEach((value) => {
|
|
440
|
+
;
|
|
441
|
+
outgoing.write(value);
|
|
442
|
+
});
|
|
443
|
+
if (done) {
|
|
444
|
+
outgoing.end();
|
|
445
|
+
} else {
|
|
446
|
+
if (values.length === 0) {
|
|
447
|
+
flushHeaders(outgoing);
|
|
448
|
+
}
|
|
449
|
+
await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise);
|
|
450
|
+
}
|
|
451
|
+
} else if (resHeaderRecord[X_ALREADY_SENT]) {
|
|
452
|
+
} else {
|
|
453
|
+
outgoing.writeHead(res.status, resHeaderRecord);
|
|
454
|
+
outgoing.end();
|
|
455
|
+
}
|
|
456
|
+
;
|
|
457
|
+
outgoing[outgoingEnded]?.();
|
|
458
|
+
};
|
|
459
|
+
var getRequestListener = (fetchCallback, options = {}) => {
|
|
460
|
+
const autoCleanupIncoming = options.autoCleanupIncoming ?? true;
|
|
461
|
+
if (options.overrideGlobalObjects !== false && global.Request !== Request) {
|
|
462
|
+
Object.defineProperty(global, "Request", {
|
|
463
|
+
value: Request
|
|
464
|
+
});
|
|
465
|
+
Object.defineProperty(global, "Response", {
|
|
466
|
+
value: Response2
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
return async (incoming, outgoing) => {
|
|
470
|
+
let res, req;
|
|
471
|
+
try {
|
|
472
|
+
req = newRequest(incoming, options.hostname);
|
|
473
|
+
let incomingEnded = !autoCleanupIncoming || incoming.method === "GET" || incoming.method === "HEAD";
|
|
474
|
+
if (!incomingEnded) {
|
|
475
|
+
;
|
|
476
|
+
incoming[wrapBodyStream] = true;
|
|
477
|
+
incoming.on("end", () => {
|
|
478
|
+
incomingEnded = true;
|
|
479
|
+
});
|
|
480
|
+
if (incoming instanceof Http2ServerRequest2) {
|
|
481
|
+
;
|
|
482
|
+
outgoing[outgoingEnded] = () => {
|
|
483
|
+
if (!incomingEnded) {
|
|
484
|
+
setTimeout(() => {
|
|
485
|
+
if (!incomingEnded) {
|
|
486
|
+
setTimeout(() => {
|
|
487
|
+
incoming.destroy();
|
|
488
|
+
outgoing.destroy();
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
outgoing.on("close", () => {
|
|
497
|
+
const abortController = req[abortControllerKey];
|
|
498
|
+
if (abortController) {
|
|
499
|
+
if (incoming.errored) {
|
|
500
|
+
req[abortControllerKey].abort(incoming.errored.toString());
|
|
501
|
+
} else if (!outgoing.writableFinished) {
|
|
502
|
+
req[abortControllerKey].abort("Client connection prematurely closed.");
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (!incomingEnded) {
|
|
506
|
+
setTimeout(() => {
|
|
507
|
+
if (!incomingEnded) {
|
|
508
|
+
setTimeout(() => {
|
|
509
|
+
incoming.destroy();
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
res = fetchCallback(req, { incoming, outgoing });
|
|
516
|
+
if (cacheKey in res) {
|
|
517
|
+
return responseViaCache(res, outgoing);
|
|
518
|
+
}
|
|
519
|
+
} catch (e) {
|
|
520
|
+
if (!res) {
|
|
521
|
+
if (options.errorHandler) {
|
|
522
|
+
res = await options.errorHandler(req ? e : toRequestError(e));
|
|
523
|
+
if (!res) {
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
} else if (!req) {
|
|
527
|
+
res = handleRequestError();
|
|
528
|
+
} else {
|
|
529
|
+
res = handleFetchError(e);
|
|
530
|
+
}
|
|
531
|
+
} else {
|
|
532
|
+
return handleResponseError(e, outgoing);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
try {
|
|
536
|
+
return await responseViaResponseObject(res, outgoing, options);
|
|
537
|
+
} catch (e) {
|
|
538
|
+
return handleResponseError(e, outgoing);
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
// ../node_modules/.pnpm/@modelcontextprotocol+sdk@1.26.0_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/server/webStandardStreamableHttp.js
|
|
544
|
+
var WebStandardStreamableHTTPServerTransport = class {
|
|
545
|
+
constructor(options = {}) {
|
|
546
|
+
this._started = false;
|
|
547
|
+
this._hasHandledRequest = false;
|
|
548
|
+
this._streamMapping = /* @__PURE__ */ new Map();
|
|
549
|
+
this._requestToStreamMapping = /* @__PURE__ */ new Map();
|
|
550
|
+
this._requestResponseMap = /* @__PURE__ */ new Map();
|
|
551
|
+
this._initialized = false;
|
|
552
|
+
this._enableJsonResponse = false;
|
|
553
|
+
this._standaloneSseStreamId = "_GET_stream";
|
|
554
|
+
this.sessionIdGenerator = options.sessionIdGenerator;
|
|
555
|
+
this._enableJsonResponse = options.enableJsonResponse ?? false;
|
|
556
|
+
this._eventStore = options.eventStore;
|
|
557
|
+
this._onsessioninitialized = options.onsessioninitialized;
|
|
558
|
+
this._onsessionclosed = options.onsessionclosed;
|
|
559
|
+
this._allowedHosts = options.allowedHosts;
|
|
560
|
+
this._allowedOrigins = options.allowedOrigins;
|
|
561
|
+
this._enableDnsRebindingProtection = options.enableDnsRebindingProtection ?? false;
|
|
562
|
+
this._retryInterval = options.retryInterval;
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Starts the transport. This is required by the Transport interface but is a no-op
|
|
566
|
+
* for the Streamable HTTP transport as connections are managed per-request.
|
|
567
|
+
*/
|
|
568
|
+
async start() {
|
|
569
|
+
if (this._started) {
|
|
570
|
+
throw new Error("Transport already started");
|
|
571
|
+
}
|
|
572
|
+
this._started = true;
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Helper to create a JSON error response
|
|
576
|
+
*/
|
|
577
|
+
createJsonErrorResponse(status, code, message, options) {
|
|
578
|
+
const error = { code, message };
|
|
579
|
+
if (options?.data !== void 0) {
|
|
580
|
+
error.data = options.data;
|
|
581
|
+
}
|
|
582
|
+
return new Response(JSON.stringify({
|
|
583
|
+
jsonrpc: "2.0",
|
|
584
|
+
error,
|
|
585
|
+
id: null
|
|
586
|
+
}), {
|
|
587
|
+
status,
|
|
588
|
+
headers: {
|
|
589
|
+
"Content-Type": "application/json",
|
|
590
|
+
...options?.headers
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Validates request headers for DNS rebinding protection.
|
|
596
|
+
* @returns Error response if validation fails, undefined if validation passes.
|
|
597
|
+
*/
|
|
598
|
+
validateRequestHeaders(req) {
|
|
599
|
+
if (!this._enableDnsRebindingProtection) {
|
|
600
|
+
return void 0;
|
|
601
|
+
}
|
|
602
|
+
if (this._allowedHosts && this._allowedHosts.length > 0) {
|
|
603
|
+
const hostHeader = req.headers.get("host");
|
|
604
|
+
if (!hostHeader || !this._allowedHosts.includes(hostHeader)) {
|
|
605
|
+
const error = `Invalid Host header: ${hostHeader}`;
|
|
606
|
+
this.onerror?.(new Error(error));
|
|
607
|
+
return this.createJsonErrorResponse(403, -32e3, error);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (this._allowedOrigins && this._allowedOrigins.length > 0) {
|
|
611
|
+
const originHeader = req.headers.get("origin");
|
|
612
|
+
if (originHeader && !this._allowedOrigins.includes(originHeader)) {
|
|
613
|
+
const error = `Invalid Origin header: ${originHeader}`;
|
|
614
|
+
this.onerror?.(new Error(error));
|
|
615
|
+
return this.createJsonErrorResponse(403, -32e3, error);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return void 0;
|
|
619
|
+
}
|
|
620
|
+
/**
|
|
621
|
+
* Handles an incoming HTTP request, whether GET, POST, or DELETE
|
|
622
|
+
* Returns a Response object (Web Standard)
|
|
623
|
+
*/
|
|
624
|
+
async handleRequest(req, options) {
|
|
625
|
+
if (!this.sessionIdGenerator && this._hasHandledRequest) {
|
|
626
|
+
throw new Error("Stateless transport cannot be reused across requests. Create a new transport per request.");
|
|
627
|
+
}
|
|
628
|
+
this._hasHandledRequest = true;
|
|
629
|
+
const validationError = this.validateRequestHeaders(req);
|
|
630
|
+
if (validationError) {
|
|
631
|
+
return validationError;
|
|
632
|
+
}
|
|
633
|
+
switch (req.method) {
|
|
634
|
+
case "POST":
|
|
635
|
+
return this.handlePostRequest(req, options);
|
|
636
|
+
case "GET":
|
|
637
|
+
return this.handleGetRequest(req);
|
|
638
|
+
case "DELETE":
|
|
639
|
+
return this.handleDeleteRequest(req);
|
|
640
|
+
default:
|
|
641
|
+
return this.handleUnsupportedRequest();
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* Writes a priming event to establish resumption capability.
|
|
646
|
+
* Only sends if eventStore is configured (opt-in for resumability) and
|
|
647
|
+
* the client's protocol version supports empty SSE data (>= 2025-11-25).
|
|
648
|
+
*/
|
|
649
|
+
async writePrimingEvent(controller, encoder, streamId, protocolVersion) {
|
|
650
|
+
if (!this._eventStore) {
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
if (protocolVersion < "2025-11-25") {
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
const primingEventId = await this._eventStore.storeEvent(streamId, {});
|
|
657
|
+
let primingEvent = `id: ${primingEventId}
|
|
658
|
+
data:
|
|
659
|
+
|
|
660
|
+
`;
|
|
661
|
+
if (this._retryInterval !== void 0) {
|
|
662
|
+
primingEvent = `id: ${primingEventId}
|
|
663
|
+
retry: ${this._retryInterval}
|
|
664
|
+
data:
|
|
665
|
+
|
|
666
|
+
`;
|
|
667
|
+
}
|
|
668
|
+
controller.enqueue(encoder.encode(primingEvent));
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Handles GET requests for SSE stream
|
|
672
|
+
*/
|
|
673
|
+
async handleGetRequest(req) {
|
|
674
|
+
const acceptHeader = req.headers.get("accept");
|
|
675
|
+
if (!acceptHeader?.includes("text/event-stream")) {
|
|
676
|
+
return this.createJsonErrorResponse(406, -32e3, "Not Acceptable: Client must accept text/event-stream");
|
|
677
|
+
}
|
|
678
|
+
const sessionError = this.validateSession(req);
|
|
679
|
+
if (sessionError) {
|
|
680
|
+
return sessionError;
|
|
681
|
+
}
|
|
682
|
+
const protocolError = this.validateProtocolVersion(req);
|
|
683
|
+
if (protocolError) {
|
|
684
|
+
return protocolError;
|
|
685
|
+
}
|
|
686
|
+
if (this._eventStore) {
|
|
687
|
+
const lastEventId = req.headers.get("last-event-id");
|
|
688
|
+
if (lastEventId) {
|
|
689
|
+
return this.replayEvents(lastEventId);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
if (this._streamMapping.get(this._standaloneSseStreamId) !== void 0) {
|
|
693
|
+
return this.createJsonErrorResponse(409, -32e3, "Conflict: Only one SSE stream is allowed per session");
|
|
694
|
+
}
|
|
695
|
+
const encoder = new TextEncoder();
|
|
696
|
+
let streamController;
|
|
697
|
+
const readable = new ReadableStream({
|
|
698
|
+
start: (controller) => {
|
|
699
|
+
streamController = controller;
|
|
700
|
+
},
|
|
701
|
+
cancel: () => {
|
|
702
|
+
this._streamMapping.delete(this._standaloneSseStreamId);
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
const headers = {
|
|
706
|
+
"Content-Type": "text/event-stream",
|
|
707
|
+
"Cache-Control": "no-cache, no-transform",
|
|
708
|
+
Connection: "keep-alive"
|
|
709
|
+
};
|
|
710
|
+
if (this.sessionId !== void 0) {
|
|
711
|
+
headers["mcp-session-id"] = this.sessionId;
|
|
712
|
+
}
|
|
713
|
+
this._streamMapping.set(this._standaloneSseStreamId, {
|
|
714
|
+
controller: streamController,
|
|
715
|
+
encoder,
|
|
716
|
+
cleanup: () => {
|
|
717
|
+
this._streamMapping.delete(this._standaloneSseStreamId);
|
|
718
|
+
try {
|
|
719
|
+
streamController.close();
|
|
720
|
+
} catch {
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
return new Response(readable, { headers });
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Replays events that would have been sent after the specified event ID
|
|
728
|
+
* Only used when resumability is enabled
|
|
729
|
+
*/
|
|
730
|
+
async replayEvents(lastEventId) {
|
|
731
|
+
if (!this._eventStore) {
|
|
732
|
+
return this.createJsonErrorResponse(400, -32e3, "Event store not configured");
|
|
733
|
+
}
|
|
734
|
+
try {
|
|
735
|
+
let streamId;
|
|
736
|
+
if (this._eventStore.getStreamIdForEventId) {
|
|
737
|
+
streamId = await this._eventStore.getStreamIdForEventId(lastEventId);
|
|
738
|
+
if (!streamId) {
|
|
739
|
+
return this.createJsonErrorResponse(400, -32e3, "Invalid event ID format");
|
|
740
|
+
}
|
|
741
|
+
if (this._streamMapping.get(streamId) !== void 0) {
|
|
742
|
+
return this.createJsonErrorResponse(409, -32e3, "Conflict: Stream already has an active connection");
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
const headers = {
|
|
746
|
+
"Content-Type": "text/event-stream",
|
|
747
|
+
"Cache-Control": "no-cache, no-transform",
|
|
748
|
+
Connection: "keep-alive"
|
|
749
|
+
};
|
|
750
|
+
if (this.sessionId !== void 0) {
|
|
751
|
+
headers["mcp-session-id"] = this.sessionId;
|
|
752
|
+
}
|
|
753
|
+
const encoder = new TextEncoder();
|
|
754
|
+
let streamController;
|
|
755
|
+
const readable = new ReadableStream({
|
|
756
|
+
start: (controller) => {
|
|
757
|
+
streamController = controller;
|
|
758
|
+
},
|
|
759
|
+
cancel: () => {
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
const replayedStreamId = await this._eventStore.replayEventsAfter(lastEventId, {
|
|
763
|
+
send: async (eventId, message) => {
|
|
764
|
+
const success = this.writeSSEEvent(streamController, encoder, message, eventId);
|
|
765
|
+
if (!success) {
|
|
766
|
+
this.onerror?.(new Error("Failed replay events"));
|
|
767
|
+
try {
|
|
768
|
+
streamController.close();
|
|
769
|
+
} catch {
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
});
|
|
774
|
+
this._streamMapping.set(replayedStreamId, {
|
|
775
|
+
controller: streamController,
|
|
776
|
+
encoder,
|
|
777
|
+
cleanup: () => {
|
|
778
|
+
this._streamMapping.delete(replayedStreamId);
|
|
779
|
+
try {
|
|
780
|
+
streamController.close();
|
|
781
|
+
} catch {
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
});
|
|
785
|
+
return new Response(readable, { headers });
|
|
786
|
+
} catch (error) {
|
|
787
|
+
this.onerror?.(error);
|
|
788
|
+
return this.createJsonErrorResponse(500, -32e3, "Error replaying events");
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Writes an event to an SSE stream via controller with proper formatting
|
|
793
|
+
*/
|
|
794
|
+
writeSSEEvent(controller, encoder, message, eventId) {
|
|
795
|
+
try {
|
|
796
|
+
let eventData = `event: message
|
|
797
|
+
`;
|
|
798
|
+
if (eventId) {
|
|
799
|
+
eventData += `id: ${eventId}
|
|
800
|
+
`;
|
|
801
|
+
}
|
|
802
|
+
eventData += `data: ${JSON.stringify(message)}
|
|
803
|
+
|
|
804
|
+
`;
|
|
805
|
+
controller.enqueue(encoder.encode(eventData));
|
|
806
|
+
return true;
|
|
807
|
+
} catch {
|
|
808
|
+
return false;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* Handles unsupported requests (PUT, PATCH, etc.)
|
|
813
|
+
*/
|
|
814
|
+
handleUnsupportedRequest() {
|
|
815
|
+
return new Response(JSON.stringify({
|
|
816
|
+
jsonrpc: "2.0",
|
|
817
|
+
error: {
|
|
818
|
+
code: -32e3,
|
|
819
|
+
message: "Method not allowed."
|
|
820
|
+
},
|
|
821
|
+
id: null
|
|
822
|
+
}), {
|
|
823
|
+
status: 405,
|
|
824
|
+
headers: {
|
|
825
|
+
Allow: "GET, POST, DELETE",
|
|
826
|
+
"Content-Type": "application/json"
|
|
827
|
+
}
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Handles POST requests containing JSON-RPC messages
|
|
832
|
+
*/
|
|
833
|
+
async handlePostRequest(req, options) {
|
|
834
|
+
try {
|
|
835
|
+
const acceptHeader = req.headers.get("accept");
|
|
836
|
+
if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
|
|
837
|
+
return this.createJsonErrorResponse(406, -32e3, "Not Acceptable: Client must accept both application/json and text/event-stream");
|
|
838
|
+
}
|
|
839
|
+
const ct = req.headers.get("content-type");
|
|
840
|
+
if (!ct || !ct.includes("application/json")) {
|
|
841
|
+
return this.createJsonErrorResponse(415, -32e3, "Unsupported Media Type: Content-Type must be application/json");
|
|
842
|
+
}
|
|
843
|
+
const requestInfo = {
|
|
844
|
+
headers: Object.fromEntries(req.headers.entries())
|
|
845
|
+
};
|
|
846
|
+
let rawMessage;
|
|
847
|
+
if (options?.parsedBody !== void 0) {
|
|
848
|
+
rawMessage = options.parsedBody;
|
|
849
|
+
} else {
|
|
850
|
+
try {
|
|
851
|
+
rawMessage = await req.json();
|
|
852
|
+
} catch {
|
|
853
|
+
return this.createJsonErrorResponse(400, -32700, "Parse error: Invalid JSON");
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
let messages;
|
|
857
|
+
try {
|
|
858
|
+
if (Array.isArray(rawMessage)) {
|
|
859
|
+
messages = rawMessage.map((msg) => JSONRPCMessageSchema.parse(msg));
|
|
860
|
+
} else {
|
|
861
|
+
messages = [JSONRPCMessageSchema.parse(rawMessage)];
|
|
862
|
+
}
|
|
863
|
+
} catch {
|
|
864
|
+
return this.createJsonErrorResponse(400, -32700, "Parse error: Invalid JSON-RPC message");
|
|
865
|
+
}
|
|
866
|
+
const isInitializationRequest = messages.some(isInitializeRequest);
|
|
867
|
+
if (isInitializationRequest) {
|
|
868
|
+
if (this._initialized && this.sessionId !== void 0) {
|
|
869
|
+
return this.createJsonErrorResponse(400, -32600, "Invalid Request: Server already initialized");
|
|
870
|
+
}
|
|
871
|
+
if (messages.length > 1) {
|
|
872
|
+
return this.createJsonErrorResponse(400, -32600, "Invalid Request: Only one initialization request is allowed");
|
|
873
|
+
}
|
|
874
|
+
this.sessionId = this.sessionIdGenerator?.();
|
|
875
|
+
this._initialized = true;
|
|
876
|
+
if (this.sessionId && this._onsessioninitialized) {
|
|
877
|
+
await Promise.resolve(this._onsessioninitialized(this.sessionId));
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
if (!isInitializationRequest) {
|
|
881
|
+
const sessionError = this.validateSession(req);
|
|
882
|
+
if (sessionError) {
|
|
883
|
+
return sessionError;
|
|
884
|
+
}
|
|
885
|
+
const protocolError = this.validateProtocolVersion(req);
|
|
886
|
+
if (protocolError) {
|
|
887
|
+
return protocolError;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
const hasRequests = messages.some(isJSONRPCRequest);
|
|
891
|
+
if (!hasRequests) {
|
|
892
|
+
for (const message of messages) {
|
|
893
|
+
this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo });
|
|
894
|
+
}
|
|
895
|
+
return new Response(null, { status: 202 });
|
|
896
|
+
}
|
|
897
|
+
const streamId = crypto.randomUUID();
|
|
898
|
+
const initRequest = messages.find((m) => isInitializeRequest(m));
|
|
899
|
+
const clientProtocolVersion = initRequest ? initRequest.params.protocolVersion : req.headers.get("mcp-protocol-version") ?? DEFAULT_NEGOTIATED_PROTOCOL_VERSION;
|
|
900
|
+
if (this._enableJsonResponse) {
|
|
901
|
+
return new Promise((resolve) => {
|
|
902
|
+
this._streamMapping.set(streamId, {
|
|
903
|
+
resolveJson: resolve,
|
|
904
|
+
cleanup: () => {
|
|
905
|
+
this._streamMapping.delete(streamId);
|
|
906
|
+
}
|
|
907
|
+
});
|
|
908
|
+
for (const message of messages) {
|
|
909
|
+
if (isJSONRPCRequest(message)) {
|
|
910
|
+
this._requestToStreamMapping.set(message.id, streamId);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
for (const message of messages) {
|
|
914
|
+
this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo });
|
|
915
|
+
}
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
const encoder = new TextEncoder();
|
|
919
|
+
let streamController;
|
|
920
|
+
const readable = new ReadableStream({
|
|
921
|
+
start: (controller) => {
|
|
922
|
+
streamController = controller;
|
|
923
|
+
},
|
|
924
|
+
cancel: () => {
|
|
925
|
+
this._streamMapping.delete(streamId);
|
|
926
|
+
}
|
|
927
|
+
});
|
|
928
|
+
const headers = {
|
|
929
|
+
"Content-Type": "text/event-stream",
|
|
930
|
+
"Cache-Control": "no-cache",
|
|
931
|
+
Connection: "keep-alive"
|
|
932
|
+
};
|
|
933
|
+
if (this.sessionId !== void 0) {
|
|
934
|
+
headers["mcp-session-id"] = this.sessionId;
|
|
935
|
+
}
|
|
936
|
+
for (const message of messages) {
|
|
937
|
+
if (isJSONRPCRequest(message)) {
|
|
938
|
+
this._streamMapping.set(streamId, {
|
|
939
|
+
controller: streamController,
|
|
940
|
+
encoder,
|
|
941
|
+
cleanup: () => {
|
|
942
|
+
this._streamMapping.delete(streamId);
|
|
943
|
+
try {
|
|
944
|
+
streamController.close();
|
|
945
|
+
} catch {
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
this._requestToStreamMapping.set(message.id, streamId);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
await this.writePrimingEvent(streamController, encoder, streamId, clientProtocolVersion);
|
|
953
|
+
for (const message of messages) {
|
|
954
|
+
let closeSSEStream;
|
|
955
|
+
let closeStandaloneSSEStream;
|
|
956
|
+
if (isJSONRPCRequest(message) && this._eventStore && clientProtocolVersion >= "2025-11-25") {
|
|
957
|
+
closeSSEStream = () => {
|
|
958
|
+
this.closeSSEStream(message.id);
|
|
959
|
+
};
|
|
960
|
+
closeStandaloneSSEStream = () => {
|
|
961
|
+
this.closeStandaloneSSEStream();
|
|
962
|
+
};
|
|
963
|
+
}
|
|
964
|
+
this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo, closeSSEStream, closeStandaloneSSEStream });
|
|
965
|
+
}
|
|
966
|
+
return new Response(readable, { status: 200, headers });
|
|
967
|
+
} catch (error) {
|
|
968
|
+
this.onerror?.(error);
|
|
969
|
+
return this.createJsonErrorResponse(400, -32700, "Parse error", { data: String(error) });
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Handles DELETE requests to terminate sessions
|
|
974
|
+
*/
|
|
975
|
+
async handleDeleteRequest(req) {
|
|
976
|
+
const sessionError = this.validateSession(req);
|
|
977
|
+
if (sessionError) {
|
|
978
|
+
return sessionError;
|
|
979
|
+
}
|
|
980
|
+
const protocolError = this.validateProtocolVersion(req);
|
|
981
|
+
if (protocolError) {
|
|
982
|
+
return protocolError;
|
|
983
|
+
}
|
|
984
|
+
await Promise.resolve(this._onsessionclosed?.(this.sessionId));
|
|
985
|
+
await this.close();
|
|
986
|
+
return new Response(null, { status: 200 });
|
|
987
|
+
}
|
|
988
|
+
/**
|
|
989
|
+
* Validates session ID for non-initialization requests.
|
|
990
|
+
* Returns Response error if invalid, undefined otherwise
|
|
991
|
+
*/
|
|
992
|
+
validateSession(req) {
|
|
993
|
+
if (this.sessionIdGenerator === void 0) {
|
|
994
|
+
return void 0;
|
|
995
|
+
}
|
|
996
|
+
if (!this._initialized) {
|
|
997
|
+
return this.createJsonErrorResponse(400, -32e3, "Bad Request: Server not initialized");
|
|
998
|
+
}
|
|
999
|
+
const sessionId = req.headers.get("mcp-session-id");
|
|
1000
|
+
if (!sessionId) {
|
|
1001
|
+
return this.createJsonErrorResponse(400, -32e3, "Bad Request: Mcp-Session-Id header is required");
|
|
1002
|
+
}
|
|
1003
|
+
if (sessionId !== this.sessionId) {
|
|
1004
|
+
return this.createJsonErrorResponse(404, -32001, "Session not found");
|
|
1005
|
+
}
|
|
1006
|
+
return void 0;
|
|
1007
|
+
}
|
|
1008
|
+
/**
|
|
1009
|
+
* Validates the MCP-Protocol-Version header on incoming requests.
|
|
1010
|
+
*
|
|
1011
|
+
* For initialization: Version negotiation handles unknown versions gracefully
|
|
1012
|
+
* (server responds with its supported version).
|
|
1013
|
+
*
|
|
1014
|
+
* For subsequent requests with MCP-Protocol-Version header:
|
|
1015
|
+
* - Accept if in supported list
|
|
1016
|
+
* - 400 if unsupported
|
|
1017
|
+
*
|
|
1018
|
+
* For HTTP requests without the MCP-Protocol-Version header:
|
|
1019
|
+
* - Accept and default to the version negotiated at initialization
|
|
1020
|
+
*/
|
|
1021
|
+
validateProtocolVersion(req) {
|
|
1022
|
+
const protocolVersion = req.headers.get("mcp-protocol-version");
|
|
1023
|
+
if (protocolVersion !== null && !SUPPORTED_PROTOCOL_VERSIONS.includes(protocolVersion)) {
|
|
1024
|
+
return this.createJsonErrorResponse(400, -32e3, `Bad Request: Unsupported protocol version: ${protocolVersion} (supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(", ")})`);
|
|
1025
|
+
}
|
|
1026
|
+
return void 0;
|
|
1027
|
+
}
|
|
1028
|
+
async close() {
|
|
1029
|
+
this._streamMapping.forEach(({ cleanup }) => {
|
|
1030
|
+
cleanup();
|
|
1031
|
+
});
|
|
1032
|
+
this._streamMapping.clear();
|
|
1033
|
+
this._requestResponseMap.clear();
|
|
1034
|
+
this.onclose?.();
|
|
1035
|
+
}
|
|
1036
|
+
/**
|
|
1037
|
+
* Close an SSE stream for a specific request, triggering client reconnection.
|
|
1038
|
+
* Use this to implement polling behavior during long-running operations -
|
|
1039
|
+
* client will reconnect after the retry interval specified in the priming event.
|
|
1040
|
+
*/
|
|
1041
|
+
closeSSEStream(requestId) {
|
|
1042
|
+
const streamId = this._requestToStreamMapping.get(requestId);
|
|
1043
|
+
if (!streamId)
|
|
1044
|
+
return;
|
|
1045
|
+
const stream = this._streamMapping.get(streamId);
|
|
1046
|
+
if (stream) {
|
|
1047
|
+
stream.cleanup();
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Close the standalone GET SSE stream, triggering client reconnection.
|
|
1052
|
+
* Use this to implement polling behavior for server-initiated notifications.
|
|
1053
|
+
*/
|
|
1054
|
+
closeStandaloneSSEStream() {
|
|
1055
|
+
const stream = this._streamMapping.get(this._standaloneSseStreamId);
|
|
1056
|
+
if (stream) {
|
|
1057
|
+
stream.cleanup();
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
async send(message, options) {
|
|
1061
|
+
let requestId = options?.relatedRequestId;
|
|
1062
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) {
|
|
1063
|
+
requestId = message.id;
|
|
1064
|
+
}
|
|
1065
|
+
if (requestId === void 0) {
|
|
1066
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) {
|
|
1067
|
+
throw new Error("Cannot send a response on a standalone SSE stream unless resuming a previous client request");
|
|
1068
|
+
}
|
|
1069
|
+
let eventId;
|
|
1070
|
+
if (this._eventStore) {
|
|
1071
|
+
eventId = await this._eventStore.storeEvent(this._standaloneSseStreamId, message);
|
|
1072
|
+
}
|
|
1073
|
+
const standaloneSse = this._streamMapping.get(this._standaloneSseStreamId);
|
|
1074
|
+
if (standaloneSse === void 0) {
|
|
1075
|
+
return;
|
|
1076
|
+
}
|
|
1077
|
+
if (standaloneSse.controller && standaloneSse.encoder) {
|
|
1078
|
+
this.writeSSEEvent(standaloneSse.controller, standaloneSse.encoder, message, eventId);
|
|
1079
|
+
}
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
const streamId = this._requestToStreamMapping.get(requestId);
|
|
1083
|
+
if (!streamId) {
|
|
1084
|
+
throw new Error(`No connection established for request ID: ${String(requestId)}`);
|
|
1085
|
+
}
|
|
1086
|
+
const stream = this._streamMapping.get(streamId);
|
|
1087
|
+
if (!this._enableJsonResponse && stream?.controller && stream?.encoder) {
|
|
1088
|
+
let eventId;
|
|
1089
|
+
if (this._eventStore) {
|
|
1090
|
+
eventId = await this._eventStore.storeEvent(streamId, message);
|
|
1091
|
+
}
|
|
1092
|
+
this.writeSSEEvent(stream.controller, stream.encoder, message, eventId);
|
|
1093
|
+
}
|
|
1094
|
+
if (isJSONRPCResultResponse(message) || isJSONRPCErrorResponse(message)) {
|
|
1095
|
+
this._requestResponseMap.set(requestId, message);
|
|
1096
|
+
const relatedIds = Array.from(this._requestToStreamMapping.entries()).filter(([_, sid]) => sid === streamId).map(([id]) => id);
|
|
1097
|
+
const allResponsesReady = relatedIds.every((id) => this._requestResponseMap.has(id));
|
|
1098
|
+
if (allResponsesReady) {
|
|
1099
|
+
if (!stream) {
|
|
1100
|
+
throw new Error(`No connection established for request ID: ${String(requestId)}`);
|
|
1101
|
+
}
|
|
1102
|
+
if (this._enableJsonResponse && stream.resolveJson) {
|
|
1103
|
+
const headers = {
|
|
1104
|
+
"Content-Type": "application/json"
|
|
1105
|
+
};
|
|
1106
|
+
if (this.sessionId !== void 0) {
|
|
1107
|
+
headers["mcp-session-id"] = this.sessionId;
|
|
1108
|
+
}
|
|
1109
|
+
const responses = relatedIds.map((id) => this._requestResponseMap.get(id));
|
|
1110
|
+
if (responses.length === 1) {
|
|
1111
|
+
stream.resolveJson(new Response(JSON.stringify(responses[0]), { status: 200, headers }));
|
|
1112
|
+
} else {
|
|
1113
|
+
stream.resolveJson(new Response(JSON.stringify(responses), { status: 200, headers }));
|
|
1114
|
+
}
|
|
1115
|
+
} else {
|
|
1116
|
+
stream.cleanup();
|
|
1117
|
+
}
|
|
1118
|
+
for (const id of relatedIds) {
|
|
1119
|
+
this._requestResponseMap.delete(id);
|
|
1120
|
+
this._requestToStreamMapping.delete(id);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
|
|
1127
|
+
// ../node_modules/.pnpm/@modelcontextprotocol+sdk@1.26.0_zod@3.25.76/node_modules/@modelcontextprotocol/sdk/dist/esm/server/streamableHttp.js
|
|
1128
|
+
var StreamableHTTPServerTransport = class {
|
|
1129
|
+
constructor(options = {}) {
|
|
1130
|
+
this._requestContext = /* @__PURE__ */ new WeakMap();
|
|
1131
|
+
this._webStandardTransport = new WebStandardStreamableHTTPServerTransport(options);
|
|
1132
|
+
this._requestListener = getRequestListener(async (webRequest) => {
|
|
1133
|
+
const context = this._requestContext.get(webRequest);
|
|
1134
|
+
return this._webStandardTransport.handleRequest(webRequest, {
|
|
1135
|
+
authInfo: context?.authInfo,
|
|
1136
|
+
parsedBody: context?.parsedBody
|
|
1137
|
+
});
|
|
1138
|
+
}, { overrideGlobalObjects: false });
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* Gets the session ID for this transport instance.
|
|
1142
|
+
*/
|
|
1143
|
+
get sessionId() {
|
|
1144
|
+
return this._webStandardTransport.sessionId;
|
|
1145
|
+
}
|
|
1146
|
+
/**
|
|
1147
|
+
* Sets callback for when the transport is closed.
|
|
1148
|
+
*/
|
|
1149
|
+
set onclose(handler) {
|
|
1150
|
+
this._webStandardTransport.onclose = handler;
|
|
1151
|
+
}
|
|
1152
|
+
get onclose() {
|
|
1153
|
+
return this._webStandardTransport.onclose;
|
|
1154
|
+
}
|
|
1155
|
+
/**
|
|
1156
|
+
* Sets callback for transport errors.
|
|
1157
|
+
*/
|
|
1158
|
+
set onerror(handler) {
|
|
1159
|
+
this._webStandardTransport.onerror = handler;
|
|
1160
|
+
}
|
|
1161
|
+
get onerror() {
|
|
1162
|
+
return this._webStandardTransport.onerror;
|
|
1163
|
+
}
|
|
1164
|
+
/**
|
|
1165
|
+
* Sets callback for incoming messages.
|
|
1166
|
+
*/
|
|
1167
|
+
set onmessage(handler) {
|
|
1168
|
+
this._webStandardTransport.onmessage = handler;
|
|
1169
|
+
}
|
|
1170
|
+
get onmessage() {
|
|
1171
|
+
return this._webStandardTransport.onmessage;
|
|
1172
|
+
}
|
|
1173
|
+
/**
|
|
1174
|
+
* Starts the transport. This is required by the Transport interface but is a no-op
|
|
1175
|
+
* for the Streamable HTTP transport as connections are managed per-request.
|
|
1176
|
+
*/
|
|
1177
|
+
async start() {
|
|
1178
|
+
return this._webStandardTransport.start();
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Closes the transport and all active connections.
|
|
1182
|
+
*/
|
|
1183
|
+
async close() {
|
|
1184
|
+
return this._webStandardTransport.close();
|
|
1185
|
+
}
|
|
1186
|
+
/**
|
|
1187
|
+
* Sends a JSON-RPC message through the transport.
|
|
1188
|
+
*/
|
|
1189
|
+
async send(message, options) {
|
|
1190
|
+
return this._webStandardTransport.send(message, options);
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* Handles an incoming HTTP request, whether GET or POST.
|
|
1194
|
+
*
|
|
1195
|
+
* This method converts Node.js HTTP objects to Web Standard Request/Response
|
|
1196
|
+
* and delegates to the underlying WebStandardStreamableHTTPServerTransport.
|
|
1197
|
+
*
|
|
1198
|
+
* @param req - Node.js IncomingMessage, optionally with auth property from middleware
|
|
1199
|
+
* @param res - Node.js ServerResponse
|
|
1200
|
+
* @param parsedBody - Optional pre-parsed body from body-parser middleware
|
|
1201
|
+
*/
|
|
1202
|
+
async handleRequest(req, res, parsedBody) {
|
|
1203
|
+
const authInfo = req.auth;
|
|
1204
|
+
const handler = getRequestListener(async (webRequest) => {
|
|
1205
|
+
return this._webStandardTransport.handleRequest(webRequest, {
|
|
1206
|
+
authInfo,
|
|
1207
|
+
parsedBody
|
|
1208
|
+
});
|
|
1209
|
+
}, { overrideGlobalObjects: false });
|
|
1210
|
+
await handler(req, res);
|
|
1211
|
+
}
|
|
1212
|
+
/**
|
|
1213
|
+
* Close an SSE stream for a specific request, triggering client reconnection.
|
|
1214
|
+
* Use this to implement polling behavior during long-running operations -
|
|
1215
|
+
* client will reconnect after the retry interval specified in the priming event.
|
|
1216
|
+
*/
|
|
1217
|
+
closeSSEStream(requestId) {
|
|
1218
|
+
this._webStandardTransport.closeSSEStream(requestId);
|
|
1219
|
+
}
|
|
1220
|
+
/**
|
|
1221
|
+
* Close the standalone GET SSE stream, triggering client reconnection.
|
|
1222
|
+
* Use this to implement polling behavior for server-initiated notifications.
|
|
1223
|
+
*/
|
|
1224
|
+
closeStandaloneSSEStream() {
|
|
1225
|
+
this._webStandardTransport.closeStandaloneSSEStream();
|
|
1226
|
+
}
|
|
1227
|
+
};
|
|
1228
|
+
export {
|
|
1229
|
+
StreamableHTTPServerTransport
|
|
1230
|
+
};
|