agents 0.0.0-b803d5e → 0.0.0-b916d85

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.
Files changed (44) hide show
  1. package/README.md +127 -22
  2. package/dist/ai-chat-agent.d.ts +7 -4
  3. package/dist/ai-chat-agent.js +64 -26
  4. package/dist/ai-chat-agent.js.map +1 -1
  5. package/dist/ai-react.d.ts +12 -9
  6. package/dist/ai-react.js +27 -27
  7. package/dist/ai-react.js.map +1 -1
  8. package/dist/{chunk-J6T74FUS.js → chunk-3IQQY2UH.js} +618 -131
  9. package/dist/chunk-3IQQY2UH.js.map +1 -0
  10. package/dist/{chunk-QSGN3REV.js → chunk-KUH345EY.js} +8 -15
  11. package/dist/chunk-KUH345EY.js.map +1 -0
  12. package/dist/{chunk-BZXOAZUX.js → chunk-PVQZBKN7.js} +5 -5
  13. package/dist/chunk-PVQZBKN7.js.map +1 -0
  14. package/dist/{chunk-Y67CHZBI.js → chunk-UNG3FXYX.js} +80 -19
  15. package/dist/chunk-UNG3FXYX.js.map +1 -0
  16. package/dist/client.d.ts +8 -2
  17. package/dist/client.js +1 -1
  18. package/dist/index-CLW1aEBr.d.ts +615 -0
  19. package/dist/index.d.ts +35 -405
  20. package/dist/index.js +10 -4
  21. package/dist/mcp/client.d.ts +290 -19
  22. package/dist/mcp/client.js +1 -1
  23. package/dist/mcp/do-oauth-client-provider.js +1 -1
  24. package/dist/mcp/index.d.ts +34 -11
  25. package/dist/mcp/index.js +71 -52
  26. package/dist/mcp/index.js.map +1 -1
  27. package/dist/observability/index.d.ts +14 -0
  28. package/dist/observability/index.js +10 -0
  29. package/dist/observability/index.js.map +1 -0
  30. package/dist/react.d.ts +49 -39
  31. package/dist/react.js +7 -7
  32. package/dist/react.js.map +1 -1
  33. package/dist/schedule.d.ts +6 -6
  34. package/dist/schedule.js +4 -4
  35. package/dist/schedule.js.map +1 -1
  36. package/dist/serializable.d.ts +32 -0
  37. package/dist/serializable.js +1 -0
  38. package/dist/serializable.js.map +1 -0
  39. package/package.json +76 -72
  40. package/src/index.ts +819 -174
  41. package/dist/chunk-BZXOAZUX.js.map +0 -1
  42. package/dist/chunk-J6T74FUS.js.map +0 -1
  43. package/dist/chunk-QSGN3REV.js.map +0 -1
  44. package/dist/chunk-Y67CHZBI.js.map +0 -1
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  MCPClientManager,
3
3
  getNamespacedData
4
- } from "../chunk-Y67CHZBI.js";
4
+ } from "../chunk-UNG3FXYX.js";
5
5
  export {
6
6
  MCPClientManager,
7
7
  getNamespacedData
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  DurableObjectOAuthClientProvider
3
- } from "../chunk-BZXOAZUX.js";
3
+ } from "../chunk-PVQZBKN7.js";
4
4
  export {
5
5
  DurableObjectOAuthClientProvider
6
6
  };
@@ -1,17 +1,40 @@
1
1
  import { MCPClientManager } from './client.js';
2
2
  import { DurableObject } from 'cloudflare:workers';
3
- import { Connection, WSMessage } from 'partyserver';
4
- import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
5
3
  import { Server } from '@modelcontextprotocol/sdk/server/index.js';
