agents 0.0.0-d4257c1 → 0.0.0-d520fe8
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 +5 -3
- package/dist/ai-chat-agent.d.ts +86 -13
- package/dist/ai-chat-agent.js +340 -74
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/{ai-chat-v5-migration-gdyLiTd8.js → ai-chat-v5-migration-BSiGZmYU.js} +2 -2
- package/dist/{ai-chat-v5-migration-gdyLiTd8.js.map → ai-chat-v5-migration-BSiGZmYU.js.map} +1 -1
- package/dist/ai-chat-v5-migration.js +1 -1
- package/dist/ai-react.d.ts +14 -9
- package/dist/ai-react.js +172 -36
- package/dist/ai-react.js.map +1 -1
- package/dist/{ai-types-BWW4umHY.d.ts → ai-types-81H_-Uxh.d.ts} +16 -8
- package/dist/{ai-types-UZlfLOYP.js → ai-types-CrMqkwc_.js} +6 -2
- package/dist/ai-types-CrMqkwc_.js.map +1 -0
- package/dist/ai-types.d.ts +4 -4
- package/dist/ai-types.js +1 -1
- package/dist/cli.d.ts +8 -0
- package/dist/cli.js +27 -0
- package/dist/cli.js.map +1 -0
- package/dist/{client-DjR-lC16.js → client-B3SR12TQ.js} +3 -3
- package/dist/{client-DjR-lC16.js.map → client-B3SR12TQ.js.map} +1 -1
- package/dist/{client-CmMi85Sj.d.ts → client-BAQA84dr.d.ts} +10 -10
- package/dist/client-BZq9qau2.js +1093 -0
- package/dist/client-BZq9qau2.js.map +1 -0
- package/dist/client-CsaP9Irq.d.ts +1528 -0
- package/dist/client.d.ts +8 -8
- package/dist/client.js +2 -2
- package/dist/codemode/ai.js +5 -5
- package/dist/{do-oauth-client-provider-CCwGwnrA.d.ts → do-oauth-client-provider-C2CHH5x-.d.ts} +2 -2
- package/dist/{do-oauth-client-provider-B2jr6UNq.js → do-oauth-client-provider-CwqK5SXm.js} +3 -2
- package/dist/do-oauth-client-provider-CwqK5SXm.js.map +1 -0
- package/dist/{index-W4JUkafc.d.ts → index-BUle9RiP.d.ts} +7 -3
- package/dist/{index-CkQU40oY.d.ts → index-Bx5KK3VJ.d.ts} +75 -46
- package/dist/index.d.ts +36 -36
- package/dist/index.js +5 -5
- package/dist/mcp/client.d.ts +4 -4
- package/dist/mcp/client.js +2 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -1
- package/dist/mcp/do-oauth-client-provider.js +1 -1
- package/dist/mcp/index.d.ts +129 -11
- package/dist/mcp/index.js +632 -32
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.js +10 -6
- package/dist/mcp/x402.js.map +1 -1
- package/dist/{mcp-BEwaCsxO.d.ts → mcp-BwPscEiF.d.ts} +2 -2
- package/dist/observability/index.d.ts +2 -2
- package/dist/observability/index.js +5 -5
- package/dist/{react-B4e1rDid.d.ts → react-CbwD4fBf.d.ts} +17 -17
- package/dist/react.d.ts +9 -9
- package/dist/react.js +2 -2
- package/dist/react.js.map +1 -1
- package/dist/{serializable-gtr9YMhp.d.ts → serializable-faDkMCai.d.ts} +8 -3
- package/dist/serializable.d.ts +5 -5
- package/dist/{src-COfG--3R.js → src-D_KKH_4c.js} +125 -120
- package/dist/src-D_KKH_4c.js.map +1 -0
- package/package.json +55 -40
- package/dist/ai-types-UZlfLOYP.js.map +0 -1
- package/dist/client-C-nwz-3N.d.ts +0 -5313
- package/dist/client-CZBVDDoO.js +0 -786
- package/dist/client-CZBVDDoO.js.map +0 -1
- package/dist/do-oauth-client-provider-B2jr6UNq.js.map +0 -1
- package/dist/src-COfG--3R.js.map +0 -1
- package/src/index.ts +0 -2031
package/dist/mcp/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { MessageType } from "../ai-types-
|
|
2
|
-
import "../client-
|
|
3
|
-
import
|
|
4
|
-
import "../do-oauth-client-provider-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { t as MessageType } from "../ai-types-CrMqkwc_.js";
|
|
2
|
+
import "../client-B3SR12TQ.js";
|
|
3
|
+
import "../client-BZq9qau2.js";
|
|
4
|
+
import "../do-oauth-client-provider-CwqK5SXm.js";
|
|
5
|
+
import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-D_KKH_4c.js";
|
|
6
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
7
|
+
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
8
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
9
|
+
import { ElicitRequestSchema, InitializeRequestSchema, JSONRPCMessageSchema, isInitializeRequest, isJSONRPCError, isJSONRPCNotification, isJSONRPCRequest, isJSONRPCResponse } from "@modelcontextprotocol/sdk/types.js";
|
|
7
10
|
|
|
8
11
|
//#region src/mcp/utils.ts
|
|
9
12
|
/**
|
|
@@ -20,7 +23,7 @@ const MCP_HTTP_METHOD_HEADER = "cf-mcp-method";
|
|
|
20
23
|
*/
|
|
21
24
|
const MCP_MESSAGE_HEADER = "cf-mcp-message";
|
|
22
25
|
const MAXIMUM_MESSAGE_SIZE_BYTES = 4 * 1024 * 1024;
|
|
23
|
-
const createStreamingHttpHandler = (basePath, namespace,
|
|
26
|
+
const createStreamingHttpHandler = (basePath, namespace, options = {}) => {
|
|
24
27
|
let pathname = basePath;
|
|
25
28
|
if (basePath === "/") pathname = "/*";
|
|
26
29
|
const basePattern = new URLPattern({ pathname });
|
|
@@ -129,7 +132,10 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
129
132
|
return new Response(body$1, { status: 400 });
|
|
130
133
|
}
|
|
131
134
|
sessionId = sessionId ?? namespace.newUniqueId().toString();
|
|
132
|
-
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, {
|
|
135
|
+
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, {
|
|
136
|
+
props: ctx.props,
|
|
137
|
+
jurisdiction: options.jurisdiction
|
|
138
|
+
});
|
|
133
139
|
const isInitialized = await agent.getInitializeRequest();
|
|
134
140
|
if (maybeInitializeRequest) await agent.setInitializeRequest(maybeInitializeRequest);
|
|
135
141
|
else if (!isInitialized) {
|
|
@@ -204,7 +210,7 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
204
210
|
if (messages.every((msg) => isJSONRPCNotification(msg) || isJSONRPCResponse(msg))) {
|
|
205
211
|
ws.close();
|
|
206
212
|
return new Response(null, {
|
|
207
|
-
headers: corsHeaders(request, corsOptions),
|
|
213
|
+
headers: corsHeaders(request, options.corsOptions),
|
|
208
214
|
status: 202
|
|
209
215
|
});
|
|
210
216
|
}
|
|
@@ -214,7 +220,7 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
214
220
|
Connection: "keep-alive",
|
|
215
221
|
"Content-Type": "text/event-stream",
|
|
216
222
|
"mcp-session-id": sessionId,
|
|
217
|
-
...corsHeaders(request, corsOptions)
|
|
223
|
+
...corsHeaders(request, options.corsOptions)
|
|
218
224
|
},
|
|
219
225
|
status: 200
|
|
220
226
|
});
|
|
@@ -242,7 +248,10 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
242
248
|
const { readable, writable } = new TransformStream();
|
|
243
249
|
const writer = writable.getWriter();
|
|
244
250
|
const encoder = new TextEncoder();
|
|
245
|
-
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, {
|
|
251
|
+
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, {
|
|
252
|
+
props: ctx.props,
|
|
253
|
+
jurisdiction: options.jurisdiction
|
|
254
|
+
});
|
|
246
255
|
if (!await agent.getInitializeRequest()) return new Response(JSON.stringify({
|
|
247
256
|
jsonrpc: "2.0",
|
|
248
257
|
error: {
|
|
@@ -291,7 +300,7 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
291
300
|
Connection: "keep-alive",
|
|
292
301
|
"Content-Type": "text/event-stream",
|
|
293
302
|
"mcp-session-id": sessionId,
|
|
294
|
-
...corsHeaders(request, corsOptions)
|
|
303
|
+
...corsHeaders(request, options.corsOptions)
|
|
295
304
|
},
|
|
296
305
|
status: 200
|
|
297
306
|
});
|
|
@@ -306,9 +315,9 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
306
315
|
id: null
|
|
307
316
|
}), {
|
|
308
317
|
status: 400,
|
|
309
|
-
headers: corsHeaders(request, corsOptions)
|
|
318
|
+
headers: corsHeaders(request, options.corsOptions)
|
|
310
319
|
});
|
|
311
|
-
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}
|
|
320
|
+
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, { jurisdiction: options.jurisdiction });
|
|
312
321
|
if (!await agent.getInitializeRequest()) return new Response(JSON.stringify({
|
|
313
322
|
jsonrpc: "2.0",
|
|
314
323
|
error: {
|
|
@@ -318,12 +327,12 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
318
327
|
id: null
|
|
319
328
|
}), {
|
|
320
329
|
status: 404,
|
|
321
|
-
headers: corsHeaders(request, corsOptions)
|
|
330
|
+
headers: corsHeaders(request, options.corsOptions)
|
|
322
331
|
});
|
|
323
332
|
ctx.waitUntil(agent.destroy().catch(() => {}));
|
|
324
333
|
return new Response(null, {
|
|
325
334
|
status: 204,
|
|
326
|
-
headers: corsHeaders(request, corsOptions)
|
|
335
|
+
headers: corsHeaders(request, options.corsOptions)
|
|
327
336
|
});
|
|
328
337
|
}
|
|
329
338
|
}
|
|
@@ -338,7 +347,7 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
338
347
|
return new Response(body, { status: 404 });
|
|
339
348
|
};
|
|
340
349
|
};
|
|
341
|
-
const createLegacySseHandler = (basePath, namespace,
|
|
350
|
+
const createLegacySseHandler = (basePath, namespace, options = {}) => {
|
|
342
351
|
let pathname = basePath;
|
|
343
352
|
if (basePath === "/") pathname = "/*";
|
|
344
353
|
const basePattern = new URLPattern({ pathname });
|
|
@@ -355,7 +364,10 @@ const createLegacySseHandler = (basePath, namespace, corsOptions) => {
|
|
|
355
364
|
endpointUrl.searchParams.set("sessionId", sessionId);
|
|
356
365
|
const endpointMessage = `event: endpoint\ndata: ${endpointUrl.pathname + endpointUrl.search + endpointUrl.hash}\n\n`;
|
|
357
366
|
writer.write(encoder.encode(endpointMessage));
|
|
358
|
-
const agent = await getAgentByName(namespace, `sse:${sessionId}`, {
|
|
367
|
+
const agent = await getAgentByName(namespace, `sse:${sessionId}`, {
|
|
368
|
+
props: ctx.props,
|
|
369
|
+
jurisdiction: options.jurisdiction
|
|
370
|
+
});
|
|
359
371
|
const existingHeaders = {};
|
|
360
372
|
request.headers.forEach((value, key) => {
|
|
361
373
|
existingHeaders[key] = value;
|
|
@@ -407,7 +419,7 @@ const createLegacySseHandler = (basePath, namespace, corsOptions) => {
|
|
|
407
419
|
"Cache-Control": "no-cache",
|
|
408
420
|
Connection: "keep-alive",
|
|
409
421
|
"Content-Type": "text/event-stream",
|
|
410
|
-
...corsHeaders(request, corsOptions)
|
|
422
|
+
...corsHeaders(request, options.corsOptions)
|
|
411
423
|
} });
|
|
412
424
|
}
|
|
413
425
|
if (request.method === "POST" && messagePattern.test(url)) {
|
|
@@ -417,15 +429,19 @@ const createLegacySseHandler = (basePath, namespace, corsOptions) => {
|
|
|
417
429
|
if (!contentType.includes("application/json")) return new Response(`Unsupported content-type: ${contentType}`, { status: 400 });
|
|
418
430
|
const contentLength = Number.parseInt(request.headers.get("content-length") || "0", 10);
|
|
419
431
|
if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) return new Response(`Request body too large: ${contentLength} bytes`, { status: 400 });
|
|
420
|
-
const agent = await getAgentByName(namespace, `sse:${sessionId}`, {
|
|
432
|
+
const agent = await getAgentByName(namespace, `sse:${sessionId}`, {
|
|
433
|
+
props: ctx.props,
|
|
434
|
+
jurisdiction: options.jurisdiction
|
|
435
|
+
});
|
|
421
436
|
const messageBody = await request.json();
|
|
422
|
-
const
|
|
437
|
+
const extraInfo = { requestInfo: { headers: Object.fromEntries(request.headers.entries()) } };
|
|
438
|
+
const error = await agent.onSSEMcpMessage(sessionId, messageBody, extraInfo);
|
|
423
439
|
if (error) return new Response(error.message, {
|
|
424
440
|
headers: {
|
|
425
441
|
"Cache-Control": "no-cache",
|
|
426
442
|
Connection: "keep-alive",
|
|
427
443
|
"Content-Type": "text/event-stream",
|
|
428
|
-
...corsHeaders(request, corsOptions)
|
|
444
|
+
...corsHeaders(request, options.corsOptions)
|
|
429
445
|
},
|
|
430
446
|
status: 400
|
|
431
447
|
});
|
|
@@ -434,7 +450,7 @@ const createLegacySseHandler = (basePath, namespace, corsOptions) => {
|
|
|
434
450
|
"Cache-Control": "no-cache",
|
|
435
451
|
Connection: "keep-alive",
|
|
436
452
|
"Content-Type": "text/event-stream",
|
|
437
|
-
...corsHeaders(request, corsOptions)
|
|
453
|
+
...corsHeaders(request, options.corsOptions)
|
|
438
454
|
},
|
|
439
455
|
status: 202
|
|
440
456
|
});
|
|
@@ -463,9 +479,12 @@ function isDurableObjectNamespace(namespace) {
|
|
|
463
479
|
//#endregion
|
|
464
480
|
//#region src/mcp/transport.ts
|
|
465
481
|
var McpSSETransport = class {
|
|
466
|
-
constructor(
|
|
482
|
+
constructor() {
|
|
467
483
|
this._started = false;
|
|
468
|
-
|
|
484
|
+
const { agent } = getCurrentAgent();
|
|
485
|
+
if (!agent) throw new Error("McpAgent was not found in Transport constructor");
|
|
486
|
+
this.sessionId = agent.getSessionId();
|
|
487
|
+
this._getWebSocket = () => agent.getWebSocket();
|
|
469
488
|
}
|
|
470
489
|
async start() {
|
|
471
490
|
if (this._started) throw new Error("Transport already started");
|
|
@@ -624,6 +643,581 @@ var StreamableHTTPServerTransport = class {
|
|
|
624
643
|
}
|
|
625
644
|
};
|
|
626
645
|
|
|
646
|
+
//#endregion
|
|
647
|
+
//#region src/mcp/client-transports.ts
|
|
648
|
+
/**
|
|
649
|
+
* Deprecated transport wrappers
|
|
650
|
+
*/
|
|
651
|
+
let didWarnAboutSSEEdgeClientTransport = false;
|
|
652
|
+
/**
|
|
653
|
+
* @deprecated Use SSEClientTransport from @modelcontextprotocol/sdk/client/sse.js instead. This alias will be removed in the next major version.
|
|
654
|
+
*/
|
|
655
|
+
var SSEEdgeClientTransport = class extends SSEClientTransport {
|
|
656
|
+
constructor(url, options) {
|
|
657
|
+
super(url, options);
|
|
658
|
+
if (!didWarnAboutSSEEdgeClientTransport) {
|
|
659
|
+
didWarnAboutSSEEdgeClientTransport = true;
|
|
660
|
+
console.warn("SSEEdgeClientTransport is deprecated. Use SSEClientTransport from @modelcontextprotocol/sdk/client/sse.js instead. SSEEdgeClientTransport will be removed in the next major version.");
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
let didWarnAboutStreamableHTTPEdgeClientTransport = false;
|
|
665
|
+
/**
|
|
666
|
+
* @deprecated Use StreamableHTTPClientTransport from @modelcontextprotocol/sdk/client/streamableHttp.js instead. This alias will be removed in the next major version.
|
|
667
|
+
*/
|
|
668
|
+
var StreamableHTTPEdgeClientTransport = class extends StreamableHTTPClientTransport {
|
|
669
|
+
constructor(url, options) {
|
|
670
|
+
super(url, options);
|
|
671
|
+
if (!didWarnAboutStreamableHTTPEdgeClientTransport) {
|
|
672
|
+
didWarnAboutStreamableHTTPEdgeClientTransport = true;
|
|
673
|
+
console.warn("StreamableHTTPEdgeClientTransport is deprecated. Use StreamableHTTPClientTransport from @modelcontextprotocol/sdk/client/streamableHttp.js instead. StreamableHTTPEdgeClientTransport will be removed in the next major version.");
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
//#endregion
|
|
679
|
+
//#region src/mcp/worker-transport.ts
|
|
680
|
+
const SUPPORTED_PROTOCOL_VERSIONS = ["2025-03-26", "2025-06-18"];
|
|
681
|
+
const DEFAULT_PROTOCOL_VERSION = "2025-03-26";
|
|
682
|
+
const MCP_PROTOCOL_VERSION_HEADER = "MCP-Protocol-Version";
|
|
683
|
+
var WorkerTransport = class {
|
|
684
|
+
constructor(options) {
|
|
685
|
+
this.started = false;
|
|
686
|
+
this.initialized = false;
|
|
687
|
+
this.enableJsonResponse = false;
|
|
688
|
+
this.standaloneSseStreamId = "_GET_stream";
|
|
689
|
+
this.streamMapping = /* @__PURE__ */ new Map();
|
|
690
|
+
this.requestToStreamMapping = /* @__PURE__ */ new Map();
|
|
691
|
+
this.requestResponseMap = /* @__PURE__ */ new Map();
|
|
692
|
+
this.stateRestored = false;
|
|
693
|
+
this.sessionIdGenerator = options?.sessionIdGenerator;
|
|
694
|
+
this.enableJsonResponse = options?.enableJsonResponse ?? false;
|
|
695
|
+
this.onsessioninitialized = options?.onsessioninitialized;
|
|
696
|
+
this.corsOptions = options?.corsOptions;
|
|
697
|
+
this.storage = options?.storage;
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Restore transport state from persistent storage.
|
|
701
|
+
* This is automatically called on start.
|
|
702
|
+
*/
|
|
703
|
+
async restoreState() {
|
|
704
|
+
if (!this.storage || this.stateRestored) return;
|
|
705
|
+
const state = await Promise.resolve(this.storage.get());
|
|
706
|
+
if (state) {
|
|
707
|
+
this.sessionId = state.sessionId;
|
|
708
|
+
this.initialized = state.initialized;
|
|
709
|
+
this.protocolVersion = state.protocolVersion;
|
|
710
|
+
}
|
|
711
|
+
this.stateRestored = true;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Persist current transport state to storage.
|
|
715
|
+
*/
|
|
716
|
+
async saveState() {
|
|
717
|
+
if (!this.storage) return;
|
|
718
|
+
const state = {
|
|
719
|
+
sessionId: this.sessionId,
|
|
720
|
+
initialized: this.initialized,
|
|
721
|
+
protocolVersion: this.protocolVersion
|
|
722
|
+
};
|
|
723
|
+
await Promise.resolve(this.storage.set(state));
|
|
724
|
+
}
|
|
725
|
+
async start() {
|
|
726
|
+
if (this.started) throw new Error("Transport already started");
|
|
727
|
+
this.started = true;
|
|
728
|
+
}
|
|
729
|
+
validateProtocolVersion(request) {
|
|
730
|
+
const versionHeader = request.headers.get(MCP_PROTOCOL_VERSION_HEADER);
|
|
731
|
+
if (!versionHeader) {
|
|
732
|
+
if (!this.protocolVersion || this.protocolVersion === DEFAULT_PROTOCOL_VERSION) return;
|
|
733
|
+
return new Response(JSON.stringify({
|
|
734
|
+
jsonrpc: "2.0",
|
|
735
|
+
error: {
|
|
736
|
+
code: -32e3,
|
|
737
|
+
message: `Bad Request: ${MCP_PROTOCOL_VERSION_HEADER} header is required`
|
|
738
|
+
},
|
|
739
|
+
id: null
|
|
740
|
+
}), {
|
|
741
|
+
status: 400,
|
|
742
|
+
headers: {
|
|
743
|
+
"Content-Type": "application/json",
|
|
744
|
+
...this.getHeaders()
|
|
745
|
+
}
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
if (!SUPPORTED_PROTOCOL_VERSIONS.includes(versionHeader)) return new Response(JSON.stringify({
|
|
749
|
+
jsonrpc: "2.0",
|
|
750
|
+
error: {
|
|
751
|
+
code: -32e3,
|
|
752
|
+
message: `Bad Request: Unsupported ${MCP_PROTOCOL_VERSION_HEADER}: ${versionHeader}. Supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(", ")}`
|
|
753
|
+
},
|
|
754
|
+
id: null
|
|
755
|
+
}), {
|
|
756
|
+
status: 400,
|
|
757
|
+
headers: {
|
|
758
|
+
"Content-Type": "application/json",
|
|
759
|
+
...this.getHeaders()
|
|
760
|
+
}
|
|
761
|
+
});
|
|
762
|
+
if (this.protocolVersion && versionHeader !== this.protocolVersion) return new Response(JSON.stringify({
|
|
763
|
+
jsonrpc: "2.0",
|
|
764
|
+
error: {
|
|
765
|
+
code: -32e3,
|
|
766
|
+
message: `Bad Request: ${MCP_PROTOCOL_VERSION_HEADER} mismatch. Expected: ${this.protocolVersion}, Got: ${versionHeader}`
|
|
767
|
+
},
|
|
768
|
+
id: null
|
|
769
|
+
}), {
|
|
770
|
+
status: 400,
|
|
771
|
+
headers: {
|
|
772
|
+
"Content-Type": "application/json",
|
|
773
|
+
...this.getHeaders()
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
getHeaders({ forPreflight } = {}) {
|
|
778
|
+
const options = {
|
|
779
|
+
origin: "*",
|
|
780
|
+
headers: "Content-Type, Accept, Authorization, mcp-session-id, MCP-Protocol-Version",
|
|
781
|
+
methods: "GET, POST, DELETE, OPTIONS",
|
|
782
|
+
exposeHeaders: "mcp-session-id",
|
|
783
|
+
maxAge: 86400,
|
|
784
|
+
...this.corsOptions
|
|
785
|
+
};
|
|
786
|
+
if (forPreflight) return {
|
|
787
|
+
"Access-Control-Allow-Origin": options.origin,
|
|
788
|
+
"Access-Control-Allow-Headers": options.headers,
|
|
789
|
+
"Access-Control-Allow-Methods": options.methods,
|
|
790
|
+
"Access-Control-Max-Age": options.maxAge.toString()
|
|
791
|
+
};
|
|
792
|
+
return {
|
|
793
|
+
"Access-Control-Allow-Origin": options.origin,
|
|
794
|
+
"Access-Control-Expose-Headers": options.exposeHeaders
|
|
795
|
+
};
|
|
796
|
+
}
|
|
797
|
+
async handleRequest(request, parsedBody) {
|
|
798
|
+
await this.restoreState();
|
|
799
|
+
switch (request.method) {
|
|
800
|
+
case "OPTIONS": return this.handleOptionsRequest(request);
|
|
801
|
+
case "GET": return this.handleGetRequest(request);
|
|
802
|
+
case "POST": return this.handlePostRequest(request, parsedBody);
|
|
803
|
+
case "DELETE": return this.handleDeleteRequest(request);
|
|
804
|
+
default: return this.handleUnsupportedRequest();
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
async handleGetRequest(request) {
|
|
808
|
+
if (!request.headers.get("Accept")?.includes("text/event-stream")) return new Response(JSON.stringify({
|
|
809
|
+
jsonrpc: "2.0",
|
|
810
|
+
error: {
|
|
811
|
+
code: -32e3,
|
|
812
|
+
message: "Not Acceptable: Client must accept text/event-stream"
|
|
813
|
+
},
|
|
814
|
+
id: null
|
|
815
|
+
}), {
|
|
816
|
+
status: 406,
|
|
817
|
+
headers: {
|
|
818
|
+
"Content-Type": "application/json",
|
|
819
|
+
...this.getHeaders()
|
|
820
|
+
}
|
|
821
|
+
});
|
|
822
|
+
const sessionError = this.validateSession(request);
|
|
823
|
+
if (sessionError) return sessionError;
|
|
824
|
+
const versionError = this.validateProtocolVersion(request);
|
|
825
|
+
if (versionError) return versionError;
|
|
826
|
+
const streamId = this.standaloneSseStreamId;
|
|
827
|
+
if (this.streamMapping.get(streamId) !== void 0) return new Response(JSON.stringify({
|
|
828
|
+
jsonrpc: "2.0",
|
|
829
|
+
error: {
|
|
830
|
+
code: -32e3,
|
|
831
|
+
message: "Conflict: Only one SSE stream is allowed per session"
|
|
832
|
+
},
|
|
833
|
+
id: null
|
|
834
|
+
}), {
|
|
835
|
+
status: 409,
|
|
836
|
+
headers: {
|
|
837
|
+
"Content-Type": "application/json",
|
|
838
|
+
...this.getHeaders()
|
|
839
|
+
}
|
|
840
|
+
});
|
|
841
|
+
const { readable, writable } = new TransformStream();
|
|
842
|
+
const writer = writable.getWriter();
|
|
843
|
+
const encoder = new TextEncoder();
|
|
844
|
+
const headers = new Headers({
|
|
845
|
+
"Content-Type": "text/event-stream",
|
|
846
|
+
"Cache-Control": "no-cache",
|
|
847
|
+
Connection: "keep-alive",
|
|
848
|
+
...this.getHeaders()
|
|
849
|
+
});
|
|
850
|
+
if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
|
|
851
|
+
const keepAlive = setInterval(() => {
|
|
852
|
+
try {
|
|
853
|
+
writer.write(encoder.encode("event: ping\ndata: \n\n"));
|
|
854
|
+
} catch {
|
|
855
|
+
clearInterval(keepAlive);
|
|
856
|
+
}
|
|
857
|
+
}, 3e4);
|
|
858
|
+
this.streamMapping.set(streamId, {
|
|
859
|
+
writer,
|
|
860
|
+
encoder,
|
|
861
|
+
cleanup: () => {
|
|
862
|
+
clearInterval(keepAlive);
|
|
863
|
+
this.streamMapping.delete(streamId);
|
|
864
|
+
writer.close().catch(() => {});
|
|
865
|
+
}
|
|
866
|
+
});
|
|
867
|
+
return new Response(readable, { headers });
|
|
868
|
+
}
|
|
869
|
+
async handlePostRequest(request, parsedBody) {
|
|
870
|
+
const acceptHeader = request.headers.get("Accept");
|
|
871
|
+
if (!acceptHeader?.includes("application/json") || !acceptHeader?.includes("text/event-stream")) return new Response(JSON.stringify({
|
|
872
|
+
jsonrpc: "2.0",
|
|
873
|
+
error: {
|
|
874
|
+
code: -32e3,
|
|
875
|
+
message: "Not Acceptable: Client must accept both application/json and text/event-stream"
|
|
876
|
+
},
|
|
877
|
+
id: null
|
|
878
|
+
}), {
|
|
879
|
+
status: 406,
|
|
880
|
+
headers: {
|
|
881
|
+
"Content-Type": "application/json",
|
|
882
|
+
...this.getHeaders()
|
|
883
|
+
}
|
|
884
|
+
});
|
|
885
|
+
if (!request.headers.get("Content-Type")?.includes("application/json")) return new Response(JSON.stringify({
|
|
886
|
+
jsonrpc: "2.0",
|
|
887
|
+
error: {
|
|
888
|
+
code: -32e3,
|
|
889
|
+
message: "Unsupported Media Type: Content-Type must be application/json"
|
|
890
|
+
},
|
|
891
|
+
id: null
|
|
892
|
+
}), {
|
|
893
|
+
status: 415,
|
|
894
|
+
headers: {
|
|
895
|
+
"Content-Type": "application/json",
|
|
896
|
+
...this.getHeaders()
|
|
897
|
+
}
|
|
898
|
+
});
|
|
899
|
+
let rawMessage = parsedBody;
|
|
900
|
+
if (rawMessage === void 0) try {
|
|
901
|
+
rawMessage = await request.json();
|
|
902
|
+
} catch {
|
|
903
|
+
return new Response(JSON.stringify({
|
|
904
|
+
jsonrpc: "2.0",
|
|
905
|
+
error: {
|
|
906
|
+
code: -32700,
|
|
907
|
+
message: "Parse error: Invalid JSON"
|
|
908
|
+
},
|
|
909
|
+
id: null
|
|
910
|
+
}), {
|
|
911
|
+
status: 400,
|
|
912
|
+
headers: {
|
|
913
|
+
"Content-Type": "application/json",
|
|
914
|
+
...this.getHeaders()
|
|
915
|
+
}
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
let messages;
|
|
919
|
+
try {
|
|
920
|
+
if (Array.isArray(rawMessage)) messages = rawMessage.map((msg) => JSONRPCMessageSchema.parse(msg));
|
|
921
|
+
else messages = [JSONRPCMessageSchema.parse(rawMessage)];
|
|
922
|
+
} catch {
|
|
923
|
+
return new Response(JSON.stringify({
|
|
924
|
+
jsonrpc: "2.0",
|
|
925
|
+
error: {
|
|
926
|
+
code: -32700,
|
|
927
|
+
message: "Parse error: Invalid JSON-RPC message"
|
|
928
|
+
},
|
|
929
|
+
id: null
|
|
930
|
+
}), {
|
|
931
|
+
status: 400,
|
|
932
|
+
headers: {
|
|
933
|
+
"Content-Type": "application/json",
|
|
934
|
+
...this.getHeaders()
|
|
935
|
+
}
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
const requestInfo = { headers: Object.fromEntries(request.headers.entries()) };
|
|
939
|
+
const isInitializationRequest = messages.some(isInitializeRequest);
|
|
940
|
+
if (isInitializationRequest) {
|
|
941
|
+
if (this.initialized && this.sessionId !== void 0) return new Response(JSON.stringify({
|
|
942
|
+
jsonrpc: "2.0",
|
|
943
|
+
error: {
|
|
944
|
+
code: -32600,
|
|
945
|
+
message: "Invalid Request: Server already initialized"
|
|
946
|
+
},
|
|
947
|
+
id: null
|
|
948
|
+
}), {
|
|
949
|
+
status: 400,
|
|
950
|
+
headers: {
|
|
951
|
+
"Content-Type": "application/json",
|
|
952
|
+
...this.getHeaders()
|
|
953
|
+
}
|
|
954
|
+
});
|
|
955
|
+
if (messages.length > 1) return new Response(JSON.stringify({
|
|
956
|
+
jsonrpc: "2.0",
|
|
957
|
+
error: {
|
|
958
|
+
code: -32600,
|
|
959
|
+
message: "Invalid Request: Only one initialization request is allowed"
|
|
960
|
+
},
|
|
961
|
+
id: null
|
|
962
|
+
}), {
|
|
963
|
+
status: 400,
|
|
964
|
+
headers: {
|
|
965
|
+
"Content-Type": "application/json",
|
|
966
|
+
...this.getHeaders()
|
|
967
|
+
}
|
|
968
|
+
});
|
|
969
|
+
const initRequest = messages.find(isInitializeRequest);
|
|
970
|
+
if (initRequest?.params) {
|
|
971
|
+
const version = initRequest.params.protocolVersion;
|
|
972
|
+
if (version && SUPPORTED_PROTOCOL_VERSIONS.includes(version)) this.protocolVersion = version;
|
|
973
|
+
else this.protocolVersion = DEFAULT_PROTOCOL_VERSION;
|
|
974
|
+
}
|
|
975
|
+
this.sessionId = this.sessionIdGenerator?.();
|
|
976
|
+
this.initialized = true;
|
|
977
|
+
await this.saveState();
|
|
978
|
+
if (this.sessionId && this.onsessioninitialized) this.onsessioninitialized(this.sessionId);
|
|
979
|
+
}
|
|
980
|
+
if (!isInitializationRequest) {
|
|
981
|
+
const sessionError = this.validateSession(request);
|
|
982
|
+
if (sessionError) return sessionError;
|
|
983
|
+
const versionError = this.validateProtocolVersion(request);
|
|
984
|
+
if (versionError) return versionError;
|
|
985
|
+
}
|
|
986
|
+
if (!messages.some(isJSONRPCRequest)) {
|
|
987
|
+
for (const message of messages) this.onmessage?.(message, { requestInfo });
|
|
988
|
+
return new Response(null, {
|
|
989
|
+
status: 202,
|
|
990
|
+
headers: { ...this.getHeaders() }
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
const streamId = crypto.randomUUID();
|
|
994
|
+
if (this.enableJsonResponse) return new Promise((resolve) => {
|
|
995
|
+
this.streamMapping.set(streamId, {
|
|
996
|
+
resolveJson: resolve,
|
|
997
|
+
cleanup: () => {
|
|
998
|
+
this.streamMapping.delete(streamId);
|
|
999
|
+
}
|
|
1000
|
+
});
|
|
1001
|
+
for (const message of messages) if (isJSONRPCRequest(message)) this.requestToStreamMapping.set(message.id, streamId);
|
|
1002
|
+
for (const message of messages) this.onmessage?.(message, { requestInfo });
|
|
1003
|
+
});
|
|
1004
|
+
const { readable, writable } = new TransformStream();
|
|
1005
|
+
const writer = writable.getWriter();
|
|
1006
|
+
const encoder = new TextEncoder();
|
|
1007
|
+
const headers = new Headers({
|
|
1008
|
+
"Content-Type": "text/event-stream",
|
|
1009
|
+
"Cache-Control": "no-cache",
|
|
1010
|
+
Connection: "keep-alive",
|
|
1011
|
+
...this.getHeaders()
|
|
1012
|
+
});
|
|
1013
|
+
if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
|
|
1014
|
+
this.streamMapping.set(streamId, {
|
|
1015
|
+
writer,
|
|
1016
|
+
encoder,
|
|
1017
|
+
cleanup: () => {
|
|
1018
|
+
this.streamMapping.delete(streamId);
|
|
1019
|
+
writer.close().catch(() => {});
|
|
1020
|
+
}
|
|
1021
|
+
});
|
|
1022
|
+
for (const message of messages) if (isJSONRPCRequest(message)) this.requestToStreamMapping.set(message.id, streamId);
|
|
1023
|
+
for (const message of messages) this.onmessage?.(message, { requestInfo });
|
|
1024
|
+
return new Response(readable, { headers });
|
|
1025
|
+
}
|
|
1026
|
+
async handleDeleteRequest(request) {
|
|
1027
|
+
const sessionError = this.validateSession(request);
|
|
1028
|
+
if (sessionError) return sessionError;
|
|
1029
|
+
const versionError = this.validateProtocolVersion(request);
|
|
1030
|
+
if (versionError) return versionError;
|
|
1031
|
+
await this.close();
|
|
1032
|
+
return new Response(null, {
|
|
1033
|
+
status: 200,
|
|
1034
|
+
headers: { ...this.getHeaders() }
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
handleOptionsRequest(_request) {
|
|
1038
|
+
return new Response(null, {
|
|
1039
|
+
status: 200,
|
|
1040
|
+
headers: { ...this.getHeaders({ forPreflight: true }) }
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
handleUnsupportedRequest() {
|
|
1044
|
+
return new Response(JSON.stringify({
|
|
1045
|
+
jsonrpc: "2.0",
|
|
1046
|
+
error: {
|
|
1047
|
+
code: -32e3,
|
|
1048
|
+
message: "Method not allowed."
|
|
1049
|
+
},
|
|
1050
|
+
id: null
|
|
1051
|
+
}), {
|
|
1052
|
+
status: 405,
|
|
1053
|
+
headers: {
|
|
1054
|
+
Allow: "GET, POST, DELETE, OPTIONS",
|
|
1055
|
+
"Content-Type": "application/json"
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
validateSession(request) {
|
|
1060
|
+
if (this.sessionIdGenerator === void 0) return;
|
|
1061
|
+
if (!this.initialized) return new Response(JSON.stringify({
|
|
1062
|
+
jsonrpc: "2.0",
|
|
1063
|
+
error: {
|
|
1064
|
+
code: -32e3,
|
|
1065
|
+
message: "Bad Request: Server not initialized"
|
|
1066
|
+
},
|
|
1067
|
+
id: null
|
|
1068
|
+
}), {
|
|
1069
|
+
status: 400,
|
|
1070
|
+
headers: {
|
|
1071
|
+
"Content-Type": "application/json",
|
|
1072
|
+
...this.getHeaders()
|
|
1073
|
+
}
|
|
1074
|
+
});
|
|
1075
|
+
const sessionId = request.headers.get("mcp-session-id");
|
|
1076
|
+
if (!sessionId) return new Response(JSON.stringify({
|
|
1077
|
+
jsonrpc: "2.0",
|
|
1078
|
+
error: {
|
|
1079
|
+
code: -32e3,
|
|
1080
|
+
message: "Bad Request: Mcp-Session-Id header is required"
|
|
1081
|
+
},
|
|
1082
|
+
id: null
|
|
1083
|
+
}), {
|
|
1084
|
+
status: 400,
|
|
1085
|
+
headers: {
|
|
1086
|
+
"Content-Type": "application/json",
|
|
1087
|
+
...this.getHeaders()
|
|
1088
|
+
}
|
|
1089
|
+
});
|
|
1090
|
+
if (sessionId !== this.sessionId) return new Response(JSON.stringify({
|
|
1091
|
+
jsonrpc: "2.0",
|
|
1092
|
+
error: {
|
|
1093
|
+
code: -32001,
|
|
1094
|
+
message: "Session not found"
|
|
1095
|
+
},
|
|
1096
|
+
id: null
|
|
1097
|
+
}), {
|
|
1098
|
+
status: 404,
|
|
1099
|
+
headers: {
|
|
1100
|
+
"Content-Type": "application/json",
|
|
1101
|
+
...this.getHeaders()
|
|
1102
|
+
}
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
async close() {
|
|
1106
|
+
for (const { cleanup } of this.streamMapping.values()) cleanup();
|
|
1107
|
+
this.streamMapping.clear();
|
|
1108
|
+
this.requestResponseMap.clear();
|
|
1109
|
+
this.onclose?.();
|
|
1110
|
+
}
|
|
1111
|
+
async send(message, options) {
|
|
1112
|
+
let requestId = options?.relatedRequestId;
|
|
1113
|
+
if (isJSONRPCResponse(message) || isJSONRPCError(message)) requestId = message.id;
|
|
1114
|
+
if (requestId === void 0) {
|
|
1115
|
+
if (isJSONRPCResponse(message) || isJSONRPCError(message)) throw new Error("Cannot send a response on a standalone SSE stream unless resuming a previous client request");
|
|
1116
|
+
const standaloneSse = this.streamMapping.get(this.standaloneSseStreamId);
|
|
1117
|
+
if (standaloneSse === void 0) return;
|
|
1118
|
+
if (standaloneSse.writer && standaloneSse.encoder) {
|
|
1119
|
+
const data = `event: message\ndata: ${JSON.stringify(message)}\n\n`;
|
|
1120
|
+
await standaloneSse.writer.write(standaloneSse.encoder.encode(data));
|
|
1121
|
+
}
|
|
1122
|
+
return;
|
|
1123
|
+
}
|
|
1124
|
+
const streamId = this.requestToStreamMapping.get(requestId);
|
|
1125
|
+
if (!streamId) throw new Error(`No connection established for request ID: ${String(requestId)}`);
|
|
1126
|
+
const response = this.streamMapping.get(streamId);
|
|
1127
|
+
if (!response) throw new Error(`No connection established for request ID: ${String(requestId)}`);
|
|
1128
|
+
if (!this.enableJsonResponse) {
|
|
1129
|
+
if (response.writer && response.encoder) {
|
|
1130
|
+
const data = `event: message\ndata: ${JSON.stringify(message)}\n\n`;
|
|
1131
|
+
await response.writer.write(response.encoder.encode(data));
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
|
|
1135
|
+
this.requestResponseMap.set(requestId, message);
|
|
1136
|
+
const relatedIds = Array.from(this.requestToStreamMapping.entries()).filter(([, sid]) => sid === streamId).map(([id]) => id);
|
|
1137
|
+
if (relatedIds.every((id) => this.requestResponseMap.has(id))) {
|
|
1138
|
+
if (this.enableJsonResponse && response.resolveJson) {
|
|
1139
|
+
const responses = relatedIds.map((id) => this.requestResponseMap.get(id));
|
|
1140
|
+
const headers = new Headers({
|
|
1141
|
+
"Content-Type": "application/json",
|
|
1142
|
+
...this.getHeaders()
|
|
1143
|
+
});
|
|
1144
|
+
if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
|
|
1145
|
+
const body = responses.length === 1 ? responses[0] : responses;
|
|
1146
|
+
response.resolveJson(new Response(JSON.stringify(body), { headers }));
|
|
1147
|
+
} else response.cleanup();
|
|
1148
|
+
for (const id of relatedIds) {
|
|
1149
|
+
this.requestResponseMap.delete(id);
|
|
1150
|
+
this.requestToStreamMapping.delete(id);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
};
|
|
1156
|
+
|
|
1157
|
+
//#endregion
|
|
1158
|
+
//#region src/mcp/auth-context.ts
|
|
1159
|
+
const authContextStorage = new AsyncLocalStorage();
|
|
1160
|
+
function getMcpAuthContext() {
|
|
1161
|
+
return authContextStorage.getStore();
|
|
1162
|
+
}
|
|
1163
|
+
function runWithAuthContext(context, fn) {
|
|
1164
|
+
return authContextStorage.run(context, fn);
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
//#endregion
|
|
1168
|
+
//#region src/mcp/handler.ts
|
|
1169
|
+
function createMcpHandler(server, options = {}) {
|
|
1170
|
+
const route = options.route ?? "/mcp";
|
|
1171
|
+
return async (request, _env, ctx) => {
|
|
1172
|
+
const url = new URL(request.url);
|
|
1173
|
+
if (route && url.pathname !== route) return new Response("Not Found", { status: 404 });
|
|
1174
|
+
const transport = options.transport ?? new WorkerTransport({
|
|
1175
|
+
sessionIdGenerator: options.sessionIdGenerator,
|
|
1176
|
+
enableJsonResponse: options.enableJsonResponse,
|
|
1177
|
+
onsessioninitialized: options.onsessioninitialized,
|
|
1178
|
+
corsOptions: options.corsOptions,
|
|
1179
|
+
storage: options.storage
|
|
1180
|
+
});
|
|
1181
|
+
const buildAuthContext = () => {
|
|
1182
|
+
if (options.authContext) return options.authContext;
|
|
1183
|
+
if (ctx.props && Object.keys(ctx.props).length > 0) return { props: ctx.props };
|
|
1184
|
+
};
|
|
1185
|
+
const handleRequest = async () => {
|
|
1186
|
+
return await transport.handleRequest(request);
|
|
1187
|
+
};
|
|
1188
|
+
const authContext = buildAuthContext();
|
|
1189
|
+
if (!transport.started) await server.connect(transport);
|
|
1190
|
+
try {
|
|
1191
|
+
if (authContext) return await runWithAuthContext(authContext, handleRequest);
|
|
1192
|
+
else return await handleRequest();
|
|
1193
|
+
} catch (error) {
|
|
1194
|
+
console.error("MCP handler error:", error);
|
|
1195
|
+
return new Response(JSON.stringify({
|
|
1196
|
+
jsonrpc: "2.0",
|
|
1197
|
+
error: {
|
|
1198
|
+
code: -32603,
|
|
1199
|
+
message: error instanceof Error ? error.message : "Internal server error"
|
|
1200
|
+
},
|
|
1201
|
+
id: null
|
|
1202
|
+
}), {
|
|
1203
|
+
status: 500,
|
|
1204
|
+
headers: { "Content-Type": "application/json" }
|
|
1205
|
+
});
|
|
1206
|
+
}
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
let didWarnAboutExperimentalCreateMcpHandler = false;
|
|
1210
|
+
/**
|
|
1211
|
+
* @deprecated This has been renamed to createMcpHandler, and experimental_createMcpHandler will be removed in the next major version
|
|
1212
|
+
*/
|
|
1213
|
+
function experimental_createMcpHandler(server, options = {}) {
|
|
1214
|
+
if (!didWarnAboutExperimentalCreateMcpHandler) {
|
|
1215
|
+
didWarnAboutExperimentalCreateMcpHandler = true;
|
|
1216
|
+
console.warn("experimental_createMcpHandler is deprecated, use createMcpHandler instead. experimental_createMcpHandler will be removed in the next major version.");
|
|
1217
|
+
}
|
|
1218
|
+
return createMcpHandler(server, options);
|
|
1219
|
+
}
|
|
1220
|
+
|
|
627
1221
|
//#endregion
|
|
628
1222
|
//#region src/mcp/index.ts
|
|
629
1223
|
var McpAgent = class McpAgent extends Agent {
|
|
@@ -663,7 +1257,7 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
663
1257
|
/** Returns a new transport matching the type of the Agent. */
|
|
664
1258
|
initTransport() {
|
|
665
1259
|
switch (this.getTransportType()) {
|
|
666
|
-
case "sse": return new McpSSETransport(
|
|
1260
|
+
case "sse": return new McpSSETransport();
|
|
667
1261
|
case "streamable-http": return new StreamableHTTPServerTransport({});
|
|
668
1262
|
}
|
|
669
1263
|
}
|
|
@@ -716,7 +1310,7 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
716
1310
|
}
|
|
717
1311
|
}
|
|
718
1312
|
/** Handles MCP Messages for the legacy SSE transport. */
|
|
719
|
-
async onSSEMcpMessage(_sessionId, messageBody) {
|
|
1313
|
+
async onSSEMcpMessage(_sessionId, messageBody, extraInfo) {
|
|
720
1314
|
if (this.getTransportType() !== "sse") return /* @__PURE__ */ new Error("Internal Server Error: Expected SSE transport");
|
|
721
1315
|
try {
|
|
722
1316
|
let parsedMessage;
|
|
@@ -727,7 +1321,7 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
727
1321
|
throw error;
|
|
728
1322
|
}
|
|
729
1323
|
if (await this._handleElicitationResponse(parsedMessage)) return null;
|
|
730
|
-
this._transport?.onmessage?.(parsedMessage);
|
|
1324
|
+
this._transport?.onmessage?.(parsedMessage, extraInfo);
|
|
731
1325
|
return null;
|
|
732
1326
|
} catch (error) {
|
|
733
1327
|
console.error("Error forwarding message to SSE:", error);
|
|
@@ -813,7 +1407,7 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
813
1407
|
/** Return a handler for the given path for this MCP.
|
|
814
1408
|
* Defaults to Streamable HTTP transport.
|
|
815
1409
|
*/
|
|
816
|
-
static serve(path, { binding = "MCP_OBJECT", corsOptions, transport = "streamable-http" } = {}) {
|
|
1410
|
+
static serve(path, { binding = "MCP_OBJECT", corsOptions, transport = "streamable-http", jurisdiction } = {}) {
|
|
817
1411
|
return { async fetch(request, env, ctx) {
|
|
818
1412
|
const corsResponse = handleCORS(request, corsOptions);
|
|
819
1413
|
if (corsResponse) return corsResponse;
|
|
@@ -822,8 +1416,14 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
822
1416
|
if (!isDurableObjectNamespace(bindingValue)) throw new Error(`Invalid McpAgent binding for ${binding}. Make sure it's a Durable Object binding.`);
|
|
823
1417
|
const namespace = bindingValue;
|
|
824
1418
|
switch (transport) {
|
|
825
|
-
case "streamable-http": return createStreamingHttpHandler(path, namespace,
|
|
826
|
-
|
|
1419
|
+
case "streamable-http": return createStreamingHttpHandler(path, namespace, {
|
|
1420
|
+
corsOptions,
|
|
1421
|
+
jurisdiction
|
|
1422
|
+
})(request, ctx);
|
|
1423
|
+
case "sse": return createLegacySseHandler(path, namespace, {
|
|
1424
|
+
corsOptions,
|
|
1425
|
+
jurisdiction
|
|
1426
|
+
})(request, ctx);
|
|
827
1427
|
default: return new Response("Invalid MCP transport mode. Only `streamable-http` or `sse` are allowed.", { status: 500 });
|
|
828
1428
|
}
|
|
829
1429
|
} };
|
|
@@ -843,5 +1443,5 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
843
1443
|
};
|
|
844
1444
|
|
|
845
1445
|
//#endregion
|
|
846
|
-
export { ElicitRequestSchema, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport };
|
|
1446
|
+
export { ElicitRequestSchema, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext };
|
|
847
1447
|
//# sourceMappingURL=index.js.map
|