agents 0.0.0-74a8c74 → 0.0.0-7856b4d

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 +49 -4
  2. package/dist/ai-chat-agent.js +101 -29
  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 +38 -22
  6. package/dist/ai-react.js.map +1 -1
  7. package/dist/ai-types.d.ts +5 -0
  8. package/dist/{chunk-SZEXGW6W.js → chunk-6RPGDIE2.js} +350 -144
  9. package/dist/chunk-6RPGDIE2.js.map +1 -0
  10. package/dist/chunk-BZXOAZUX.js +106 -0
  11. package/dist/chunk-BZXOAZUX.js.map +1 -0
  12. package/dist/chunk-OYJXQRRH.js +465 -0
  13. package/dist/chunk-OYJXQRRH.js.map +1 -0
  14. package/dist/chunk-VCSB47AK.js +116 -0
  15. package/dist/chunk-VCSB47AK.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 +120 -17
  20. package/dist/index.js +8 -7
  21. package/dist/mcp/client.d.ts +142 -34
  22. package/dist/mcp/client.js +3 -780
  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/do-oauth-client-provider.js.map +1 -0
  27. package/dist/mcp/index.d.ts +50 -7
  28. package/dist/mcp/index.js +718 -71
  29. package/dist/mcp/index.js.map +1 -1
  30. package/dist/react.d.ts +85 -5
  31. package/dist/react.js +48 -27
  32. package/dist/react.js.map +1 -1
  33. package/dist/serializable.d.ts +32 -0
  34. package/dist/serializable.js +1 -0
  35. package/dist/serializable.js.map +1 -0
  36. package/package.json +28 -5
  37. package/src/index.ts +396 -60
  38. package/dist/chunk-EZ76ZGDB.js +0 -1721
  39. package/dist/chunk-EZ76ZGDB.js.map +0 -1
  40. package/dist/chunk-SZEXGW6W.js.map +0 -1
@@ -1,13 +1,22 @@
1
+ import {
2
+ MCPClientManager
3
+ } from "./chunk-OYJXQRRH.js";
4
+ import {
5
+ DurableObjectOAuthClientProvider
6
+ } from "./chunk-BZXOAZUX.js";
7
+ import {
8
+ camelCaseToKebabCase
9
+ } from "./chunk-VCSB47AK.js";
10
+
1
11
  // src/index.ts
2
12
  import {
3
13
  Server,
4
- routePartykitRequest,
5
- getServerByName
14
+ getServerByName,
15
+ routePartykitRequest
6
16
  } from "partyserver";
7
17
  import { parseCronExpression } from "cron-schedule";
8
18
  import { nanoid } from "nanoid";
9
- import { AsyncLocalStorage } from "node:async_hooks";
10
- import { WorkflowEntrypoint as CFWorkflowEntrypoint } from "cloudflare:workers";
19
+ import { AsyncLocalStorage } from "async_hooks";
11
20
  function isRPCRequest(msg) {
12
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);
13
22
  }
@@ -23,8 +32,6 @@ function unstable_callable(metadata = {}) {
23
32
  return target;
24
33
  };
25
34
  }
