agents 0.0.0-96add46 → 0.0.0-9741e80
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/ai-chat-agent.d.ts +2 -2
- package/dist/ai-chat-agent.js +2 -2
- package/dist/ai-react.d.ts +3 -3
- package/dist/cli.d.ts +8 -0
- package/dist/cli.js +27 -0
- package/dist/cli.js.map +1 -0
- package/dist/{client-C1R7IU9g.d.ts → client-BEMdXPYJ.d.ts} +15 -13
- package/dist/{client-9Ld2_lnt.js → client-JMskg2fw.js} +10 -3
- package/dist/client-JMskg2fw.js.map +1 -0
- package/dist/codemode/ai.js +2 -2
- package/dist/{index-ZB3447pb.d.ts → index-BXG3v10y.d.ts} +4 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/mcp/client.d.ts +1 -1
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/index.d.ts +31 -6
- package/dist/mcp/index.js +200 -65
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.js +2 -2
- package/dist/{react-CLI3oFqf.d.ts → react-k5kPzKcj.d.ts} +2 -2
- package/dist/react.d.ts +3 -3
- package/dist/{src-Dz0H9hSU.js → src-nFNV3Ttx.js} +26 -10
- package/dist/{src-Dz0H9hSU.js.map → src-nFNV3Ttx.js.map} +1 -1
- package/package.json +10 -3
- package/dist/client-9Ld2_lnt.js.map +0 -1
package/dist/mcp/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { t as MessageType } from "../ai-types-B3aQaFv3.js";
|
|
2
2
|
import "../client-BfiZ3HQd.js";
|
|
3
|
-
import { a as SSEEdgeClientTransport, i as StreamableHTTPEdgeClientTransport } from "../client-
|
|
3
|
+
import { a as SSEEdgeClientTransport, i as StreamableHTTPEdgeClientTransport } from "../client-JMskg2fw.js";
|
|
4
4
|
import "../do-oauth-client-provider-CswoD5Lu.js";
|
|
5
|
-
import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-
|
|
5
|
+
import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-nFNV3Ttx.js";
|
|
6
6
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
7
7
|
import { ElicitRequestSchema, InitializeRequestSchema, JSONRPCMessageSchema, isInitializeRequest, isJSONRPCError, isJSONRPCNotification, isJSONRPCRequest, isJSONRPCResponse } from "@modelcontextprotocol/sdk/types.js";
|
|
8
8
|
|
|
@@ -21,7 +21,7 @@ const MCP_HTTP_METHOD_HEADER = "cf-mcp-method";
|
|
|
21
21
|
*/
|
|
22
22
|
const MCP_MESSAGE_HEADER = "cf-mcp-message";
|
|
23
23
|
const MAXIMUM_MESSAGE_SIZE_BYTES = 4 * 1024 * 1024;
|
|
24
|
-
const createStreamingHttpHandler = (basePath, namespace,
|
|
24
|
+
const createStreamingHttpHandler = (basePath, namespace, options = {}) => {
|
|
25
25
|
let pathname = basePath;
|
|
26
26
|
if (basePath === "/") pathname = "/*";
|
|
27
27
|
const basePattern = new URLPattern({ pathname });
|
|
@@ -130,7 +130,10 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
130
130
|
return new Response(body$1, { status: 400 });
|
|
131
131
|
}
|
|
132
132
|
sessionId = sessionId ?? namespace.newUniqueId().toString();
|
|
133
|
-
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, {
|
|
133
|
+
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, {
|
|
134
|
+
props: ctx.props,
|
|
135
|
+
jurisdiction: options.jurisdiction
|
|
136
|
+
});
|
|
134
137
|
const isInitialized = await agent.getInitializeRequest();
|
|
135
138
|
if (maybeInitializeRequest) await agent.setInitializeRequest(maybeInitializeRequest);
|
|
136
139
|
else if (!isInitialized) {
|
|
@@ -205,7 +208,7 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
205
208
|
if (messages.every((msg) => isJSONRPCNotification(msg) || isJSONRPCResponse(msg))) {
|
|
206
209
|
ws.close();
|
|
207
210
|
return new Response(null, {
|
|
208
|
-
headers: corsHeaders(request, corsOptions),
|
|
211
|
+
headers: corsHeaders(request, options.corsOptions),
|
|
209
212
|
status: 202
|
|
210
213
|
});
|
|
211
214
|
}
|
|
@@ -215,7 +218,7 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
215
218
|
Connection: "keep-alive",
|
|
216
219
|
"Content-Type": "text/event-stream",
|
|
217
220
|
"mcp-session-id": sessionId,
|
|
218
|
-
...corsHeaders(request, corsOptions)
|
|
221
|
+
...corsHeaders(request, options.corsOptions)
|
|
219
222
|
},
|
|
220
223
|
status: 200
|
|
221
224
|
});
|
|
@@ -243,7 +246,10 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
243
246
|
const { readable, writable } = new TransformStream();
|
|
244
247
|
const writer = writable.getWriter();
|
|
245
248
|
const encoder = new TextEncoder();
|
|
246
|
-
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, {
|
|
249
|
+
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, {
|
|
250
|
+
props: ctx.props,
|
|
251
|
+
jurisdiction: options.jurisdiction
|
|
252
|
+
});
|
|
247
253
|
if (!await agent.getInitializeRequest()) return new Response(JSON.stringify({
|
|
248
254
|
jsonrpc: "2.0",
|
|
249
255
|
error: {
|
|
@@ -292,7 +298,7 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
292
298
|
Connection: "keep-alive",
|
|
293
299
|
"Content-Type": "text/event-stream",
|
|
294
300
|
"mcp-session-id": sessionId,
|
|
295
|
-
...corsHeaders(request, corsOptions)
|
|
301
|
+
...corsHeaders(request, options.corsOptions)
|
|
296
302
|
},
|
|
297
303
|
status: 200
|
|
298
304
|
});
|
|
@@ -307,9 +313,9 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
307
313
|
id: null
|
|
308
314
|
}), {
|
|
309
315
|
status: 400,
|
|
310
|
-
headers: corsHeaders(request, corsOptions)
|
|
316
|
+
headers: corsHeaders(request, options.corsOptions)
|
|
311
317
|
});
|
|
312
|
-
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}
|
|
318
|
+
const agent = await getAgentByName(namespace, `streamable-http:${sessionId}`, { jurisdiction: options.jurisdiction });
|
|
313
319
|
if (!await agent.getInitializeRequest()) return new Response(JSON.stringify({
|
|
314
320
|
jsonrpc: "2.0",
|
|
315
321
|
error: {
|
|
@@ -319,12 +325,12 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
319
325
|
id: null
|
|
320
326
|
}), {
|
|
321
327
|
status: 404,
|
|
322
|
-
headers: corsHeaders(request, corsOptions)
|
|
328
|
+
headers: corsHeaders(request, options.corsOptions)
|
|
323
329
|
});
|
|
324
330
|
ctx.waitUntil(agent.destroy().catch(() => {}));
|
|
325
331
|
return new Response(null, {
|
|
326
332
|
status: 204,
|
|
327
|
-
headers: corsHeaders(request, corsOptions)
|
|
333
|
+
headers: corsHeaders(request, options.corsOptions)
|
|
328
334
|
});
|
|
329
335
|
}
|
|
330
336
|
}
|
|
@@ -339,7 +345,7 @@ const createStreamingHttpHandler = (basePath, namespace, corsOptions) => {
|
|
|
339
345
|
return new Response(body, { status: 404 });
|
|
340
346
|
};
|
|
341
347
|
};
|
|
342
|
-
const createLegacySseHandler = (basePath, namespace,
|
|
348
|
+
const createLegacySseHandler = (basePath, namespace, options = {}) => {
|
|
343
349
|
let pathname = basePath;
|
|
344
350
|
if (basePath === "/") pathname = "/*";
|
|
345
351
|
const basePattern = new URLPattern({ pathname });
|
|
@@ -356,7 +362,10 @@ const createLegacySseHandler = (basePath, namespace, corsOptions) => {
|
|
|
356
362
|
endpointUrl.searchParams.set("sessionId", sessionId);
|
|
357
363
|
const endpointMessage = `event: endpoint\ndata: ${endpointUrl.pathname + endpointUrl.search + endpointUrl.hash}\n\n`;
|
|
358
364
|
writer.write(encoder.encode(endpointMessage));
|
|
359
|
-
const agent = await getAgentByName(namespace, `sse:${sessionId}`, {
|
|
365
|
+
const agent = await getAgentByName(namespace, `sse:${sessionId}`, {
|
|
366
|
+
props: ctx.props,
|
|
367
|
+
jurisdiction: options.jurisdiction
|
|
368
|
+
});
|
|
360
369
|
const existingHeaders = {};
|
|
361
370
|
request.headers.forEach((value, key) => {
|
|
362
371
|
existingHeaders[key] = value;
|
|
@@ -408,7 +417,7 @@ const createLegacySseHandler = (basePath, namespace, corsOptions) => {
|
|
|
408
417
|
"Cache-Control": "no-cache",
|
|
409
418
|
Connection: "keep-alive",
|
|
410
419
|
"Content-Type": "text/event-stream",
|
|
411
|
-
...corsHeaders(request, corsOptions)
|
|
420
|
+
...corsHeaders(request, options.corsOptions)
|
|
412
421
|
} });
|
|
413
422
|
}
|
|
414
423
|
if (request.method === "POST" && messagePattern.test(url)) {
|
|
@@ -418,7 +427,10 @@ const createLegacySseHandler = (basePath, namespace, corsOptions) => {
|
|
|
418
427
|
if (!contentType.includes("application/json")) return new Response(`Unsupported content-type: ${contentType}`, { status: 400 });
|
|
419
428
|
const contentLength = Number.parseInt(request.headers.get("content-length") || "0", 10);
|
|
420
429
|
if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) return new Response(`Request body too large: ${contentLength} bytes`, { status: 400 });
|
|
421
|
-
const agent = await getAgentByName(namespace, `sse:${sessionId}`, {
|
|
430
|
+
const agent = await getAgentByName(namespace, `sse:${sessionId}`, {
|
|
431
|
+
props: ctx.props,
|
|
432
|
+
jurisdiction: options.jurisdiction
|
|
433
|
+
});
|
|
422
434
|
const messageBody = await request.json();
|
|
423
435
|
const error = await agent.onSSEMcpMessage(sessionId, messageBody);
|
|
424
436
|
if (error) return new Response(error.message, {
|
|
@@ -426,7 +438,7 @@ const createLegacySseHandler = (basePath, namespace, corsOptions) => {
|
|
|
426
438
|
"Cache-Control": "no-cache",
|
|
427
439
|
Connection: "keep-alive",
|
|
428
440
|
"Content-Type": "text/event-stream",
|
|
429
|
-
...corsHeaders(request, corsOptions)
|
|
441
|
+
...corsHeaders(request, options.corsOptions)
|
|
430
442
|
},
|
|
431
443
|
status: 400
|
|
432
444
|
});
|
|
@@ -435,7 +447,7 @@ const createLegacySseHandler = (basePath, namespace, corsOptions) => {
|
|
|
435
447
|
"Cache-Control": "no-cache",
|
|
436
448
|
Connection: "keep-alive",
|
|
437
449
|
"Content-Type": "text/event-stream",
|
|
438
|
-
...corsHeaders(request, corsOptions)
|
|
450
|
+
...corsHeaders(request, options.corsOptions)
|
|
439
451
|
},
|
|
440
452
|
status: 202
|
|
441
453
|
});
|
|
@@ -627,6 +639,9 @@ var StreamableHTTPServerTransport = class {
|
|
|
627
639
|
|
|
628
640
|
//#endregion
|
|
629
641
|
//#region src/mcp/worker-transport.ts
|
|
642
|
+
const SUPPORTED_PROTOCOL_VERSIONS = ["2025-03-26", "2025-06-18"];
|
|
643
|
+
const DEFAULT_PROTOCOL_VERSION = "2025-03-26";
|
|
644
|
+
const MCP_PROTOCOL_VERSION_HEADER = "MCP-Protocol-Version";
|
|
630
645
|
var WorkerTransport = class {
|
|
631
646
|
constructor(options) {
|
|
632
647
|
this.started = false;
|
|
@@ -639,11 +654,80 @@ var WorkerTransport = class {
|
|
|
639
654
|
this.sessionIdGenerator = options?.sessionIdGenerator;
|
|
640
655
|
this.enableJsonResponse = options?.enableJsonResponse ?? false;
|
|
641
656
|
this.onsessioninitialized = options?.onsessioninitialized;
|
|
657
|
+
this.corsOptions = options?.corsOptions;
|
|
642
658
|
}
|
|
643
659
|
async start() {
|
|
644
660
|
if (this.started) throw new Error("Transport already started");
|
|
645
661
|
this.started = true;
|
|
646
662
|
}
|
|
663
|
+
validateProtocolVersion(request) {
|
|
664
|
+
const versionHeader = request.headers.get(MCP_PROTOCOL_VERSION_HEADER);
|
|
665
|
+
if (!versionHeader) {
|
|
666
|
+
if (!this.protocolVersion || this.protocolVersion === DEFAULT_PROTOCOL_VERSION) return;
|
|
667
|
+
return new Response(JSON.stringify({
|
|
668
|
+
jsonrpc: "2.0",
|
|
669
|
+
error: {
|
|
670
|
+
code: -32e3,
|
|
671
|
+
message: `Bad Request: ${MCP_PROTOCOL_VERSION_HEADER} header is required`
|
|
672
|
+
},
|
|
673
|
+
id: null
|
|
674
|
+
}), {
|
|
675
|
+
status: 400,
|
|
676
|
+
headers: {
|
|
677
|
+
"Content-Type": "application/json",
|
|
678
|
+
...this.getHeaders()
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
if (!SUPPORTED_PROTOCOL_VERSIONS.includes(versionHeader)) return new Response(JSON.stringify({
|
|
683
|
+
jsonrpc: "2.0",
|
|
684
|
+
error: {
|
|
685
|
+
code: -32e3,
|
|
686
|
+
message: `Bad Request: Unsupported ${MCP_PROTOCOL_VERSION_HEADER}: ${versionHeader}. Supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(", ")}`
|
|
687
|
+
},
|
|
688
|
+
id: null
|
|
689
|
+
}), {
|
|
690
|
+
status: 400,
|
|
691
|
+
headers: {
|
|
692
|
+
"Content-Type": "application/json",
|
|
693
|
+
...this.getHeaders()
|
|
694
|
+
}
|
|
695
|
+
});
|
|
696
|
+
if (this.protocolVersion && versionHeader !== this.protocolVersion) return new Response(JSON.stringify({
|
|
697
|
+
jsonrpc: "2.0",
|
|
698
|
+
error: {
|
|
699
|
+
code: -32e3,
|
|
700
|
+
message: `Bad Request: ${MCP_PROTOCOL_VERSION_HEADER} mismatch. Expected: ${this.protocolVersion}, Got: ${versionHeader}`
|
|
701
|
+
},
|
|
702
|
+
id: null
|
|
703
|
+
}), {
|
|
704
|
+
status: 400,
|
|
705
|
+
headers: {
|
|
706
|
+
"Content-Type": "application/json",
|
|
707
|
+
...this.getHeaders()
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
getHeaders({ forPreflight } = {}) {
|
|
712
|
+
const options = {
|
|
713
|
+
origin: "*",
|
|
714
|
+
headers: "Content-Type, Accept, Authorization, mcp-session-id, MCP-Protocol-Version",
|
|
715
|
+
methods: "GET, POST, DELETE, OPTIONS",
|
|
716
|
+
exposeHeaders: "mcp-session-id",
|
|
717
|
+
maxAge: 86400,
|
|
718
|
+
...this.corsOptions
|
|
719
|
+
};
|
|
720
|
+
if (forPreflight) return {
|
|
721
|
+
"Access-Control-Allow-Origin": options.origin,
|
|
722
|
+
"Access-Control-Allow-Headers": options.headers,
|
|
723
|
+
"Access-Control-Allow-Methods": options.methods,
|
|
724
|
+
"Access-Control-Max-Age": options.maxAge.toString()
|
|
725
|
+
};
|
|
726
|
+
return {
|
|
727
|
+
"Access-Control-Allow-Origin": options.origin,
|
|
728
|
+
"Access-Control-Expose-Headers": options.exposeHeaders
|
|
729
|
+
};
|
|
730
|
+
}
|
|
647
731
|
async handleRequest(request, parsedBody) {
|
|
648
732
|
switch (request.method) {
|
|
649
733
|
case "OPTIONS": return this.handleOptionsRequest(request);
|
|
@@ -663,10 +747,15 @@ var WorkerTransport = class {
|
|
|
663
747
|
id: null
|
|
664
748
|
}), {
|
|
665
749
|
status: 406,
|
|
666
|
-
headers: {
|
|
750
|
+
headers: {
|
|
751
|
+
"Content-Type": "application/json",
|
|
752
|
+
...this.getHeaders()
|
|
753
|
+
}
|
|
667
754
|
});
|
|
668
|
-
const
|
|
669
|
-
if (
|
|
755
|
+
const sessionError = this.validateSession(request);
|
|
756
|
+
if (sessionError) return sessionError;
|
|
757
|
+
const versionError = this.validateProtocolVersion(request);
|
|
758
|
+
if (versionError) return versionError;
|
|
670
759
|
const streamId = this.standaloneSseStreamId;
|
|
671
760
|
if (this.streamMapping.get(streamId) !== void 0) return new Response(JSON.stringify({
|
|
672
761
|
jsonrpc: "2.0",
|
|
@@ -677,7 +766,10 @@ var WorkerTransport = class {
|
|
|
677
766
|
id: null
|
|
678
767
|
}), {
|
|
679
768
|
status: 409,
|
|
680
|
-
headers: {
|
|
769
|
+
headers: {
|
|
770
|
+
"Content-Type": "application/json",
|
|
771
|
+
...this.getHeaders()
|
|
772
|
+
}
|
|
681
773
|
});
|
|
682
774
|
const { readable, writable } = new TransformStream();
|
|
683
775
|
const writer = writable.getWriter();
|
|
@@ -686,8 +778,7 @@ var WorkerTransport = class {
|
|
|
686
778
|
"Content-Type": "text/event-stream",
|
|
687
779
|
"Cache-Control": "no-cache",
|
|
688
780
|
Connection: "keep-alive",
|
|
689
|
-
|
|
690
|
-
"Access-Control-Expose-Headers": "mcp-session-id"
|
|
781
|
+
...this.getHeaders()
|
|
691
782
|
});
|
|
692
783
|
if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
|
|
693
784
|
const keepAlive = setInterval(() => {
|
|
@@ -719,7 +810,10 @@ var WorkerTransport = class {
|
|
|
719
810
|
id: null
|
|
720
811
|
}), {
|
|
721
812
|
status: 406,
|
|
722
|
-
headers: {
|
|
813
|
+
headers: {
|
|
814
|
+
"Content-Type": "application/json",
|
|
815
|
+
...this.getHeaders()
|
|
816
|
+
}
|
|
723
817
|
});
|
|
724
818
|
if (!request.headers.get("Content-Type")?.includes("application/json")) return new Response(JSON.stringify({
|
|
725
819
|
jsonrpc: "2.0",
|
|
@@ -730,7 +824,10 @@ var WorkerTransport = class {
|
|
|
730
824
|
id: null
|
|
731
825
|
}), {
|
|
732
826
|
status: 415,
|
|
733
|
-
headers: {
|
|
827
|
+
headers: {
|
|
828
|
+
"Content-Type": "application/json",
|
|
829
|
+
...this.getHeaders()
|
|
830
|
+
}
|
|
734
831
|
});
|
|
735
832
|
let rawMessage = parsedBody;
|
|
736
833
|
if (rawMessage === void 0) try {
|
|
@@ -745,7 +842,10 @@ var WorkerTransport = class {
|
|
|
745
842
|
id: null
|
|
746
843
|
}), {
|
|
747
844
|
status: 400,
|
|
748
|
-
headers: {
|
|
845
|
+
headers: {
|
|
846
|
+
"Content-Type": "application/json",
|
|
847
|
+
...this.getHeaders()
|
|
848
|
+
}
|
|
749
849
|
});
|
|
750
850
|
}
|
|
751
851
|
let messages;
|
|
@@ -762,9 +862,13 @@ var WorkerTransport = class {
|
|
|
762
862
|
id: null
|
|
763
863
|
}), {
|
|
764
864
|
status: 400,
|
|
765
|
-
headers: {
|
|
865
|
+
headers: {
|
|
866
|
+
"Content-Type": "application/json",
|
|
867
|
+
...this.getHeaders()
|
|
868
|
+
}
|
|
766
869
|
});
|
|
767
870
|
}
|
|
871
|
+
const requestInfo = { headers: Object.fromEntries(request.headers.entries()) };
|
|
768
872
|
const isInitializationRequest = messages.some(isInitializeRequest);
|
|
769
873
|
if (isInitializationRequest) {
|
|
770
874
|
if (this.initialized && this.sessionId !== void 0) return new Response(JSON.stringify({
|
|
@@ -776,7 +880,10 @@ var WorkerTransport = class {
|
|
|
776
880
|
id: null
|
|
777
881
|
}), {
|
|
778
882
|
status: 400,
|
|
779
|
-
headers: {
|
|
883
|
+
headers: {
|
|
884
|
+
"Content-Type": "application/json",
|
|
885
|
+
...this.getHeaders()
|
|
886
|
+
}
|
|
780
887
|
});
|
|
781
888
|
if (messages.length > 1) return new Response(JSON.stringify({
|
|
782
889
|
jsonrpc: "2.0",
|
|
@@ -787,21 +894,32 @@ var WorkerTransport = class {
|
|
|
787
894
|
id: null
|
|
788
895
|
}), {
|
|
789
896
|
status: 400,
|
|
790
|
-
headers: {
|
|
897
|
+
headers: {
|
|
898
|
+
"Content-Type": "application/json",
|
|
899
|
+
...this.getHeaders()
|
|
900
|
+
}
|
|
791
901
|
});
|
|
902
|
+
const initRequest = messages.find(isInitializeRequest);
|
|
903
|
+
if (initRequest?.params) {
|
|
904
|
+
const version = initRequest.params.protocolVersion;
|
|
905
|
+
if (version && SUPPORTED_PROTOCOL_VERSIONS.includes(version)) this.protocolVersion = version;
|
|
906
|
+
else this.protocolVersion = DEFAULT_PROTOCOL_VERSION;
|
|
907
|
+
}
|
|
792
908
|
this.sessionId = this.sessionIdGenerator?.();
|
|
793
909
|
this.initialized = true;
|
|
794
910
|
if (this.sessionId && this.onsessioninitialized) this.onsessioninitialized(this.sessionId);
|
|
795
911
|
}
|
|
796
912
|
if (!isInitializationRequest) {
|
|
797
|
-
const
|
|
798
|
-
if (
|
|
913
|
+
const sessionError = this.validateSession(request);
|
|
914
|
+
if (sessionError) return sessionError;
|
|
915
|
+
const versionError = this.validateProtocolVersion(request);
|
|
916
|
+
if (versionError) return versionError;
|
|
799
917
|
}
|
|
800
918
|
if (!messages.some(isJSONRPCRequest)) {
|
|
801
|
-
for (const message of messages) this.onmessage?.(message);
|
|
919
|
+
for (const message of messages) this.onmessage?.(message, { requestInfo });
|
|
802
920
|
return new Response(null, {
|
|
803
921
|
status: 202,
|
|
804
|
-
headers: {
|
|
922
|
+
headers: { ...this.getHeaders() }
|
|
805
923
|
});
|
|
806
924
|
}
|
|
807
925
|
const streamId = crypto.randomUUID();
|
|
@@ -813,7 +931,7 @@ var WorkerTransport = class {
|
|
|
813
931
|
}
|
|
814
932
|
});
|
|
815
933
|
for (const message of messages) if (isJSONRPCRequest(message)) this.requestToStreamMapping.set(message.id, streamId);
|
|
816
|
-
for (const message of messages) this.onmessage?.(message);
|
|
934
|
+
for (const message of messages) this.onmessage?.(message, { requestInfo });
|
|
817
935
|
});
|
|
818
936
|
const { readable, writable } = new TransformStream();
|
|
819
937
|
const writer = writable.getWriter();
|
|
@@ -822,8 +940,7 @@ var WorkerTransport = class {
|
|
|
822
940
|
"Content-Type": "text/event-stream",
|
|
823
941
|
"Cache-Control": "no-cache",
|
|
824
942
|
Connection: "keep-alive",
|
|
825
|
-
|
|
826
|
-
"Access-Control-Expose-Headers": "mcp-session-id"
|
|
943
|
+
...this.getHeaders()
|
|
827
944
|
});
|
|
828
945
|
if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
|
|
829
946
|
this.streamMapping.set(streamId, {
|
|
@@ -835,28 +952,24 @@ var WorkerTransport = class {
|
|
|
835
952
|
}
|
|
836
953
|
});
|
|
837
954
|
for (const message of messages) if (isJSONRPCRequest(message)) this.requestToStreamMapping.set(message.id, streamId);
|
|
838
|
-
for (const message of messages) this.onmessage?.(message);
|
|
955
|
+
for (const message of messages) this.onmessage?.(message, { requestInfo });
|
|
839
956
|
return new Response(readable, { headers });
|
|
840
957
|
}
|
|
841
958
|
async handleDeleteRequest(request) {
|
|
842
|
-
const
|
|
843
|
-
if (
|
|
959
|
+
const sessionError = this.validateSession(request);
|
|
960
|
+
if (sessionError) return sessionError;
|
|
961
|
+
const versionError = this.validateProtocolVersion(request);
|
|
962
|
+
if (versionError) return versionError;
|
|
844
963
|
await this.close();
|
|
845
964
|
return new Response(null, {
|
|
846
965
|
status: 200,
|
|
847
|
-
headers: {
|
|
966
|
+
headers: { ...this.getHeaders() }
|
|
848
967
|
});
|
|
849
968
|
}
|
|
850
969
|
handleOptionsRequest(_request) {
|
|
851
|
-
const headers = new Headers({
|
|
852
|
-
"Access-Control-Allow-Origin": "*",
|
|
853
|
-
"Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
|
|
854
|
-
"Access-Control-Allow-Headers": "Content-Type, Accept, Authorization, mcp-session-id",
|
|
855
|
-
"Access-Control-Max-Age": "86400"
|
|
856
|
-
});
|
|
857
970
|
return new Response(null, {
|
|
858
|
-
status:
|
|
859
|
-
headers
|
|
971
|
+
status: 200,
|
|
972
|
+
headers: { ...this.getHeaders({ forPreflight: true }) }
|
|
860
973
|
});
|
|
861
974
|
}
|
|
862
975
|
handleUnsupportedRequest() {
|
|
@@ -876,7 +989,7 @@ var WorkerTransport = class {
|
|
|
876
989
|
});
|
|
877
990
|
}
|
|
878
991
|
validateSession(request) {
|
|
879
|
-
if (this.sessionIdGenerator === void 0) return
|
|
992
|
+
if (this.sessionIdGenerator === void 0) return;
|
|
880
993
|
if (!this.initialized) return new Response(JSON.stringify({
|
|
881
994
|
jsonrpc: "2.0",
|
|
882
995
|
error: {
|
|
@@ -886,7 +999,10 @@ var WorkerTransport = class {
|
|
|
886
999
|
id: null
|
|
887
1000
|
}), {
|
|
888
1001
|
status: 400,
|
|
889
|
-
headers: {
|
|
1002
|
+
headers: {
|
|
1003
|
+
"Content-Type": "application/json",
|
|
1004
|
+
...this.getHeaders()
|
|
1005
|
+
}
|
|
890
1006
|
});
|
|
891
1007
|
const sessionId = request.headers.get("mcp-session-id");
|
|
892
1008
|
if (!sessionId) return new Response(JSON.stringify({
|
|
@@ -898,7 +1014,10 @@ var WorkerTransport = class {
|
|
|
898
1014
|
id: null
|
|
899
1015
|
}), {
|
|
900
1016
|
status: 400,
|
|
901
|
-
headers: {
|
|
1017
|
+
headers: {
|
|
1018
|
+
"Content-Type": "application/json",
|
|
1019
|
+
...this.getHeaders()
|
|
1020
|
+
}
|
|
902
1021
|
});
|
|
903
1022
|
if (sessionId !== this.sessionId) return new Response(JSON.stringify({
|
|
904
1023
|
jsonrpc: "2.0",
|
|
@@ -909,9 +1028,11 @@ var WorkerTransport = class {
|
|
|
909
1028
|
id: null
|
|
910
1029
|
}), {
|
|
911
1030
|
status: 404,
|
|
912
|
-
headers: {
|
|
1031
|
+
headers: {
|
|
1032
|
+
"Content-Type": "application/json",
|
|
1033
|
+
...this.getHeaders()
|
|
1034
|
+
}
|
|
913
1035
|
});
|
|
914
|
-
return true;
|
|
915
1036
|
}
|
|
916
1037
|
async close() {
|
|
917
1038
|
for (const { cleanup } of this.streamMapping.values()) cleanup();
|
|
@@ -950,8 +1071,7 @@ var WorkerTransport = class {
|
|
|
950
1071
|
const responses = relatedIds.map((id) => this.requestResponseMap.get(id));
|
|
951
1072
|
const headers = new Headers({
|
|
952
1073
|
"Content-Type": "application/json",
|
|
953
|
-
|
|
954
|
-
"Access-Control-Expose-Headers": "mcp-session-id"
|
|
1074
|
+
...this.getHeaders()
|
|
955
1075
|
});
|
|
956
1076
|
if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
|
|
957
1077
|
const body = responses.length === 1 ? responses[0] : responses;
|
|
@@ -978,7 +1098,7 @@ function runWithAuthContext(context, fn) {
|
|
|
978
1098
|
|
|
979
1099
|
//#endregion
|
|
980
1100
|
//#region src/mcp/handler.ts
|
|
981
|
-
function
|
|
1101
|
+
function createMcpHandler(server, options = {}) {
|
|
982
1102
|
const route = options.route ?? "/mcp";
|
|
983
1103
|
return async (request, _env, ctx) => {
|
|
984
1104
|
const url = new URL(request.url);
|
|
@@ -991,10 +1111,8 @@ function experimental_createMcpHandler(server, options = {}) {
|
|
|
991
1111
|
return await transport.handleRequest(request);
|
|
992
1112
|
};
|
|
993
1113
|
try {
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
else response = await handleRequest();
|
|
997
|
-
return response;
|
|
1114
|
+
if (authContext) return await runWithAuthContext(authContext, handleRequest);
|
|
1115
|
+
else return await handleRequest();
|
|
998
1116
|
} catch (error) {
|
|
999
1117
|
console.error("MCP handler error:", error);
|
|
1000
1118
|
return new Response(JSON.stringify({
|
|
@@ -1011,6 +1129,17 @@ function experimental_createMcpHandler(server, options = {}) {
|
|
|
1011
1129
|
}
|
|
1012
1130
|
};
|
|
1013
1131
|
}
|
|
1132
|
+
let didWarnAboutExperimentalCreateMcpHandler = false;
|
|
1133
|
+
/**
|
|
1134
|
+
* @deprecated This has been renamed to createMcpHandler, and experimental_createMcpHandler will be removed in the next major version
|
|
1135
|
+
*/
|
|
1136
|
+
function experimental_createMcpHandler(server, options = {}) {
|
|
1137
|
+
if (!didWarnAboutExperimentalCreateMcpHandler) {
|
|
1138
|
+
didWarnAboutExperimentalCreateMcpHandler = true;
|
|
1139
|
+
console.warn("experimental_createMcpHandler is deprecated, use createMcpHandler instead. experimental_createMcpHandler will be removed in the next major version.");
|
|
1140
|
+
}
|
|
1141
|
+
return createMcpHandler(server, options);
|
|
1142
|
+
}
|
|
1014
1143
|
|
|
1015
1144
|
//#endregion
|
|
1016
1145
|
//#region src/mcp/index.ts
|
|
@@ -1201,7 +1330,7 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
1201
1330
|
/** Return a handler for the given path for this MCP.
|
|
1202
1331
|
* Defaults to Streamable HTTP transport.
|
|
1203
1332
|
*/
|
|
1204
|
-
static serve(path, { binding = "MCP_OBJECT", corsOptions, transport = "streamable-http" } = {}) {
|
|
1333
|
+
static serve(path, { binding = "MCP_OBJECT", corsOptions, transport = "streamable-http", jurisdiction } = {}) {
|
|
1205
1334
|
return { async fetch(request, env, ctx) {
|
|
1206
1335
|
const corsResponse = handleCORS(request, corsOptions);
|
|
1207
1336
|
if (corsResponse) return corsResponse;
|
|
@@ -1210,8 +1339,14 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
1210
1339
|
if (!isDurableObjectNamespace(bindingValue)) throw new Error(`Invalid McpAgent binding for ${binding}. Make sure it's a Durable Object binding.`);
|
|
1211
1340
|
const namespace = bindingValue;
|
|
1212
1341
|
switch (transport) {
|
|
1213
|
-
case "streamable-http": return createStreamingHttpHandler(path, namespace,
|
|
1214
|
-
|
|
1342
|
+
case "streamable-http": return createStreamingHttpHandler(path, namespace, {
|
|
1343
|
+
corsOptions,
|
|
1344
|
+
jurisdiction
|
|
1345
|
+
})(request, ctx);
|
|
1346
|
+
case "sse": return createLegacySseHandler(path, namespace, {
|
|
1347
|
+
corsOptions,
|
|
1348
|
+
jurisdiction
|
|
1349
|
+
})(request, ctx);
|
|
1215
1350
|
default: return new Response("Invalid MCP transport mode. Only `streamable-http` or `sse` are allowed.", { status: 500 });
|
|
1216
1351
|
}
|
|
1217
1352
|
} };
|
|
@@ -1231,5 +1366,5 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
1231
1366
|
};
|
|
1232
1367
|
|
|
1233
1368
|
//#endregion
|
|
1234
|
-
export { ElicitRequestSchema, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, experimental_createMcpHandler, getMcpAuthContext };
|
|
1369
|
+
export { ElicitRequestSchema, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext };
|
|
1235
1370
|
//# sourceMappingURL=index.js.map
|