agents 0.0.0-8a1eb98 → 0.0.0-8a4558c

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