agents 0.0.0-36d81b3 → 0.0.0-385f0b2

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 (37) hide show
  1. package/dist/ai-chat-agent.d.ts +27 -1
  2. package/dist/ai-chat-agent.js +100 -103
  3. package/dist/ai-chat-agent.js.map +1 -1
  4. package/dist/ai-react.d.ts +12 -0
  5. package/dist/ai-react.js +2 -1
  6. package/dist/ai-react.js.map +1 -1
  7. package/dist/chunk-BZXOAZUX.js +106 -0
  8. package/dist/chunk-BZXOAZUX.js.map +1 -0
  9. package/dist/{chunk-HMLY7DHA.js → chunk-NOUFNU2O.js} +1 -5
  10. package/dist/{chunk-WNIGPPNB.js → chunk-NPGUKHFR.js} +292 -127
  11. package/dist/chunk-NPGUKHFR.js.map +1 -0
  12. package/dist/chunk-QSGN3REV.js +123 -0
  13. package/dist/chunk-QSGN3REV.js.map +1 -0
  14. package/dist/{chunk-Q5ZBHY4Z.js → chunk-Y67CHZBI.js} +32 -24
  15. package/dist/chunk-Y67CHZBI.js.map +1 -0
  16. package/dist/client.d.ts +9 -1
  17. package/dist/client.js +7 -126
  18. package/dist/client.js.map +1 -1
  19. package/dist/index.d.ts +67 -6
  20. package/dist/index.js +5 -3
  21. package/dist/mcp/client.d.ts +21 -15
  22. package/dist/mcp/client.js +2 -2
  23. package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
  24. package/dist/mcp/do-oauth-client-provider.js +4 -103
  25. package/dist/mcp/do-oauth-client-provider.js.map +1 -1
  26. package/dist/mcp/index.d.ts +14 -4
  27. package/dist/mcp/index.js +107 -135
  28. package/dist/mcp/index.js.map +1 -1
  29. package/dist/react.d.ts +14 -0
  30. package/dist/react.js +5 -1
  31. package/dist/react.js.map +1 -1
  32. package/dist/schedule.js +1 -1
  33. package/package.json +9 -6
  34. package/src/index.ts +348 -47
  35. package/dist/chunk-Q5ZBHY4Z.js.map +0 -1
  36. package/dist/chunk-WNIGPPNB.js.map +0 -1
  37. /package/dist/{chunk-HMLY7DHA.js.map → chunk-NOUFNU2O.js.map} +0 -0
package/dist/mcp/index.js CHANGED
@@ -1,13 +1,10 @@
1
1
  import {
2
2
  Agent
3
- } from "../chunk-WNIGPPNB.js";
4
- import "../chunk-Q5ZBHY4Z.js";
5
- import {
6
- __privateAdd,
7
- __privateGet,
8
- __privateMethod,
9
- __privateSet
10
- } from "../chunk-HMLY7DHA.js";
3
+ } from "../chunk-NPGUKHFR.js";
4
+ import "../chunk-BZXOAZUX.js";
5
+ import "../chunk-QSGN3REV.js";
6
+ import "../chunk-Y67CHZBI.js";
7
+ import "../chunk-NOUFNU2O.js";
11
8
 
12
9
  // src/mcp/index.ts
13
10
  import { DurableObject } from "cloudflare:workers";
@@ -20,37 +17,38 @@ import {
20
17
  JSONRPCMessageSchema
21
18
  } from "@modelcontextprotocol/sdk/types.js";
22
19
  var MAXIMUM_MESSAGE_SIZE_BYTES = 4 * 1024 * 1024;
