agents 0.0.0-947fd07 → 0.0.0-94d9a2e
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 +49 -4
- package/dist/ai-chat-agent.js +129 -66
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +12 -0
- package/dist/ai-react.js +38 -24
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +5 -0
- package/dist/chunk-BZXOAZUX.js +106 -0
- package/dist/chunk-BZXOAZUX.js.map +1 -0
- package/dist/{chunk-3PRPPPJY.js → chunk-J6T74FUS.js} +327 -140
- package/dist/chunk-J6T74FUS.js.map +1 -0
- package/dist/chunk-QSGN3REV.js +123 -0
- package/dist/chunk-QSGN3REV.js.map +1 -0
- package/dist/chunk-Y67CHZBI.js +464 -0
- package/dist/chunk-Y67CHZBI.js.map +1 -0
- package/dist/client.d.ts +9 -1
- package/dist/client.js +6 -133
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +120 -18
- package/dist/index.js +6 -6
- package/dist/mcp/client.d.ts +783 -0
- package/dist/mcp/client.js +9 -0
- package/dist/mcp/do-oauth-client-provider.d.ts +41 -0
- package/dist/mcp/do-oauth-client-provider.js +7 -0
- package/dist/mcp/do-oauth-client-provider.js.map +1 -0
- package/dist/mcp/index.d.ts +84 -0
- package/dist/mcp/index.js +779 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/react.d.ts +73 -0
- package/dist/react.js +48 -29
- package/dist/react.js.map +1 -1
- package/dist/schedule.js +0 -2
- package/dist/schedule.js.map +1 -1
- package/package.json +37 -8
- package/src/index.ts +394 -66
- package/dist/chunk-3PRPPPJY.js.map +0 -1
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/mcp.d.ts +0 -58
- package/dist/mcp.js +0 -945
- package/dist/mcp.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → mcp/client.js.map} +0 -0
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "./chunk-
|
|
2
|
+
DurableObjectOAuthClientProvider
|
|
3
|
+
} from "./chunk-BZXOAZUX.js";
|
|
4
|
+
import {
|
|
5
|
+
camelCaseToKebabCase
|
|
6
|
+
} from "./chunk-QSGN3REV.js";
|
|
7
|
+
import {
|
|
8
|
+
MCPClientManager
|
|
9
|
+
} from "./chunk-Y67CHZBI.js";
|
|
7
10
|
|
|
8
11
|
// src/index.ts
|
|
9
12
|
import {
|
|
@@ -14,7 +17,6 @@ import {
|
|
|
14
17
|
import { parseCronExpression } from "cron-schedule";
|
|
15
18
|
import { nanoid } from "nanoid";
|
|
16
19
|
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
|
}
|
|
@@ -23,15 +25,13 @@ function isStateUpdateMessage(msg) {
|
|
|
23
25
|
}
|
|
24
26
|
var callableMetadata = /* @__PURE__ */ new Map();
|
|
25
27
|
function unstable_callable(metadata = {}) {
|
|
26
|
-
return function callableDecorator(target,
|
|
28
|
+
return function callableDecorator(target, context) {
|
|
27
29
|
if (!callableMetadata.has(target)) {
|
|
28
30
|
callableMetadata.set(target, metadata);
|
|
29
31
|
}
|
|
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
|
|
43
|
-
|
|
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
|
-
|
|
48
|
-
|
|
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
|
|
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
|
+
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
|
+
};
|
|
77
164
|
const _onMessage = this.onMessage.bind(this);
|
|
78
165
|
this.onMessage = async (connection, message) => {
|
|
79
|
-
return
|
|
166
|
+
return agentContext.run(
|
|
80
167
|
{ agent: this, connection, request: void 0 },
|
|
81
168
|
async () => {
|
|
82
169
|
if (typeof message !== "string") {
|
|
83
|
-
return
|
|
170
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
84
171
|
}
|
|
85
172
|
let parsed;
|
|
86
173
|
try {
|
|
87
174
|
parsed = JSON.parse(message);
|
|
88
175
|
} catch (e) {
|
|
89
|
-
return
|
|
176
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
90
177
|
}
|
|
91
178
|
if (isStateUpdateMessage(parsed)) {
|
|
92
|
-
|
|
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 (!
|
|
189
|
+
if (!this._isCallable(method)) {
|
|
103
190
|
throw new Error(`Method ${method} is not callable`);
|
|
104
191
|
}
|
|
105
192
|
const metadata = callableMetadata.get(methodFn);
|
|
@@ -129,13 +216,13 @@ var Agent = class extends Server {
|
|
|
129
216
|
}
|
|
130
217
|
return;
|
|
131
218
|
}
|
|
132
|
-
return
|
|
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
|
|
225
|
+
return agentContext.run(
|
|
139
226
|
{ agent: this, connection, request: ctx2.request },
|
|
140
227
|
async () => {
|
|
141
228
|
setTimeout(() => {
|
|
@@ -147,18 +234,56 @@ var Agent = class extends Server {
|
|
|
147
234
|
})
|
|
148
235
|
);
|
|
149
236
|
}
|
|
150
|
-
|
|
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));
|
|
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
|
+
await 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
|
+
);
|
|
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
|
+
};
|
|
155
280
|
}
|
|
156
281
|
/**
|
|
157
282
|
* Current state of the Agent
|
|
158
283
|
*/
|
|
159
284
|
get state() {
|
|
160
|
-
if (
|
|
161
|
-
return
|
|
285
|
+
if (this._state !== DEFAULT_STATE) {
|
|
286
|
+
return this._state;
|
|
162
287
|
}
|
|
163
288
|
const wasChanged = this.sql`
|
|
164
289
|
SELECT state FROM cf_agents_state WHERE id = ${STATE_WAS_CHANGED}
|
|
@@ -169,8 +294,8 @@ var Agent = class extends Server {
|
|
|
169
294
|
if (wasChanged[0]?.state === "true" || // we do this check for people who updated their code before we shipped wasChanged
|
|
170
295
|
result[0]?.state) {
|
|
171
296
|
const state = result[0]?.state;
|
|
172
|
-
|
|
173
|
-
return
|
|
297
|
+
this._state = JSON.parse(state);
|
|
298
|
+
return this._state;
|
|
174
299
|
}
|
|
175
300
|
if (this.initialState === DEFAULT_STATE) {
|
|
176
301
|
return void 0;
|
|
@@ -198,12 +323,39 @@ var Agent = class extends Server {
|
|
|
198
323
|
throw this.onError(e);
|
|
199
324
|
}
|
|
200
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
|
+
}
|
|
201
353
|
/**
|
|
202
354
|
* Update the Agent's state
|
|
203
355
|
* @param state New state to set
|
|
204
356
|
*/
|
|
205
357
|
setState(state) {
|
|
206
|
-
|
|
358
|
+
this._setStateInternal(state, "server");
|
|
207
359
|
}
|
|
208
360
|
/**
|
|
209
361
|
* Called when the Agent's state is updated
|
|
@@ -217,13 +369,20 @@ var Agent = class extends Server {
|
|
|
217
369
|
* @param email Email message to process
|
|
218
370
|
*/
|
|
219
371
|
onEmail(email) {
|
|
220
|
-
return
|
|
372
|
+
return agentContext.run(
|
|
221
373
|
{ agent: this, connection: void 0, request: void 0 },
|
|
222
374
|
async () => {
|
|
223
375
|
console.error("onEmail not implemented");
|
|
224
376
|
}
|
|
225
377
|
);
|
|
226
378
|
}
|
|
379
|
+
async _tryCatch(fn) {
|
|
380
|
+
try {
|
|
381
|
+
return await fn();
|
|
382
|
+
} catch (e) {
|
|
383
|
+
throw this.onError(e);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
227
386
|
onError(connectionOrError, error) {
|
|
228
387
|
let theError;
|
|
229
388
|
if (connectionOrError && error) {
|
|
@@ -273,7 +432,7 @@ var Agent = class extends Server {
|
|
|
273
432
|
payload
|
|
274
433
|
)}, 'scheduled', ${timestamp})
|
|
275
434
|
`;
|
|
276
|
-
await
|
|
435
|
+
await this._scheduleNextAlarm();
|
|
277
436
|
return {
|
|
278
437
|
id,
|
|
279
438
|
callback,
|
|
@@ -291,7 +450,7 @@ var Agent = class extends Server {
|
|
|
291
450
|
payload
|
|
292
451
|
)}, 'delayed', ${when}, ${timestamp})
|
|
293
452
|
`;
|
|
294
|
-
await
|
|
453
|
+
await this._scheduleNextAlarm();
|
|
295
454
|
return {
|
|
296
455
|
id,
|
|
297
456
|
callback,
|
|
@@ -310,7 +469,7 @@ var Agent = class extends Server {
|
|
|
310
469
|
payload
|
|
311
470
|
)}, 'cron', ${when}, ${timestamp})
|
|
312
471
|
`;
|
|
313
|
-
await
|
|
472
|
+
await this._scheduleNextAlarm();
|
|
314
473
|
return {
|
|
315
474
|
id,
|
|
316
475
|
callback,
|
|
@@ -351,10 +510,6 @@ var Agent = class extends Server {
|
|
|
351
510
|
query += " AND id = ?";
|
|
352
511
|
params.push(criteria.id);
|
|
353
512
|
}
|
|
354
|
-
if (criteria.description) {
|
|
355
|
-
query += " AND description = ?";
|
|
356
|
-
params.push(criteria.description);
|
|
357
|
-
}
|
|
358
513
|
if (criteria.type) {
|
|
359
514
|
query += " AND type = ?";
|
|
360
515
|
params.push(criteria.type);
|
|
@@ -381,47 +536,21 @@ var Agent = class extends Server {
|
|
|
381
536
|
*/
|
|
382
537
|
async cancelSchedule(id) {
|
|
383
538
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
384
|
-
await
|
|
539
|
+
await this._scheduleNextAlarm();
|
|
385
540
|
return true;
|
|
386
541
|
}
|
|
387
|
-
|
|
388
|
-
* Method called when an alarm fires
|
|
389
|
-
* Executes any scheduled tasks that are due
|
|
390
|
-
*/
|
|
391
|
-
async alarm() {
|
|
392
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
542
|
+
async _scheduleNextAlarm() {
|
|
393
543
|
const result = this.sql`
|
|
394
|
-
SELECT
|
|
544
|
+
SELECT time FROM cf_agents_schedules
|
|
545
|
+
WHERE time > ${Math.floor(Date.now() / 1e3)}
|
|
546
|
+
ORDER BY time ASC
|
|
547
|
+
LIMIT 1
|
|
395
548
|
`;
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
continue;
|
|
401
|
-
}
|
|
402
|
-
await context.run(
|
|
403
|
-
{ agent: this, connection: void 0, request: void 0 },
|
|
404
|
-
async () => {
|
|
405
|
-
try {
|
|
406
|
-
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
407
|
-
} catch (e) {
|
|
408
|
-
console.error(`error executing callback "${row.callback}"`, e);
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
);
|
|
412
|
-
if (row.type === "cron") {
|
|
413
|
-
const nextExecutionTime = getNextCronTime(row.cron);
|
|
414
|
-
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
|
|
415
|
-
this.sql`
|
|
416
|
-
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
417
|
-
`;
|
|
418
|
-
} else {
|
|
419
|
-
this.sql`
|
|
420
|
-
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
421
|
-
`;
|
|
422
|
-
}
|
|
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);
|
|
423
553
|
}
|
|
424
|
-
await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
|
|
425
554
|
}
|
|
426
555
|
/**
|
|
427
556
|
* Destroy the Agent, removing all state and scheduled tasks
|
|
@@ -429,62 +558,127 @@ var Agent = class extends Server {
|
|
|
429
558
|
async destroy() {
|
|
430
559
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
431
560
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
561
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
432
562
|
await this.ctx.storage.deleteAlarm();
|
|
433
563
|
await this.ctx.storage.deleteAll();
|
|
434
564
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
})
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
565
|
+
/**
|
|
566
|
+
* Get all methods marked as callable on this Agent
|
|
567
|
+
* @returns A map of method names to their metadata
|
|
568
|
+
*/
|
|
569
|
+
_isCallable(method) {
|
|
570
|
+
return callableMetadata.has(this[method]);
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Connect to a new MCP Server
|
|
574
|
+
*
|
|
575
|
+
* @param url MCP Server SSE URL
|
|
576
|
+
* @param callbackHost Base host for the agent, used for the redirect URI.
|
|
577
|
+
* @param agentsPrefix agents routing prefix if not using `agents`
|
|
578
|
+
* @param options MCP client and transport (header) options
|
|
579
|
+
* @returns authUrl
|
|
580
|
+
*/
|
|
581
|
+
async addMcpServer(serverName, url, callbackHost, agentsPrefix = "agents", options) {
|
|
582
|
+
const callbackUrl = `${callbackHost}/${agentsPrefix}/${camelCaseToKebabCase(this._ParentClass.name)}/${this.name}/callback`;
|
|
583
|
+
const result = await this._connectToMcpServerInternal(
|
|
584
|
+
serverName,
|
|
585
|
+
url,
|
|
586
|
+
callbackUrl,
|
|
587
|
+
options
|
|
588
|
+
);
|
|
589
|
+
this.broadcast(
|
|
590
|
+
JSON.stringify({
|
|
591
|
+
type: "cf_agent_mcp_servers",
|
|
592
|
+
mcp: this.getMcpServers()
|
|
593
|
+
})
|
|
594
|
+
);
|
|
595
|
+
return result;
|
|
596
|
+
}
|
|
597
|
+
async _connectToMcpServerInternal(serverName, url, callbackUrl, options, reconnect) {
|
|
598
|
+
const authProvider = new DurableObjectOAuthClientProvider(
|
|
599
|
+
this.ctx.storage,
|
|
600
|
+
this.name,
|
|
601
|
+
callbackUrl
|
|
602
|
+
);
|
|
603
|
+
if (reconnect) {
|
|
604
|
+
authProvider.serverId = reconnect.id;
|
|
605
|
+
if (reconnect.oauthClientId) {
|
|
606
|
+
authProvider.clientId = reconnect.oauthClientId;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
let headerTransportOpts = {};
|
|
610
|
+
if (options?.transport?.headers) {
|
|
611
|
+
headerTransportOpts = {
|
|
612
|
+
eventSourceInit: {
|
|
613
|
+
fetch: (url2, init) => fetch(url2, {
|
|
614
|
+
...init,
|
|
615
|
+
headers: options?.transport?.headers
|
|
616
|
+
})
|
|
617
|
+
},
|
|
618
|
+
requestInit: {
|
|
619
|
+
headers: options?.transport?.headers
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
const { id, authUrl, clientId } = await this.mcp.connect(url, {
|
|
624
|
+
reconnect,
|
|
625
|
+
transport: {
|
|
626
|
+
...headerTransportOpts,
|
|
627
|
+
authProvider
|
|
628
|
+
},
|
|
629
|
+
client: options?.client
|
|
459
630
|
});
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
631
|
+
this.sql`
|
|
632
|
+
INSERT OR REPLACE INTO cf_agents_mcp_servers (id, name, server_url, client_id, auth_url, callback_url, server_options)
|
|
633
|
+
VALUES (
|
|
634
|
+
${id},
|
|
635
|
+
${serverName},
|
|
636
|
+
${url},
|
|
637
|
+
${clientId ?? null},
|
|
638
|
+
${authUrl ?? null},
|
|
639
|
+
${callbackUrl},
|
|
640
|
+
${options ? JSON.stringify(options) : null}
|
|
641
|
+
);
|
|
642
|
+
`;
|
|
643
|
+
return {
|
|
644
|
+
id,
|
|
645
|
+
authUrl
|
|
646
|
+
};
|
|
467
647
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
WHERE time > ${Math.floor(Date.now() / 1e3)}
|
|
473
|
-
ORDER BY time ASC
|
|
474
|
-
LIMIT 1
|
|
648
|
+
async removeMcpServer(id) {
|
|
649
|
+
this.mcp.closeConnection(id);
|
|
650
|
+
this.sql`
|
|
651
|
+
DELETE FROM cf_agents_mcp_servers WHERE id = ${id};
|
|
475
652
|
`;
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
653
|
+
this.broadcast(
|
|
654
|
+
JSON.stringify({
|
|
655
|
+
type: "cf_agent_mcp_servers",
|
|
656
|
+
mcp: this.getMcpServers()
|
|
657
|
+
})
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
getMcpServers() {
|
|
661
|
+
const mcpState = {
|
|
662
|
+
servers: {},
|
|
663
|
+
tools: this.mcp.listTools(),
|
|
664
|
+
prompts: this.mcp.listPrompts(),
|
|
665
|
+
resources: this.mcp.listResources()
|
|
666
|
+
};
|
|
667
|
+
const servers = this.sql`
|
|
668
|
+
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
669
|
+
`;
|
|
670
|
+
for (const server of servers) {
|
|
671
|
+
mcpState.servers[server.id] = {
|
|
672
|
+
name: server.name,
|
|
673
|
+
server_url: server.server_url,
|
|
674
|
+
auth_url: server.auth_url,
|
|
675
|
+
state: this.mcp.mcpConnections[server.id].connectionState,
|
|
676
|
+
instructions: this.mcp.mcpConnections[server.id].instructions ?? null,
|
|
677
|
+
capabilities: this.mcp.mcpConnections[server.id].serverCapabilities ?? null
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
return mcpState;
|
|
480
681
|
}
|
|
481
|
-
};
|
|
482
|
-
/**
|
|
483
|
-
* Get all methods marked as callable on this Agent
|
|
484
|
-
* @returns A map of method names to their metadata
|
|
485
|
-
*/
|
|
486
|
-
isCallable_fn = function(method) {
|
|
487
|
-
return callableMetadata.has(this[method]);
|
|
488
682
|
};
|
|
489
683
|
/**
|
|
490
684
|
* Agent configuration options
|
|
@@ -531,66 +725,59 @@ async function routeAgentRequest(request, env, options) {
|
|
|
531
725
|
}
|
|
532
726
|
async function routeAgentEmail(email, env, options) {
|
|
533
727
|
}
|
|
534
|
-
function getAgentByName(namespace, name, options) {
|
|
728
|
+
async function getAgentByName(namespace, name, options) {
|
|
535
729
|
return getServerByName(namespace, name, options);
|
|
536
730
|
}
|
|
537
|
-
var _connection, _id, _closed;
|
|
538
731
|
var StreamingResponse = class {
|
|
539
732
|
constructor(connection, id) {
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
__privateSet(this, _connection, connection);
|
|
544
|
-
__privateSet(this, _id, id);
|
|
733
|
+
this._closed = false;
|
|
734
|
+
this._connection = connection;
|
|
735
|
+
this._id = id;
|
|
545
736
|
}
|
|
546
737
|
/**
|
|
547
738
|
* Send a chunk of data to the client
|
|
548
739
|
* @param chunk The data to send
|
|
549
740
|
*/
|
|
550
741
|
send(chunk) {
|
|
551
|
-
if (
|
|
742
|
+
if (this._closed) {
|
|
552
743
|
throw new Error("StreamingResponse is already closed");
|
|
553
744
|
}
|
|
554
745
|
const response = {
|
|
555
746
|
type: "rpc",
|
|
556
|
-
id:
|
|
747
|
+
id: this._id,
|
|
557
748
|
success: true,
|
|
558
749
|
result: chunk,
|
|
559
750
|
done: false
|
|
560
751
|
};
|
|
561
|
-
|
|
752
|
+
this._connection.send(JSON.stringify(response));
|
|
562
753
|
}
|
|
563
754
|
/**
|
|
564
755
|
* End the stream and send the final chunk (if any)
|
|
565
756
|
* @param finalChunk Optional final chunk of data to send
|
|
566
757
|
*/
|
|
567
758
|
end(finalChunk) {
|
|
568
|
-
if (
|
|
759
|
+
if (this._closed) {
|
|
569
760
|
throw new Error("StreamingResponse is already closed");
|
|
570
761
|
}
|
|
571
|
-
|
|
762
|
+
this._closed = true;
|
|
572
763
|
const response = {
|
|
573
764
|
type: "rpc",
|
|
574
|
-
id:
|
|
765
|
+
id: this._id,
|
|
575
766
|
success: true,
|
|
576
767
|
result: finalChunk,
|
|
577
768
|
done: true
|
|
578
769
|
};
|
|
579
|
-
|
|
770
|
+
this._connection.send(JSON.stringify(response));
|
|
580
771
|
}
|
|
581
772
|
};
|
|
582
|
-
_connection = new WeakMap();
|
|
583
|
-
_id = new WeakMap();
|
|
584
|
-
_closed = new WeakMap();
|
|
585
773
|
|
|
586
774
|
export {
|
|
587
775
|
unstable_callable,
|
|
588
|
-
|
|
589
|
-
context,
|
|
776
|
+
getCurrentAgent,
|
|
590
777
|
Agent,
|
|
591
778
|
routeAgentRequest,
|
|
592
779
|
routeAgentEmail,
|
|
593
780
|
getAgentByName,
|
|
594
781
|
StreamingResponse
|
|
595
782
|
};
|
|
596
|
-
//# sourceMappingURL=chunk-
|
|
783
|
+
//# sourceMappingURL=chunk-J6T74FUS.js.map
|