agents 0.0.0-dc0e8de → 0.0.0-dc7a99c

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 (45) hide show
  1. package/dist/ai-chat-agent.d.ts +32 -5
  2. package/dist/ai-chat-agent.js +149 -115
  3. package/dist/ai-chat-agent.js.map +1 -1
  4. package/dist/ai-react.d.ts +17 -4
  5. package/dist/ai-react.js +28 -29
  6. package/dist/ai-react.js.map +1 -1
  7. package/dist/chunk-767EASBA.js +106 -0
  8. package/dist/chunk-767EASBA.js.map +1 -0
  9. package/dist/{chunk-Q5ZBHY4Z.js → chunk-E3LCYPCB.js} +49 -36
  10. package/dist/chunk-E3LCYPCB.js.map +1 -0
  11. package/dist/{chunk-HD4VEHBA.js → chunk-JFRK72K3.js} +463 -161
  12. package/dist/chunk-JFRK72K3.js.map +1 -0
  13. package/dist/chunk-NKZZ66QY.js +116 -0
  14. package/dist/chunk-NKZZ66QY.js.map +1 -0
  15. package/dist/client.d.ts +15 -1
  16. package/dist/client.js +6 -126
  17. package/dist/client.js.map +1 -1
  18. package/dist/index-CITGJflw.d.ts +486 -0
  19. package/dist/index.d.ts +25 -308
  20. package/dist/index.js +4 -3
  21. package/dist/mcp/client.d.ts +301 -23
  22. package/dist/mcp/client.js +1 -2
  23. package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
  24. package/dist/mcp/do-oauth-client-provider.js +3 -103
  25. package/dist/mcp/do-oauth-client-provider.js.map +1 -1
  26. package/dist/mcp/index.d.ts +17 -7
  27. package/dist/mcp/index.js +147 -173
  28. package/dist/mcp/index.js.map +1 -1
  29. package/dist/observability/index.d.ts +12 -0
  30. package/dist/observability/index.js +10 -0
  31. package/dist/react.d.ts +85 -5
  32. package/dist/react.js +20 -8
  33. package/dist/react.js.map +1 -1
  34. package/dist/schedule.d.ts +6 -6
  35. package/dist/schedule.js +4 -6
  36. package/dist/schedule.js.map +1 -1
  37. package/dist/serializable.d.ts +32 -0
  38. package/dist/serializable.js +1 -0
  39. package/dist/serializable.js.map +1 -0
  40. package/package.json +75 -68
  41. package/src/index.ts +506 -83
  42. package/dist/chunk-HD4VEHBA.js.map +0 -1
  43. package/dist/chunk-HMLY7DHA.js +0 -16
  44. package/dist/chunk-Q5ZBHY4Z.js.map +0 -1
  45. /package/dist/{chunk-HMLY7DHA.js.map → observability/index.js.map} +0 -0
@@ -1,22 +1,22 @@
1
1
  import {
2
2
  MCPClientManager
3
- } from "./chunk-Q5ZBHY4Z.js";
3
+ } from "./chunk-E3LCYPCB.js";
4
4
  import {
5
- __privateAdd,
6
- __privateGet,
7
- __privateMethod,
8
- __privateSet
9
- } from "./chunk-HMLY7DHA.js";
5
+ DurableObjectOAuthClientProvider
6
+ } from "./chunk-767EASBA.js";
7
+ import {
8
+ camelCaseToKebabCase
9
+ } from "./chunk-NKZZ66QY.js";
10
10
 
11
11
  // src/index.ts
12
+ import { AsyncLocalStorage } from "async_hooks";
13
+ import { parseCronExpression } from "cron-schedule";
14
+ import { nanoid } from "nanoid";
12
15
  import {
13
- Server,
16
+ getServerByName,
14
17
  routePartykitRequest,
15
- getServerByName
18
+ Server
16
19
  } from "partyserver";
17
- import { parseCronExpression } from "cron-schedule";
18
- import { nanoid } from "nanoid";
19
- import { AsyncLocalStorage } from "node:async_hooks";
20
20
  function isRPCRequest(msg) {
21
21
  return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "rpc" && "id" in msg && typeof msg.id === "string" && "method" in msg && typeof msg.method === "string" && "args" in msg && Array.isArray(msg.args);
22
22
  }
@@ -51,19 +51,74 @@ function getCurrentAgent() {
51
51
  }
52
52
  return store;
53
53
  }