23
- function handleCORS(request, corsOptions) {
24
- const origin = request.headers.get("Origin") || "*";
25
- const corsHeaders = {
26
- "Access-Control-Allow-Origin": corsOptions?.origin || origin,
27
- "Access-Control-Allow-Methods": corsOptions?.methods || "GET, POST, OPTIONS",
28
- "Access-Control-Allow-Headers": corsOptions?.headers || "Content-Type",
29
- "Access-Control-Max-Age": (corsOptions?.maxAge || 86400).toString()
20
+ function corsHeaders(request, corsOptions = {}) {
21
+ const origin = "*";
22
+ return {
23
+ "Access-Control-Allow-Origin": corsOptions.origin || origin,
24
+ "Access-Control-Allow-Methods": corsOptions.methods || "GET, POST, OPTIONS",
25
+ "Access-Control-Allow-Headers": corsOptions.headers || "Content-Type, mcp-session-id",
26
+ "Access-Control-Max-Age": (corsOptions.maxAge || 86400).toString(),
27
+ "Access-Control-Expose-Headers": corsOptions.exposeHeaders || "mcp-session-id"
30
28
  };
29
+ }
30
+ function handleCORS(request, corsOptions) {
31
31
  if (request.method === "OPTIONS") {
32
- return new Response(null, { headers: corsHeaders });
32
+ return new Response(null, { headers: corsHeaders(request, corsOptions) });
33
33
  }
34
34
  return null;
35
35
  }
36
- var _getWebSocket, _started;
37
36
  var McpSSETransport = class {
38
37
  constructor(getWebSocket) {
39
- __privateAdd(this, _getWebSocket);
40
- __privateAdd(this, _started, false);
41
- __privateSet(this, _getWebSocket, getWebSocket);
38
+ this._started = false;
39
+ this._getWebSocket = getWebSocket;
42
40
  }
43
41
  async start() {
44
- if (__privateGet(this, _started)) {
42
+ if (this._started) {
45
43
  throw new Error("Transport already started");
46
44
  }
47
- __privateSet(this, _started, true);
45
+ this._started = true;
48
46
  }
49
47
  async send(message) {
50
- if (!__privateGet(this, _started)) {
48
+ if (!this._started) {
51
49
  throw new Error("Transport not started");
52
50
  }
53
- const websocket = __privateGet(this, _getWebSocket).call(this);
51
+ const websocket = this._getWebSocket();
54
52
  if (!websocket) {
55
53
  throw new Error("WebSocket not connected");
56
54
  }
@@ -65,52 +63,40 @@ var McpSSETransport = class {
65
63
  this.onclose?.();
66
64
  }
67
65
  };
68
- _getWebSocket = new WeakMap();
69
- _started = new WeakMap();
70
- var _getWebSocketForGetRequest, _getWebSocketForMessageID, _notifyResponseIdSent, _started2;
71
66
  var McpStreamableHttpTransport = class {
72
67
  constructor(getWebSocketForMessageID, notifyResponseIdSent) {
73
- // TODO: If there is an open connection to send server-initiated messages
74
- // back, we should use that connection
75
- __privateAdd(this, _getWebSocketForGetRequest);
76
- // Get the appropriate websocket connection for a given message id
77
- __privateAdd(this, _getWebSocketForMessageID);
78
- // Notify the server that a response has been sent for a given message id
79
- // so that it may clean up it's mapping of message ids to connections
80
- // once they are no longer needed
81
- __privateAdd(this, _notifyResponseIdSent);
82
- __privateAdd(this, _started2, false);
83
- __privateSet(this, _getWebSocketForMessageID, getWebSocketForMessageID);
84
- __privateSet(this, _notifyResponseIdSent, notifyResponseIdSent);
85
- __privateSet(this, _getWebSocketForGetRequest, () => null);
68
+ this._started = false;
69
+ this._getWebSocketForMessageID = getWebSocketForMessageID;
70
+ this._notifyResponseIdSent = notifyResponseIdSent;
71
+ this._getWebSocketForGetRequest = () => null;
86
72
  }
87
73
  async start() {
88
- if (__privateGet(this, _started2)) {
74
+ if (this._started) {
89
75
  throw new Error("Transport already started");
90
76
  }
91
- __privateSet(this, _started2, true);
77
+ this._started = true;
92
78
  }
93
79
  async send(message) {
94
- if (!__privateGet(this, _started2)) {
80
+ if (!this._started) {
95
81
  throw new Error("Transport not started");
96
82
  }
97
83
  let websocket = null;
98
84
  if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
99
- websocket = __privateGet(this, _getWebSocketForMessageID).call(this, message.id.toString());
85
+ websocket = this._getWebSocketForMessageID(message.id.toString());
100
86
  if (!websocket) {
101
87
  throw new Error(
102
88
  `Could not find WebSocket for message id: ${message.id}`
103
89
  );
104
90
  }
105
91
  } else if (isJSONRPCRequest(message)) {
106
- websocket = __privateGet(this, _getWebSocketForGetRequest).call(this);
92
+ websocket = this._getWebSocketForGetRequest();
107
93
  } else if (isJSONRPCNotification(message)) {
108
94
  websocket = null;
109
95
  }
110
96
  try {
111
97
  websocket?.send(JSON.stringify(message));
112
98
  if (isJSONRPCResponse(message)) {
113
- __privateGet(this, _notifyResponseIdSent).call(this, message.id.toString());
99
+ this._notifyResponseIdSent(message.id.toString());
114
100
  }
115
101
  } catch (error) {
116
102
  this.onerror?.(error);
@@ -121,28 +107,16 @@ var McpStreamableHttpTransport = class {
121
107
  this.onclose?.();
122
108
  }
123
109
  };
124
- _getWebSocketForGetRequest = new WeakMap();
125
- _getWebSocketForMessageID = new WeakMap();
126
- _notifyResponseIdSent = new WeakMap();
127
- _started2 = new WeakMap();
128
- var _status, _transport, _transportType, _requestIdToConnectionId, _agent, _McpAgent_instances, initialize_fn;
129
- var _McpAgent = class _McpAgent extends DurableObject {
110
+ var McpAgent = class _McpAgent extends DurableObject {
130
111
  constructor(ctx, env) {
131
112
  var _a;
132
113
  super(ctx, env);
133
- __privateAdd(this, _McpAgent_instances);
134
- __privateAdd(this, _status, "zero");
135
- __privateAdd(this, _transport);
136
- __privateAdd(this, _transportType, "unset");
137
- __privateAdd(this, _requestIdToConnectionId, /* @__PURE__ */ new Map());
138
- /**
139
- * Since McpAgent's _aren't_ yet real "Agents", let's only expose a couple of the methods
140
- * to the outer class: initialState/state/setState/onStateUpdate/sql
141
- */
142
- __privateAdd(this, _agent);
114
+ this._status = "zero";
115
+ this._transportType = "unset";
116
+ this._requestIdToConnectionId = /* @__PURE__ */ new Map();
143
117
  this.initRun = false;
144
118
  const self = this;
145
- __privateSet(this, _agent, new (_a = class extends Agent {
119
+ this._agent = new (_a = class extends Agent {
146
120
  onStateUpdate(state, source) {
147
121
  return self.onStateUpdate(state, source);
148
122
  }
@@ -151,26 +125,26 @@ var _McpAgent = class _McpAgent extends DurableObject {
151
125
  }
152
126
  }, _a.options = {
153
127
  hibernate: true
154
- }, _a)(ctx, env));
128
+ }, _a)(ctx, env);
155
129
  }
156
130
  get mcp() {
157
- return __privateGet(this, _agent).mcp;
131
+ return this._agent.mcp;
158
132
  }
159
133
  get state() {
160
- return __privateGet(this, _agent).state;
134
+ return this._agent.state;
161
135
  }
162
136
  sql(strings, ...values) {
163
- return __privateGet(this, _agent).sql(strings, ...values);
137
+ return this._agent.sql(strings, ...values);
164
138
  }
165
139
  setState(state) {
166
- return __privateGet(this, _agent).setState(state);
140
+ return this._agent.setState(state);
167
141
  }
168
142
  onStateUpdate(state, source) {
169
143
  }
170
144
  async onStart() {
171
145
  var _a;
172
146
  const self = this;
173
- __privateSet(this, _agent, new (_a = class extends Agent {
147
+ this._agent = new (_a = class extends Agent {
174
148
  constructor() {
175
149
  super(...arguments);
176
150
  this.initialState = self.initialState;
@@ -183,22 +157,22 @@ var _McpAgent = class _McpAgent extends DurableObject {
183
157
  }
184
158
  }, _a.options = {
185
159
  hibernate: true
186
- }, _a)(this.ctx, this.env));
160
+ }, _a)(this.ctx, this.env);
187
161
  this.props = await this.ctx.storage.get("props");
188
- __privateSet(this, _transportType, await this.ctx.storage.get(
162
+ this._transportType = await this.ctx.storage.get(
189
163
  "transportType"
190
- ));
164
+ );
191
165
  await this._init(this.props);
192
166
  const server = await this.server;
193
- if (__privateGet(this, _transportType) === "sse") {
194
- __privateSet(this, _transport, new McpSSETransport(() => this.getWebSocket()));
195
- await server.connect(__privateGet(this, _transport));
196
- } else if (__privateGet(this, _transportType) === "streamable-http") {
197
- __privateSet(this, _transport, new McpStreamableHttpTransport(
167
+ if (this._transportType === "sse") {
168
+ this._transport = new McpSSETransport(() => this.getWebSocket());
169
+ await server.connect(this._transport);
170
+ } else if (this._transportType === "streamable-http") {
171
+ this._transport = new McpStreamableHttpTransport(
198
172
  (id) => this.getWebSocketForResponseID(id),
199
- (id) => __privateGet(this, _requestIdToConnectionId).delete(id)
200
- ));
201
- await server.connect(__privateGet(this, _transport));
173
+ (id) => this._requestIdToConnectionId.delete(id)
174
+ );
175
+ await server.connect(this._transport);
202
176
  }
203
177
  }
204
178
  async _init(props) {
@@ -218,10 +192,17 @@ var _McpAgent = class _McpAgent extends DurableObject {
218
192
  async isInitialized() {
219
193
  return await this.ctx.storage.get("initialized") === true;
220
194
  }
195
+ async _initialize() {
196
+ await this.ctx.blockConcurrencyWhile(async () => {
197
+ this._status = "starting";
198
+ await this.onStart();
199
+ this._status = "started";
200
+ });
201
+ }
221
202
  // Allow the worker to fetch a websocket connection to the agent
222
203
  async fetch(request) {
223
- if (__privateGet(this, _status) !== "started") {
224
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
204
+ if (this._status !== "started") {
205
+ await this._initialize();
225
206
  }
226
207
  if (request.headers.get("Upgrade") !== "websocket") {
227
208
  return new Response("Expected WebSocket Upgrade request", {
@@ -238,24 +219,24 @@ var _McpAgent = class _McpAgent extends DurableObject {
238
219
  return new Response("Websocket already connected", { status: 400 });
239
220
  }
240
221
  await this.ctx.storage.put("transportType", "sse");
241
- __privateSet(this, _transportType, "sse");
242
- if (!__privateGet(this, _transport)) {
243
- __privateSet(this, _transport, new McpSSETransport(() => this.getWebSocket()));
244
- await server.connect(__privateGet(this, _transport));
222
+ this._transportType = "sse";
223
+ if (!this._transport) {
224
+ this._transport = new McpSSETransport(() => this.getWebSocket());
225
+ await server.connect(this._transport);
245
226
  }
246
- return __privateGet(this, _agent).fetch(request);
227
+ return this._agent.fetch(request);
247
228
  }
248
229
  case "/streamable-http": {
249
- if (!__privateGet(this, _transport)) {
250
- __privateSet(this, _transport, new McpStreamableHttpTransport(
230
+ if (!this._transport) {
231
+ this._transport = new McpStreamableHttpTransport(
251
232
  (id) => this.getWebSocketForResponseID(id),
252
- (id) => __privateGet(this, _requestIdToConnectionId).delete(id)
253
- ));
254
- await server.connect(__privateGet(this, _transport));
233
+ (id) => this._requestIdToConnectionId.delete(id)
234
+ );
235
+ await server.connect(this._transport);
255
236
  }
256
237
  await this.ctx.storage.put("transportType", "streamable-http");
257
- __privateSet(this, _transportType, "streamable-http");
258
- return __privateGet(this, _agent).fetch(request);
238
+ this._transportType = "streamable-http";
239
+ return this._agent.fetch(request);
259
240
  }
260
241
  default:
261
242
  return new Response(
@@ -274,19 +255,19 @@ var _McpAgent = class _McpAgent extends DurableObject {
274
255
  return websockets[0];
275
256
  }
276
257
  getWebSocketForResponseID(id) {
277
- const connectionId = __privateGet(this, _requestIdToConnectionId).get(id);
258
+ const connectionId = this._requestIdToConnectionId.get(id);
278
259
  if (connectionId === void 0) {
279
260
  return null;
280
261
  }
281
- return __privateGet(this, _agent).getConnection(connectionId) ?? null;
262
+ return this._agent.getConnection(connectionId) ?? null;
282
263
  }
283
264
  // All messages received here. This is currently never called
284
265
  async onMessage(connection, event) {
285
- if (__privateGet(this, _transportType) !== "streamable-http") {
266
+ if (this._transportType !== "streamable-http") {
286
267
  const err = new Error(
287
268
  "Internal Server Error: Expected streamable-http protocol"
288
269
  );
289
- __privateGet(this, _transport)?.onerror?.(err);
270
+ this._transport?.onerror?.(err);
290
271
  return;
291
272
  }
292
273
  let message;
@@ -294,21 +275,21 @@ var _McpAgent = class _McpAgent extends DurableObject {
294
275
  const data = typeof event === "string" ? event : new TextDecoder().decode(event);
295
276
  message = JSONRPCMessageSchema.parse(JSON.parse(data));
296
277
  } catch (error) {
297
- __privateGet(this, _transport)?.onerror?.(error);
278
+ this._transport?.onerror?.(error);
298
279
  return;
299
280
  }
300
281
  if (isJSONRPCRequest(message)) {
301
- __privateGet(this, _requestIdToConnectionId).set(message.id.toString(), connection.id);
282
+ this._requestIdToConnectionId.set(message.id.toString(), connection.id);
302
283
  }
303
- __privateGet(this, _transport)?.onmessage?.(message);
284
+ this._transport?.onmessage?.(message);
304
285
  }
305
286
  // All messages received over SSE after the initial connection has been established
306
287
  // will be passed here
307
288
  async onSSEMcpMessage(sessionId, request) {
308
- if (__privateGet(this, _status) !== "started") {
309
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
289
+ if (this._status !== "started") {
290
+ await this._initialize();
310
291
  }
311
- if (__privateGet(this, _transportType) !== "sse") {
292
+ if (this._transportType !== "sse") {
312
293
  return new Error("Internal Server Error: Expected SSE protocol");
313
294
  }
314
295
  try {
@@ -317,35 +298,36 @@ var _McpAgent = class _McpAgent extends DurableObject {
317
298
  try {
318
299
  parsedMessage = JSONRPCMessageSchema.parse(message);
319
300
  } catch (error) {
320
- __privateGet(this, _transport)?.onerror?.(error);
301
+ this._transport?.onerror?.(error);
321
302
  throw error;
322
303
  }
323
- __privateGet(this, _transport)?.onmessage?.(parsedMessage);
304
+ this._transport?.onmessage?.(parsedMessage);
324
305
  return null;
325
306
  } catch (error) {
326
- __privateGet(this, _transport)?.onerror?.(error);
307
+ console.error("Error forwarding message to SSE:", error);
308
+ this._transport?.onerror?.(error);
327
309
  return error;
328
310
  }
329
311
  }
330
312
  // Delegate all websocket events to the underlying agent
331
313
  async webSocketMessage(ws, event) {
332
- if (__privateGet(this, _status) !== "started") {
333
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
314
+ if (this._status !== "started") {
315
+ await this._initialize();
334
316
  }
335
- return await __privateGet(this, _agent).webSocketMessage(ws, event);
317
+ return await this._agent.webSocketMessage(ws, event);
336
318
  }
337
319
  // WebSocket event handlers for hibernation support
338
320
  async webSocketError(ws, error) {
339
- if (__privateGet(this, _status) !== "started") {
340
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
321
+ if (this._status !== "started") {
322
+ await this._initialize();
341
323
  }
342
- return await __privateGet(this, _agent).webSocketError(ws, error);
324
+ return await this._agent.webSocketError(ws, error);
343
325
  }
344
326
  async webSocketClose(ws, code, reason, wasClean) {
345
- if (__privateGet(this, _status) !== "started") {
346
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
327
+ if (this._status !== "started") {
328
+ await this._initialize();
347
329
  }
348
- return await __privateGet(this, _agent).webSocketClose(ws, code, reason, wasClean);
330
+ return await this._agent.webSocketClose(ws, code, reason, wasClean);
349
331
  }
350
332
  static mount(path, {
351
333
  binding = "MCP_OBJECT",
@@ -459,7 +441,7 @@ data: ${JSON.stringify(result.data)}
459
441
  "Content-Type": "text/event-stream",
460
442
  "Cache-Control": "no-cache",
461
443
  Connection: "keep-alive",
462
- "Access-Control-Allow-Origin": corsOptions?.origin || "*"
444
+ ...corsHeaders(request, corsOptions)
463
445
  }
464
446
  });
465
447
  }
@@ -499,7 +481,7 @@ data: ${JSON.stringify(result.data)}
499
481
  "Content-Type": "text/event-stream",
500
482
  "Cache-Control": "no-cache",
501
483
  Connection: "keep-alive",
502
- "Access-Control-Allow-Origin": corsOptions?.origin || "*"
484
+ ...corsHeaders(request, corsOptions)
503
485
  }
504
486
  });
505
487
  }
@@ -509,7 +491,7 @@ data: ${JSON.stringify(result.data)}
509
491
  "Content-Type": "text/event-stream",
510
492
  "Cache-Control": "no-cache",
511
493
  Connection: "keep-alive",
512
- "Access-Control-Allow-Origin": corsOptions?.origin || "*"
494
+ ...corsHeaders(request, corsOptions)
513
495
  }
514
496
  });
515
497
  }
@@ -661,6 +643,7 @@ data: ${JSON.stringify(result.data)}
661
643
  const doStub = namespace.get(id);
662
644
  const isInitialized = await doStub.isInitialized();
663
645
  if (isInitializationRequest) {
646
+ await doStub._init(ctx.props);
664
647
  await doStub.setInitialized();
665
648
  } else if (!isInitialized) {
666
649
  const body2 = JSON.stringify({
@@ -756,7 +739,10 @@ data: ${JSON.stringify(result.data)}
756
739
  ws.send(JSON.stringify(message));
757
740
  }
758
741
  ws.close();
759
- return new Response(null, { status: 202 });
742
+ return new Response(null, {
743
+ status: 202,
744
+ headers: corsHeaders(request, corsOptions)
745
+ });
760
746
  }
761
747
  for (const message of messages) {
762
748
  if (isJSONRPCRequest(message)) {
@@ -770,7 +756,7 @@ data: ${JSON.stringify(result.data)}
770
756
  "Cache-Control": "no-cache",
771
757
  Connection: "keep-alive",
772
758
  "mcp-session-id": sessionId,
773
- "Access-Control-Allow-Origin": corsOptions?.origin || "*"
759
+ ...corsHeaders(request, corsOptions)
774
760
  },
775
761
  status: 200
776
762
  });
@@ -788,20 +774,6 @@ data: ${JSON.stringify(result.data)}
788
774
  };
789
775
  }
790
776
  };
791
- _status = new WeakMap();
792
- _transport = new WeakMap();
793
- _transportType = new WeakMap();
794
- _requestIdToConnectionId = new WeakMap();
795
- _agent = new WeakMap();
796
- _McpAgent_instances = new WeakSet();
797
- initialize_fn = async function() {
798
- await this.ctx.blockConcurrencyWhile(async () => {
799
- __privateSet(this, _status, "starting");
800
- await this.onStart();
801
- __privateSet(this, _status, "started");
802
- });
803
- };
804
- var McpAgent = _McpAgent;
805
777
  export {
806
778
  McpAgent
807
779
  };