agents 0.0.0-ff431ff → 0.0.0-ff45307

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 (63) hide show
  1. package/README.md +5 -3
  2. package/dist/ai-chat-agent.d.ts +94 -13
  3. package/dist/ai-chat-agent.js +346 -74
  4. package/dist/ai-chat-agent.js.map +1 -1
  5. package/dist/{ai-chat-v5-migration-DBHGW4Hv.js → ai-chat-v5-migration-BSiGZmYU.js} +1 -1
  6. package/dist/{ai-chat-v5-migration-DBHGW4Hv.js.map → ai-chat-v5-migration-BSiGZmYU.js.map} +1 -1
  7. package/dist/ai-chat-v5-migration.js +1 -1
  8. package/dist/ai-react.d.ts +14 -9
  9. package/dist/ai-react.js +179 -29
  10. package/dist/ai-react.js.map +1 -1
  11. package/dist/{ai-types-D5YoPrBZ.d.ts → ai-types-81H_-Uxh.d.ts} +15 -7
  12. package/dist/{ai-types-B3aQaFv3.js → ai-types-CrMqkwc_.js} +5 -1
  13. package/dist/ai-types-CrMqkwc_.js.map +1 -0
  14. package/dist/ai-types.d.ts +1 -1
  15. package/dist/ai-types.js +1 -1
  16. package/dist/cli/index.d.ts +1 -0
  17. package/dist/{cli.js → cli/index.js} +7 -6
  18. package/dist/cli/index.js.map +1 -0
  19. package/dist/{client-BfiZ3HQd.js → client-B3SR12TQ.js} +2 -2
  20. package/dist/{client-BfiZ3HQd.js.map → client-B3SR12TQ.js.map} +1 -1
  21. package/dist/{client-CbWe9FBd.d.ts → client-BAQA84dr.d.ts} +2 -2
  22. package/dist/client-CFhjXCiO.js +1093 -0
  23. package/dist/client-CFhjXCiO.js.map +1 -0
  24. package/dist/client-CwqTTb-B.d.ts +833 -0
  25. package/dist/client.d.ts +2 -2
  26. package/dist/client.js +2 -2
  27. package/dist/codemode/ai.js +5 -5
  28. package/dist/{do-oauth-client-provider-DGc5pP0l.d.ts → do-oauth-client-provider-C2CHH5x-.d.ts} +1 -1
  29. package/dist/{do-oauth-client-provider-CswoD5Lu.js → do-oauth-client-provider-CwqK5SXm.js} +2 -1
  30. package/dist/do-oauth-client-provider-CwqK5SXm.js.map +1 -0
  31. package/dist/{index-DhJCaDWd.d.ts → index-BUle9RiP.d.ts} +2 -2
  32. package/dist/{index-DxXJbYBM.d.ts → index-DJ4vV2-x.d.ts} +53 -26
  33. package/dist/index.d.ts +6 -6
  34. package/dist/index.js +5 -5
  35. package/dist/mcp/client.d.ts +4 -4
  36. package/dist/mcp/client.js +2 -1
  37. package/dist/mcp/do-oauth-client-provider.d.ts +1 -1
  38. package/dist/mcp/do-oauth-client-provider.js +1 -1
  39. package/dist/mcp/index.d.ts +73 -34
  40. package/dist/mcp/index.js +95 -18
  41. package/dist/mcp/index.js.map +1 -1
  42. package/dist/mcp/x402.js +10 -6
  43. package/dist/mcp/x402.js.map +1 -1
  44. package/dist/{mcp-Dw5vDrY8.d.ts → mcp-BwPscEiF.d.ts} +1 -1
  45. package/dist/observability/index.d.ts +2 -2
  46. package/dist/observability/index.js +5 -5
  47. package/dist/{react-BIh615qN.d.ts → react-w4GT20p4.d.ts} +33 -35
  48. package/dist/react.d.ts +9 -9
  49. package/dist/react.js +2 -2
  50. package/dist/react.js.map +1 -1
  51. package/dist/{serializable-CymX8ovI.d.ts → serializable-faDkMCai.d.ts} +1 -1
  52. package/dist/serializable.d.ts +1 -1
  53. package/dist/{src-nFNV3Ttx.js → src-tXpYCgas.js} +99 -131
  54. package/dist/src-tXpYCgas.js.map +1 -0
  55. package/package.json +48 -40
  56. package/dist/ai-types-B3aQaFv3.js.map +0 -1
  57. package/dist/cli.d.ts +0 -8
  58. package/dist/cli.js.map +0 -1
  59. package/dist/client-DbS2uI6p.d.ts +0 -5315
  60. package/dist/client-JMskg2fw.js +0 -793
  61. package/dist/client-JMskg2fw.js.map +0 -1
  62. package/dist/do-oauth-client-provider-CswoD5Lu.js.map +0 -1
  63. package/dist/src-nFNV3Ttx.js.map +0 -1
