agents 0.0.0-54e1986 → 0.0.0-569680f
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 +5 -4
- package/dist/ai-chat-agent.js +64 -26
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +9 -8
- package/dist/ai-react.js +27 -27
- package/dist/ai-react.js.map +1 -1
- package/dist/{chunk-BZXOAZUX.js → chunk-767EASBA.js} +5 -5
- package/dist/{chunk-BZXOAZUX.js.map → chunk-767EASBA.js.map} +1 -1
- package/dist/{chunk-QSGN3REV.js → chunk-NKZZ66QY.js} +8 -15
- package/dist/chunk-NKZZ66QY.js.map +1 -0
- package/dist/{chunk-RIYR6FR6.js → chunk-RX76B6DL.js} +328 -80
- package/dist/chunk-RX76B6DL.js.map +1 -0
- package/dist/{chunk-Y67CHZBI.js → chunk-TN7QOY4S.js} +23 -18
- package/dist/chunk-TN7QOY4S.js.map +1 -0
- package/dist/client.d.ts +6 -0
- package/dist/client.js +1 -1
- package/dist/index-aBwVVXj7.d.ts +529 -0
- package/dist/index.d.ts +32 -394
- package/dist/index.js +12 -4
- package/dist/mcp/client.d.ts +281 -9
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/do-oauth-client-provider.js +1 -1
- package/dist/mcp/index.d.ts +6 -6
- package/dist/mcp/index.js +55 -51
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +12 -0
- package/dist/observability/index.js +10 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/react.d.ts +76 -10
- package/dist/react.js +16 -6
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +6 -6
- package/dist/schedule.js +4 -4
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/dist/serializable.js.map +1 -0
- package/package.json +76 -71
- package/src/index.ts +395 -100
- package/dist/chunk-QSGN3REV.js.map +0 -1
- package/dist/chunk-RIYR6FR6.js.map +0 -1
- package/dist/chunk-Y67CHZBI.js.map +0 -1
|
@@ -1,22 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MCPClientManager
|
|
3
|
+
} from "./chunk-TN7QOY4S.js";
|
|
1
4
|
import {
|
|
2
5
|
DurableObjectOAuthClientProvider
|
|
3
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-767EASBA.js";
|
|
4
7
|
import {
|
|
5
8
|
camelCaseToKebabCase
|
|
6
|
-
} from "./chunk-
|
|
7
|
-
import {
|
|
8
|
-
MCPClientManager
|
|
9
|
-
} from "./chunk-Y67CHZBI.js";
|
|
9
|
+
} from "./chunk-NKZZ66QY.js";
|
|
10
10
|
|
|
11
11
|
// src/index.ts
|
|
12
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
13
|
+
import { parseCronExpression } from "cron-schedule";
|
|
14
|
+
import { nanoid } from "nanoid";
|
|
15
|
+
import { EmailMessage } from "cloudflare:email";
|
|
12
16
|
import {
|
|
13
17
|
Server,
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
getServerByName,
|
|
19
|
+
routePartykitRequest
|
|
16
20
|
} from "partyserver";
|
|
17
|
-
import { parseCronExpression } from "cron-schedule";
|
|
18
|
-
import { nanoid } from "nanoid";
|
|
19
|
-
import { AsyncLocalStorage } from "node:async_hooks";
|
|
20
21
|
function isRPCRequest(msg) {
|
|
21
22
|
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
23
|
}
|
|
@@ -62,6 +63,10 @@ var Agent = class extends Server {
|
|
|
62
63
|
* Override to provide default state values
|
|
63
64
|
*/
|
|
64
65
|
this.initialState = DEFAULT_STATE;
|
|
66
|
+
/**
|
|
67
|
+
* The observability implementation to use for the Agent
|
|
68
|
+
*/
|
|
69
|
+
this.observability = genericObservability;
|
|
65
70
|
/**
|
|
66
71
|
* Method called when an alarm fires.
|
|
67
72
|
* Executes any scheduled tasks that are due.
|
|
@@ -85,6 +90,16 @@ var Agent = class extends Server {
|
|
|
85
90
|
{ agent: this, connection: void 0, request: void 0 },
|
|
86
91
|
async () => {
|
|
87
92
|
try {
|
|
93
|
+
this.observability?.emit(
|
|
94
|
+
{
|
|
95
|
+
displayMessage: `Schedule ${row.id} executed`,
|
|
96
|
+
id: nanoid(),
|
|
97
|
+
payload: row,
|
|
98
|
+
timestamp: Date.now(),
|
|
99
|
+
type: "schedule:execute"
|
|
100
|
+
},
|
|
101
|
+
this.ctx
|
|
102
|
+
);
|
|
88
103
|
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
89
104
|
} catch (e) {
|
|
90
105
|
console.error(`error executing callback "${row.callback}"`, e);
|
|
@@ -148,13 +163,13 @@ var Agent = class extends Server {
|
|
|
148
163
|
await this.mcp.handleCallbackRequest(request);
|
|
149
164
|
this.broadcast(
|
|
150
165
|
JSON.stringify({
|
|
151
|
-
|
|
152
|
-
|
|
166
|
+
mcp: this.getMcpServers(),
|
|
167
|
+
type: "cf_agent_mcp_servers"
|
|
153
168
|
})
|
|
154
169
|
);
|
|
155
170
|
return new Response("<script>window.close();</script>", {
|
|
156
|
-
|
|
157
|
-
|
|
171
|
+
headers: { "content-type": "text/html" },
|
|
172
|
+
status: 200
|
|
158
173
|
});
|
|
159
174
|
}
|
|
160
175
|
return this._tryCatch(() => _onRequest(request));
|
|
@@ -172,7 +187,7 @@ var Agent = class extends Server {
|
|
|
172
187
|
let parsed;
|
|
173
188
|
try {
|
|
174
189
|
parsed = JSON.parse(message);
|
|
175
|
-
} catch (
|
|
190
|
+
} catch (_e) {
|
|
176
191
|
return this._tryCatch(() => _onMessage(connection, message));
|
|
177
192
|
}
|
|
178
193
|
if (isStateUpdateMessage(parsed)) {
|
|
@@ -196,20 +211,35 @@ var Agent = class extends Server {
|
|
|
196
211
|
return;
|
|
197
212
|
}
|
|
198
213
|
const result = await methodFn.apply(this, args);
|
|
214
|
+
this.observability?.emit(
|
|
215
|
+
{
|
|
216
|
+
displayMessage: `RPC call to ${method}`,
|
|
217
|
+
id: nanoid(),
|
|
218
|
+
payload: {
|
|
219
|
+
args,
|
|
220
|
+
method,
|
|
221
|
+
streaming: metadata?.streaming,
|
|
222
|
+
success: true
|
|
223
|
+
},
|
|
224
|
+
timestamp: Date.now(),
|
|
225
|
+
type: "rpc"
|
|
226
|
+
},
|
|
227
|
+
this.ctx
|
|
228
|
+
);
|
|
199
229
|
const response = {
|
|
200
|
-
|
|
230
|
+
done: true,
|
|
201
231
|
id,
|
|
202
|
-
success: true,
|
|
203
232
|
result,
|
|
204
|
-
|
|
233
|
+
success: true,
|
|
234
|
+
type: "rpc"
|
|
205
235
|
};
|
|
206
236
|
connection.send(JSON.stringify(response));
|
|
207
237
|
} catch (e) {
|
|
208
238
|
const response = {
|
|
209
|
-
|
|
239
|
+
error: e instanceof Error ? e.message : "Unknown error occurred",
|
|
210
240
|
id: parsed.id,
|
|
211
241
|
success: false,
|
|
212
|
-
|
|
242
|
+
type: "rpc"
|
|
213
243
|
};
|
|
214
244
|
connection.send(JSON.stringify(response));
|
|
215
245
|
console.error("RPC error:", e);
|
|
@@ -229,17 +259,29 @@ var Agent = class extends Server {
|
|
|
229
259
|
if (this.state) {
|
|
230
260
|
connection.send(
|
|
231
261
|
JSON.stringify({
|
|
232
|
-
|
|
233
|
-
|
|
262
|
+
state: this.state,
|
|
263
|
+
type: "cf_agent_state"
|
|
234
264
|
})
|
|
235
265
|
);
|
|
236
266
|
}
|
|
237
267
|
connection.send(
|
|
238
268
|
JSON.stringify({
|
|
239
|
-
|
|
240
|
-
|
|
269
|
+
mcp: this.getMcpServers(),
|
|
270
|
+
type: "cf_agent_mcp_servers"
|
|
241
271
|
})
|
|
242
272
|
);
|
|
273
|
+
this.observability?.emit(
|
|
274
|
+
{
|
|
275
|
+
displayMessage: "Connection established",
|
|
276
|
+
id: nanoid(),
|
|
277
|
+
payload: {
|
|
278
|
+
connectionId: connection.id
|
|
279
|
+
},
|
|
280
|
+
timestamp: Date.now(),
|
|
281
|
+
type: "connect"
|
|
282
|
+
},
|
|
283
|
+
this.ctx
|
|
284
|
+
);
|
|
243
285
|
return this._tryCatch(() => _onConnect(connection, ctx2));
|
|
244
286
|
}, 20);
|
|
245
287
|
}
|
|
@@ -253,7 +295,7 @@ var Agent = class extends Server {
|
|
|
253
295
|
const servers = this.sql`
|
|
254
296
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
255
297
|
`;
|
|
256
|
-
|
|
298
|
+
Promise.allSettled(
|
|
257
299
|
servers.map((server) => {
|
|
258
300
|
return this._connectToMcpServerInternal(
|
|
259
301
|
server.name,
|
|
@@ -266,13 +308,14 @@ var Agent = class extends Server {
|
|
|
266
308
|
}
|
|
267
309
|
);
|
|
268
310
|
})
|
|
269
|
-
)
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
311
|
+
).then((_results) => {
|
|
312
|
+
this.broadcast(
|
|
313
|
+
JSON.stringify({
|
|
314
|
+
mcp: this.getMcpServers(),
|
|
315
|
+
type: "cf_agent_mcp_servers"
|
|
316
|
+
})
|
|
317
|
+
);
|
|
318
|
+
});
|
|
276
319
|
await this._tryCatch(() => _onStart());
|
|
277
320
|
}
|
|
278
321
|
);
|
|
@@ -324,6 +367,7 @@ var Agent = class extends Server {
|
|
|
324
367
|
}
|
|
325
368
|
}
|
|
326
369
|
_setStateInternal(state, source = "server") {
|
|
370
|
+
const previousState = this._state;
|
|
327
371
|
this._state = state;
|
|
328
372
|
this.sql`
|
|
329
373
|
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
@@ -335,8 +379,8 @@ var Agent = class extends Server {
|
|
|
335
379
|
`;
|
|
336
380
|
this.broadcast(
|
|
337
381
|
JSON.stringify({
|
|
338
|
-
|
|
339
|
-
|
|
382
|
+
state,
|
|
383
|
+
type: "cf_agent_state"
|
|
340
384
|
}),
|
|
341
385
|
source !== "server" ? [source.id] : []
|
|
342
386
|
);
|
|
@@ -345,6 +389,19 @@ var Agent = class extends Server {
|
|
|
345
389
|
return agentContext.run(
|
|
346
390
|
{ agent: this, connection, request },
|
|
347
391
|
async () => {
|
|
392
|
+
this.observability?.emit(
|
|
393
|
+
{
|
|
394
|
+
displayMessage: "State updated",
|
|
395
|
+
id: nanoid(),
|
|
396
|
+
payload: {
|
|
397
|
+
previousState,
|
|
398
|
+
state
|
|
399
|
+
},
|
|
400
|
+
timestamp: Date.now(),
|
|
401
|
+
type: "state:update"
|
|
402
|
+
},
|
|
403
|
+
this.ctx
|
|
404
|
+
);
|
|
348
405
|
return this.onStateUpdate(state, source);
|
|
349
406
|
}
|
|
350
407
|
);
|
|
@@ -362,20 +419,34 @@ var Agent = class extends Server {
|
|
|
362
419
|
* @param state Updated state
|
|
363
420
|
* @param source Source of the state update ("server" or a client connection)
|
|
364
421
|
*/
|
|
422
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: overridden later
|
|
365
423
|
onStateUpdate(state, source) {
|
|
366
424
|
}
|
|
367
425
|
/**
|
|
368
426
|
* Called when the Agent receives an email
|
|
427
|
+
* Override this method to handle incoming emails
|
|
369
428
|
* @param email Email message to process
|
|
370
429
|
*/
|
|
371
|
-
onEmail(email) {
|
|
430
|
+
async onEmail(email) {
|
|
372
431
|
return agentContext.run(
|
|
373
432
|
{ agent: this, connection: void 0, request: void 0 },
|
|
374
433
|
async () => {
|
|
375
|
-
console.
|
|
434
|
+
console.log("Received email from:", email.from, "to:", email.to);
|
|
435
|
+
console.log("Subject:", email.headers.get("subject"));
|
|
436
|
+
console.log("Override onEmail() in your agent to process emails");
|
|
376
437
|
}
|
|
377
438
|
);
|
|
378
439
|
}
|
|
440
|
+
async sendEmail(emailBinding, from, fromName, options) {
|
|
441
|
+
const agentName = camelCaseToKebabCase(this._ParentClass.name);
|
|
442
|
+
const agentId = this.name;
|
|
443
|
+
return sendEmailWithRouting(emailBinding, from, fromName, {
|
|
444
|
+
...options,
|
|
445
|
+
agentName,
|
|
446
|
+
agentId,
|
|
447
|
+
includeRoutingHeaders: true
|
|
448
|
+
});
|
|
449
|
+
}
|
|
379
450
|
async _tryCatch(fn) {
|
|
380
451
|
try {
|
|
381
452
|
return await fn();
|
|
@@ -418,6 +489,16 @@ var Agent = class extends Server {
|
|
|
418
489
|
*/
|
|
419
490
|
async schedule(when, callback, payload) {
|
|
420
491
|
const id = nanoid(9);
|
|
492
|
+
const emitScheduleCreate = (schedule) => this.observability?.emit(
|
|
493
|
+
{
|
|
494
|
+
displayMessage: `Schedule ${schedule.id} created`,
|
|
495
|
+
id: nanoid(),
|
|
496
|
+
payload: schedule,
|
|
497
|
+
timestamp: Date.now(),
|
|
498
|
+
type: "schedule:create"
|
|
499
|
+
},
|
|
500
|
+
this.ctx
|
|
501
|
+
);
|
|
421
502
|
if (typeof callback !== "string") {
|
|
422
503
|
throw new Error("Callback must be a string");
|
|
423
504
|
}
|
|
@@ -433,13 +514,15 @@ var Agent = class extends Server {
|
|
|
433
514
|
)}, 'scheduled', ${timestamp})
|
|
434
515
|
`;
|
|
435
516
|
await this._scheduleNextAlarm();
|
|
436
|
-
|
|
437
|
-
id,
|
|
517
|
+
const schedule = {
|
|
438
518
|
callback,
|
|
519
|
+
id,
|
|
439
520
|
payload,
|
|
440
521
|
time: timestamp,
|
|
441
522
|
type: "scheduled"
|
|
442
523
|
};
|
|
524
|
+
emitScheduleCreate(schedule);
|
|
525
|
+
return schedule;
|
|
443
526
|
}
|
|
444
527
|
if (typeof when === "number") {
|
|
445
528
|
const time = new Date(Date.now() + when * 1e3);
|
|
@@ -451,14 +534,16 @@ var Agent = class extends Server {
|
|
|
451
534
|
)}, 'delayed', ${when}, ${timestamp})
|
|
452
535
|
`;
|
|
453
536
|
await this._scheduleNextAlarm();
|
|
454
|
-
|
|
455
|
-
id,
|
|
537
|
+
const schedule = {
|
|
456
538
|
callback,
|
|
457
|
-
payload,
|
|
458
539
|
delayInSeconds: when,
|
|
540
|
+
id,
|
|
541
|
+
payload,
|
|
459
542
|
time: timestamp,
|
|
460
543
|
type: "delayed"
|
|
461
544
|
};
|
|
545
|
+
emitScheduleCreate(schedule);
|
|
546
|
+
return schedule;
|
|
462
547
|
}
|
|
463
548
|
if (typeof when === "string") {
|
|
464
549
|
const nextExecutionTime = getNextCronTime(when);
|
|
@@ -470,14 +555,16 @@ var Agent = class extends Server {
|
|
|
470
555
|
)}, 'cron', ${when}, ${timestamp})
|
|
471
556
|
`;
|
|
472
557
|
await this._scheduleNextAlarm();
|
|
473
|
-
|
|
474
|
-
id,
|
|
558
|
+
const schedule = {
|
|
475
559
|
callback,
|
|
476
|
-
payload,
|
|
477
560
|
cron: when,
|
|
561
|
+
id,
|
|
562
|
+
payload,
|
|
478
563
|
time: timestamp,
|
|
479
564
|
type: "cron"
|
|
480
565
|
};
|
|
566
|
+
emitScheduleCreate(schedule);
|
|
567
|
+
return schedule;
|
|
481
568
|
}
|
|
482
569
|
throw new Error("Invalid schedule type");
|
|
483
570
|
}
|
|
@@ -535,6 +622,19 @@ var Agent = class extends Server {
|
|
|
535
622
|
* @returns true if the task was cancelled, false otherwise
|
|
536
623
|
*/
|
|
537
624
|
async cancelSchedule(id) {
|
|
625
|
+
const schedule = await this.getSchedule(id);
|
|
626
|
+
if (schedule) {
|
|
627
|
+
this.observability?.emit(
|
|
628
|
+
{
|
|
629
|
+
displayMessage: `Schedule ${id} cancelled`,
|
|
630
|
+
id: nanoid(),
|
|
631
|
+
payload: schedule,
|
|
632
|
+
timestamp: Date.now(),
|
|
633
|
+
type: "schedule:cancel"
|
|
634
|
+
},
|
|
635
|
+
this.ctx
|
|
636
|
+
);
|
|
637
|
+
}
|
|
538
638
|
this.sql`DELETE FROM cf_agents_schedules WHERE id = ${id}`;
|
|
539
639
|
await this._scheduleNextAlarm();
|
|
540
640
|
return true;
|
|
@@ -561,7 +661,22 @@ var Agent = class extends Server {
|
|
|
561
661
|
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
562
662
|
await this.ctx.storage.deleteAlarm();
|
|
563
663
|
await this.ctx.storage.deleteAll();
|
|
664
|
+
this.ctx.abort("destroyed");
|
|
665
|
+
this.observability?.emit(
|
|
666
|
+
{
|
|
667
|
+
displayMessage: "Agent destroyed",
|
|
668
|
+
id: nanoid(),
|
|
669
|
+
payload: {},
|
|
670
|
+
timestamp: Date.now(),
|
|
671
|
+
type: "destroy"
|
|
672
|
+
},
|
|
673
|
+
this.ctx
|
|
674
|
+
);
|
|
564
675
|
}
|
|
676
|
+
/**
|
|
677
|
+
* Get all methods marked as callable on this Agent
|
|
678
|
+
* @returns A map of method names to their metadata
|
|
679
|
+
*/
|
|
565
680
|
_isCallable(method) {
|
|
566
681
|
return callableMetadata.has(this[method]);
|
|
567
682
|
}
|
|
@@ -582,15 +697,28 @@ var Agent = class extends Server {
|
|
|
582
697
|
callbackUrl,
|
|
583
698
|
options
|
|
584
699
|
);
|
|
700
|
+
this.sql`
|
|
701
|
+
INSERT
|
|
702
|
+
OR REPLACE INTO cf_agents_mcp_servers (id, name, server_url, client_id, auth_url, callback_url, server_options)
|
|
703
|
+
VALUES (
|
|
704
|
+
${result.id},
|
|
705
|
+
${serverName},
|
|
706
|
+
${url},
|
|
707
|
+
${result.clientId ?? null},
|
|
708
|
+
${result.authUrl ?? null},
|
|
709
|
+
${callbackUrl},
|
|
710
|
+
${options ? JSON.stringify(options) : null}
|
|
711
|
+
);
|
|
712
|
+
`;
|
|
585
713
|
this.broadcast(
|
|
586
714
|
JSON.stringify({
|
|
587
|
-
|
|
588
|
-
|
|
715
|
+
mcp: this.getMcpServers(),
|
|
716
|
+
type: "cf_agent_mcp_servers"
|
|
589
717
|
})
|
|
590
718
|
);
|
|
591
719
|
return result;
|
|
592
720
|
}
|
|
593
|
-
async _connectToMcpServerInternal(
|
|
721
|
+
async _connectToMcpServerInternal(_serverName, url, callbackUrl, options, reconnect) {
|
|
594
722
|
const authProvider = new DurableObjectOAuthClientProvider(
|
|
595
723
|
this.ctx.storage,
|
|
596
724
|
this.name,
|
|
@@ -617,28 +745,17 @@ var Agent = class extends Server {
|
|
|
617
745
|
};
|
|
618
746
|
}
|
|
619
747
|
const { id, authUrl, clientId } = await this.mcp.connect(url, {
|
|
748
|
+
client: options?.client,
|
|
620
749
|
reconnect,
|
|
621
750
|
transport: {
|
|
622
751
|
...headerTransportOpts,
|
|
623
752
|
authProvider
|
|
624
|
-
}
|
|
625
|
-
client: options?.client
|
|
753
|
+
}
|
|
626
754
|
});
|
|
627
|
-
this.sql`
|
|
628
|
-
INSERT OR REPLACE INTO cf_agents_mcp_servers (id, name, server_url, client_id, auth_url, callback_url, server_options)
|
|
629
|
-
VALUES (
|
|
630
|
-
${id},
|
|
631
|
-
${serverName},
|
|
632
|
-
${url},
|
|
633
|
-
${clientId ?? null},
|
|
634
|
-
${authUrl ?? null},
|
|
635
|
-
${callbackUrl},
|
|
636
|
-
${options ? JSON.stringify(options) : null}
|
|
637
|
-
);
|
|
638
|
-
`;
|
|
639
755
|
return {
|
|
640
|
-
|
|
641
|
-
|
|
756
|
+
authUrl,
|
|
757
|
+
clientId,
|
|
758
|
+
id
|
|
642
759
|
};
|
|
643
760
|
}
|
|
644
761
|
async removeMcpServer(id) {
|
|
@@ -648,27 +765,31 @@ var Agent = class extends Server {
|
|
|
648
765
|
`;
|
|
649
766
|
this.broadcast(
|
|
650
767
|
JSON.stringify({
|
|
651
|
-
|
|
652
|
-
|
|
768
|
+
mcp: this.getMcpServers(),
|
|
769
|
+
type: "cf_agent_mcp_servers"
|
|
653
770
|
})
|
|
654
771
|
);
|
|
655
772
|
}
|
|
656
|
-
|
|
773
|
+
getMcpServers() {
|
|
657
774
|
const mcpState = {
|
|
658
|
-
servers: {},
|
|
659
|
-
tools: this.mcp.listTools(),
|
|
660
775
|
prompts: this.mcp.listPrompts(),
|
|
661
|
-
resources: this.mcp.listResources()
|
|
776
|
+
resources: this.mcp.listResources(),
|
|
777
|
+
servers: {},
|
|
778
|
+
tools: this.mcp.listTools()
|
|
662
779
|
};
|
|
663
780
|
const servers = this.sql`
|
|
664
781
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
665
782
|
`;
|
|
666
783
|
for (const server of servers) {
|
|
784
|
+
const serverConn = this.mcp.mcpConnections[server.id];
|
|
667
785
|
mcpState.servers[server.id] = {
|
|
786
|
+
auth_url: server.auth_url,
|
|
787
|
+
capabilities: serverConn?.serverCapabilities ?? null,
|
|
788
|
+
instructions: serverConn?.instructions ?? null,
|
|
668
789
|
name: server.name,
|
|
669
790
|
server_url: server.server_url,
|
|
670
|
-
|
|
671
|
-
state:
|
|
791
|
+
// mark as "authenticating" because the server isn't automatically connected, so it's pending authenticating
|
|
792
|
+
state: serverConn?.connectionState ?? "authenticating"
|
|
672
793
|
};
|
|
673
794
|
}
|
|
674
795
|
return mcpState;
|
|
@@ -684,9 +805,9 @@ Agent.options = {
|
|
|
684
805
|
};
|
|
685
806
|
async function routeAgentRequest(request, env, options) {
|
|
686
807
|
const corsHeaders = options?.cors === true ? {
|
|
687
|
-
"Access-Control-Allow-Origin": "*",
|
|
688
|
-
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
|
|
689
808
|
"Access-Control-Allow-Credentials": "true",
|
|
809
|
+
"Access-Control-Allow-Methods": "GET, POST, HEAD, OPTIONS",
|
|
810
|
+
"Access-Control-Allow-Origin": "*",
|
|
690
811
|
"Access-Control-Max-Age": "86400"
|
|
691
812
|
} : options?.cors;
|
|
692
813
|
if (request.method === "OPTIONS") {
|
|
@@ -717,7 +838,105 @@ async function routeAgentRequest(request, env, options) {
|
|
|
717
838
|
}
|
|
718
839
|
return response;
|
|
719
840
|
}
|
|
841
|
+
function createHeaderBasedResolver() {
|
|
842
|
+
return async (email, _env) => {
|
|
843
|
+
const messageId = email.headers.get("message-id");
|
|
844
|
+
if (messageId) {
|
|
845
|
+
const messageIdMatch = messageId.match(/<([^@]+)@([^>]+)>/);
|
|
846
|
+
if (messageIdMatch) {
|
|
847
|
+
const [, agentId2, domain] = messageIdMatch;
|
|
848
|
+
const agentName2 = domain.split(".")[0];
|
|
849
|
+
return { agentName: agentName2, agentId: agentId2 };
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
const references = email.headers.get("references");
|
|
853
|
+
if (references) {
|
|
854
|
+
const referencesMatch = references.match(
|
|
855
|
+
/<([A-Za-z0-9+/]{43}=)@([^>]+)>/
|
|
856
|
+
);
|
|
857
|
+
if (referencesMatch) {
|
|
858
|
+
const [, base64Id, domain] = referencesMatch;
|
|
859
|
+
const agentId2 = Buffer.from(base64Id, "base64").toString("hex");
|
|
860
|
+
const agentName2 = domain.split(".")[0];
|
|
861
|
+
return { agentName: agentName2, agentId: agentId2 };
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
const agentName = email.headers.get("x-agent-name");
|
|
865
|
+
const agentId = email.headers.get("x-agent-id");
|
|
866
|
+
if (agentName && agentId) {
|
|
867
|
+
return { agentName, agentId };
|
|
868
|
+
}
|
|
869
|
+
return null;
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
function createAddressBasedResolver(defaultAgentName) {
|
|
873
|
+
return async (email, _env) => {
|
|
874
|
+
const emailMatch = email.to.match(/^([^+@]+)(?:\+([^@]+))?@(.+)$/);
|
|
875
|
+
if (!emailMatch) {
|
|
876
|
+
return null;
|
|
877
|
+
}
|
|
878
|
+
const [, localPart, subAddress] = emailMatch;
|
|
879
|
+
if (subAddress) {
|
|
880
|
+
return {
|
|
881
|
+
agentName: localPart,
|
|
882
|
+
agentId: subAddress
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
return {
|
|
886
|
+
agentName: defaultAgentName,
|
|
887
|
+
agentId: localPart
|
|
888
|
+
};
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
function createCatchAllResolver(agentName, agentId) {
|
|
892
|
+
return async () => ({ agentName, agentId });
|
|
893
|
+
}
|
|
720
894
|
async function routeAgentEmail(email, env, options) {
|
|
895
|
+
const routingInfo = await options.resolver(email, env);
|
|
896
|
+
if (!routingInfo) {
|
|
897
|
+
console.warn("No routing information found for email, dropping message");
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
const namespaceBinding = env[routingInfo.agentName];
|
|
901
|
+
if (!namespaceBinding) {
|
|
902
|
+
console.error(
|
|
903
|
+
`Agent namespace '${routingInfo.agentName}' not found in environment`
|
|
904
|
+
);
|
|
905
|
+
return;
|
|
906
|
+
}
|
|
907
|
+
if (typeof namespaceBinding !== "object" || !("idFromName" in namespaceBinding) || typeof namespaceBinding.idFromName !== "function") {
|
|
908
|
+
console.error(
|
|
909
|
+
`Environment binding '${routingInfo.agentName}' is not an AgentNamespace (found: ${typeof namespaceBinding})`
|
|
910
|
+
);
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
const namespace = namespaceBinding;
|
|
914
|
+
const agent = await getAgentByName(namespace, routingInfo.agentId);
|
|
915
|
+
await agent.onEmail(email);
|
|
916
|
+
}
|
|
917
|
+
async function sendEmailWithRouting(emailBinding, from, fromName, options) {
|
|
918
|
+
const { createMimeMessage } = await import("mimetext");
|
|
919
|
+
const msg = createMimeMessage();
|
|
920
|
+
msg.setSender({ addr: from, name: fromName });
|
|
921
|
+
msg.setRecipient(options.to);
|
|
922
|
+
msg.setSubject(options.subject);
|
|
923
|
+
msg.addMessage({
|
|
924
|
+
contentType: options.contentType || "text/plain",
|
|
925
|
+
data: options.body
|
|
926
|
+
});
|
|
927
|
+
if (options.includeRoutingHeaders && options.agentName && options.agentId) {
|
|
928
|
+
const domain = options.domain || from.split("@")[1];
|
|
929
|
+
const messageId = `<${options.agentId}@${domain}>`;
|
|
930
|
+
msg.setHeader("Message-ID", messageId);
|
|
931
|
+
msg.setHeader("X-Agent-Name", options.agentName);
|
|
932
|
+
msg.setHeader("X-Agent-ID", options.agentId);
|
|
933
|
+
}
|
|
934
|
+
if (options.headers) {
|
|
935
|
+
for (const [key, value] of Object.entries(options.headers)) {
|
|
936
|
+
msg.setHeader(key, value);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
await emailBinding.send(new EmailMessage(from, options.to, msg.asRaw()));
|
|
721
940
|
}
|
|
722
941
|
async function getAgentByName(namespace, name, options) {
|
|
723
942
|
return getServerByName(namespace, name, options);
|
|
@@ -737,11 +956,11 @@ var StreamingResponse = class {
|
|
|
737
956
|
throw new Error("StreamingResponse is already closed");
|
|
738
957
|
}
|
|
739
958
|
const response = {
|
|
740
|
-
|
|
959
|
+
done: false,
|
|
741
960
|
id: this._id,
|
|
742
|
-
success: true,
|
|
743
961
|
result: chunk,
|
|
744
|
-
|
|
962
|
+
success: true,
|
|
963
|
+
type: "rpc"
|
|
745
964
|
};
|
|
746
965
|
this._connection.send(JSON.stringify(response));
|
|
747
966
|
}
|
|
@@ -755,23 +974,52 @@ var StreamingResponse = class {
|
|
|
755
974
|
}
|
|
756
975
|
this._closed = true;
|
|
757
976
|
const response = {
|
|
758
|
-
|
|
977
|
+
done: true,
|
|
759
978
|
id: this._id,
|
|
760
|
-
success: true,
|
|
761
979
|
result: finalChunk,
|
|
762
|
-
|
|
980
|
+
success: true,
|
|
981
|
+
type: "rpc"
|
|
763
982
|
};
|
|
764
983
|
this._connection.send(JSON.stringify(response));
|
|
765
984
|
}
|
|
766
985
|
};
|
|
767
986
|
|
|
987
|
+
// src/observability/index.ts
|
|
988
|
+
var genericObservability = {
|
|
989
|
+
emit(event) {
|
|
990
|
+
if (isLocalMode()) {
|
|
991
|
+
console.log(event.displayMessage);
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
console.log(event);
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
var localMode = false;
|
|
998
|
+
function isLocalMode() {
|
|
999
|
+
if (localMode) {
|
|
1000
|
+
return true;
|
|
1001
|
+
}
|
|
1002
|
+
const { request } = getCurrentAgent();
|
|
1003
|
+
if (!request) {
|
|
1004
|
+
return false;
|
|
1005
|
+
}
|
|
1006
|
+
const url = new URL(request.url);
|
|
1007
|
+
localMode = url.hostname === "localhost";
|
|
1008
|
+
return localMode;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
768
1011
|
export {
|
|
1012
|
+
genericObservability,
|
|
769
1013
|
unstable_callable,
|
|
770
1014
|
getCurrentAgent,
|
|
771
1015
|
Agent,
|
|
772
1016
|
routeAgentRequest,
|
|
1017
|
+
createHeaderBasedResolver,
|
|
1018
|
+
createAddressBasedResolver,
|
|
1019
|
+
createCatchAllResolver,
|
|
773
1020
|
routeAgentEmail,
|
|
1021
|
+
sendEmailWithRouting,
|
|
774
1022
|
getAgentByName,
|
|
775
1023
|
StreamingResponse
|
|
776
1024
|
};
|
|
777
|
-
//# sourceMappingURL=chunk-
|
|
1025
|
+
//# sourceMappingURL=chunk-RX76B6DL.js.map
|