agents 0.0.0-36d81b3 → 0.0.0-385f0b2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-chat-agent.d.ts +27 -1
- package/dist/ai-chat-agent.js +100 -103
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +12 -0
- package/dist/ai-react.js +2 -1
- package/dist/ai-react.js.map +1 -1
- package/dist/chunk-BZXOAZUX.js +106 -0
- package/dist/chunk-BZXOAZUX.js.map +1 -0
- package/dist/{chunk-HMLY7DHA.js → chunk-NOUFNU2O.js} +1 -5
- package/dist/{chunk-WNIGPPNB.js → chunk-NPGUKHFR.js} +292 -127
- package/dist/chunk-NPGUKHFR.js.map +1 -0
- package/dist/chunk-QSGN3REV.js +123 -0
- package/dist/chunk-QSGN3REV.js.map +1 -0
- package/dist/{chunk-Q5ZBHY4Z.js → chunk-Y67CHZBI.js} +32 -24
- package/dist/chunk-Y67CHZBI.js.map +1 -0
- package/dist/client.d.ts +9 -1
- package/dist/client.js +7 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +67 -6
- package/dist/index.js +5 -3
- package/dist/mcp/client.d.ts +21 -15
- package/dist/mcp/client.js +2 -2
- package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
- package/dist/mcp/do-oauth-client-provider.js +4 -103
- package/dist/mcp/do-oauth-client-provider.js.map +1 -1
- package/dist/mcp/index.d.ts +14 -4
- package/dist/mcp/index.js +107 -135
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +14 -0
- package/dist/react.js +5 -1
- package/dist/react.js.map +1 -1
- package/dist/schedule.js +1 -1
- package/package.json +9 -6
- package/src/index.ts +348 -47
- package/dist/chunk-Q5ZBHY4Z.js.map +0 -1
- package/dist/chunk-WNIGPPNB.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → chunk-NOUFNU2O.js.map} +0 -0
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DurableObjectOAuthClientProvider
|
|
3
|
+
} from "./chunk-BZXOAZUX.js";
|
|
4
|
+
import {
|
|
5
|
+
camelCaseToKebabCase
|
|
6
|
+
} from "./chunk-QSGN3REV.js";
|
|
1
7
|
import {
|
|
2
8
|
MCPClientManager
|
|
3
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-Y67CHZBI.js";
|
|
4
10
|
import {
|
|
5
11
|
__privateAdd,
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
__privateSet
|
|
9
|
-
} from "./chunk-HMLY7DHA.js";
|
|
12
|
+
__privateMethod
|
|
13
|
+
} from "./chunk-NOUFNU2O.js";
|
|
10
14
|
|
|
11
15
|
// src/index.ts
|
|
12
16
|
import {
|
|
@@ -51,19 +55,62 @@ function getCurrentAgent() {
|
|
|
51
55
|
}
|
|
52
56
|
return store;
|
|
53
57
|
}
|
|
54
|
-
var
|
|
58
|
+
var _Agent_instances, connectToMcpServerInternal_fn, getMcpServerStateInternal_fn;
|
|
55
59
|
var Agent = class extends Server {
|
|
56
60
|
constructor(ctx, env) {
|
|
57
61
|
super(ctx, env);
|
|
58
62
|
__privateAdd(this, _Agent_instances);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.mcp = new MCPClientManager(
|
|
63
|
+
this._state = DEFAULT_STATE;
|
|
64
|
+
this._ParentClass = Object.getPrototypeOf(this).constructor;
|
|
65
|
+
this.mcp = new MCPClientManager(this._ParentClass.name, "0.0.1");
|
|
62
66
|
/**
|
|
63
67
|
* Initial state for the Agent
|
|
64
68
|
* Override to provide default state values
|
|
65
69
|
*/
|
|
66
70
|
this.initialState = DEFAULT_STATE;
|
|
71
|
+
/**
|
|
72
|
+
* Method called when an alarm fires.
|
|
73
|
+
* Executes any scheduled tasks that are due.
|
|
74
|
+
*
|
|
75
|
+
* @remarks
|
|
76
|
+
* To schedule a task, please use the `this.schedule` method instead.
|
|
77
|
+
* See {@link https://developers.cloudflare.com/agents/api-reference/schedule-tasks/}
|
|
78
|
+
*/
|
|
79
|
+
this.alarm = async () => {
|
|
80
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
81
|
+
const result = this.sql`
|
|
82
|
+
SELECT * FROM cf_agents_schedules WHERE time <= ${now}
|
|
83
|
+
`;
|
|
84
|
+
for (const row of result || []) {
|
|
85
|
+
const callback = this[row.callback];
|
|
86
|
+
if (!callback) {
|
|
87
|
+
console.error(`callback ${row.callback} not found`);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
await agentContext.run(
|
|
91
|
+
{ agent: this, connection: void 0, request: void 0 },
|
|
92
|
+
async () => {
|
|
93
|
+
try {
|
|
94
|
+
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
95
|
+
} catch (e) {
|
|
96
|
+
console.error(`error executing callback "${row.callback}"`, e);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
if (row.type === "cron") {
|
|
101
|
+
const nextExecutionTime = getNextCronTime(row.cron);
|
|
102
|
+
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
|
|
103
|
+
this.sql`
|
|
104
|
+
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
105
|
+
`;
|
|
106
|
+
} else {
|
|
107
|
+
this.sql`
|
|
108
|
+
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
109
|
+
`;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
await this._scheduleNextAlarm();
|
|
113
|
+
};
|
|
67
114
|
this.sql`
|
|
68
115
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
69
116
|
id TEXT PRIMARY KEY NOT NULL,
|
|
@@ -71,7 +118,7 @@ var Agent = class extends Server {
|
|
|
71
118
|
)
|
|
72
119
|
`;
|
|
73
120
|
void this.ctx.blockConcurrencyWhile(async () => {
|
|
74
|
-
return
|
|
121
|
+
return this._tryCatch(async () => {
|
|
75
122
|
this.sql`
|
|
76
123
|
CREATE TABLE IF NOT EXISTS cf_agents_schedules (
|
|
77
124
|
id TEXT PRIMARY KEY NOT NULL DEFAULT (randomblob(9)),
|
|
@@ -87,22 +134,55 @@ var Agent = class extends Server {
|
|
|
87
134
|
await this.alarm();
|
|
88
135
|
});
|
|
89
136
|
});
|
|
137
|
+
this.sql`
|
|
138
|
+
CREATE TABLE IF NOT EXISTS cf_agents_mcp_servers (
|
|
139
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
140
|
+
name TEXT NOT NULL,
|
|
141
|
+
server_url TEXT NOT NULL,
|
|
142
|
+
callback_url TEXT NOT NULL,
|
|
143
|
+
client_id TEXT,
|
|
144
|
+
auth_url TEXT,
|
|
145
|
+
server_options TEXT
|
|
146
|
+
)
|
|
147
|
+
`;
|
|
148
|
+
const _onRequest = this.onRequest.bind(this);
|
|
149
|
+
this.onRequest = (request) => {
|
|
150
|
+
return agentContext.run(
|
|
151
|
+
{ agent: this, connection: void 0, request },
|
|
152
|
+
async () => {
|
|
153
|
+
if (this.mcp.isCallbackRequest(request)) {
|
|
154
|
+
await this.mcp.handleCallbackRequest(request);
|
|
155
|
+
this.broadcast(
|
|
156
|
+
JSON.stringify({
|
|
157
|
+
type: "cf_agent_mcp_servers",
|
|
158
|
+
mcp: __privateMethod(this, _Agent_instances, getMcpServerStateInternal_fn).call(this)
|
|
159
|
+
})
|
|
160
|
+
);
|
|
161
|
+
return new Response("<script>window.close();</script>", {
|
|
162
|
+
status: 200,
|
|
163
|
+
headers: { "content-type": "text/html" }
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return this._tryCatch(() => _onRequest(request));
|
|
167
|
+
}
|
|
168
|
+
);
|
|
169
|
+
};
|
|
90
170
|
const _onMessage = this.onMessage.bind(this);
|
|
91
171
|
this.onMessage = async (connection, message) => {
|
|
92
172
|
return agentContext.run(
|
|
93
173
|
{ agent: this, connection, request: void 0 },
|
|
94
174
|
async () => {
|
|
95
175
|
if (typeof message !== "string") {
|
|
96
|
-
return
|
|
176
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
97
177
|
}
|
|
98
178
|
let parsed;
|
|
99
179
|
try {
|
|
100
180
|
parsed = JSON.parse(message);
|
|
101
181
|
} catch (e) {
|
|
102
|
-
return
|
|
182
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
103
183
|
}
|
|
104
184
|
if (isStateUpdateMessage(parsed)) {
|
|
105
|
-
|
|
185
|
+
this._setStateInternal(parsed.state, connection);
|
|
106
186
|
return;
|
|
107
187
|
}
|
|
108
188
|
if (isRPCRequest(parsed)) {
|
|
@@ -112,7 +192,7 @@ var Agent = class extends Server {
|
|
|
112
192
|
if (typeof methodFn !== "function") {
|
|
113
193
|
throw new Error(`Method ${method} does not exist`);
|
|
114
194
|
}
|
|
115
|
-
if (!
|
|
195
|
+
if (!this._isCallable(method)) {
|
|
116
196
|
throw new Error(`Method ${method} is not callable`);
|
|
117
197
|
}
|
|
118
198
|
const metadata = callableMetadata.get(methodFn);
|
|
@@ -142,7 +222,7 @@ var Agent = class extends Server {
|
|
|
142
222
|
}
|
|
143
223
|
return;
|
|
144
224
|
}
|
|
145
|
-
return
|
|
225
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
146
226
|
}
|
|
147
227
|
);
|
|
148
228
|
};
|
|
@@ -160,18 +240,50 @@ var Agent = class extends Server {
|
|
|
160
240
|
})
|
|
161
241
|
);
|
|
162
242
|
}
|
|
163
|
-
|
|
243
|
+
connection.send(
|
|
244
|
+
JSON.stringify({
|
|
245
|
+
type: "cf_agent_mcp_servers",
|
|
246
|
+
mcp: __privateMethod(this, _Agent_instances, getMcpServerStateInternal_fn).call(this)
|
|
247
|
+
})
|
|
248
|
+
);
|
|
249
|
+
return this._tryCatch(() => _onConnect(connection, ctx2));
|
|
164
250
|
}, 20);
|
|
165
251
|
}
|
|
166
252
|
);
|
|
167
253
|
};
|
|
254
|
+
const _onStart = this.onStart.bind(this);
|
|
255
|
+
this.onStart = async () => {
|
|
256
|
+
return agentContext.run(
|
|
257
|
+
{ agent: this, connection: void 0, request: void 0 },
|
|
258
|
+
async () => {
|
|
259
|
+
const servers = this.sql`
|
|
260
|
+
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
261
|
+
`;
|
|
262
|
+
await Promise.allSettled(
|
|
263
|
+
servers.map((server) => {
|
|
264
|
+
return __privateMethod(this, _Agent_instances, connectToMcpServerInternal_fn).call(this, server.name, server.server_url, server.callback_url, server.server_options ? JSON.parse(server.server_options) : void 0, {
|
|
265
|
+
id: server.id,
|
|
266
|
+
oauthClientId: server.client_id ?? void 0
|
|
267
|
+
});
|
|
268
|
+
})
|
|
269
|
+
);
|
|
270
|
+
this.broadcast(
|
|
271
|
+
JSON.stringify({
|
|
272
|
+
type: "cf_agent_mcp_servers",
|
|
273
|
+
mcp: __privateMethod(this, _Agent_instances, getMcpServerStateInternal_fn).call(this)
|
|
274
|
+
})
|
|
275
|
+
);
|
|
276
|
+
await this._tryCatch(() => _onStart());
|
|
277
|
+
}
|
|
278
|
+
);
|
|
279
|
+
};
|
|
168
280
|
}
|
|
169
281
|
/**
|
|
170
282
|
* Current state of the Agent
|
|
171
283
|
*/
|
|
172
284
|
get state() {
|
|
173
|
-
if (
|
|
174
|
-
return
|
|
285
|
+
if (this._state !== DEFAULT_STATE) {
|
|
286
|
+
return this._state;
|
|
175
287
|
}
|
|
176
288
|
const wasChanged = this.sql`
|
|
177
289
|
SELECT state FROM cf_agents_state WHERE id = ${STATE_WAS_CHANGED}
|
|
@@ -182,8 +294,8 @@ var Agent = class extends Server {
|
|
|
182
294
|
if (wasChanged[0]?.state === "true" || // we do this check for people who updated their code before we shipped wasChanged
|
|
183
295
|
result[0]?.state) {
|
|
184
296
|
const state = result[0]?.state;
|
|
185
|
-
|
|
186
|
-
return
|
|
297
|
+
this._state = JSON.parse(state);
|
|
298
|
+
return this._state;
|
|
187
299
|
}
|
|
188
300
|
if (this.initialState === DEFAULT_STATE) {
|
|
189
301
|
return void 0;
|
|
@@ -211,12 +323,39 @@ var Agent = class extends Server {
|
|
|
211
323
|
throw this.onError(e);
|
|
212
324
|
}
|
|
213
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
|
+
}
|
|
214
353
|
/**
|
|
215
354
|
* Update the Agent's state
|
|
216
355
|
* @param state New state to set
|
|
217
356
|
*/
|
|
218
357
|
setState(state) {
|
|
219
|
-
|
|
358
|
+
this._setStateInternal(state, "server");
|
|
220
359
|
}
|
|
221
360
|
/**
|
|
222
361
|
* Called when the Agent's state is updated
|
|
@@ -237,6 +376,13 @@ var Agent = class extends Server {
|
|
|
237
376
|
}
|
|
238
377
|
);
|
|
239
378
|
}
|
|
379
|
+
async _tryCatch(fn) {
|
|
380
|
+
try {
|
|
381
|
+
return await fn();
|
|
382
|
+
} catch (e) {
|
|
383
|
+
throw this.onError(e);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
240
386
|
onError(connectionOrError, error) {
|
|
241
387
|
let theError;
|
|
242
388
|
if (connectionOrError && error) {
|
|
@@ -286,7 +432,7 @@ var Agent = class extends Server {
|
|
|
286
432
|
payload
|
|
287
433
|
)}, 'scheduled', ${timestamp})
|
|
288
434
|
`;
|
|
289
|
-
await
|
|
435
|
+
await this._scheduleNextAlarm();
|
|
290
436
|
return {
|
|
291
437
|
id,
|
|
292
438
|
callback,
|
|
@@ -304,7 +450,7 @@ var Agent = class extends Server {
|
|
|
304
450
|
payload
|
|
305
451
|
)}, 'delayed', ${when}, ${timestamp})
|
|
306
452
|
`;
|
|
307
|
-
await
|
|
453
|
+
await this._scheduleNextAlarm();
|
|
308
454
|
return {
|
|
309
455
|
id,
|
|
310
456
|
callback,
|
|
@@ -323,7 +469,7 @@ var Agent = class extends Server {
|
|
|
323
469
|
payload
|
|
324
470
|
)}, 'cron', ${when}, ${timestamp})
|
|
325
471
|
`;
|
|
326
|
-
await
|
|
472
|
+
await this._scheduleNextAlarm();
|
|
327
473
|
return {
|
|
328
474
|
id,
|
|
329
475
|
callback,
|
|
@@ -390,47 +536,21 @@ var Agent = class extends Server {
|
|
|
390
536
|
*/
|
|
391
537
|
async cancelSchedule(id) {
|
|
392
538
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
393
|
-
await
|
|
539
|
+
await this._scheduleNextAlarm();
|
|
394
540
|
return true;
|
|
395
541
|
}
|
|
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);
|
|
542
|
+
async _scheduleNextAlarm() {
|
|
402
543
|
const result = this.sql`
|
|
403
|
-
SELECT
|
|
544
|
+
SELECT time FROM cf_agents_schedules
|
|
545
|
+
WHERE time > ${Math.floor(Date.now() / 1e3)}
|
|
546
|
+
ORDER BY time ASC
|
|
547
|
+
LIMIT 1
|
|
404
548
|
`;
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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
|
-
}
|
|
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);
|
|
432
553
|
}
|
|
433
|
-
await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
|
|
434
554
|
}
|
|
435
555
|
/**
|
|
436
556
|
* Destroy the Agent, removing all state and scheduled tasks
|
|
@@ -438,66 +558,117 @@ var Agent = class extends Server {
|
|
|
438
558
|
async destroy() {
|
|
439
559
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
440
560
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
561
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
441
562
|
await this.ctx.storage.deleteAlarm();
|
|
442
563
|
await this.ctx.storage.deleteAll();
|
|
443
564
|
}
|
|
565
|
+
_isCallable(method) {
|
|
566
|
+
return callableMetadata.has(this[method]);
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Connect to a new MCP Server
|
|
570
|
+
*
|
|
571
|
+
* @param url MCP Server SSE URL
|
|
572
|
+
* @param callbackHost Base host for the agent, used for the redirect URI.
|
|
573
|
+
* @param agentsPrefix agents routing prefix if not using `agents`
|
|
574
|
+
* @param options MCP client and transport (header) options
|
|
575
|
+
* @returns authUrl
|
|
576
|
+
*/
|
|
577
|
+
async addMcpServer(serverName, url, callbackHost, agentsPrefix = "agents", options) {
|
|
578
|
+
const callbackUrl = `${callbackHost}/${agentsPrefix}/${camelCaseToKebabCase(this._ParentClass.name)}/${this.name}/callback`;
|
|
579
|
+
const result = await __privateMethod(this, _Agent_instances, connectToMcpServerInternal_fn).call(this, serverName, url, callbackUrl, options);
|
|
580
|
+
this.broadcast(
|
|
581
|
+
JSON.stringify({
|
|
582
|
+
type: "cf_agent_mcp_servers",
|
|
583
|
+
mcp: __privateMethod(this, _Agent_instances, getMcpServerStateInternal_fn).call(this)
|
|
584
|
+
})
|
|
585
|
+
);
|
|
586
|
+
return result;
|
|
587
|
+
}
|
|
588
|
+
async removeMcpServer(id) {
|
|
589
|
+
this.mcp.closeConnection(id);
|
|
590
|
+
this.sql`
|
|
591
|
+
DELETE FROM cf_agents_mcp_servers WHERE id = ${id};
|
|
592
|
+
`;
|
|
593
|
+
this.broadcast(
|
|
594
|
+
JSON.stringify({
|
|
595
|
+
type: "cf_agent_mcp_servers",
|
|
596
|
+
mcp: __privateMethod(this, _Agent_instances, getMcpServerStateInternal_fn).call(this)
|
|
597
|
+
})
|
|
598
|
+
);
|
|
599
|
+
}
|
|
444
600
|
};
|
|
445
|
-
_state = new WeakMap();
|
|
446
|
-
_ParentClass = new WeakMap();
|
|
447
601
|
_Agent_instances = new WeakSet();
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
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] : []
|
|
602
|
+
connectToMcpServerInternal_fn = async function(serverName, url, callbackUrl, options, reconnect) {
|
|
603
|
+
const authProvider = new DurableObjectOAuthClientProvider(
|
|
604
|
+
this.ctx.storage,
|
|
605
|
+
this.name,
|
|
606
|
+
callbackUrl
|
|
464
607
|
);
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
608
|
+
if (reconnect) {
|
|
609
|
+
authProvider.serverId = reconnect.id;
|
|
610
|
+
if (reconnect.oauthClientId) {
|
|
611
|
+
authProvider.clientId = reconnect.oauthClientId;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
let headerTransportOpts = {};
|
|
615
|
+
if (options?.transport?.headers) {
|
|
616
|
+
headerTransportOpts = {
|
|
617
|
+
eventSourceInit: {
|
|
618
|
+
fetch: (url2, init) => fetch(url2, {
|
|
619
|
+
...init,
|
|
620
|
+
headers: options?.transport?.headers
|
|
621
|
+
})
|
|
622
|
+
},
|
|
623
|
+
requestInit: {
|
|
624
|
+
headers: options?.transport?.headers
|
|
471
625
|
}
|
|
472
|
-
|
|
473
|
-
});
|
|
474
|
-
};
|
|
475
|
-
tryCatch_fn = async function(fn) {
|
|
476
|
-
try {
|
|
477
|
-
return await fn();
|
|
478
|
-
} catch (e) {
|
|
479
|
-
throw this.onError(e);
|
|
626
|
+
};
|
|
480
627
|
}
|
|
628
|
+
const { id, authUrl, clientId } = await this.mcp.connect(url, {
|
|
629
|
+
reconnect,
|
|
630
|
+
transport: {
|
|
631
|
+
...headerTransportOpts,
|
|
632
|
+
authProvider
|
|
633
|
+
},
|
|
634
|
+
client: options?.client
|
|
635
|
+
});
|
|
636
|
+
this.sql`
|
|
637
|
+
INSERT OR REPLACE INTO cf_agents_mcp_servers (id, name, server_url, client_id, auth_url, callback_url, server_options)
|
|
638
|
+
VALUES (
|
|
639
|
+
${id},
|
|
640
|
+
${serverName},
|
|
641
|
+
${url},
|
|
642
|
+
${clientId ?? null},
|
|
643
|
+
${authUrl ?? null},
|
|
644
|
+
${callbackUrl},
|
|
645
|
+
${options ? JSON.stringify(options) : null}
|
|
646
|
+
);
|
|
647
|
+
`;
|
|
648
|
+
return {
|
|
649
|
+
id,
|
|
650
|
+
authUrl
|
|
651
|
+
};
|
|
481
652
|
};
|
|
482
|
-
|
|
483
|
-
const
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
653
|
+
getMcpServerStateInternal_fn = function() {
|
|
654
|
+
const mcpState = {
|
|
655
|
+
servers: {},
|
|
656
|
+
tools: this.mcp.listTools(),
|
|
657
|
+
prompts: this.mcp.listPrompts(),
|
|
658
|
+
resources: this.mcp.listResources()
|
|
659
|
+
};
|
|
660
|
+
const servers = this.sql`
|
|
661
|
+
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
488
662
|
`;
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
663
|
+
for (const server of servers) {
|
|
664
|
+
mcpState.servers[server.id] = {
|
|
665
|
+
name: server.name,
|
|
666
|
+
server_url: server.server_url,
|
|
667
|
+
auth_url: server.auth_url,
|
|
668
|
+
state: this.mcp.mcpConnections[server.id].connectionState
|
|
669
|
+
};
|
|
493
670
|
}
|
|
494
|
-
|
|
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]);
|
|
671
|
+
return mcpState;
|
|
501
672
|
};
|
|
502
673
|
/**
|
|
503
674
|
* Agent configuration options
|
|
@@ -544,57 +715,51 @@ async function routeAgentRequest(request, env, options) {
|
|
|
544
715
|
}
|
|
545
716
|
async function routeAgentEmail(email, env, options) {
|
|
546
717
|
}
|
|
547
|
-
function getAgentByName(namespace, name, options) {
|
|
718
|
+
async function getAgentByName(namespace, name, options) {
|
|
548
719
|
return getServerByName(namespace, name, options);
|
|
549
720
|
}
|
|
550
|
-
var _connection, _id, _closed;
|
|
551
721
|
var StreamingResponse = class {
|
|
552
722
|
constructor(connection, id) {
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
__privateSet(this, _connection, connection);
|
|
557
|
-
__privateSet(this, _id, id);
|
|
723
|
+
this._closed = false;
|
|
724
|
+
this._connection = connection;
|
|
725
|
+
this._id = id;
|
|
558
726
|
}
|
|
559
727
|
/**
|
|
560
728
|
* Send a chunk of data to the client
|
|
561
729
|
* @param chunk The data to send
|
|
562
730
|
*/
|
|
563
731
|
send(chunk) {
|
|
564
|
-
if (
|
|
732
|
+
if (this._closed) {
|
|
565
733
|
throw new Error("StreamingResponse is already closed");
|
|
566
734
|
}
|
|
567
735
|
const response = {
|
|
568
736
|
type: "rpc",
|
|
569
|
-
id:
|
|
737
|
+
id: this._id,
|
|
570
738
|
success: true,
|
|
571
739
|
result: chunk,
|
|
572
740
|
done: false
|
|
573
741
|
};
|
|
574
|
-
|
|
742
|
+
this._connection.send(JSON.stringify(response));
|
|
575
743
|
}
|
|
576
744
|
/**
|
|
577
745
|
* End the stream and send the final chunk (if any)
|
|
578
746
|
* @param finalChunk Optional final chunk of data to send
|
|
579
747
|
*/
|
|
580
748
|
end(finalChunk) {
|
|
581
|
-
if (
|
|
749
|
+
if (this._closed) {
|
|
582
750
|
throw new Error("StreamingResponse is already closed");
|
|
583
751
|
}
|
|
584
|
-
|
|
752
|
+
this._closed = true;
|
|
585
753
|
const response = {
|
|
586
754
|
type: "rpc",
|
|
587
|
-
id:
|
|
755
|
+
id: this._id,
|
|
588
756
|
success: true,
|
|
589
757
|
result: finalChunk,
|
|
590
758
|
done: true
|
|
591
759
|
};
|
|
592
|
-
|
|
760
|
+
this._connection.send(JSON.stringify(response));
|
|
593
761
|
}
|
|
594
762
|
};
|
|
595
|
-
_connection = new WeakMap();
|
|
596
|
-
_id = new WeakMap();
|
|
597
|
-
_closed = new WeakMap();
|
|
598
763
|
|
|
599
764
|
export {
|
|
600
765
|
unstable_callable,
|
|
@@ -605,4 +770,4 @@ export {
|
|
|
605
770
|
getAgentByName,
|
|
606
771
|
StreamingResponse
|
|
607
772
|
};
|
|
608
|
-
//# sourceMappingURL=chunk-
|
|
773
|
+
//# sourceMappingURL=chunk-NPGUKHFR.js.map
|