26
- var WorkflowEntrypoint = class extends CFWorkflowEntrypoint {
27
- };
28
35
  function getNextCronTime(cron) {
29
36
  const interval = parseCronExpression(cron);
30
37
  return interval.getNextDate();
@@ -32,69 +39,72 @@ function getNextCronTime(cron) {
32
39
  var STATE_ROW_ID = "cf_state_row_id";
33
40
  var STATE_WAS_CHANGED = "cf_state_was_changed";
34
41
  var DEFAULT_STATE = {};
35
- var unstable_context = new AsyncLocalStorage();
36
- var Agent = class extends Server {
37
- #state = DEFAULT_STATE;
38
- /**
39
- * Initial state for the Agent
40
- * Override to provide default state values
41
- */
42
- initialState = DEFAULT_STATE;
43
- /**
44
- * Current state of the Agent
45
- */
46
- get state() {
47
- if (this.#state !== DEFAULT_STATE) {
48
- return this.#state;
49
- }
50
- const wasChanged = this.sql`
51
- SELECT state FROM cf_agents_state WHERE id = ${STATE_WAS_CHANGED}
52
- `;
53
- const result = this.sql`
54
- SELECT state FROM cf_agents_state WHERE id = ${STATE_ROW_ID}
55
- `;
56
- if (wasChanged[0]?.state === "true" || // we do this check for people who updated their code before we shipped wasChanged
57
- result[0]?.state) {
58
- const state = result[0]?.state;
59
- this.#state = JSON.parse(state);
60
- return this.#state;
61
- }
62
- if (this.initialState === DEFAULT_STATE) {
63
- return void 0;
64
- }
65
- this.setState(this.initialState);
66
- return this.initialState;
67
- }
68
- /**
69
- * Agent configuration options
70
- */
71
- static options = {
72
- /** Whether the Agent should hibernate when inactive */
73
- hibernate: true
74
- // default to hibernate
75
- };
76
- /**
77
- * Execute SQL queries against the Agent's database
78
- * @template T Type of the returned rows
79
- * @param strings SQL query template strings
80
- * @param values Values to be inserted into the query
81
- * @returns Array of query results
82
- */
83
- sql(strings, ...values) {
84
- let query = "";
85
- try {
86
- query = strings.reduce(
87
- (acc, str, i) => acc + str + (i < values.length ? "?" : ""),
88
- ""
89
- );
90
- return [...this.ctx.storage.sql.exec(query, ...values)];
91
- } catch (e) {
92
- console.error(`failed to execute sql query: ${query}`, e);
93
- throw this.onError(e);
94
- }
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
+ };
95
51
  }
52
+ return store;
53
+ }
54
+ var Agent = class extends Server {
96
55
  constructor(ctx, env) {
97
56
  super(ctx, env);
57
+ this._state = DEFAULT_STATE;
58
+ this._ParentClass = Object.getPrototypeOf(this).constructor;
59
+ this.mcp = new MCPClientManager(this._ParentClass.name, "0.0.1");
60
+ /**
61
+ * Initial state for the Agent
62
+ * Override to provide default state values
63
+ */
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
+ };
98
108
  this.sql`
99
109
  CREATE TABLE IF NOT EXISTS cf_agents_state (
100
110
  id TEXT PRIMARY KEY NOT NULL,
@@ -102,7 +112,7 @@ var Agent = class extends Server {
102
112
  )
103
113
  `;
104
114
  void this.ctx.blockConcurrencyWhile(async () => {
105
- return this.#tryCatch(async () => {
115
+ return this._tryCatch(async () => {
106
116
  this.sql`
107
117
  CREATE TABLE IF NOT EXISTS cf_agents_schedules (
108
118
  id TEXT PRIMARY KEY NOT NULL DEFAULT (randomblob(9)),
@@ -118,22 +128,55 @@ var Agent = class extends Server {
118
128
  await this.alarm();
119
129
  });
120
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
+ };
121
164
  const _onMessage = this.onMessage.bind(this);
122
165
  this.onMessage = async (connection, message) => {
123
- return unstable_context.run(
166
+ return agentContext.run(
124
167
  { agent: this, connection, request: void 0 },
125
168
  async () => {
126
169
  if (typeof message !== "string") {
127
- return this.#tryCatch(() => _onMessage(connection, message));
170
+ return this._tryCatch(() => _onMessage(connection, message));
128
171
  }
129
172
  let parsed;
130
173
  try {
131
174
  parsed = JSON.parse(message);
132
175
  } catch (e) {
133
- return this.#tryCatch(() => _onMessage(connection, message));
176
+ return this._tryCatch(() => _onMessage(connection, message));
134
177
  }
135
178
  if (isStateUpdateMessage(parsed)) {
136
- this.#setStateInternal(parsed.state, connection);
179
+ this._setStateInternal(parsed.state, connection);
137
180
  return;
138
181
  }
139
182
  if (isRPCRequest(parsed)) {
@@ -143,7 +186,7 @@ var Agent = class extends Server {
143
186
  if (typeof methodFn !== "function") {
144
187
  throw new Error(`Method ${method} does not exist`);
145
188
  }
146
- if (!this.#isCallable(method)) {
189
+ if (!this._isCallable(method)) {
147
190
  throw new Error(`Method ${method} is not callable`);
148
191
  }
149
192
  const metadata = callableMetadata.get(methodFn);
@@ -173,13 +216,13 @@ var Agent = class extends Server {
173
216
  }
174
217
  return;
175
218
  }
176
- return this.#tryCatch(() => _onMessage(connection, message));
219
+ return this._tryCatch(() => _onMessage(connection, message));
177
220
  }
178
221
  );
179
222
  };
180
223
  const _onConnect = this.onConnect.bind(this);
181
224
  this.onConnect = (connection, ctx2) => {
182
- return unstable_context.run(
225
+ return agentContext.run(
183
226
  { agent: this, connection, request: ctx2.request },
184
227
  async () => {
185
228
  setTimeout(() => {
@@ -191,14 +234,97 @@ var Agent = class extends Server {
191
234
  })
192
235
  );
193
236
  }
194
- return this.#tryCatch(() => _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));
195
244
  }, 20);
196
245
  }
197
246
  );
198
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
+ };
280
+ }
281
+ /**
282
+ * Current state of the Agent
283
+ */
284
+ get state() {
285
+ if (this._state !== DEFAULT_STATE) {
286
+ return this._state;
287
+ }
288
+ const wasChanged = this.sql`
289
+ SELECT state FROM cf_agents_state WHERE id = ${STATE_WAS_CHANGED}
290
+ `;
291
+ const result = this.sql`
292
+ SELECT state FROM cf_agents_state WHERE id = ${STATE_ROW_ID}
293
+ `;
294
+ if (wasChanged[0]?.state === "true" || // we do this check for people who updated their code before we shipped wasChanged
295
+ result[0]?.state) {
296
+ const state = result[0]?.state;
297
+ this._state = JSON.parse(state);
298
+ return this._state;
299
+ }
300
+ if (this.initialState === DEFAULT_STATE) {
301
+ return void 0;
302
+ }
303
+ this.setState(this.initialState);
304
+ return this.initialState;
199
305
  }
200
- #setStateInternal(state, source = "server") {
201
- this.#state = state;
306
+ /**
307
+ * Execute SQL queries against the Agent's database
308
+ * @template T Type of the returned rows
309
+ * @param strings SQL query template strings
310
+ * @param values Values to be inserted into the query
311
+ * @returns Array of query results
312
+ */
313
+ sql(strings, ...values) {
314
+ let query = "";
315
+ try {
316
+ query = strings.reduce(
317
+ (acc, str, i) => acc + str + (i < values.length ? "?" : ""),
318
+ ""
319
+ );
320
+ return [...this.ctx.storage.sql.exec(query, ...values)];
321
+ } catch (e) {
322
+ console.error(`failed to execute sql query: ${query}`, e);
323
+ throw this.onError(e);
324
+ }
325
+ }
326
+ _setStateInternal(state, source = "server") {
327
+ this._state = state;
202
328
  this.sql`
203
329
  INSERT OR REPLACE INTO cf_agents_state (id, state)
204
330
  VALUES (${STATE_ROW_ID}, ${JSON.stringify(state)})
@@ -214,9 +340,9 @@ var Agent = class extends Server {
214
340
  }),
215
341
  source !== "server" ? [source.id] : []
216
342
  );
217
- return this.#tryCatch(() => {
218
- const { connection, request } = unstable_context.getStore() || {};
219
- return unstable_context.run(
343
+ return this._tryCatch(() => {
344
+ const { connection, request } = agentContext.getStore() || {};
345
+ return agentContext.run(
220
346
  { agent: this, connection, request },
221
347
  async () => {
222
348
  return this.onStateUpdate(state, source);
@@ -229,7 +355,7 @@ var Agent = class extends Server {
229
355
  * @param state New state to set
230
356
  */
231
357
  setState(state) {
232
- this.#setStateInternal(state, "server");
358
+ this._setStateInternal(state, "server");
233
359
  }
234
360
  /**
235
361
  * Called when the Agent's state is updated
@@ -243,14 +369,14 @@ var Agent = class extends Server {
243
369
  * @param email Email message to process
244
370
  */
245
371
  onEmail(email) {
246
- return unstable_context.run(
372
+ return agentContext.run(
247
373
  { agent: this, connection: void 0, request: void 0 },
248
374
  async () => {
249
375
  console.error("onEmail not implemented");
250
376
  }
251
377
  );
252
378
  }
253
- async #tryCatch(fn) {
379
+ async _tryCatch(fn) {
254
380
  try {
255
381
  return await fn();
256
382
  } catch (e) {
@@ -306,7 +432,7 @@ var Agent = class extends Server {
306
432
  payload
307
433
  )}, 'scheduled', ${timestamp})
308
434
  `;
309
- await this.#scheduleNextAlarm();
435
+ await this._scheduleNextAlarm();
310
436
  return {
311
437
  id,
312
438
  callback,
@@ -324,7 +450,7 @@ var Agent = class extends Server {
324
450
  payload
325
451
  )}, 'delayed', ${when}, ${timestamp})
326
452
  `;
327
- await this.#scheduleNextAlarm();
453
+ await this._scheduleNextAlarm();
328
454
  return {
329
455
  id,
330
456
  callback,
@@ -343,7 +469,7 @@ var Agent = class extends Server {
343
469
  payload
344
470
  )}, 'cron', ${when}, ${timestamp})
345
471
  `;
346
- await this.#scheduleNextAlarm();
472
+ await this._scheduleNextAlarm();
347
473
  return {
348
474
  id,
349
475
  callback,
@@ -410,10 +536,10 @@ var Agent = class extends Server {
410
536
  */
411
537
  async cancelSchedule(id) {
412
538
  this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
413
- await this.#scheduleNextAlarm();
539
+ await this._scheduleNextAlarm();
414
540
  return true;
415
541
  }
416
- async #scheduleNextAlarm() {
542
+ async _scheduleNextAlarm() {
417
543
  const result = this.sql`
418
544
  SELECT time FROM cf_agents_schedules
419
545
  WHERE time > ${Math.floor(Date.now() / 1e3)}
@@ -426,61 +552,144 @@ var Agent = class extends Server {
426
552
  await this.ctx.storage.setAlarm(nextTime);
427
553
  }
428
554
  }
429
- /**
430
- * Method called when an alarm fires
431
- * Executes any scheduled tasks that are due
432
- */
433
- async alarm() {
434
- const now = Math.floor(Date.now() / 1e3);
435
- const result = this.sql`
436
- SELECT * FROM cf_agents_schedules WHERE time <= ${now}
437
- `;
438
- for (const row of result || []) {
439
- const callback = this[row.callback];
440
- if (!callback) {
441
- console.error(`callback ${row.callback} not found`);
442
- continue;
443
- }
444
- await unstable_context.run(
445
- { agent: this, connection: void 0, request: void 0 },
446
- async () => {
447
- try {
448
- await callback.bind(this)(JSON.parse(row.payload), row);
449
- } catch (e) {
450
- console.error(`error executing callback "${row.callback}"`, e);
451
- }
452
- }
453
- );
454
- if (row.type === "cron") {
455
- const nextExecutionTime = getNextCronTime(row.cron);
456
- const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
457
- this.sql`
458
- UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
459
- `;
460
- } else {
461
- this.sql`
462
- DELETE FROM cf_agents_schedules WHERE id = ${row.id}
463
- `;
464
- }
465
- }
466
- await this.#scheduleNextAlarm();
467
- }
468
555
  /**
469
556
  * Destroy the Agent, removing all state and scheduled tasks
470
557
  */
471
558
  async destroy() {
472
559
  this.sql`DROP TABLE IF EXISTS cf_agents_state`;
473
560
  this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
561
+ this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
474
562
  await this.ctx.storage.deleteAlarm();
475
563
  await this.ctx.storage.deleteAll();
564
+ this.ctx.abort("destroyed");
476
565
  }
477
566
  /**
478
567
  * Get all methods marked as callable on this Agent
479
568
  * @returns A map of method names to their metadata
480
569
  */
481
- #isCallable(method) {
570
+ _isCallable(method) {
482
571
  return callableMetadata.has(this[method]);
483
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;
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
+ })
659
+ );
660
+ }
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;
670
+ `;
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;
684
+ }
685
+ };
686
+ /**
687
+ * Agent configuration options
688
+ */
689
+ Agent.options = {
690
+ /** Whether the Agent should hibernate when inactive */
691
+ hibernate: true
692
+ // default to hibernate
484
693
  };
485
694
  async function routeAgentRequest(request, env, options) {
486
695
  const corsHeaders = options?.cors === true ? {
@@ -519,62 +728,59 @@ async function routeAgentRequest(request, env, options) {
519
728
  }
520
729
  async function routeAgentEmail(email, env, options) {
521
730
  }
522
- function getAgentByName(namespace, name, options) {
731
+ async function getAgentByName(namespace, name, options) {
523
732
  return getServerByName(namespace, name, options);
524
733
  }
525
734
  var StreamingResponse = class {
526
- #connection;
527
- #id;
528
- #closed = false;
529
735
  constructor(connection, id) {
530
- this.#connection = connection;
531
- this.#id = id;
736
+ this._closed = false;
737
+ this._connection = connection;
738
+ this._id = id;
532
739
  }
533
740
  /**
534
741
  * Send a chunk of data to the client
535
742
  * @param chunk The data to send
536
743
  */
537
744
  send(chunk) {
538
- if (this.#closed) {
745
+ if (this._closed) {
539
746
  throw new Error("StreamingResponse is already closed");
540
747
  }
541
748
  const response = {
542
749
  type: "rpc",
543
- id: this.#id,
750
+ id: this._id,
544
751
  success: true,
545
752
  result: chunk,
546
753
  done: false
547
754
  };
548
- this.#connection.send(JSON.stringify(response));
755
+ this._connection.send(JSON.stringify(response));
549
756
  }
550
757
  /**
551
758
  * End the stream and send the final chunk (if any)
552
759
  * @param finalChunk Optional final chunk of data to send
553
760
  */
554
761
  end(finalChunk) {
555
- if (this.#closed) {
762
+ if (this._closed) {
556
763
  throw new Error("StreamingResponse is already closed");
557
764
  }
558
- this.#closed = true;
765
+ this._closed = true;
559
766
  const response = {
560
767
  type: "rpc",
561
- id: this.#id,
768
+ id: this._id,
562
769
  success: true,
563
770
  result: finalChunk,
564
771
  done: true
565
772
  };
566
- this.#connection.send(JSON.stringify(response));
773
+ this._connection.send(JSON.stringify(response));
567
774
  }
568
775
  };
569
776
 
570
777
  export {
571
778
  unstable_callable,
572
- WorkflowEntrypoint,
573
- unstable_context,
779
+ getCurrentAgent,
574
780
  Agent,
575
781
  routeAgentRequest,
576
782
  routeAgentEmail,
577
783
  getAgentByName,
578
784
  StreamingResponse
579
785
  };
580
- //# sourceMappingURL=chunk-SZEXGW6W.js.map
786
+ //# sourceMappingURL=chunk-6RPGDIE2.js.map