agents 0.0.0-f973b54 → 0.0.0-fb503ba
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 +28 -2
- package/dist/ai-chat-agent.js +99 -103
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +13 -0
- package/dist/ai-react.js +1 -2
- 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-HN5JVKAZ.js → chunk-MXJNY43J.js} +317 -138
- package/dist/chunk-MXJNY43J.js.map +1 -0
- package/dist/{chunk-Q5ZBHY4Z.js → chunk-OYJXQRRH.js} +33 -24
- package/dist/chunk-OYJXQRRH.js.map +1 -0
- package/dist/chunk-VCSB47AK.js +116 -0
- package/dist/chunk-VCSB47AK.js.map +1 -0
- package/dist/client.d.ts +15 -1
- package/dist/client.js +6 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +105 -8
- package/dist/index.js +4 -3
- package/dist/mcp/client.d.ts +21 -15
- package/dist/mcp/client.js +1 -2
- package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
- package/dist/mcp/do-oauth-client-provider.js +3 -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 +111 -137
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +85 -5
- package/dist/react.js +14 -2
- package/dist/react.js.map +1 -1
- package/dist/schedule.js +0 -2
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/package.json +10 -7
- package/src/index.ts +367 -53
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-HN5JVKAZ.js.map +0 -1
- package/dist/chunk-Q5ZBHY4Z.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → serializable.js.map} +0 -0
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MCPClientManager
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-OYJXQRRH.js";
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "./chunk-
|
|
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
|
-
|
|
15
|
-
|
|
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 "
|
|
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
|
}
|
|
@@ -43,25 +43,68 @@ var agentContext = new AsyncLocalStorage();
|
|
|
43
43
|
function getCurrentAgent() {
|
|
44
44
|
const store = agentContext.getStore();
|
|
45
45
|
if (!store) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
return {
|
|
47
|
+
agent: void 0,
|
|
48
|
+
connection: void 0,
|
|
49
|
+
request: void 0
|
|
50
|
+
};
|
|
49
51
|
}
|
|
50
52
|
return store;
|
|
51
53
|
}
|
|
52
|
-
var _state, _ParentClass, _Agent_instances, setStateInternal_fn, tryCatch_fn, scheduleNextAlarm_fn, isCallable_fn;
|
|
53
54
|
var Agent = class extends Server {
|
|
54
55
|
constructor(ctx, env) {
|
|
55
56
|
super(ctx, env);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
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");
|
|
60
60
|
/**
|
|
61
61
|
* Initial state for the Agent
|
|
62
62
|
* Override to provide default state values
|
|
63
63
|
*/
|
|
64
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
|
+
};
|
|
65
108
|
this.sql`
|
|
66
109
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
67
110
|
id TEXT PRIMARY KEY NOT NULL,
|
|
@@ -69,7 +112,7 @@ var Agent = class extends Server {
|
|
|
69
112
|
)
|
|
70
113
|
`;
|
|
71
114
|
void this.ctx.blockConcurrencyWhile(async () => {
|
|
72
|
-
return
|
|
115
|
+
return this._tryCatch(async () => {
|
|
73
116
|
this.sql`
|
|
74
117
|
CREATE TABLE IF NOT EXISTS cf_agents_schedules (
|
|
75
118
|
id TEXT PRIMARY KEY NOT NULL DEFAULT (randomblob(9)),
|
|
@@ -85,22 +128,55 @@ var Agent = class extends Server {
|
|
|
85
128
|
await this.alarm();
|
|
86
129
|
});
|
|
87
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
|
+
};
|
|
88
164
|
const _onMessage = this.onMessage.bind(this);
|
|
89
165
|
this.onMessage = async (connection, message) => {
|
|
90
166
|
return agentContext.run(
|
|
91
167
|
{ agent: this, connection, request: void 0 },
|
|
92
168
|
async () => {
|
|
93
169
|
if (typeof message !== "string") {
|
|
94
|
-
return
|
|
170
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
95
171
|
}
|
|
96
172
|
let parsed;
|
|
97
173
|
try {
|
|
98
174
|
parsed = JSON.parse(message);
|
|
99
175
|
} catch (e) {
|
|
100
|
-
return
|
|
176
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
101
177
|
}
|
|
102
178
|
if (isStateUpdateMessage(parsed)) {
|
|
103
|
-
|
|
179
|
+
this._setStateInternal(parsed.state, connection);
|
|
104
180
|
return;
|
|
105
181
|
}
|
|
106
182
|
if (isRPCRequest(parsed)) {
|
|
@@ -110,7 +186,7 @@ var Agent = class extends Server {
|
|
|
110
186
|
if (typeof methodFn !== "function") {
|
|
111
187
|
throw new Error(`Method ${method} does not exist`);
|
|
112
188
|
}
|
|
113
|
-
if (!
|
|
189
|
+
if (!this._isCallable(method)) {
|
|
114
190
|
throw new Error(`Method ${method} is not callable`);
|
|
115
191
|
}
|
|
116
192
|
const metadata = callableMetadata.get(methodFn);
|
|
@@ -140,7 +216,7 @@ var Agent = class extends Server {
|
|
|
140
216
|
}
|
|
141
217
|
return;
|
|
142
218
|
}
|
|
143
|
-
return
|
|
219
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
144
220
|
}
|
|
145
221
|
);
|
|
146
222
|
};
|
|
@@ -158,18 +234,56 @@ var Agent = class extends Server {
|
|
|
158
234
|
})
|
|
159
235
|
);
|
|
160
236
|
}
|
|
161
|
-
|
|
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));
|
|
162
244
|
}, 20);
|
|
163
245
|
}
|
|
164
246
|
);
|
|
165
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
|
+
};
|
|
166
280
|
}
|
|
167
281
|
/**
|
|
168
282
|
* Current state of the Agent
|
|
169
283
|
*/
|
|
170
284
|
get state() {
|
|
171
|
-
if (
|
|
172
|
-
return
|
|
285
|
+
if (this._state !== DEFAULT_STATE) {
|
|
286
|
+
return this._state;
|
|
173
287
|
}
|
|
174
288
|
const wasChanged = this.sql`
|
|
175
289
|
SELECT state FROM cf_agents_state WHERE id = ${STATE_WAS_CHANGED}
|
|
@@ -180,8 +294,8 @@ var Agent = class extends Server {
|
|
|
180
294
|
if (wasChanged[0]?.state === "true" || // we do this check for people who updated their code before we shipped wasChanged
|
|
181
295
|
result[0]?.state) {
|
|
182
296
|
const state = result[0]?.state;
|
|
183
|
-
|
|
184
|
-
return
|
|
297
|
+
this._state = JSON.parse(state);
|
|
298
|
+
return this._state;
|
|
185
299
|
}
|
|
186
300
|
if (this.initialState === DEFAULT_STATE) {
|
|
187
301
|
return void 0;
|
|
@@ -209,12 +323,39 @@ var Agent = class extends Server {
|
|
|
209
323
|
throw this.onError(e);
|
|
210
324
|
}
|
|
211
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
|
+
}
|
|
212
353
|
/**
|
|
213
354
|
* Update the Agent's state
|
|
214
355
|
* @param state New state to set
|
|
215
356
|
*/
|
|
216
357
|
setState(state) {
|
|
217
|
-
|
|
358
|
+
this._setStateInternal(state, "server");
|
|
218
359
|
}
|
|
219
360
|
/**
|
|
220
361
|
* Called when the Agent's state is updated
|
|
@@ -235,6 +376,13 @@ var Agent = class extends Server {
|
|
|
235
376
|
}
|
|
236
377
|
);
|
|
237
378
|
}
|
|
379
|
+
async _tryCatch(fn) {
|
|
380
|
+
try {
|
|
381
|
+
return await fn();
|
|
382
|
+
} catch (e) {
|
|
383
|
+
throw this.onError(e);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
238
386
|
onError(connectionOrError, error) {
|
|
239
387
|
let theError;
|
|
240
388
|
if (connectionOrError && error) {
|
|
@@ -284,7 +432,7 @@ var Agent = class extends Server {
|
|
|
284
432
|
payload
|
|
285
433
|
)}, 'scheduled', ${timestamp})
|
|
286
434
|
`;
|
|
287
|
-
await
|
|
435
|
+
await this._scheduleNextAlarm();
|
|
288
436
|
return {
|
|
289
437
|
id,
|
|
290
438
|
callback,
|
|
@@ -302,7 +450,7 @@ var Agent = class extends Server {
|
|
|
302
450
|
payload
|
|
303
451
|
)}, 'delayed', ${when}, ${timestamp})
|
|
304
452
|
`;
|
|
305
|
-
await
|
|
453
|
+
await this._scheduleNextAlarm();
|
|
306
454
|
return {
|
|
307
455
|
id,
|
|
308
456
|
callback,
|
|
@@ -321,7 +469,7 @@ var Agent = class extends Server {
|
|
|
321
469
|
payload
|
|
322
470
|
)}, 'cron', ${when}, ${timestamp})
|
|
323
471
|
`;
|
|
324
|
-
await
|
|
472
|
+
await this._scheduleNextAlarm();
|
|
325
473
|
return {
|
|
326
474
|
id,
|
|
327
475
|
callback,
|
|
@@ -388,47 +536,21 @@ var Agent = class extends Server {
|
|
|
388
536
|
*/
|
|
389
537
|
async cancelSchedule(id) {
|
|
390
538
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
391
|
-
await
|
|
539
|
+
await this._scheduleNextAlarm();
|
|
392
540
|
return true;
|
|
393
541
|
}
|
|
394
|
-
|
|
395
|
-
* Method called when an alarm fires
|
|
396
|
-
* Executes any scheduled tasks that are due
|
|
397
|
-
*/
|
|
398
|
-
async alarm() {
|
|
399
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
542
|
+
async _scheduleNextAlarm() {
|
|
400
543
|
const result = this.sql`
|
|
401
|
-
SELECT
|
|
544
|
+
SELECT time FROM cf_agents_schedules
|
|
545
|
+
WHERE time > ${Math.floor(Date.now() / 1e3)}
|
|
546
|
+
ORDER BY time ASC
|
|
547
|
+
LIMIT 1
|
|
402
548
|
`;
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
continue;
|
|
408
|
-
}
|
|
409
|
-
await agentContext.run(
|
|
410
|
-
{ agent: this, connection: void 0, request: void 0 },
|
|
411
|
-
async () => {
|
|
412
|
-
try {
|
|
413
|
-
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
414
|
-
} catch (e) {
|
|
415
|
-
console.error(`error executing callback "${row.callback}"`, e);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
);
|
|
419
|
-
if (row.type === "cron") {
|
|
420
|
-
const nextExecutionTime = getNextCronTime(row.cron);
|
|
421
|
-
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
|
|
422
|
-
this.sql`
|
|
423
|
-
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
424
|
-
`;
|
|
425
|
-
} else {
|
|
426
|
-
this.sql`
|
|
427
|
-
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
428
|
-
`;
|
|
429
|
-
}
|
|
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);
|
|
430
553
|
}
|
|
431
|
-
await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
|
|
432
554
|
}
|
|
433
555
|
/**
|
|
434
556
|
* Destroy the Agent, removing all state and scheduled tasks
|
|
@@ -436,67 +558,130 @@ var Agent = class extends Server {
|
|
|
436
558
|
async destroy() {
|
|
437
559
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
438
560
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
561
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
439
562
|
await this.ctx.storage.deleteAlarm();
|
|
440
563
|
await this.ctx.storage.deleteAll();
|
|
441
564
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
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;
|
|
469
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
|
|
630
|
+
});
|
|
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
|
+
};
|
|
647
|
+
}
|
|
648
|
+
async removeMcpServer(id) {
|
|
649
|
+
this.mcp.closeConnection(id);
|
|
650
|
+
this.sql`
|
|
651
|
+
DELETE FROM cf_agents_mcp_servers WHERE id = ${id};
|
|
652
|
+
`;
|
|
653
|
+
this.broadcast(
|
|
654
|
+
JSON.stringify({
|
|
655
|
+
type: "cf_agent_mcp_servers",
|
|
656
|
+
mcp: this.getMcpServers()
|
|
657
|
+
})
|
|
470
658
|
);
|
|
471
|
-
});
|
|
472
|
-
};
|
|
473
|
-
tryCatch_fn = async function(fn) {
|
|
474
|
-
try {
|
|
475
|
-
return await fn();
|
|
476
|
-
} catch (e) {
|
|
477
|
-
throw this.onError(e);
|
|
478
659
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
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;
|
|
486
669
|
`;
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
670
|
+
for (const server of servers) {
|
|
671
|
+
const serverConn = this.mcp.mcpConnections[server.id];
|
|
672
|
+
mcpState.servers[server.id] = {
|
|
673
|
+
name: server.name,
|
|
674
|
+
server_url: server.server_url,
|
|
675
|
+
auth_url: server.auth_url,
|
|
676
|
+
// mark as "authenticating" because the server isn't automatically connected, so it's pending authenticating
|
|
677
|
+
state: serverConn?.connectionState ?? "authenticating",
|
|
678
|
+
instructions: serverConn?.instructions ?? null,
|
|
679
|
+
capabilities: serverConn?.serverCapabilities ?? null
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
return mcpState;
|
|
491
683
|
}
|
|
492
684
|
};
|
|
493
|
-
/**
|
|
494
|
-
* Get all methods marked as callable on this Agent
|
|
495
|
-
* @returns A map of method names to their metadata
|
|
496
|
-
*/
|
|
497
|
-
isCallable_fn = function(method) {
|
|
498
|
-
return callableMetadata.has(this[method]);
|
|
499
|
-
};
|
|
500
685
|
/**
|
|
501
686
|
* Agent configuration options
|
|
502
687
|
*/
|
|
@@ -542,57 +727,51 @@ async function routeAgentRequest(request, env, options) {
|
|
|
542
727
|
}
|
|
543
728
|
async function routeAgentEmail(email, env, options) {
|
|
544
729
|
}
|
|
545
|
-
function getAgentByName(namespace, name, options) {
|
|
730
|
+
async function getAgentByName(namespace, name, options) {
|
|
546
731
|
return getServerByName(namespace, name, options);
|
|
547
732
|
}
|
|
548
|
-
var _connection, _id, _closed;
|
|
549
733
|
var StreamingResponse = class {
|
|
550
734
|
constructor(connection, id) {
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
__privateSet(this, _connection, connection);
|
|
555
|
-
__privateSet(this, _id, id);
|
|
735
|
+
this._closed = false;
|
|
736
|
+
this._connection = connection;
|
|
737
|
+
this._id = id;
|
|
556
738
|
}
|
|
557
739
|
/**
|
|
558
740
|
* Send a chunk of data to the client
|
|
559
741
|
* @param chunk The data to send
|
|
560
742
|
*/
|
|
561
743
|
send(chunk) {
|
|
562
|
-
if (
|
|
744
|
+
if (this._closed) {
|
|
563
745
|
throw new Error("StreamingResponse is already closed");
|
|
564
746
|
}
|
|
565
747
|
const response = {
|
|
566
748
|
type: "rpc",
|
|
567
|
-
id:
|
|
749
|
+
id: this._id,
|
|
568
750
|
success: true,
|
|
569
751
|
result: chunk,
|
|
570
752
|
done: false
|
|
571
753
|
};
|
|
572
|
-
|
|
754
|
+
this._connection.send(JSON.stringify(response));
|
|
573
755
|
}
|
|
574
756
|
/**
|
|
575
757
|
* End the stream and send the final chunk (if any)
|
|
576
758
|
* @param finalChunk Optional final chunk of data to send
|
|
577
759
|
*/
|
|
578
760
|
end(finalChunk) {
|
|
579
|
-
if (
|
|
761
|
+
if (this._closed) {
|
|
580
762
|
throw new Error("StreamingResponse is already closed");
|
|
581
763
|
}
|
|
582
|
-
|
|
764
|
+
this._closed = true;
|
|
583
765
|
const response = {
|
|
584
766
|
type: "rpc",
|
|
585
|
-
id:
|
|
767
|
+
id: this._id,
|
|
586
768
|
success: true,
|
|
587
769
|
result: finalChunk,
|
|
588
770
|
done: true
|
|
589
771
|
};
|
|
590
|
-
|
|
772
|
+
this._connection.send(JSON.stringify(response));
|
|
591
773
|
}
|
|
592
774
|
};
|
|
593
|
-
_connection = new WeakMap();
|
|
594
|
-
_id = new WeakMap();
|
|
595
|
-
_closed = new WeakMap();
|
|
596
775
|
|
|
597
776
|
export {
|
|
598
777
|
unstable_callable,
|
|
@@ -603,4 +782,4 @@ export {
|
|
|
603
782
|
getAgentByName,
|
|
604
783
|
StreamingResponse
|
|
605
784
|
};
|
|
606
|
-
//# sourceMappingURL=chunk-
|
|
785
|
+
//# sourceMappingURL=chunk-MXJNY43J.js.map
|