package/dist/mcp/index.js CHANGED
@@ -1,9 +1,11 @@
1
- import { t as MessageType } from "../ai-types-B3aQaFv3.js";
2
- import "../client-BfiZ3HQd.js";
3
- import { a as SSEEdgeClientTransport, i as StreamableHTTPEdgeClientTransport } from "../client-JMskg2fw.js";
4
- import "../do-oauth-client-provider-CswoD5Lu.js";
5
- import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-nFNV3Ttx.js";
1
+ import { t as MessageType } from "../ai-types-CrMqkwc_.js";
2
+ import "../client-B3SR12TQ.js";
3
+ import "../client-CFhjXCiO.js";
4
+ import "../do-oauth-client-provider-CwqK5SXm.js";
5
+ import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-tXpYCgas.js";
6
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";
7
9
  import { ElicitRequestSchema, InitializeRequestSchema, JSONRPCMessageSchema, isInitializeRequest, isJSONRPCError, isJSONRPCNotification, isJSONRPCRequest, isJSONRPCResponse } from "@modelcontextprotocol/sdk/types.js";
8
10
 
9
11
  //#region src/mcp/utils.ts
@@ -432,7 +434,8 @@ const createLegacySseHandler = (basePath, namespace, options = {}) => {
432
434
  jurisdiction: options.jurisdiction
433
435
  });
434
436
  const messageBody = await request.json();
