agents 0.0.0-b30ffda → 0.0.0-b803d5e
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 +45 -3
- package/dist/ai-chat-agent.js +117 -55
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +12 -0
- package/dist/ai-react.js +24 -13
- 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-XG52S6YY.js → chunk-J6T74FUS.js} +325 -133
- 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 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +120 -11
- package/dist/index.js +8 -6
- package/dist/mcp/client.d.ts +33 -13
- package/dist/mcp/client.js +3 -402
- package/dist/mcp/client.js.map +1 -1
- 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 +44 -5
- package/dist/mcp/index.js +588 -158
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +72 -0
- 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/package.json +21 -5
- package/src/index.ts +389 -51
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-HMLY7DHA.js.map +0 -1
- package/dist/chunk-XG52S6YY.js.map +0 -1
|
@@ -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 {
|
|
@@ -36,18 +39,72 @@ function getNextCronTime(cron) {
|
|
|
36
39
|
var STATE_ROW_ID = "cf_state_row_id";
|
|
37
40
|
var STATE_WAS_CHANGED = "cf_state_was_changed";
|
|
38
41
|
var DEFAULT_STATE = {};
|
|
39
|
-
var
|
|
40
|
-
|
|
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
|
+
}
|
|
41
54
|
var Agent = class extends Server {
|
|
42
55
|
constructor(ctx, env) {
|
|
43
56
|
super(ctx, env);
|
|
44
|
-
|
|
45
|
-
|
|
57
|
+
this._state = DEFAULT_STATE;
|
|
58
|
+
this._ParentClass = Object.getPrototypeOf(this).constructor;
|
|
59
|
+
this.mcp = new MCPClientManager(this._ParentClass.name, "0.0.1");
|
|
46
60
|
/**
|
|
47
61
|
* Initial state for the Agent
|
|
48
62
|
* Override to provide default state values
|
|
49
63
|
*/
|
|
50
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
|
+
};
|
|
51
108
|
this.sql`
|
|
52
109
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
53
110
|
id TEXT PRIMARY KEY NOT NULL,
|
|
@@ -55,7 +112,7 @@ var Agent = class extends Server {
|
|
|
55
112
|
)
|
|
56
113
|
`;
|
|
57
114
|
void this.ctx.blockConcurrencyWhile(async () => {
|
|
58
|
-
return
|
|
115
|
+
return this._tryCatch(async () => {
|
|
59
116
|
this.sql`
|
|
60
117
|
CREATE TABLE IF NOT EXISTS cf_agents_schedules (
|
|
61
118
|
id TEXT PRIMARY KEY NOT NULL DEFAULT (randomblob(9)),
|
|
@@ -71,22 +128,55 @@ var Agent = class extends Server {
|
|
|
71
128
|
await this.alarm();
|
|
72
129
|
});
|
|
73
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
|
+
};
|
|
74
164
|
const _onMessage = this.onMessage.bind(this);
|
|
75
165
|
this.onMessage = async (connection, message) => {
|
|
76
|
-
return
|
|
166
|
+
return agentContext.run(
|
|
77
167
|
{ agent: this, connection, request: void 0 },
|
|
78
168
|
async () => {
|
|
79
169
|
if (typeof message !== "string") {
|
|
80
|
-
return
|
|
170
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
81
171
|
}
|
|
82
172
|
let parsed;
|
|
83
173
|
try {
|
|
84
174
|
parsed = JSON.parse(message);
|
|
85
175
|
} catch (e) {
|
|
86
|
-
return
|
|
176
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
87
177
|
}
|
|
88
178
|
if (isStateUpdateMessage(parsed)) {
|
|
89
|
-
|
|
179
|
+
this._setStateInternal(parsed.state, connection);
|
|
90
180
|
return;
|
|
91
181
|
}
|
|
92
182
|
if (isRPCRequest(parsed)) {
|
|
@@ -96,7 +186,7 @@ var Agent = class extends Server {
|
|
|
96
186
|
if (typeof methodFn !== "function") {
|
|
97
187
|
throw new Error(`Method ${method} does not exist`);
|
|
98
188
|
}
|
|
99
|
-
if (!
|
|
189
|
+
if (!this._isCallable(method)) {
|
|
100
190
|
throw new Error(`Method ${method} is not callable`);
|
|
101
191
|
}
|
|
102
192
|
const metadata = callableMetadata.get(methodFn);
|
|
@@ -126,13 +216,13 @@ var Agent = class extends Server {
|
|
|
126
216
|
}
|
|
127
217
|
return;
|
|
128
218
|
}
|
|
129
|
-
return
|
|
219
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
130
220
|
}
|
|
131
221
|
);
|
|
132
222
|
};
|
|
133
223
|
const _onConnect = this.onConnect.bind(this);
|
|
134
224
|
this.onConnect = (connection, ctx2) => {
|
|
135
|
-
return
|
|
225
|
+
return agentContext.run(
|
|
136
226
|
{ agent: this, connection, request: ctx2.request },
|
|
137
227
|
async () => {
|
|
138
228
|
setTimeout(() => {
|
|
@@ -144,18 +234,56 @@ var Agent = class extends Server {
|
|
|
144
234
|
})
|
|
145
235
|
);
|
|
146
236
|
}
|
|
147
|
-
|
|
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));
|
|
148
244
|
}, 20);
|
|
149
245
|
}
|
|
150
246
|
);
|
|
151
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
|
+
};
|
|
152
280
|
}
|
|
153
281
|
/**
|
|
154
282
|
* Current state of the Agent
|
|
155
283
|
*/
|
|
156
284
|
get state() {
|
|
157
|
-
if (
|
|
158
|
-
return
|
|
285
|
+
if (this._state !== DEFAULT_STATE) {
|
|
286
|
+
return this._state;
|
|
159
287
|
}
|
|
160
288
|
const wasChanged = this.sql`
|
|
161
289
|
SELECT state FROM cf_agents_state WHERE id = ${STATE_WAS_CHANGED}
|
|
@@ -166,8 +294,8 @@ var Agent = class extends Server {
|
|
|
166
294
|
if (wasChanged[0]?.state === "true" || // we do this check for people who updated their code before we shipped wasChanged
|
|
167
295
|
result[0]?.state) {
|
|
168
296
|
const state = result[0]?.state;
|
|
169
|
-
|
|
170
|
-
return
|
|
297
|
+
this._state = JSON.parse(state);
|
|
298
|
+
return this._state;
|
|
171
299
|
}
|
|
172
300
|
if (this.initialState === DEFAULT_STATE) {
|
|
173
301
|
return void 0;
|
|
@@ -195,12 +323,39 @@ var Agent = class extends Server {
|
|
|
195
323
|
throw this.onError(e);
|
|
196
324
|
}
|
|
197
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
|
+
}
|
|
198
353
|
/**
|
|
199
354
|
* Update the Agent's state
|
|
200
355
|
* @param state New state to set
|
|
201
356
|
*/
|
|
202
357
|
setState(state) {
|
|
203
|
-
|
|
358
|
+
this._setStateInternal(state, "server");
|
|
204
359
|
}
|
|
205
360
|
/**
|
|
206
361
|
* Called when the Agent's state is updated
|
|
@@ -214,13 +369,20 @@ var Agent = class extends Server {
|
|
|
214
369
|
* @param email Email message to process
|
|
215
370
|
*/
|
|
216
371
|
onEmail(email) {
|
|
217
|
-
return
|
|
372
|
+
return agentContext.run(
|
|
218
373
|
{ agent: this, connection: void 0, request: void 0 },
|
|
219
374
|
async () => {
|
|
220
375
|
console.error("onEmail not implemented");
|
|
221
376
|
}
|
|
222
377
|
);
|
|
223
378
|
}
|
|
379
|
+
async _tryCatch(fn) {
|
|
380
|
+
try {
|
|
381
|
+
return await fn();
|
|
382
|
+
} catch (e) {
|
|
383
|
+
throw this.onError(e);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
224
386
|
onError(connectionOrError, error) {
|
|
225
387
|
let theError;
|
|
226
388
|
if (connectionOrError && error) {
|
|
@@ -270,7 +432,7 @@ var Agent = class extends Server {
|
|
|
270
432
|
payload
|
|
271
433
|
)}, 'scheduled', ${timestamp})
|
|
272
434
|
`;
|
|
273
|
-
await
|
|
435
|
+
await this._scheduleNextAlarm();
|
|
274
436
|
return {
|
|
275
437
|
id,
|
|
276
438
|
callback,
|
|
@@ -288,7 +450,7 @@ var Agent = class extends Server {
|
|
|
288
450
|
payload
|
|
289
451
|
)}, 'delayed', ${when}, ${timestamp})
|
|
290
452
|
`;
|
|
291
|
-
await
|
|
453
|
+
await this._scheduleNextAlarm();
|
|
292
454
|
return {
|
|
293
455
|
id,
|
|
294
456
|
callback,
|
|
@@ -307,7 +469,7 @@ var Agent = class extends Server {
|
|
|
307
469
|
payload
|
|
308
470
|
)}, 'cron', ${when}, ${timestamp})
|
|
309
471
|
`;
|
|
310
|
-
await
|
|
472
|
+
await this._scheduleNextAlarm();
|
|
311
473
|
return {
|
|
312
474
|
id,
|
|
313
475
|
callback,
|
|
@@ -374,47 +536,21 @@ var Agent = class extends Server {
|
|
|
374
536
|
*/
|
|
375
537
|
async cancelSchedule(id) {
|
|
376
538
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
377
|
-
await
|
|
539
|
+
await this._scheduleNextAlarm();
|
|
378
540
|
return true;
|
|
379
541
|
}
|
|
380
|
-
|
|
381
|
-
* Method called when an alarm fires
|
|
382
|
-
* Executes any scheduled tasks that are due
|
|
383
|
-
*/
|
|
384
|
-
async alarm() {
|
|
385
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
542
|
+
async _scheduleNextAlarm() {
|
|
386
543
|
const result = this.sql`
|
|
387
|
-
SELECT
|
|
544
|
+
SELECT time FROM cf_agents_schedules
|
|
545
|
+
WHERE time > ${Math.floor(Date.now() / 1e3)}
|
|
546
|
+
ORDER BY time ASC
|
|
547
|
+
LIMIT 1
|
|
388
548
|
`;
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
continue;
|
|
394
|
-
}
|
|
395
|
-
await unstable_context.run(
|
|
396
|
-
{ agent: this, connection: void 0, request: void 0 },
|
|
397
|
-
async () => {
|
|
398
|
-
try {
|
|
399
|
-
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
400
|
-
} catch (e) {
|
|
401
|
-
console.error(`error executing callback "${row.callback}"`, e);
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
);
|
|
405
|
-
if (row.type === "cron") {
|
|
406
|
-
const nextExecutionTime = getNextCronTime(row.cron);
|
|
407
|
-
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
|
|
408
|
-
this.sql`
|
|
409
|
-
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
410
|
-
`;
|
|
411
|
-
} else {
|
|
412
|
-
this.sql`
|
|
413
|
-
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
414
|
-
`;
|
|
415
|
-
}
|
|
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);
|
|
416
553
|
}
|
|
417
|
-
await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
|
|
418
554
|
}
|
|
419
555
|
/**
|
|
420
556
|
* Destroy the Agent, removing all state and scheduled tasks
|
|
@@ -422,66 +558,128 @@ var Agent = class extends Server {
|
|
|
422
558
|
async destroy() {
|
|
423
559
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
424
560
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
561
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
425
562
|
await this.ctx.storage.deleteAlarm();
|
|
426
563
|
await this.ctx.storage.deleteAll();
|
|
427
564
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
})
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
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;
|
|
454
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
|
+
})
|
|
455
658
|
);
|
|
456
|
-
});
|
|
457
|
-
};
|
|
458
|
-
tryCatch_fn = async function(fn) {
|
|
459
|
-
try {
|
|
460
|
-
return await fn();
|
|
461
|
-
} catch (e) {
|
|
462
|
-
throw this.onError(e);
|
|
463
659
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
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;
|
|
471
669
|
`;
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
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;
|
|
476
681
|
}
|
|
477
682
|
};
|
|
478
|
-
/**
|
|
479
|
-
* Get all methods marked as callable on this Agent
|
|
480
|
-
* @returns A map of method names to their metadata
|
|
481
|
-
*/
|
|
482
|
-
isCallable_fn = function(method) {
|
|
483
|
-
return callableMetadata.has(this[method]);
|
|
484
|
-
};
|
|
485
683
|
/**
|
|
486
684
|
* Agent configuration options
|
|
487
685
|
*/
|
|
@@ -527,65 +725,59 @@ async function routeAgentRequest(request, env, options) {
|
|
|
527
725
|
}
|
|
528
726
|
async function routeAgentEmail(email, env, options) {
|
|
529
727
|
}
|
|
530
|
-
function getAgentByName(namespace, name, options) {
|
|
728
|
+
async function getAgentByName(namespace, name, options) {
|
|
531
729
|
return getServerByName(namespace, name, options);
|
|
532
730
|
}
|
|
533
|
-
var _connection, _id, _closed;
|
|
534
731
|
var StreamingResponse = class {
|
|
535
732
|
constructor(connection, id) {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
__privateSet(this, _connection, connection);
|
|
540
|
-
__privateSet(this, _id, id);
|
|
733
|
+
this._closed = false;
|
|
734
|
+
this._connection = connection;
|
|
735
|
+
this._id = id;
|
|
541
736
|
}
|
|
542
737
|
/**
|
|
543
738
|
* Send a chunk of data to the client
|
|
544
739
|
* @param chunk The data to send
|
|
545
740
|
*/
|
|
546
741
|
send(chunk) {
|
|
547
|
-
if (
|
|
742
|
+
if (this._closed) {
|
|
548
743
|
throw new Error("StreamingResponse is already closed");
|
|
549
744
|
}
|
|
550
745
|
const response = {
|
|
551
746
|
type: "rpc",
|
|
552
|
-
id:
|
|
747
|
+
id: this._id,
|
|
553
748
|
success: true,
|
|
554
749
|
result: chunk,
|
|
555
750
|
done: false
|
|
556
751
|
};
|
|
557
|
-
|
|
752
|
+
this._connection.send(JSON.stringify(response));
|
|
558
753
|
}
|
|
559
754
|
/**
|
|
560
755
|
* End the stream and send the final chunk (if any)
|
|
561
756
|
* @param finalChunk Optional final chunk of data to send
|
|
562
757
|
*/
|
|
563
758
|
end(finalChunk) {
|
|
564
|
-
if (
|
|
759
|
+
if (this._closed) {
|
|
565
760
|
throw new Error("StreamingResponse is already closed");
|
|
566
761
|
}
|
|
567
|
-
|
|
762
|
+
this._closed = true;
|
|
568
763
|
const response = {
|
|
569
764
|
type: "rpc",
|
|
570
|
-
id:
|
|
765
|
+
id: this._id,
|
|
571
766
|
success: true,
|
|
572
767
|
result: finalChunk,
|
|
573
768
|
done: true
|
|
574
769
|
};
|
|
575
|
-
|
|
770
|
+
this._connection.send(JSON.stringify(response));
|
|
576
771
|
}
|
|
577
772
|
};
|
|
578
|
-
_connection = new WeakMap();
|
|
579
|
-
_id = new WeakMap();
|
|
580
|
-
_closed = new WeakMap();
|
|
581
773
|
|
|
582
774
|
export {
|
|
583
775
|
unstable_callable,
|
|
584
|
-
|
|
776
|
+
getCurrentAgent,
|
|
585
777
|
Agent,
|
|
586
778
|
routeAgentRequest,
|
|
587
779
|
routeAgentEmail,
|
|
588
780
|
getAgentByName,
|
|
589
781
|
StreamingResponse
|
|
590
782
|
};
|
|
591
|
-
//# sourceMappingURL=chunk-
|
|
783
|
+
//# sourceMappingURL=chunk-J6T74FUS.js.map
|