54
- var _state, _ParentClass, _Agent_instances, setStateInternal_fn, tryCatch_fn, scheduleNextAlarm_fn, isCallable_fn;
55
54
  var Agent = class extends Server {
56
55
  constructor(ctx, env) {
57
56
  super(ctx, env);
58
- __privateAdd(this, _Agent_instances);
59
- __privateAdd(this, _state, DEFAULT_STATE);
60
- __privateAdd(this, _ParentClass, Object.getPrototypeOf(this).constructor);
61
- this.mcp = new MCPClientManager(__privateGet(this, _ParentClass).name, "0.0.1");
57
+ this._state = DEFAULT_STATE;
58
+ this._ParentClass = Object.getPrototypeOf(this).constructor;
59
+ this.mcp = new MCPClientManager(this._ParentClass.name, "0.0.1");
62
60
  /**
63
61
  * Initial state for the Agent
64
62
  * Override to provide default state values
65
63
  */
66
64
  this.initialState = DEFAULT_STATE;
65
+ /**
66
+ * The observability implementation to use for the Agent
67
+ */
68
+ this.observability = genericObservability;
69
+ /**
70
+ * Method called when an alarm fires.
71
+ * Executes any scheduled tasks that are due.
72
+ *
73
+ * @remarks
74
+ * To schedule a task, please use the `this.schedule` method instead.
75
+ * See {@link https://developers.cloudflare.com/agents/api-reference/schedule-tasks/}
76
+ */
77
+ this.alarm = async () => {
78
+ const now = Math.floor(Date.now() / 1e3);
79
+ const result = this.sql`
80
+ SELECT * FROM cf_agents_schedules WHERE time <= ${now}
81
+ `;
82
+ for (const row of result || []) {
83
+ const callback = this[row.callback];
84
+ if (!callback) {
85
+ console.error(`callback ${row.callback} not found`);
86
+ continue;
87
+ }
88
+ await agentContext.run(
89
+ { agent: this, connection: void 0, request: void 0 },
90
+ async () => {
91
+ try {
92
+ this.observability?.emit(
93
+ {
94
+ displayMessage: `Schedule ${row.id} executed`,
95
+ id: nanoid(),
96
+ payload: row,
97
+ timestamp: Date.now(),
98
+ type: "schedule:execute"
99
+ },
100
+ this.ctx
101
+ );
102
+ await callback.bind(this)(JSON.parse(row.payload), row);
103
+ } catch (e) {
104
+ console.error(`error executing callback "${row.callback}"`, e);
105
+ }
106
+ }
107
+ );
108
+ if (row.type === "cron") {
109
+ const nextExecutionTime = getNextCronTime(row.cron);
110
+ const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
111
+ this.sql`
112
+ UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
113
+ `;
114
+ } else {
115
+ this.sql`
116
+ DELETE FROM cf_agents_schedules WHERE id = ${row.id}
117
+ `;
118
+ }
119
+ }
120
+ await this._scheduleNextAlarm();
121
+ };
67
122
  this.sql`
68
123
  CREATE TABLE IF NOT EXISTS cf_agents_state (
69
124
  id TEXT PRIMARY KEY NOT NULL,
@@ -71,7 +126,7 @@ var Agent = class extends Server {
71
126
  )
72
127
  `;
73
128
  void this.ctx.blockConcurrencyWhile(async () => {
74
- return __privateMethod(this, _Agent_instances, tryCatch_fn).call(this, async () => {
129
+ return this._tryCatch(async () => {
75
130
  this.sql`
76
131
  CREATE TABLE IF NOT EXISTS cf_agents_schedules (
77
132
  id TEXT PRIMARY KEY NOT NULL DEFAULT (randomblob(9)),
@@ -87,22 +142,55 @@ var Agent = class extends Server {
87
142
  await this.alarm();
88
143
  });
89
144
  });
145
+ this.sql`
146
+ CREATE TABLE IF NOT EXISTS cf_agents_mcp_servers (
147
+ id TEXT PRIMARY KEY NOT NULL,
148
+ name TEXT NOT NULL,
149
+ server_url TEXT NOT NULL,
150
+ callback_url TEXT NOT NULL,
151
+ client_id TEXT,
152
+ auth_url TEXT,
153
+ server_options TEXT
154
+ )
155
+ `;
156
+ const _onRequest = this.onRequest.bind(this);
157
+ this.onRequest = (request) => {
158
+ return agentContext.run(
159
+ { agent: this, connection: void 0, request },
160
+ async () => {
161
+ if (this.mcp.isCallbackRequest(request)) {
162
+ await this.mcp.handleCallbackRequest(request);
163
+ this.broadcast(
164
+ JSON.stringify({
165
+ mcp: this.getMcpServers(),
166
+ type: "cf_agent_mcp_servers"
167
+ })
168
+ );
169
+ return new Response("<script>window.close();</script>", {
170
+ headers: { "content-type": "text/html" },
171
+ status: 200
172
+ });
173
+ }
174
+ return this._tryCatch(() => _onRequest(request));
175
+ }
176
+ );
177
+ };
90
178
  const _onMessage = this.onMessage.bind(this);
91
179
  this.onMessage = async (connection, message) => {
92
180
  return agentContext.run(
93
181
  { agent: this, connection, request: void 0 },
94
182
  async () => {
95
183
  if (typeof message !== "string") {
96
- return __privateMethod(this, _Agent_instances, tryCatch_fn).call(this, () => _onMessage(connection, message));
184
+ return this._tryCatch(() => _onMessage(connection, message));
97
185
  }
98
186
  let parsed;
99
187
  try {
100
188
  parsed = JSON.parse(message);
101
- } catch (e) {
102
- return __privateMethod(this, _Agent_instances, tryCatch_fn).call(this, () => _onMessage(connection, message));
189
+ } catch (_e) {
190
+ return this._tryCatch(() => _onMessage(connection, message));
103
191
  }
104
192
  if (isStateUpdateMessage(parsed)) {
105
- __privateMethod(this, _Agent_instances, setStateInternal_fn).call(this, parsed.state, connection);
193
+ this._setStateInternal(parsed.state, connection);
106
194
  return;
107
195
  }
108
196
  if (isRPCRequest(parsed)) {
@@ -112,7 +200,7 @@ var Agent = class extends Server {
112
200
  if (typeof methodFn !== "function") {
113
201
  throw new Error(`Method ${method} does not exist`);
114
202
  }
115
- if (!__privateMethod(this, _Agent_instances, isCallable_fn).call(this, method)) {
203
+ if (!this._isCallable(method)) {
116
204
  throw new Error(`Method ${method} is not callable`);
117
205
  }
118
206
  const metadata = callableMetadata.get(methodFn);
@@ -122,27 +210,42 @@ var Agent = class extends Server {
122
210
  return;
123
211
  }
124
212
  const result = await methodFn.apply(this, args);
213
+ this.observability?.emit(
214
+ {
215
+ displayMessage: `RPC call to ${method}`,
216
+ id: nanoid(),
217
+ payload: {
218
+ args,
219
+ method,
220
+ streaming: metadata?.streaming,
221
+ success: true
222
+ },
223
+ timestamp: Date.now(),
224
+ type: "rpc"
225
+ },
226
+ this.ctx
227
+ );
125
228
  const response = {
126
- type: "rpc",
229
+ done: true,
127
230
  id,
128
- success: true,
129
231
  result,
130
- done: true
232
+ success: true,
233
+ type: "rpc"
131
234
  };
132
235
  connection.send(JSON.stringify(response));
133
236
  } catch (e) {
134
237
  const response = {
135
- type: "rpc",
238
+ error: e instanceof Error ? e.message : "Unknown error occurred",
136
239
  id: parsed.id,
137
240
  success: false,
138
- error: e instanceof Error ? e.message : "Unknown error occurred"
241
+ type: "rpc"
139
242
  };
140
243
  connection.send(JSON.stringify(response));
141
244
  console.error("RPC error:", e);
142
245
  }
143
246
  return;
144
247
  }
145
- return __privateMethod(this, _Agent_instances, tryCatch_fn).call(this, () => _onMessage(connection, message));
248
+ return this._tryCatch(() => _onMessage(connection, message));
146
249
  }
147
250
  );
148
251
  };
@@ -155,23 +258,74 @@ var Agent = class extends Server {
155
258
  if (this.state) {
156
259
  connection.send(
157
260
  JSON.stringify({
158
- type: "cf_agent_state",
159
- state: this.state
261
+ state: this.state,
262
+ type: "cf_agent_state"
160
263
  })
161
264
  );
162
265
  }
163
- return __privateMethod(this, _Agent_instances, tryCatch_fn).call(this, () => _onConnect(connection, ctx2));
266
+ connection.send(
267
+ JSON.stringify({
268
+ mcp: this.getMcpServers(),
269
+ type: "cf_agent_mcp_servers"
270
+ })
271
+ );
272
+ this.observability?.emit(
273
+ {
274
+ displayMessage: "Connection established",
275
+ id: nanoid(),
276
+ payload: {
277
+ connectionId: connection.id
278
+ },
279
+ timestamp: Date.now(),
280
+ type: "connect"
281
+ },
282
+ this.ctx
283
+ );
284
+ return this._tryCatch(() => _onConnect(connection, ctx2));
164
285
  }, 20);
165
286
  }
166
287
  );
167
288
  };
289
+ const _onStart = this.onStart.bind(this);
290
+ this.onStart = async () => {
291
+ return agentContext.run(
292
+ { agent: this, connection: void 0, request: void 0 },
293
+ async () => {
294
+ const servers = this.sql`
295
+ SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
296
+ `;
297
+ Promise.allSettled(
298
+ servers.map((server) => {
299
+ return this._connectToMcpServerInternal(
300
+ server.name,
301
+ server.server_url,
302
+ server.callback_url,
303
+ server.server_options ? JSON.parse(server.server_options) : void 0,
304
+ {
305
+ id: server.id,
306
+ oauthClientId: server.client_id ?? void 0
307
+ }
308
+ );
309
+ })
310
+ ).then((_results) => {
311
+ this.broadcast(
312
+ JSON.stringify({
313
+ mcp: this.getMcpServers(),
314
+ type: "cf_agent_mcp_servers"
315
+ })
316
+ );
317
+ });
318
+ await this._tryCatch(() => _onStart());
319
+ }
320
+ );
321
+ };
168
322
  }
169
323
  /**
170
324
  * Current state of the Agent
171
325
  */
172
326
  get state() {
173
- if (__privateGet(this, _state) !== DEFAULT_STATE) {
174
- return __privateGet(this, _state);
327
+ if (this._state !== DEFAULT_STATE) {
328
+ return this._state;
175
329
  }
176
330
  const wasChanged = this.sql`
177
331
  SELECT state FROM cf_agents_state WHERE id = ${STATE_WAS_CHANGED}
@@ -182,8 +336,8 @@ var Agent = class extends Server {
182
336
  if (wasChanged[0]?.state === "true" || // we do this check for people who updated their code before we shipped wasChanged
183
337
  result[0]?.state) {
184
338
  const state = result[0]?.state;
185
- __privateSet(this, _state, JSON.parse(state));
186
- return __privateGet(this, _state);
339
+ this._state = JSON.parse(state);
340
+ return this._state;
187
341
  }
188
342
  if (this.initialState === DEFAULT_STATE) {
189
343
  return void 0;
@@ -211,24 +365,67 @@ var Agent = class extends Server {
211
365
  throw this.onError(e);
212
366
  }
213
367
  }
368
+ _setStateInternal(state, source = "server") {
369
+ const previousState = this._state;
370
+ this._state = state;
371
+ this.sql`
372
+ INSERT OR REPLACE INTO cf_agents_state (id, state)
373
+ VALUES (${STATE_ROW_ID}, ${JSON.stringify(state)})
374
+ `;
375
+ this.sql`
376
+ INSERT OR REPLACE INTO cf_agents_state (id, state)
377
+ VALUES (${STATE_WAS_CHANGED}, ${JSON.stringify(true)})
378
+ `;
379
+ this.broadcast(
380
+ JSON.stringify({
381
+ state,
382
+ type: "cf_agent_state"
383
+ }),
384
+ source !== "server" ? [source.id] : []
385
+ );
386
+ return this._tryCatch(() => {
387
+ const { connection, request } = agentContext.getStore() || {};
388
+ return agentContext.run(
389
+ { agent: this, connection, request },
390
+ async () => {
391
+ this.observability?.emit(
392
+ {
393
+ displayMessage: "State updated",
394
+ id: nanoid(),
395
+ payload: {
396
+ previousState,
397
+ state
398
+ },
399
+ timestamp: Date.now(),
400
+ type: "state:update"
401
+ },
402
+ this.ctx
403
+ );
404
+ return this.onStateUpdate(state, source);
405
+ }
406
+ );
407
+ });
408
+ }
214
409
  /**
215
410
  * Update the Agent's state
216
411
  * @param state New state to set
217
412
  */
218
413
  setState(state) {
219
- __privateMethod(this, _Agent_instances, setStateInternal_fn).call(this, state, "server");
414
+ this._setStateInternal(state, "server");
220
415
  }
221
416
  /**
222
417
  * Called when the Agent's state is updated
223
418
  * @param state Updated state
224
419
  * @param source Source of the state update ("server" or a client connection)
225
420
  */
421
+ // biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later
226
422
  onStateUpdate(state, source) {
227
423
  }
228
424
  /**
229
425
  * Called when the Agent receives an email
230
426
  * @param email Email message to process
231
427
  */
428
+ // biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later
232
429
  onEmail(email) {
233
430
  return agentContext.run(
234
431
  { agent: this, connection: void 0, request: void 0 },
@@ -237,6 +434,13 @@ var Agent = class extends Server {
237
434
  }
238
435
  );
239
436
  }
437
+ async _tryCatch(fn) {
438
+ try {
439
+ return await fn();
440
+ } catch (e) {
441
+ throw this.onError(e);
442
+ }
443
+ }
240
444
  onError(connectionOrError, error) {
241
445
  let theError;
242
446
  if (connectionOrError && error) {
@@ -272,6 +476,16 @@ var Agent = class extends Server {
272
476
  */
273
477
  async schedule(when, callback, payload) {
274
478
  const id = nanoid(9);
479
+ const emitScheduleCreate = (schedule) => this.observability?.emit(
480
+ {
481
+ displayMessage: `Schedule ${schedule.id} created`,
482
+ id: nanoid(),
483
+ payload: schedule,
484
+ timestamp: Date.now(),
485
+ type: "schedule:create"
486
+ },
487
+ this.ctx
488
+ );
275
489
  if (typeof callback !== "string") {
276
490
  throw new Error("Callback must be a string");
277
491
  }
@@ -286,14 +500,16 @@ var Agent = class extends Server {
286
500
  payload
287
501
  )}, 'scheduled', ${timestamp})
288
502
  `;
289
- await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
290
- return {
291
- id,
503
+ await this._scheduleNextAlarm();
504
+ const schedule = {
292
505
  callback,
506
+ id,
293
507
  payload,
294
508
  time: timestamp,
295
509
  type: "scheduled"
296
510
  };
511
+ emitScheduleCreate(schedule);
512
+ return schedule;
297
513
  }
298
514
  if (typeof when === "number") {
299
515
  const time = new Date(Date.now() + when * 1e3);
@@ -304,15 +520,17 @@ var Agent = class extends Server {
304
520
  payload
305
521
  )}, 'delayed', ${when}, ${timestamp})
306
522
  `;
307
- await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
308
- return {
309
- id,
523
+ await this._scheduleNextAlarm();
524
+ const schedule = {
310
525
  callback,
311
- payload,
312
526
  delayInSeconds: when,
527
+ id,
528
+ payload,
313
529
  time: timestamp,
314
530
  type: "delayed"
315
531
  };
532
+ emitScheduleCreate(schedule);
533
+ return schedule;
316
534
  }
317
535
  if (typeof when === "string") {
318
536
  const nextExecutionTime = getNextCronTime(when);
@@ -323,15 +541,17 @@ var Agent = class extends Server {
323
541
  payload
324
542
  )}, 'cron', ${when}, ${timestamp})
325
543
  `;
326
- await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
327
- return {
328
- id,
544
+ await this._scheduleNextAlarm();
545
+ const schedule = {
329
546
  callback,
330
- payload,
331
547
  cron: when,
548
+ id,
549
+ payload,
332
550
  time: timestamp,
333
551
  type: "cron"
334
552
  };
553
+ emitScheduleCreate(schedule);
554
+ return schedule;
335
555
  }
336
556
  throw new Error("Invalid schedule type");
337
557
  }
@@ -389,48 +609,35 @@ var Agent = class extends Server {
389
609
  * @returns true if the task was cancelled, false otherwise
390
610
  */
391
611
  async cancelSchedule(id) {
612
+ const schedule = await this.getSchedule(id);
613
+ if (schedule) {
614
+ this.observability?.emit(
615
+ {
616
+ displayMessage: `Schedule ${id} cancelled`,
617
+ id: nanoid(),
618
+ payload: schedule,
619
+ timestamp: Date.now(),
620
+ type: "schedule:cancel"
621
+ },
622
+ this.ctx
623
+ );
624
+ }
392
625
  this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
393
- await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
626
+ await this._scheduleNextAlarm();
394
627
  return true;
395
628
  }
396
- /**
397
- * Method called when an alarm fires
398
- * Executes any scheduled tasks that are due
399
- */
400
- async alarm() {
401
- const now = Math.floor(Date.now() / 1e3);
629
+ async _scheduleNextAlarm() {
402
630
  const result = this.sql`
403
- SELECT * FROM cf_agents_schedules WHERE time <= ${now}
631
+ SELECT time FROM cf_agents_schedules
632
+ WHERE time > ${Math.floor(Date.now() / 1e3)}
633
+ ORDER BY time ASC
634
+ LIMIT 1
404
635
  `;
405
- for (const row of result || []) {
406
- const callback = this[row.callback];
407
- if (!callback) {
408
- console.error(`callback ${row.callback} not found`);
409
- continue;
410
- }
411
- await agentContext.run(
412
- { agent: this, connection: void 0, request: void 0 },
413
- async () => {
414
- try {
415
- await callback.bind(this)(JSON.parse(row.payload), row);
416
- } catch (e) {
417
- console.error(`error executing callback "${row.callback}"`, e);
418
- }
419
- }
420
- );
421
- if (row.type === "cron") {
422
- const nextExecutionTime = getNextCronTime(row.cron);
423
- const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
424
- this.sql`
425
- UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
426
- `;
427
- } else {
428
- this.sql`
429
- DELETE FROM cf_agents_schedules WHERE id = ${row.id}
430
- `;
431
- }
636
+ if (!result) return;
637
+ if (result.length > 0 && "time" in result[0]) {
638
+ const nextTime = result[0].time * 1e3;
639
+ await this.ctx.storage.setAlarm(nextTime);
432
640
  }
433
- await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
434
641
  }
435
642
  /**
436
643
  * Destroy the Agent, removing all state and scheduled tasks
@@ -438,67 +645,143 @@ var Agent = class extends Server {
438
645
  async destroy() {
439
646
  this.sql`DROP TABLE IF EXISTS cf_agents_state`;
440
647
  this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
648
+ this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
441
649
  await this.ctx.storage.deleteAlarm();
442
650
  await this.ctx.storage.deleteAll();
651
+ this.ctx.abort("destroyed");
652
+ this.observability?.emit(
653
+ {
654
+ displayMessage: "Agent destroyed",
655
+ id: nanoid(),
656
+ payload: {},
657
+ timestamp: Date.now(),
658
+ type: "destroy"
659
+ },
660
+ this.ctx
661
+ );
443
662
  }
444
- };
445
- _state = new WeakMap();
446
- _ParentClass = new WeakMap();
447
- _Agent_instances = new WeakSet();
448
- setStateInternal_fn = function(state, source = "server") {
449
- __privateSet(this, _state, state);
450
- this.sql`
451
- INSERT OR REPLACE INTO cf_agents_state (id, state)
452
- VALUES (${STATE_ROW_ID}, ${JSON.stringify(state)})
453
- `;
454
- this.sql`
455
- INSERT OR REPLACE INTO cf_agents_state (id, state)
456
- VALUES (${STATE_WAS_CHANGED}, ${JSON.stringify(true)})
457
- `;
458
- this.broadcast(
459
- JSON.stringify({
460
- type: "cf_agent_state",
461
- state
462
- }),
463
- source !== "server" ? [source.id] : []
464
- );
465
- return __privateMethod(this, _Agent_instances, tryCatch_fn).call(this, () => {
466
- const { connection, request } = agentContext.getStore() || {};
467
- return agentContext.run(
468
- { agent: this, connection, request },
469
- async () => {
470
- return this.onStateUpdate(state, source);
663
+ /**
664
+ * Get all methods marked as callable on this Agent
665
+ * @returns A map of method names to their metadata
666
+ */
667
+ _isCallable(method) {
668
+ return callableMetadata.has(this[method]);
669
+ }
670
+ /**
671
+ * Connect to a new MCP Server
672
+ *
673
+ * @param url MCP Server SSE URL
674
+ * @param callbackHost Base host for the agent, used for the redirect URI.
675
+ * @param agentsPrefix agents routing prefix if not using `agents`
676
+ * @param options MCP client and transport (header) options
677
+ * @returns authUrl
678
+ */
679
+ async addMcpServer(serverName, url, callbackHost, agentsPrefix = "agents", options) {
680
+ const callbackUrl = `${callbackHost}/${agentsPrefix}/${camelCaseToKebabCase(this._ParentClass.name)}/${this.name}/callback`;
681
+ const result = await this._connectToMcpServerInternal(
682
+ serverName,
683
+ url,
684
+ callbackUrl,
685
+ options
686
+ );
687
+ this.sql`
688
+ INSERT
689
+ OR REPLACE INTO cf_agents_mcp_servers (id, name, server_url, client_id, auth_url, callback_url, server_options)
690
+ VALUES (
691
+ ${result.id},
692
+ ${serverName},
693
+ ${url},
694
+ ${result.clientId ?? null},
695
+ ${result.authUrl ?? null},
696
+ ${callbackUrl},
697
+ ${options ? JSON.stringify(options) : null}
698
+ );
699
+ `;
700
+ this.broadcast(
701
+ JSON.stringify({
702
+ mcp: this.getMcpServers(),
703
+ type: "cf_agent_mcp_servers"
704
+ })
705
+ );
706
+ return result;
707
+ }
708
+ async _connectToMcpServerInternal(_serverName, url, callbackUrl, options, reconnect) {
709
+ const authProvider = new DurableObjectOAuthClientProvider(
710
+ this.ctx.storage,
711
+ this.name,
712
+ callbackUrl
713
+ );
714
+ if (reconnect) {
715
+ authProvider.serverId = reconnect.id;
716
+ if (reconnect.oauthClientId) {
717
+ authProvider.clientId = reconnect.oauthClientId;
718
+ }
719
+ }
720
+ let headerTransportOpts = {};
721
+ if (options?.transport?.headers) {
722
+ headerTransportOpts = {
723
+ eventSourceInit: {
724
+ fetch: (url2, init) => fetch(url2, {
725
+ ...init,
726
+ headers: options?.transport?.headers
727
+ })
728
+ },
729
+ requestInit: {
730
+ headers: options?.transport?.headers
731
+ }
732
+ };
733
+ }
734
+ const { id, authUrl, clientId } = await this.mcp.connect(url, {
735
+ client: options?.client,
736
+ reconnect,
737
+ transport: {
738
+ ...headerTransportOpts,
739
+ authProvider
471
740
  }
741
+ });
742
+ return {
743
+ authUrl,
744
+ clientId,
745
+ id
746
+ };
747
+ }
748
+ async removeMcpServer(id) {
749
+ this.mcp.closeConnection(id);
750
+ this.sql`
751
+ DELETE FROM cf_agents_mcp_servers WHERE id = ${id};
752
+ `;
753
+ this.broadcast(
754
+ JSON.stringify({
755
+ mcp: this.getMcpServers(),
756
+ type: "cf_agent_mcp_servers"
757
+ })
472
758
  );
473
- });
474
- };
475
- tryCatch_fn = async function(fn) {
476
- try {
477
- return await fn();
478
- } catch (e) {
479
- throw this.onError(e);
480
759
  }
481
- };
482
- scheduleNextAlarm_fn = async function() {
483
- const result = this.sql`
484
- SELECT time FROM cf_agents_schedules
485
- WHERE time > ${Math.floor(Date.now() / 1e3)}
486
- ORDER BY time ASC
487
- LIMIT 1
760
+ getMcpServers() {
761
+ const mcpState = {
762
+ prompts: this.mcp.listPrompts(),
763
+ resources: this.mcp.listResources(),
764
+ servers: {},
765
+ tools: this.mcp.listTools()
766
+ };
767
+ const servers = this.sql`
768
+ SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
488
769
  `;
489
- if (!result) return;
490
- if (result.length > 0 && "time" in result[0]) {
491
- const nextTime = result[0].time * 1e3;
492
- await this.ctx.storage.setAlarm(nextTime);
770
+ for (const server of servers) {
771
+ const serverConn = this.mcp.mcpConnections[server.id];
772
+ mcpState.servers[server.id] = {
773
+ auth_url: server.auth_url,
774
+ capabilities: serverConn?.serverCapabilities ?? null,
775
+ instructions: serverConn?.instructions ?? null,
776
+ name: server.name,
777
+ server_url: server.server_url,
778
+ // mark as "authenticating" because the server isn't automatically connected, so it's pending authenticating
779
+ state: serverConn?.connectionState ?? "authenticating"
780
+ };
781
+ }
782
+ return mcpState;
493
783
  }
494
784
  };
495
- /**
496
- * Get all methods marked as callable on this Agent
497
- * @returns A map of method names to their metadata
498
- */
499
- isCallable_fn = function(method) {
500
- return callableMetadata.has(this[method]);
501
- };
502
785
  /**
503
786
  * Agent configuration options
504
787
  */
@@ -509,9 +792,9 @@ Agent.options = {
509
792
  };
510
793
  async function routeAgentRequest(request, env, options) {
511
794
  const corsHeaders = options?.cors === true ? {
512
- "Access-Control-Allow-Origin": "*",
513
- "Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
514
795
  "Access-Control-Allow-Credentials": "true",
796
+ "Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
797
+ "Access-Control-Allow-Origin": "*",
515
798
  "Access-Control-Max-Age": "86400"
516
799
  } : options?.cors;
517
800
  if (request.method === "OPTIONS") {
@@ -542,61 +825,80 @@ async function routeAgentRequest(request, env, options) {
542
825
  }
543
826
  return response;
544
827
  }
545
- async function routeAgentEmail(email, env, options) {
828
+ async function routeAgentEmail(_email, _env, _options) {
546
829
  }
547
830
  async function getAgentByName(namespace, name, options) {
548
831
  return getServerByName(namespace, name, options);
549
832
  }
550
- var _connection, _id, _closed;
551
833
  var StreamingResponse = class {
552
834
  constructor(connection, id) {
553
- __privateAdd(this, _connection);
554
- __privateAdd(this, _id);
555
- __privateAdd(this, _closed, false);
556
- __privateSet(this, _connection, connection);
557
- __privateSet(this, _id, id);
835
+ this._closed = false;
836
+ this._connection = connection;
837
+ this._id = id;
558
838
  }
559
839
  /**
560
840
  * Send a chunk of data to the client
561
841
  * @param chunk The data to send
562
842
  */
563
843
  send(chunk) {
564
- if (__privateGet(this, _closed)) {
844
+ if (this._closed) {
565
845
  throw new Error("StreamingResponse is already closed");
566
846
  }
567
847
  const response = {
568
- type: "rpc",
569
- id: __privateGet(this, _id),
570
- success: true,
848
+ done: false,
849
+ id: this._id,
571
850
  result: chunk,
572
- done: false
851
+ success: true,
852
+ type: "rpc"
573
853
  };
574
- __privateGet(this, _connection).send(JSON.stringify(response));
854
+ this._connection.send(JSON.stringify(response));
575
855
  }
576
856
  /**
577
857
  * End the stream and send the final chunk (if any)
578
858
  * @param finalChunk Optional final chunk of data to send
579
859
  */
580
860
  end(finalChunk) {
581
- if (__privateGet(this, _closed)) {
861
+ if (this._closed) {
582
862
  throw new Error("StreamingResponse is already closed");
583
863
  }
584
- __privateSet(this, _closed, true);
864
+ this._closed = true;
585
865
  const response = {
586
- type: "rpc",
587
- id: __privateGet(this, _id),
588
- success: true,
866
+ done: true,
867
+ id: this._id,
589
868
  result: finalChunk,
590
- done: true
869
+ success: true,
870
+ type: "rpc"
591
871
  };
592
- __privateGet(this, _connection).send(JSON.stringify(response));
872
+ this._connection.send(JSON.stringify(response));
873
+ }
874
+ };
875
+
876
+ // src/observability/index.ts
877
+ var genericObservability = {
878
+ emit(event) {
879
+ if (isLocalMode()) {
880
+ console.log(event.displayMessage);
881
+ return;
882
+ }
883
+ console.log(event);
593
884
  }
594
885
  };
595
- _connection = new WeakMap();
596
- _id = new WeakMap();
597
- _closed = new WeakMap();
886
+ var localMode = false;
887
+ function isLocalMode() {
888
+ if (localMode) {
889
+ return true;
890
+ }
891
+ const { request } = getCurrentAgent();
892
+ if (!request) {
893
+ return false;
894
+ }
895
+ const url = new URL(request.url);
896
+ localMode = url.hostname === "localhost";
897
+ return localMode;
898
+ }
598
899
 
599
900
  export {
901
+ genericObservability,
600
902
  unstable_callable,
601
903
  getCurrentAgent,
602
904
  Agent,
@@ -605,4 +907,4 @@ export {
605
907
  getAgentByName,
606
908
  StreamingResponse
607
909
  };
608
- //# sourceMappingURL=chunk-HD4VEHBA.js.map
910
+ //# sourceMappingURL=chunk-JFRK72K3.js.map