agents 0.0.0-0cd2489 → 0.0.0-1211d68
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 +99 -103
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +12 -0
- package/dist/ai-react.js +0 -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-5W7ZWKOP.js → chunk-J6T74FUS.js} +299 -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-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 +6 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +104 -7
- 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 +11 -1
- package/dist/mcp/index.js +104 -135
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +71 -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 +11 -8
- package/src/index.ts +344 -46
- package/dist/chunk-5W7ZWKOP.js.map +0 -1
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-HMLY7DHA.js.map +0 -1
- package/dist/chunk-Q5ZBHY4Z.js.map +0 -1
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-
|
|
2
|
+
DurableObjectOAuthClientProvider
|
|
3
|
+
} from "./chunk-BZXOAZUX.js";
|
|
4
|
+
import {
|
|
5
|
+
camelCaseToKebabCase
|
|
6
|
+
} from "./chunk-QSGN3REV.js";
|
|
4
7
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
__privateMethod,
|
|
8
|
-
__privateSet
|
|
9
|
-
} from "./chunk-HMLY7DHA.js";
|
|
8
|
+
MCPClientManager
|
|
9
|
+
} from "./chunk-Y67CHZBI.js";
|
|
10
10
|
|
|
11
11
|
// src/index.ts
|
|
12
12
|
import {
|
|
@@ -51,19 +51,60 @@ function getCurrentAgent() {
|
|
|
51
51
|
}
|
|
52
52
|
return store;
|
|
53
53
|
}
|
|
54
|
-
var _state, _ParentClass, _Agent_instances, setStateInternal_fn, tryCatch_fn, scheduleNextAlarm_fn, isCallable_fn;
|
|
55
54
|
var Agent = class extends Server {
|
|
56
55
|
constructor(ctx, env) {
|
|
57
56
|
super(ctx, env);
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
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");
|
|
62
60
|
/**
|
|
63
61
|
* Initial state for the Agent
|
|
64
62
|
* Override to provide default state values
|
|
65
63
|
*/
|
|
66
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
|
+
};
|
|
67
108
|
this.sql`
|
|
68
109
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
69
110
|
id TEXT PRIMARY KEY NOT NULL,
|
|
@@ -71,7 +112,7 @@ var Agent = class extends Server {
|
|
|
71
112
|
)
|
|
72
113
|
`;
|
|
73
114
|
void this.ctx.blockConcurrencyWhile(async () => {
|
|
74
|
-
return
|
|
115
|
+
return this._tryCatch(async () => {
|
|
75
116
|
this.sql`
|
|
76
117
|
CREATE TABLE IF NOT EXISTS cf_agents_schedules (
|
|
77
118
|
id TEXT PRIMARY KEY NOT NULL DEFAULT (randomblob(9)),
|
|
@@ -87,12 +128,36 @@ var Agent = class extends Server {
|
|
|
87
128
|
await this.alarm();
|
|
88
129
|
});
|
|
89
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
|
+
`;
|
|
90
142
|
const _onRequest = this.onRequest.bind(this);
|
|
91
143
|
this.onRequest = (request) => {
|
|
92
144
|
return agentContext.run(
|
|
93
145
|
{ agent: this, connection: void 0, request },
|
|
94
146
|
async () => {
|
|
95
|
-
|
|
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));
|
|
96
161
|
}
|
|
97
162
|
);
|
|
98
163
|
};
|
|
@@ -102,16 +167,16 @@ var Agent = class extends Server {
|
|
|
102
167
|
{ agent: this, connection, request: void 0 },
|
|
103
168
|
async () => {
|
|
104
169
|
if (typeof message !== "string") {
|
|
105
|
-
return
|
|
170
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
106
171
|
}
|
|
107
172
|
let parsed;
|
|
108
173
|
try {
|
|
109
174
|
parsed = JSON.parse(message);
|
|
110
175
|
} catch (e) {
|
|
111
|
-
return
|
|
176
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
112
177
|
}
|
|
113
178
|
if (isStateUpdateMessage(parsed)) {
|
|
114
|
-
|
|
179
|
+
this._setStateInternal(parsed.state, connection);
|
|
115
180
|
return;
|
|
116
181
|
}
|
|
117
182
|
if (isRPCRequest(parsed)) {
|
|
@@ -121,7 +186,7 @@ var Agent = class extends Server {
|
|
|
121
186
|
if (typeof methodFn !== "function") {
|
|
122
187
|
throw new Error(`Method ${method} does not exist`);
|
|
123
188
|
}
|
|
124
|
-
if (!
|
|
189
|
+
if (!this._isCallable(method)) {
|
|
125
190
|
throw new Error(`Method ${method} is not callable`);
|
|
126
191
|
}
|
|
127
192
|
const metadata = callableMetadata.get(methodFn);
|
|
@@ -151,7 +216,7 @@ var Agent = class extends Server {
|
|
|
151
216
|
}
|
|
152
217
|
return;
|
|
153
218
|
}
|
|
154
|
-
return
|
|
219
|
+
return this._tryCatch(() => _onMessage(connection, message));
|
|
155
220
|
}
|
|
156
221
|
);
|
|
157
222
|
};
|
|
@@ -169,18 +234,56 @@ var Agent = class extends Server {
|
|
|
169
234
|
})
|
|
170
235
|
);
|
|
171
236
|
}
|
|
172
|
-
|
|
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));
|
|
173
244
|
}, 20);
|
|
174
245
|
}
|
|
175
246
|
);
|
|
176
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
|
+
};
|
|
177
280
|
}
|
|
178
281
|
/**
|
|
179
282
|
* Current state of the Agent
|
|
180
283
|
*/
|
|
181
284
|
get state() {
|
|
182
|
-
if (
|
|
183
|
-
return
|
|
285
|
+
if (this._state !== DEFAULT_STATE) {
|
|
286
|
+
return this._state;
|
|
184
287
|
}
|
|
185
288
|
const wasChanged = this.sql`
|
|
186
289
|
SELECT state FROM cf_agents_state WHERE id = ${STATE_WAS_CHANGED}
|
|
@@ -191,8 +294,8 @@ var Agent = class extends Server {
|
|
|
191
294
|
if (wasChanged[0]?.state === "true" || // we do this check for people who updated their code before we shipped wasChanged
|
|
192
295
|
result[0]?.state) {
|
|
193
296
|
const state = result[0]?.state;
|
|
194
|
-
|
|
195
|
-
return
|
|
297
|
+
this._state = JSON.parse(state);
|
|
298
|
+
return this._state;
|
|
196
299
|
}
|
|
197
300
|
if (this.initialState === DEFAULT_STATE) {
|
|
198
301
|
return void 0;
|
|
@@ -220,12 +323,39 @@ var Agent = class extends Server {
|
|
|
220
323
|
throw this.onError(e);
|
|
221
324
|
}
|
|
222
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
|
+
}
|
|
223
353
|
/**
|
|
224
354
|
* Update the Agent's state
|
|
225
355
|
* @param state New state to set
|
|
226
356
|
*/
|
|
227
357
|
setState(state) {
|
|
228
|
-
|
|
358
|
+
this._setStateInternal(state, "server");
|
|
229
359
|
}
|
|
230
360
|
/**
|
|
231
361
|
* Called when the Agent's state is updated
|
|
@@ -246,6 +376,13 @@ var Agent = class extends Server {
|
|
|
246
376
|
}
|
|
247
377
|
);
|
|
248
378
|
}
|
|
379
|
+
async _tryCatch(fn) {
|
|
380
|
+
try {
|
|
381
|
+
return await fn();
|
|
382
|
+
} catch (e) {
|
|
383
|
+
throw this.onError(e);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
249
386
|
onError(connectionOrError, error) {
|
|
250
387
|
let theError;
|
|
251
388
|
if (connectionOrError && error) {
|
|
@@ -295,7 +432,7 @@ var Agent = class extends Server {
|
|
|
295
432
|
payload
|
|
296
433
|
)}, 'scheduled', ${timestamp})
|
|
297
434
|
`;
|
|
298
|
-
await
|
|
435
|
+
await this._scheduleNextAlarm();
|
|
299
436
|
return {
|
|
300
437
|
id,
|
|
301
438
|
callback,
|
|
@@ -313,7 +450,7 @@ var Agent = class extends Server {
|
|
|
313
450
|
payload
|
|
314
451
|
)}, 'delayed', ${when}, ${timestamp})
|
|
315
452
|
`;
|
|
316
|
-
await
|
|
453
|
+
await this._scheduleNextAlarm();
|
|
317
454
|
return {
|
|
318
455
|
id,
|
|
319
456
|
callback,
|
|
@@ -332,7 +469,7 @@ var Agent = class extends Server {
|
|
|
332
469
|
payload
|
|
333
470
|
)}, 'cron', ${when}, ${timestamp})
|
|
334
471
|
`;
|
|
335
|
-
await
|
|
472
|
+
await this._scheduleNextAlarm();
|
|
336
473
|
return {
|
|
337
474
|
id,
|
|
338
475
|
callback,
|
|
@@ -399,47 +536,21 @@ var Agent = class extends Server {
|
|
|
399
536
|
*/
|
|
400
537
|
async cancelSchedule(id) {
|
|
401
538
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
402
|
-
await
|
|
539
|
+
await this._scheduleNextAlarm();
|
|
403
540
|
return true;
|
|
404
541
|
}
|
|
405
|
-
|
|
406
|
-
* Method called when an alarm fires
|
|
407
|
-
* Executes any scheduled tasks that are due
|
|
408
|
-
*/
|
|
409
|
-
async alarm() {
|
|
410
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
542
|
+
async _scheduleNextAlarm() {
|
|
411
543
|
const result = this.sql`
|
|
412
|
-
SELECT
|
|
544
|
+
SELECT time FROM cf_agents_schedules
|
|
545
|
+
WHERE time > ${Math.floor(Date.now() / 1e3)}
|
|
546
|
+
ORDER BY time ASC
|
|
547
|
+
LIMIT 1
|
|
413
548
|
`;
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
continue;
|
|
419
|
-
}
|
|
420
|
-
await agentContext.run(
|
|
421
|
-
{ agent: this, connection: void 0, request: void 0 },
|
|
422
|
-
async () => {
|
|
423
|
-
try {
|
|
424
|
-
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
425
|
-
} catch (e) {
|
|
426
|
-
console.error(`error executing callback "${row.callback}"`, e);
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
);
|
|
430
|
-
if (row.type === "cron") {
|
|
431
|
-
const nextExecutionTime = getNextCronTime(row.cron);
|
|
432
|
-
const nextTimestamp = Math.floor(nextExecutionTime.getTime() / 1e3);
|
|
433
|
-
this.sql`
|
|
434
|
-
UPDATE cf_agents_schedules SET time = ${nextTimestamp} WHERE id = ${row.id}
|
|
435
|
-
`;
|
|
436
|
-
} else {
|
|
437
|
-
this.sql`
|
|
438
|
-
DELETE FROM cf_agents_schedules WHERE id = ${row.id}
|
|
439
|
-
`;
|
|
440
|
-
}
|
|
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);
|
|
441
553
|
}
|
|
442
|
-
await __privateMethod(this, _Agent_instances, scheduleNextAlarm_fn).call(this);
|
|
443
554
|
}
|
|
444
555
|
/**
|
|
445
556
|
* Destroy the Agent, removing all state and scheduled tasks
|
|
@@ -447,67 +558,128 @@ var Agent = class extends Server {
|
|
|
447
558
|
async destroy() {
|
|
448
559
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
449
560
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
561
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
450
562
|
await this.ctx.storage.deleteAlarm();
|
|
451
563
|
await this.ctx.storage.deleteAll();
|
|
452
564
|
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
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;
|
|
480
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
|
+
})
|
|
481
658
|
);
|
|
482
|
-
});
|
|
483
|
-
};
|
|
484
|
-
tryCatch_fn = async function(fn) {
|
|
485
|
-
try {
|
|
486
|
-
return await fn();
|
|
487
|
-
} catch (e) {
|
|
488
|
-
throw this.onError(e);
|
|
489
659
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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;
|
|
497
669
|
`;
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
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;
|
|
502
681
|
}
|
|
503
682
|
};
|
|
504
|
-
/**
|
|
505
|
-
* Get all methods marked as callable on this Agent
|
|
506
|
-
* @returns A map of method names to their metadata
|
|
507
|
-
*/
|
|
508
|
-
isCallable_fn = function(method) {
|
|
509
|
-
return callableMetadata.has(this[method]);
|
|
510
|
-
};
|
|
511
683
|
/**
|
|
512
684
|
* Agent configuration options
|
|
513
685
|
*/
|
|
@@ -556,54 +728,48 @@ async function routeAgentEmail(email, env, options) {
|
|
|
556
728
|
async function getAgentByName(namespace, name, options) {
|
|
557
729
|
return getServerByName(namespace, name, options);
|
|
558
730
|
}
|
|
559
|
-
var _connection, _id, _closed;
|
|
560
731
|
var StreamingResponse = class {
|
|
561
732
|
constructor(connection, id) {
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
__privateSet(this, _connection, connection);
|
|
566
|
-
__privateSet(this, _id, id);
|
|
733
|
+
this._closed = false;
|
|
734
|
+
this._connection = connection;
|
|
735
|
+
this._id = id;
|
|
567
736
|
}
|
|
568
737
|
/**
|
|
569
738
|
* Send a chunk of data to the client
|
|
570
739
|
* @param chunk The data to send
|
|
571
740
|
*/
|
|
572
741
|
send(chunk) {
|
|
573
|
-
if (
|
|
742
|
+
if (this._closed) {
|
|
574
743
|
throw new Error("StreamingResponse is already closed");
|
|
575
744
|
}
|
|
576
745
|
const response = {
|
|
577
746
|
type: "rpc",
|
|
578
|
-
id:
|
|
747
|
+
id: this._id,
|
|
579
748
|
success: true,
|
|
580
749
|
result: chunk,
|
|
581
750
|
done: false
|
|
582
751
|
};
|
|
583
|
-
|
|
752
|
+
this._connection.send(JSON.stringify(response));
|
|
584
753
|
}
|
|
585
754
|
/**
|
|
586
755
|
* End the stream and send the final chunk (if any)
|
|
587
756
|
* @param finalChunk Optional final chunk of data to send
|
|
588
757
|
*/
|
|
589
758
|
end(finalChunk) {
|
|
590
|
-
if (
|
|
759
|
+
if (this._closed) {
|
|
591
760
|
throw new Error("StreamingResponse is already closed");
|
|
592
761
|
}
|
|
593
|
-
|
|
762
|
+
this._closed = true;
|
|
594
763
|
const response = {
|
|
595
764
|
type: "rpc",
|
|
596
|
-
id:
|
|
765
|
+
id: this._id,
|
|
597
766
|
success: true,
|
|
598
767
|
result: finalChunk,
|
|
599
768
|
done: true
|
|
600
769
|
};
|
|
601
|
-
|
|
770
|
+
this._connection.send(JSON.stringify(response));
|
|
602
771
|
}
|
|
603
772
|
};
|
|
604
|
-
_connection = new WeakMap();
|
|
605
|
-
_id = new WeakMap();
|
|
606
|
-
_closed = new WeakMap();
|
|
607
773
|
|
|
608
774
|
export {
|
|
609
775
|
unstable_callable,
|
|
@@ -614,4 +780,4 @@ export {
|
|
|
614
780
|
getAgentByName,
|
|
615
781
|
StreamingResponse
|
|
616
782
|
};
|
|
617
|
-
//# sourceMappingURL=chunk-
|
|
783
|
+
//# sourceMappingURL=chunk-J6T74FUS.js.map
|