agents 0.0.0-7291743 → 0.0.0-75614c2

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