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