435
- const error = await agent.onSSEMcpMessage(sessionId, messageBody);
437
+ const extraInfo = { requestInfo: { headers: Object.fromEntries(request.headers.entries()) } };
438
+ const error = await agent.onSSEMcpMessage(sessionId, messageBody, extraInfo);
436
439
  if (error) return new Response(error.message, {
437
440
  headers: {
438
441
  "Cache-Control": "no-cache",
@@ -476,9 +479,12 @@ function isDurableObjectNamespace(namespace) {
476
479
  //#endregion
477
480
  //#region src/mcp/transport.ts
478
481
  var McpSSETransport = class {
479
- constructor(getWebSocket) {
482
+ constructor() {
480
483
  this._started = false;
481
- this._getWebSocket = getWebSocket;
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();
482
488
  }
483
489
  async start() {
484
490
  if (this._started) throw new Error("Transport already started");
@@ -637,6 +643,38 @@ var StreamableHTTPServerTransport = class {
637
643
  }
638
644
  };
639
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
+
640
678
  //#endregion
641
679
  //#region src/mcp/worker-transport.ts
642
680
  const SUPPORTED_PROTOCOL_VERSIONS = ["2025-03-26", "2025-06-18"];
@@ -651,10 +689,38 @@ var WorkerTransport = class {
651
689
  this.streamMapping = /* @__PURE__ */ new Map();
652
690
  this.requestToStreamMapping = /* @__PURE__ */ new Map();
653
691
  this.requestResponseMap = /* @__PURE__ */ new Map();
692
+ this.stateRestored = false;
654
693
  this.sessionIdGenerator = options?.sessionIdGenerator;
655
694
  this.enableJsonResponse = options?.enableJsonResponse ?? false;
656
695
  this.onsessioninitialized = options?.onsessioninitialized;
657
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));
658
724
  }
659
725
  async start() {
660
726
  if (this.started) throw new Error("Transport already started");
@@ -729,6 +795,7 @@ var WorkerTransport = class {
729
795
  };
730
796
  }
731
797
  async handleRequest(request, parsedBody) {
798
+ await this.restoreState();
732
799
  switch (request.method) {
733
800
  case "OPTIONS": return this.handleOptionsRequest(request);
734
801
  case "GET": return this.handleGetRequest(request);
@@ -801,7 +868,7 @@ var WorkerTransport = class {
801
868
  }
802
869
  async handlePostRequest(request, parsedBody) {
803
870
  const acceptHeader = request.headers.get("Accept");
804
- if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) return new Response(JSON.stringify({
871
+ if (!acceptHeader?.includes("application/json") || !acceptHeader?.includes("text/event-stream")) return new Response(JSON.stringify({
805
872
  jsonrpc: "2.0",
806
873
  error: {
807
874
  code: -32e3,
@@ -907,6 +974,7 @@ var WorkerTransport = class {
907
974
  }
908
975
  this.sessionId = this.sessionIdGenerator?.();
909
976
  this.initialized = true;
977
+ await this.saveState();
910
978
  if (this.sessionId && this.onsessioninitialized) this.onsessioninitialized(this.sessionId);
911
979
  }
912
980
  if (!isInitializationRequest) {
@@ -1040,8 +1108,8 @@ var WorkerTransport = class {
1040
1108
  this.requestResponseMap.clear();
1041
1109
  this.onclose?.();
1042
1110
  }
1043
- async send(message) {
1044
- let requestId;
1111
+ async send(message, options) {
1112
+ let requestId = options?.relatedRequestId;
1045
1113
  if (isJSONRPCResponse(message) || isJSONRPCError(message)) requestId = message.id;
1046
1114
  if (requestId === void 0) {
1047
1115
  if (isJSONRPCResponse(message) || isJSONRPCError(message)) throw new Error("Cannot send a response on a standalone SSE stream unless resuming a previous client request");
@@ -1103,13 +1171,22 @@ function createMcpHandler(server, options = {}) {
1103
1171
  return async (request, _env, ctx) => {
1104
1172
  const url = new URL(request.url);
1105
1173
  if (route && url.pathname !== route) return new Response("Not Found", { status: 404 });
1106
- const oauthCtx = ctx;
1107
- const authContext = oauthCtx.props ? { props: oauthCtx.props } : void 0;
1108
- const transport = new WorkerTransport(options);
1109
- await server.connect(transport);
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
+ };
1110
1185
  const handleRequest = async () => {
1111
1186
  return await transport.handleRequest(request);
1112
1187
  };
1188
+ const authContext = buildAuthContext();
1189
+ if (!transport.started) await server.connect(transport);
1113
1190
  try {
1114
1191
  if (authContext) return await runWithAuthContext(authContext, handleRequest);
1115
1192
  else return await handleRequest();
@@ -1180,7 +1257,7 @@ var McpAgent = class McpAgent extends Agent {
1180
1257
  /** Returns a new transport matching the type of the Agent. */
1181
1258
  initTransport() {
1182
1259
  switch (this.getTransportType()) {
1183
- case "sse": return new McpSSETransport(() => this.getWebSocket());
1260
+ case "sse": return new McpSSETransport();
1184
1261
  case "streamable-http": return new StreamableHTTPServerTransport({});
1185
1262
  }
1186
1263
  }
@@ -1233,7 +1310,7 @@ var McpAgent = class McpAgent extends Agent {
1233
1310
  }
1234
1311
  }
1235
1312
  /** Handles MCP Messages for the legacy SSE transport. */
1236
- async onSSEMcpMessage(_sessionId, messageBody) {
1313
+ async onSSEMcpMessage(_sessionId, messageBody, extraInfo) {
1237
1314
  if (this.getTransportType() !== "sse") return /* @__PURE__ */ new Error("Internal Server Error: Expected SSE transport");
1238
1315
  try {
1239
1316
  let parsedMessage;
@@ -1244,7 +1321,7 @@ var McpAgent = class McpAgent extends Agent {
1244
1321
  throw error;
1245
1322
  }
1246
1323
  if (await this._handleElicitationResponse(parsedMessage)) return null;
1247
- this._transport?.onmessage?.(parsedMessage);
1324
+ this._transport?.onmessage?.(parsedMessage, extraInfo);
1248
1325
  return null;
1249
1326
  } catch (error) {
1250
1327
  console.error("Error forwarding message to SSE:", error);