4
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
5
+ import { Connection, WSMessage } from 'partyserver';
6
+ import { SSEClientTransport, SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
7
+ import { StreamableHTTPClientTransport, StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
6
8
  import 'zod';
7
- import '@modelcontextprotocol/sdk/types.js';
8
9
  import '@modelcontextprotocol/sdk/client/index.js';
9
- import '@modelcontextprotocol/sdk/client/sse.js';
10
+ import '@modelcontextprotocol/sdk/shared/protocol.js';
11
+ import '@modelcontextprotocol/sdk/types.js';
12
+ import 'ai';
10
13
  import './do-oauth-client-provider.js';
11
14
  import '@modelcontextprotocol/sdk/client/auth.js';
12
15
  import '@modelcontextprotocol/sdk/shared/auth.js';
13
- import '@modelcontextprotocol/sdk/shared/protocol.js';
14
- import 'ai';
16
+
17
+ declare class SSEEdgeClientTransport extends SSEClientTransport {
18
+ private authProvider;
19
+ /**
20
+ * Creates a new EdgeSSEClientTransport, which overrides fetch to be compatible with the CF workers environment
21
+ */
22
+ constructor(url: URL, options: SSEClientTransportOptions);
23
+ authHeaders(): Promise<{
24
+ Authorization: string;
25
+ } | undefined>;
26
+ }
27
+
28
+ declare class StreamableHTTPEdgeClientTransport extends StreamableHTTPClientTransport {
29
+ private authProvider;
30
+ /**
31
+ * Creates a new StreamableHTTPEdgeClientTransport, which overrides fetch to be compatible with the CF workers environment
32
+ */
33
+ constructor(url: URL, options: StreamableHTTPClientTransportOptions);
34
+ authHeaders(): Promise<{
35
+ Authorization: string;
36
+ } | undefined>;
37
+ }
15
38
 
16
39
  interface CORSOptions {
17
40
  origin?: string;
@@ -57,23 +80,23 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
57
80
  getWebSocket(): WebSocket | null;
58
81
  getWebSocketForResponseID(id: string): WebSocket | null;
59
82
  onMessage(connection: Connection, event: WSMessage): Promise<void>;
60
- onSSEMcpMessage(sessionId: string, request: Request): Promise<Error | null>;
83
+ onSSEMcpMessage(_sessionId: string, request: Request): Promise<Error | null>;
61
84
  webSocketMessage(ws: WebSocket, event: ArrayBuffer | string): Promise<void>;
62
85
  webSocketError(ws: WebSocket, error: unknown): Promise<void>;
63
86
  webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): Promise<void>;
64
- static mount(path: string, { binding, corsOptions, }?: {
87
+ static mount(path: string, { binding, corsOptions }?: {
65
88
  binding?: string;
66
89
  corsOptions?: CORSOptions;
67
90
  }): {
68
91
  fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
69
92
  };
70
- static serveSSE(path: string, { binding, corsOptions, }?: {
93
+ static serveSSE(path: string, { binding, corsOptions }?: {
71
94
  binding?: string;
72
95
  corsOptions?: CORSOptions;
73
96
  }): {
74
97
  fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
75
98
  };
76
- static serve(path: string, { binding, corsOptions, }?: {
99
+ static serve(path: string, { binding, corsOptions }?: {
77
100
  binding?: string;
78
101
  corsOptions?: CORSOptions;
79
102
  }): {
@@ -81,4 +104,4 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
81
104
  };
82
105
  }
83
106
 
84
- export { McpAgent };
107
+ export { McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport };
package/dist/mcp/index.js CHANGED
@@ -1,31 +1,37 @@
1
1
  import {
2
2
  Agent
3
- } from "../chunk-J6T74FUS.js";
4
- import "../chunk-BZXOAZUX.js";
5
- import "../chunk-QSGN3REV.js";
6
- import "../chunk-Y67CHZBI.js";
3
+ } from "../chunk-3IQQY2UH.js";
4
+ import {
5
+ SSEEdgeClientTransport,
6
+ StreamableHTTPEdgeClientTransport
7
+ } from "../chunk-UNG3FXYX.js";
8
+ import "../chunk-PVQZBKN7.js";
9
+ import "../chunk-KUH345EY.js";
7
10
 
8
11
  // src/mcp/index.ts
9
12
  import { DurableObject } from "cloudflare:workers";
10
13
  import {
11
14
  InitializeRequestSchema,
15
+ JSONRPCMessageSchema,
12
16
  isJSONRPCError,
13
17
  isJSONRPCNotification,
14
18
  isJSONRPCRequest,
15
- isJSONRPCResponse,
16
- JSONRPCMessageSchema
19
+ isJSONRPCResponse
17
20
  } from "@modelcontextprotocol/sdk/types.js";
18
21
  var MAXIMUM_MESSAGE_SIZE_BYTES = 4 * 1024 * 1024;
19
- function corsHeaders(request, corsOptions = {}) {
22
+ function corsHeaders(_request, corsOptions = {}) {
20
23
  const origin = "*";
21
24
  return {
22
- "Access-Control-Allow-Origin": corsOptions.origin || origin,
25
+ "Access-Control-Allow-Headers": corsOptions.headers || "Content-Type, mcp-session-id, mcp-protocol-version",
23
26
  "Access-Control-Allow-Methods": corsOptions.methods || "GET, POST, OPTIONS",
24
- "Access-Control-Allow-Headers": corsOptions.headers || "Content-Type, mcp-session-id",
25
- "Access-Control-Max-Age": (corsOptions.maxAge || 86400).toString(),
26
- "Access-Control-Expose-Headers": corsOptions.exposeHeaders || "mcp-session-id"
27
+ "Access-Control-Allow-Origin": corsOptions.origin || origin,
28
+ "Access-Control-Expose-Headers": corsOptions.exposeHeaders || "mcp-session-id",
29
+ "Access-Control-Max-Age": (corsOptions.maxAge || 86400).toString()
27
30
  };
28
31
  }
32
+ function isDurableObjectNamespace(namespace) {
33
+ return typeof namespace === "object" && namespace !== null && "newUniqueId" in namespace && typeof namespace.newUniqueId === "function" && "idFromName" in namespace && typeof namespace.idFromName === "function";
34
+ }
29
35
  function handleCORS(request, corsOptions) {
30
36
  if (request.method === "OPTIONS") {
31
37
  return new Response(null, { headers: corsHeaders(request, corsOptions) });
@@ -138,6 +144,7 @@ var McpAgent = class _McpAgent extends DurableObject {
138
144
  setState(state) {
139
145
  return this._agent.setState(state);
140
146
  }
147
+ // biome-ignore lint/correctness/noUnusedFunctionParameters: overriden later
141
148
  onStateUpdate(state, source) {
142
149
  }
143
150
  async onStart() {
@@ -284,7 +291,7 @@ var McpAgent = class _McpAgent extends DurableObject {
284
291
  }
285
292
  // All messages received over SSE after the initial connection has been established
286
293
  // will be passed here
287
- async onSSEMcpMessage(sessionId, request) {
294
+ async onSSEMcpMessage(_sessionId, request) {
288
295
  if (this._status !== "started") {
289
296
  await this._initialize();
290
297
  }
@@ -356,7 +363,7 @@ var McpAgent = class _McpAgent extends DurableObject {
356
363
  );
357
364
  return new Response("Invalid binding", { status: 500 });
358
365
  }
359
- if (bindingValue.toString() !== "[object DurableObjectNamespace]") {
366
+ if (!isDurableObjectNamespace(bindingValue)) {
360
367
  return new Response("Invalid binding", { status: 500 });
361
368
  }
362
369
  const namespace = bindingValue;
@@ -379,9 +386,14 @@ data: ${relativeUrlWithSession}
379
386
  await doStub._init(ctx.props);
380
387
  const upgradeUrl = new URL(request.url);
381
388
  upgradeUrl.pathname = "/sse";
389
+ const existingHeaders = {};
390
+ request.headers.forEach((value, key) => {
391
+ existingHeaders[key] = value;
392
+ });
382
393
  const response = await doStub.fetch(
383
394
  new Request(upgradeUrl, {
384
395
  headers: {
396
+ ...existingHeaders,
385
397
  Upgrade: "websocket",
386
398
  // Required by PartyServer
387
399
  "x-partykit-room": sessionId
@@ -417,10 +429,10 @@ data: ${JSON.stringify(result.data)}
417
429
  onMessage(event).catch(console.error);
418
430
  });
419
431
  ws.addEventListener("error", (error) => {
420
- async function onError(error2) {
432
+ async function onError(_error) {
421
433
  try {
422
434
  await writer.close();
423
- } catch (e) {
435
+ } catch (_e) {
424
436
  }
425
437
  }
426
438
  onError(error).catch(console.error);
@@ -437,9 +449,9 @@ data: ${JSON.stringify(result.data)}
437
449
  });
438
450
  return new Response(readable, {
439
451
  headers: {
440
- "Content-Type": "text/event-stream",
441
452
  "Cache-Control": "no-cache",
442
453
  Connection: "keep-alive",
454
+ "Content-Type": "text/event-stream",
443
455
  ...corsHeaders(request, corsOptions)
444
456
  }
445
457
  });
@@ -475,23 +487,23 @@ data: ${JSON.stringify(result.data)}
475
487
  const error = await doStub.onSSEMcpMessage(sessionId, request);
476
488
  if (error) {
477
489
  return new Response(error.message, {
478
- status: 400,
479
490
  headers: {
480
- "Content-Type": "text/event-stream",
481
491
  "Cache-Control": "no-cache",
482
492
  Connection: "keep-alive",
493
+ "Content-Type": "text/event-stream",
483
494
  ...corsHeaders(request, corsOptions)
484
- }
495
+ },
496
+ status: 400
485
497
  });
486
498
  }
487
499
  return new Response("Accepted", {
488
- status: 202,
489
500
  headers: {
490
- "Content-Type": "text/event-stream",
491
501
  "Cache-Control": "no-cache",
492
502
  Connection: "keep-alive",
503
+ "Content-Type": "text/event-stream",
493
504
  ...corsHeaders(request, corsOptions)
494
- }
505
+ },
506
+ status: 202
495
507
  });
496
508
  }
497
509
  return new Response("Not Found", { status: 404 });
@@ -521,7 +533,7 @@ data: ${JSON.stringify(result.data)}
521
533
  );
522
534
  return new Response("Invalid binding", { status: 500 });
523
535
  }
524
- if (bindingValue.toString() !== "[object DurableObjectNamespace]") {
536
+ if (!isDurableObjectNamespace(bindingValue)) {
525
537
  return new Response("Invalid binding", { status: 500 });
526
538
  }
527
539
  const namespace = bindingValue;
@@ -529,24 +541,24 @@ data: ${JSON.stringify(result.data)}
529
541
  const acceptHeader = request.headers.get("accept");
530
542
  if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
531
543
  const body2 = JSON.stringify({
532
- jsonrpc: "2.0",
533
544
  error: {
534
545
  code: -32e3,
535
546
  message: "Not Acceptable: Client must accept both application/json and text/event-stream"
536
547
  },
537
- id: null
548
+ id: null,
549
+ jsonrpc: "2.0"
538
550
  });
539
551
  return new Response(body2, { status: 406 });
540
552
  }
541
553
  const ct = request.headers.get("content-type");
542
554
  if (!ct || !ct.includes("application/json")) {
543
555
  const body2 = JSON.stringify({
544
- jsonrpc: "2.0",
545
556
  error: {
546
557
  code: -32e3,
547
558
  message: "Unsupported Media Type: Content-Type must be application/json"
548
559
  },
549
- id: null
560
+ id: null,
561
+ jsonrpc: "2.0"
550
562
  });
551
563
  return new Response(body2, { status: 415 });
552
564
  }
@@ -556,12 +568,12 @@ data: ${JSON.stringify(result.data)}
556
568
  );
557
569
  if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) {
558
570
  const body2 = JSON.stringify({
559
- jsonrpc: "2.0",
560
571
  error: {
561
572
  code: -32e3,
562
573
  message: `Request body too large. Maximum size is ${MAXIMUM_MESSAGE_SIZE_BYTES} bytes`
563
574
  },
564
- id: null
575
+ id: null,
576
+ jsonrpc: "2.0"
565
577
  });
566
578
  return new Response(body2, { status: 413 });
567
579
  }
@@ -569,14 +581,14 @@ data: ${JSON.stringify(result.data)}
569
581
  let rawMessage;
570
582
  try {
571
583
  rawMessage = await request.json();
572
- } catch (error) {
584
+ } catch (_error) {
573
585
  const body2 = JSON.stringify({
574
- jsonrpc: "2.0",
575
586
  error: {
576
587
  code: -32700,
577
588
  message: "Parse error: Invalid JSON"
578
589
  },
579
- id: null
590
+ id: null,
591
+ jsonrpc: "2.0"
580
592
  });
581
593
  return new Response(body2, { status: 400 });
582
594
  }
@@ -590,12 +602,12 @@ data: ${JSON.stringify(result.data)}
590
602
  for (const msg of arrayMessage) {
591
603
  if (!JSONRPCMessageSchema.safeParse(msg).success) {
592
604
  const body2 = JSON.stringify({
593
- jsonrpc: "2.0",
594
605
  error: {
595
606
  code: -32700,
596
607
  message: "Parse error: Invalid JSON-RPC message"
597
608
  },
598
- id: null
609
+ id: null,
610
+ jsonrpc: "2.0"
599
611
  });
600
612
  return new Response(body2, { status: 400 });
601
613
  }
@@ -606,34 +618,34 @@ data: ${JSON.stringify(result.data)}
606
618
  );
607
619
  if (isInitializationRequest && sessionId) {
608
620
  const body2 = JSON.stringify({
609
- jsonrpc: "2.0",
610
621
  error: {
611
622
  code: -32600,
612
623
  message: "Invalid Request: Initialization requests must not include a sessionId"
613
624
  },
614
- id: null
625
+ id: null,
626
+ jsonrpc: "2.0"
615
627
  });
616
628
  return new Response(body2, { status: 400 });
617
629
  }
618
630
  if (isInitializationRequest && messages.length > 1) {
619
631
  const body2 = JSON.stringify({
620
- jsonrpc: "2.0",
621
632
  error: {
622
633
  code: -32600,
623
634
  message: "Invalid Request: Only one initialization request is allowed"
624
635
  },
625
- id: null
636
+ id: null,
637
+ jsonrpc: "2.0"
626
638
  });
627
639
  return new Response(body2, { status: 400 });
628
640
  }
629
641
  if (!isInitializationRequest && !sessionId) {
630
642
  const body2 = JSON.stringify({
631
- jsonrpc: "2.0",
632
643
  error: {
633
644
  code: -32e3,
634
645
  message: "Bad Request: Mcp-Session-Id header is required"
635
646
  },
636
- id: null
647
+ id: null,
648
+ jsonrpc: "2.0"
637
649
  });
638
650
  return new Response(body2, { status: 400 });
639
651
  }
@@ -646,12 +658,12 @@ data: ${JSON.stringify(result.data)}
646
658
  await doStub.setInitialized();
647
659
  } else if (!isInitialized) {
648
660
  const body2 = JSON.stringify({
649
- jsonrpc: "2.0",
650
661
  error: {
651
662
  code: -32001,
652
663
  message: "Session not found"
653
664
  },
654
- id: null
665
+ id: null,
666
+ jsonrpc: "2.0"
655
667
  });
656
668
  return new Response(body2, { status: 404 });
657
669
  }
@@ -660,9 +672,14 @@ data: ${JSON.stringify(result.data)}
660
672
  const encoder = new TextEncoder();
661
673
  const upgradeUrl = new URL(request.url);
662
674
  upgradeUrl.pathname = "/streamable-http";
675
+ const existingHeaders = {};
676
+ request.headers.forEach((value, key) => {
677
+ existingHeaders[key] = value;
678
+ });
663
679
  const response = await doStub.fetch(
664
680
  new Request(upgradeUrl, {
665
681
  headers: {
682
+ ...existingHeaders,
666
683
  Upgrade: "websocket",
667
684
  // Required by PartyServer
668
685
  "x-partykit-room": sessionId
@@ -674,12 +691,12 @@ data: ${JSON.stringify(result.data)}
674
691
  console.error("Failed to establish WebSocket connection");
675
692
  await writer.close();
676
693
  const body2 = JSON.stringify({
677
- jsonrpc: "2.0",
678
694
  error: {
679
695
  code: -32001,
680
696
  message: "Failed to establish WebSocket connection"
681
697
  },
682
- id: null
698
+ id: null,
699
+ jsonrpc: "2.0"
683
700
  });
684
701
  return new Response(body2, { status: 500 });
685
702
  }
@@ -712,10 +729,10 @@ data: ${JSON.stringify(result.data)}
712
729
  onMessage(event).catch(console.error);
713
730
  });
714
731
  ws.addEventListener("error", (error) => {
715
- async function onError(error2) {
732
+ async function onError(_error) {
716
733
  try {
717
734
  await writer.close();
718
- } catch (e) {
735
+ } catch (_e) {
719
736
  }
720
737
  }
721
738
  onError(error).catch(console.error);
@@ -739,8 +756,8 @@ data: ${JSON.stringify(result.data)}
739
756
  }
740
757
  ws.close();
741
758
  return new Response(null, {
742
- status: 202,
743
- headers: corsHeaders(request, corsOptions)
759
+ headers: corsHeaders(request, corsOptions),
760
+ status: 202
744
761
  });
745
762
  }
746
763
  for (const message of messages) {
@@ -751,9 +768,9 @@ data: ${JSON.stringify(result.data)}
751
768
  }
752
769
  return new Response(readable, {
753
770
  headers: {
754
- "Content-Type": "text/event-stream",
755
771
  "Cache-Control": "no-cache",
756
772
  Connection: "keep-alive",
773
+ "Content-Type": "text/event-stream",
757
774
  "mcp-session-id": sessionId,
758
775
  ...corsHeaders(request, corsOptions)
759
776
  },
@@ -761,12 +778,12 @@ data: ${JSON.stringify(result.data)}
761
778
  });
762
779
  }
763
780
  const body = JSON.stringify({
764
- jsonrpc: "2.0",
765
781
  error: {
766
782
  code: -32e3,
767
783
  message: "Method not allowed"
768
784
  },
769
- id: null
785
+ id: null,
786
+ jsonrpc: "2.0"
770
787
  });
771
788
  return new Response(body, { status: 405 });
772
789
  }
@@ -774,6 +791,8 @@ data: ${JSON.stringify(result.data)}
774
791
  }
775
792
  };
776
793
  export {
777
- McpAgent
794
+ McpAgent,
795
+ SSEEdgeClientTransport,
796
+ StreamableHTTPEdgeClientTransport
778
797
  };
779
798
  //# sourceMappingURL=index.js.map