agents 0.0.0-c69f616 → 0.0.0-c6d9bf1
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/README.md +129 -7
- package/dist/_esm-LV5FJ3HK.js +3922 -0
- package/dist/_esm-LV5FJ3HK.js.map +1 -0
- package/dist/ai-chat-agent.d.ts +10 -8
- package/dist/ai-chat-agent.js +444 -60
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-chat-v5-migration.d.ts +152 -0
- package/dist/ai-chat-v5-migration.js +20 -0
- package/dist/ai-chat-v5-migration.js.map +1 -0
- package/dist/ai-react.d.ts +66 -70
- package/dist/ai-react.js +252 -99
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +37 -19
- package/dist/ai-types.js +7 -0
- package/dist/ccip-CMBYN64O.js +15 -0
- package/dist/ccip-CMBYN64O.js.map +1 -0
- package/dist/{chunk-OJFA7RKX.js → chunk-254F4GDT.js} +188 -115
- package/dist/chunk-254F4GDT.js.map +1 -0
- package/dist/{chunk-HY7ZLHJB.js → chunk-3OT2NNEW.js} +412 -69
- package/dist/chunk-3OT2NNEW.js.map +1 -0
- package/dist/chunk-5Y6BEZDY.js +276 -0
- package/dist/chunk-5Y6BEZDY.js.map +1 -0
- package/dist/chunk-BER7KXUJ.js +18 -0
- package/dist/chunk-BER7KXUJ.js.map +1 -0
- package/dist/chunk-JJBFIGUC.js +5202 -0
- package/dist/chunk-JJBFIGUC.js.map +1 -0
- package/dist/chunk-PR4QN5HX.js +43 -0
- package/dist/chunk-PR4QN5HX.js.map +1 -0
- package/dist/{chunk-KUH345EY.js → chunk-QEVM4BVL.js} +5 -5
- package/dist/chunk-QEVM4BVL.js.map +1 -0
- package/dist/chunk-TYAY6AU6.js +159 -0
- package/dist/chunk-TYAY6AU6.js.map +1 -0
- package/dist/chunk-UJVEAURM.js +150 -0
- package/dist/chunk-UJVEAURM.js.map +1 -0
- package/dist/{chunk-PVQZBKN7.js → chunk-Z44WASMA.js} +11 -3
- package/dist/chunk-Z44WASMA.js.map +1 -0
- package/dist/{client-CH-eFIfq.d.ts → client-DVoPb3-C.d.ts} +762 -99
- package/dist/client.js +3 -1
- package/dist/codemode/ai.d.ts +25 -0
- package/dist/codemode/ai.js +5112 -0
- package/dist/codemode/ai.js.map +1 -0
- package/dist/index.d.ts +548 -32
- package/dist/index.js +8 -4
- package/dist/mcp/client.d.ts +2 -1
- package/dist/mcp/client.js +2 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -0
- package/dist/mcp/do-oauth-client-provider.js +2 -1
- package/dist/mcp/index.d.ts +50 -72
- package/dist/mcp/index.js +909 -718
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/x402.d.ts +39 -0
- package/dist/mcp/x402.js +3195 -0
- package/dist/mcp/x402.js.map +1 -0
- package/dist/mcp-BH1fJeiU.d.ts +58 -0
- package/dist/observability/index.d.ts +34 -14
- package/dist/observability/index.js +6 -4
- package/dist/react.d.ts +13 -7
- package/dist/react.js +107 -7
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +79 -5
- package/dist/schedule.js +17 -2
- package/dist/schedule.js.map +1 -1
- package/dist/secp256k1-M22GZP2U.js +2193 -0
- package/dist/secp256k1-M22GZP2U.js.map +1 -0
- package/package.json +32 -9
- package/src/index.ts +272 -136
- package/dist/chunk-HY7ZLHJB.js.map +0 -1
- package/dist/chunk-KUH345EY.js.map +0 -1
- package/dist/chunk-OJFA7RKX.js.map +0 -1
- package/dist/chunk-PVQZBKN7.js.map +0 -1
- package/dist/index-BVoermIz.d.ts +0 -615
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
+
DisposableStore,
|
|
2
3
|
MCPClientManager
|
|
3
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-3OT2NNEW.js";
|
|
4
5
|
import {
|
|
5
6
|
DurableObjectOAuthClientProvider
|
|
6
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-Z44WASMA.js";
|
|
7
8
|
import {
|
|
8
9
|
camelCaseToKebabCase
|
|
9
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-QEVM4BVL.js";
|
|
10
11
|
|
|
11
12
|
// src/index.ts
|
|
12
13
|
import { AsyncLocalStorage } from "async_hooks";
|
|
@@ -19,13 +20,13 @@ import {
|
|
|
19
20
|
routePartykitRequest
|
|
20
21
|
} from "partyserver";
|
|
21
22
|
function isRPCRequest(msg) {
|
|
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);
|
|
23
|
+
return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "rpc" /* RPC */ && "id" in msg && typeof msg.id === "string" && "method" in msg && typeof msg.method === "string" && "args" in msg && Array.isArray(msg.args);
|
|
23
24
|
}
|
|
24
25
|
function isStateUpdateMessage(msg) {
|
|
25
|
-
return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "cf_agent_state" && "state" in msg;
|
|
26
|
+
return typeof msg === "object" && msg !== null && "type" in msg && msg.type === "cf_agent_state" /* CF_AGENT_STATE */ && "state" in msg;
|
|
26
27
|
}
|
|
27
28
|
var callableMetadata = /* @__PURE__ */ new Map();
|
|
28
|
-
function
|
|
29
|
+
function callable(metadata = {}) {
|
|
29
30
|
return function callableDecorator(target, context) {
|
|
30
31
|
if (!callableMetadata.has(target)) {
|
|
31
32
|
callableMetadata.set(target, metadata);
|
|
@@ -33,6 +34,16 @@ function unstable_callable(metadata = {}) {
|
|
|
33
34
|
return target;
|
|
34
35
|
};
|
|
35
36
|
}
|
|
37
|
+
var didWarnAboutUnstableCallable = false;
|
|
38
|
+
var unstable_callable = (metadata = {}) => {
|
|
39
|
+
if (!didWarnAboutUnstableCallable) {
|
|
40
|
+
didWarnAboutUnstableCallable = true;
|
|
41
|
+
console.warn(
|
|
42
|
+
"unstable_callable is deprecated, use callable instead. unstable_callable will be removed in the next major version."
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
callable(metadata);
|
|
46
|
+
};
|
|
36
47
|
function getNextCronTime(cron) {
|
|
37
48
|
const interval = parseCronExpression(cron);
|
|
38
49
|
return interval.getNextDate();
|
|
@@ -55,7 +66,10 @@ function getCurrentAgent() {
|
|
|
55
66
|
}
|
|
56
67
|
function withAgentContext(method) {
|
|
57
68
|
return function(...args) {
|
|
58
|
-
const { connection, request, email } = getCurrentAgent();
|
|
69
|
+
const { connection, request, email, agent } = getCurrentAgent();
|
|
70
|
+
if (agent === this) {
|
|
71
|
+
return method.apply(this, args);
|
|
72
|
+
}
|
|
59
73
|
return agentContext.run({ agent: this, connection, request, email }, () => {
|
|
60
74
|
return method.apply(this, args);
|
|
61
75
|
});
|
|
@@ -65,8 +79,12 @@ var _Agent = class _Agent extends Server {
|
|
|
65
79
|
constructor(ctx, env) {
|
|
66
80
|
super(ctx, env);
|
|
67
81
|
this._state = DEFAULT_STATE;
|
|
82
|
+
this._disposables = new DisposableStore();
|
|
68
83
|
this._ParentClass = Object.getPrototypeOf(this).constructor;
|
|
69
|
-
this.mcp = new MCPClientManager(
|
|
84
|
+
this.mcp = new MCPClientManager(
|
|
85
|
+
this._ParentClass.name,
|
|
86
|
+
"0.0.1"
|
|
87
|
+
);
|
|
70
88
|
/**
|
|
71
89
|
* Initial state for the Agent
|
|
72
90
|
* Override to provide default state values
|
|
@@ -110,7 +128,10 @@ var _Agent = class _Agent extends Server {
|
|
|
110
128
|
{
|
|
111
129
|
displayMessage: `Schedule ${row.id} executed`,
|
|
112
130
|
id: nanoid(),
|
|
113
|
-
payload:
|
|
131
|
+
payload: {
|
|
132
|
+
callback: row.callback,
|
|
133
|
+
id: row.id
|
|
134
|
+
},
|
|
114
135
|
timestamp: Date.now(),
|
|
115
136
|
type: "schedule:execute"
|
|
116
137
|
},
|
|
@@ -137,7 +158,20 @@ var _Agent = class _Agent extends Server {
|
|
|
137
158
|
}
|
|
138
159
|
await this._scheduleNextAlarm();
|
|
139
160
|
};
|
|
140
|
-
this.
|
|
161
|
+
if (!wrappedClasses.has(this.constructor)) {
|
|
162
|
+
this._autoWrapCustomMethods();
|
|
163
|
+
wrappedClasses.add(this.constructor);
|
|
164
|
+
}
|
|
165
|
+
this._disposables.add(
|
|
166
|
+
this.mcp.onConnected(async () => {
|
|
167
|
+
this.broadcastMcpServers();
|
|
168
|
+
})
|
|
169
|
+
);
|
|
170
|
+
this._disposables.add(
|
|
171
|
+
this.mcp.onObservabilityEvent((event) => {
|
|
172
|
+
this.observability?.emit(event);
|
|
173
|
+
})
|
|
174
|
+
);
|
|
141
175
|
this.sql`
|
|
142
176
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
143
177
|
id TEXT PRIMARY KEY NOT NULL,
|
|
@@ -186,17 +220,16 @@ var _Agent = class _Agent extends Server {
|
|
|
186
220
|
{ agent: this, connection: void 0, request, email: void 0 },
|
|
187
221
|
async () => {
|
|
188
222
|
if (this.mcp.isCallbackRequest(request)) {
|
|
189
|
-
await this.mcp.handleCallbackRequest(request);
|
|
190
|
-
this.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
});
|
|
223
|
+
const result = await this.mcp.handleCallbackRequest(request);
|
|
224
|
+
this.broadcastMcpServers();
|
|
225
|
+
if (result.authSuccess) {
|
|
226
|
+
this.mcp.establishConnection(result.serverId).catch((error) => {
|
|
227
|
+
console.error("Background connection failed:", error);
|
|
228
|
+
}).finally(() => {
|
|
229
|
+
this.broadcastMcpServers();
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
return this.handleOAuthCallbackResponse(result, request);
|
|
200
233
|
}
|
|
201
234
|
return this._tryCatch(() => _onRequest(request));
|
|
202
235
|
}
|
|
@@ -242,10 +275,8 @@ var _Agent = class _Agent extends Server {
|
|
|
242
275
|
displayMessage: `RPC call to ${method}`,
|
|
243
276
|
id: nanoid(),
|
|
244
277
|
payload: {
|
|
245
|
-
args,
|
|
246
278
|
method,
|
|
247
|
-
streaming: metadata?.streaming
|
|
248
|
-
success: true
|
|
279
|
+
streaming: metadata?.streaming
|
|
249
280
|
},
|
|
250
281
|
timestamp: Date.now(),
|
|
251
282
|
type: "rpc"
|
|
@@ -257,7 +288,7 @@ var _Agent = class _Agent extends Server {
|
|
|
257
288
|
id,
|
|
258
289
|
result,
|
|
259
290
|
success: true,
|
|
260
|
-
type: "rpc"
|
|
291
|
+
type: "rpc" /* RPC */
|
|
261
292
|
};
|
|
262
293
|
connection.send(JSON.stringify(response));
|
|
263
294
|
} catch (e) {
|
|
@@ -265,7 +296,7 @@ var _Agent = class _Agent extends Server {
|
|
|
265
296
|
error: e instanceof Error ? e.message : "Unknown error occurred",
|
|
266
297
|
id: parsed.id,
|
|
267
298
|
success: false,
|
|
268
|
-
type: "rpc"
|
|
299
|
+
type: "rpc" /* RPC */
|
|
269
300
|
};
|
|
270
301
|
connection.send(JSON.stringify(response));
|
|
271
302
|
console.error("RPC error:", e);
|
|
@@ -280,41 +311,39 @@ var _Agent = class _Agent extends Server {
|
|
|
280
311
|
this.onConnect = (connection, ctx2) => {
|
|
281
312
|
return agentContext.run(
|
|
282
313
|
{ agent: this, connection, request: ctx2.request, email: void 0 },
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
if (this.state) {
|
|
286
|
-
connection.send(
|
|
287
|
-
JSON.stringify({
|
|
288
|
-
state: this.state,
|
|
289
|
-
type: "cf_agent_state"
|
|
290
|
-
})
|
|
291
|
-
);
|
|
292
|
-
}
|
|
314
|
+
() => {
|
|
315
|
+
if (this.state) {
|
|
293
316
|
connection.send(
|
|
294
317
|
JSON.stringify({
|
|
295
|
-
|
|
296
|
-
type: "
|
|
318
|
+
state: this.state,
|
|
319
|
+
type: "cf_agent_state" /* CF_AGENT_STATE */
|
|
297
320
|
})
|
|
298
321
|
);
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
322
|
+
}
|
|
323
|
+
connection.send(
|
|
324
|
+
JSON.stringify({
|
|
325
|
+
mcp: this.getMcpServers(),
|
|
326
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
327
|
+
})
|
|
328
|
+
);
|
|
329
|
+
this.observability?.emit(
|
|
330
|
+
{
|
|
331
|
+
displayMessage: "Connection established",
|
|
332
|
+
id: nanoid(),
|
|
333
|
+
payload: {
|
|
334
|
+
connectionId: connection.id
|
|
308
335
|
},
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
336
|
+
timestamp: Date.now(),
|
|
337
|
+
type: "connect"
|
|
338
|
+
},
|
|
339
|
+
this.ctx
|
|
340
|
+
);
|
|
341
|
+
return this._tryCatch(() => _onConnect(connection, ctx2));
|
|
313
342
|
}
|
|
314
343
|
);
|
|
315
344
|
};
|
|
316
345
|
const _onStart = this.onStart.bind(this);
|
|
317
|
-
this.onStart = async () => {
|
|
346
|
+
this.onStart = async (props) => {
|
|
318
347
|
return agentContext.run(
|
|
319
348
|
{
|
|
320
349
|
agent: this,
|
|
@@ -323,13 +352,21 @@ var _Agent = class _Agent extends Server {
|
|
|
323
352
|
email: void 0
|
|
324
353
|
},
|
|
325
354
|
async () => {
|
|
326
|
-
|
|
355
|
+
await this._tryCatch(() => {
|
|
356
|
+
const servers = this.sql`
|
|
327
357
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
328
358
|
`;
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
servers.
|
|
332
|
-
|
|
359
|
+
this.broadcastMcpServers();
|
|
360
|
+
if (servers && Array.isArray(servers) && servers.length > 0) {
|
|
361
|
+
servers.forEach((server) => {
|
|
362
|
+
if (server.callback_url) {
|
|
363
|
+
this.mcp.registerCallbackUrl(
|
|
364
|
+
`${server.callback_url}/${server.id}`
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
servers.forEach((server) => {
|
|
369
|
+
this._connectToMcpServerInternal(
|
|
333
370
|
server.name,
|
|
334
371
|
server.server_url,
|
|
335
372
|
server.callback_url,
|
|
@@ -338,18 +375,19 @@ var _Agent = class _Agent extends Server {
|
|
|
338
375
|
id: server.id,
|
|
339
376
|
oauthClientId: server.client_id ?? void 0
|
|
340
377
|
}
|
|
341
|
-
)
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
378
|
+
).then(() => {
|
|
379
|
+
this.broadcastMcpServers();
|
|
380
|
+
}).catch((error) => {
|
|
381
|
+
console.error(
|
|
382
|
+
`Error connecting to MCP server: ${server.name} (${server.server_url})`,
|
|
383
|
+
error
|
|
384
|
+
);
|
|
385
|
+
this.broadcastMcpServers();
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
return _onStart(props);
|
|
390
|
+
});
|
|
353
391
|
}
|
|
354
392
|
);
|
|
355
393
|
};
|
|
@@ -400,7 +438,6 @@ var _Agent = class _Agent extends Server {
|
|
|
400
438
|
}
|
|
401
439
|
}
|
|
402
440
|
_setStateInternal(state, source = "server") {
|
|
403
|
-
const previousState = this._state;
|
|
404
441
|
this._state = state;
|
|
405
442
|
this.sql`
|
|
406
443
|
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
@@ -413,7 +450,7 @@ var _Agent = class _Agent extends Server {
|
|
|
413
450
|
this.broadcast(
|
|
414
451
|
JSON.stringify({
|
|
415
452
|
state,
|
|
416
|
-
type: "cf_agent_state"
|
|
453
|
+
type: "cf_agent_state" /* CF_AGENT_STATE */
|
|
417
454
|
}),
|
|
418
455
|
source !== "server" ? [source.id] : []
|
|
419
456
|
);
|
|
@@ -426,10 +463,7 @@ var _Agent = class _Agent extends Server {
|
|
|
426
463
|
{
|
|
427
464
|
displayMessage: "State updated",
|
|
428
465
|
id: nanoid(),
|
|
429
|
-
payload: {
|
|
430
|
-
previousState,
|
|
431
|
-
state
|
|
432
|
-
},
|
|
466
|
+
payload: {},
|
|
433
467
|
timestamp: Date.now(),
|
|
434
468
|
type: "state:update"
|
|
435
469
|
},
|
|
@@ -546,28 +580,22 @@ var _Agent = class _Agent extends Server {
|
|
|
546
580
|
while (proto && proto !== Object.prototype && depth < 10) {
|
|
547
581
|
const methodNames = Object.getOwnPropertyNames(proto);
|
|
548
582
|
for (const methodName of methodNames) {
|
|
549
|
-
|
|
583
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
584
|
+
if (baseMethods.has(methodName) || methodName.startsWith("_") || !descriptor || !!descriptor.get || typeof descriptor.value !== "function") {
|
|
550
585
|
continue;
|
|
551
586
|
}
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
wrappedFunction,
|
|
563
|
-
callableMetadata.get(
|
|
564
|
-
this[methodName]
|
|
565
|
-
)
|
|
566
|
-
);
|
|
567
|
-
}
|
|
568
|
-
this.constructor.prototype[methodName] = wrappedFunction;
|
|
569
|
-
}
|
|
587
|
+
const wrappedFunction = withAgentContext(
|
|
588
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
589
|
+
this[methodName]
|
|
590
|
+
// biome-ignore lint/suspicious/noExplicitAny: I can't typescript
|
|
591
|
+
);
|
|
592
|
+
if (this._isCallable(methodName)) {
|
|
593
|
+
callableMetadata.set(
|
|
594
|
+
wrappedFunction,
|
|
595
|
+
callableMetadata.get(this[methodName])
|
|
596
|
+
);
|
|
570
597
|
}
|
|
598
|
+
this.constructor.prototype[methodName] = wrappedFunction;
|
|
571
599
|
}
|
|
572
600
|
proto = Object.getPrototypeOf(proto);
|
|
573
601
|
depth++;
|
|
@@ -714,7 +742,10 @@ var _Agent = class _Agent extends Server {
|
|
|
714
742
|
{
|
|
715
743
|
displayMessage: `Schedule ${schedule.id} created`,
|
|
716
744
|
id: nanoid(),
|
|
717
|
-
payload:
|
|
745
|
+
payload: {
|
|
746
|
+
callback,
|
|
747
|
+
id
|
|
748
|
+
},
|
|
718
749
|
timestamp: Date.now(),
|
|
719
750
|
type: "schedule:create"
|
|
720
751
|
},
|
|
@@ -849,7 +880,10 @@ var _Agent = class _Agent extends Server {
|
|
|
849
880
|
{
|
|
850
881
|
displayMessage: `Schedule ${id} cancelled`,
|
|
851
882
|
id: nanoid(),
|
|
852
|
-
payload:
|
|
883
|
+
payload: {
|
|
884
|
+
callback: schedule.callback,
|
|
885
|
+
id: schedule.id
|
|
886
|
+
},
|
|
853
887
|
timestamp: Date.now(),
|
|
854
888
|
type: "schedule:cancel"
|
|
855
889
|
},
|
|
@@ -883,6 +917,8 @@ var _Agent = class _Agent extends Server {
|
|
|
883
917
|
this.sql`DROP TABLE IF EXISTS cf_agents_queues`;
|
|
884
918
|
await this.ctx.storage.deleteAlarm();
|
|
885
919
|
await this.ctx.storage.deleteAll();
|
|
920
|
+
this._disposables.dispose();
|
|
921
|
+
await this.mcp.dispose?.();
|
|
886
922
|
this.ctx.abort("destroyed");
|
|
887
923
|
this.observability?.emit(
|
|
888
924
|
{
|
|
@@ -905,14 +941,26 @@ var _Agent = class _Agent extends Server {
|
|
|
905
941
|
/**
|
|
906
942
|
* Connect to a new MCP Server
|
|
907
943
|
*
|
|
944
|
+
* @param serverName Name of the MCP server
|
|
908
945
|
* @param url MCP Server SSE URL
|
|
909
|
-
* @param callbackHost Base host for the agent, used for the redirect URI.
|
|
946
|
+
* @param callbackHost Base host for the agent, used for the redirect URI. If not provided, will be derived from the current request.
|
|
910
947
|
* @param agentsPrefix agents routing prefix if not using `agents`
|
|
911
|
-
* @param options MCP client and transport
|
|
948
|
+
* @param options MCP client and transport options
|
|
912
949
|
* @returns authUrl
|
|
913
950
|
*/
|
|
914
951
|
async addMcpServer(serverName, url, callbackHost, agentsPrefix = "agents", options) {
|
|
915
|
-
|
|
952
|
+
let resolvedCallbackHost = callbackHost;
|
|
953
|
+
if (!resolvedCallbackHost) {
|
|
954
|
+
const { request } = getCurrentAgent();
|
|
955
|
+
if (!request) {
|
|
956
|
+
throw new Error(
|
|
957
|
+
"callbackHost is required when not called within a request context"
|
|
958
|
+
);
|
|
959
|
+
}
|
|
960
|
+
const requestUrl = new URL(request.url);
|
|
961
|
+
resolvedCallbackHost = `${requestUrl.protocol}//${requestUrl.host}`;
|
|
962
|
+
}
|
|
963
|
+
const callbackUrl = `${resolvedCallbackHost}/${agentsPrefix}/${camelCaseToKebabCase(this._ParentClass.name)}/${this.name}/callback`;
|
|
916
964
|
const result = await this._connectToMcpServerInternal(
|
|
917
965
|
serverName,
|
|
918
966
|
url,
|
|
@@ -932,12 +980,7 @@ var _Agent = class _Agent extends Server {
|
|
|
932
980
|
${options ? JSON.stringify(options) : null}
|
|
933
981
|
);
|
|
934
982
|
`;
|
|
935
|
-
this.
|
|
936
|
-
JSON.stringify({
|
|
937
|
-
mcp: this.getMcpServers(),
|
|
938
|
-
type: "cf_agent_mcp_servers"
|
|
939
|
-
})
|
|
940
|
-
);
|
|
983
|
+
this.broadcastMcpServers();
|
|
941
984
|
return result;
|
|
942
985
|
}
|
|
943
986
|
async _connectToMcpServerInternal(_serverName, url, callbackUrl, options, reconnect) {
|
|
@@ -952,6 +995,7 @@ var _Agent = class _Agent extends Server {
|
|
|
952
995
|
authProvider.clientId = reconnect.oauthClientId;
|
|
953
996
|
}
|
|
954
997
|
}
|
|
998
|
+
const transportType = options?.transport?.type ?? "auto";
|
|
955
999
|
let headerTransportOpts = {};
|
|
956
1000
|
if (options?.transport?.headers) {
|
|
957
1001
|
headerTransportOpts = {
|
|
@@ -971,7 +1015,8 @@ var _Agent = class _Agent extends Server {
|
|
|
971
1015
|
reconnect,
|
|
972
1016
|
transport: {
|
|
973
1017
|
...headerTransportOpts,
|
|
974
|
-
authProvider
|
|
1018
|
+
authProvider,
|
|
1019
|
+
type: transportType
|
|
975
1020
|
}
|
|
976
1021
|
});
|
|
977
1022
|
return {
|
|
@@ -982,15 +1027,11 @@ var _Agent = class _Agent extends Server {
|
|
|
982
1027
|
}
|
|
983
1028
|
async removeMcpServer(id) {
|
|
984
1029
|
this.mcp.closeConnection(id);
|
|
1030
|
+
this.mcp.unregisterCallbackUrl(id);
|
|
985
1031
|
this.sql`
|
|
986
1032
|
DELETE FROM cf_agents_mcp_servers WHERE id = ${id};
|
|
987
1033
|
`;
|
|
988
|
-
this.
|
|
989
|
-
JSON.stringify({
|
|
990
|
-
mcp: this.getMcpServers(),
|
|
991
|
-
type: "cf_agent_mcp_servers"
|
|
992
|
-
})
|
|
993
|
-
);
|
|
1034
|
+
this.broadcastMcpServers();
|
|
994
1035
|
}
|
|
995
1036
|
getMcpServers() {
|
|
996
1037
|
const mcpState = {
|
|
@@ -1018,6 +1059,36 @@ var _Agent = class _Agent extends Server {
|
|
|
1018
1059
|
}
|
|
1019
1060
|
return mcpState;
|
|
1020
1061
|
}
|
|
1062
|
+
broadcastMcpServers() {
|
|
1063
|
+
this.broadcast(
|
|
1064
|
+
JSON.stringify({
|
|
1065
|
+
mcp: this.getMcpServers(),
|
|
1066
|
+
type: "cf_agent_mcp_servers" /* CF_AGENT_MCP_SERVERS */
|
|
1067
|
+
})
|
|
1068
|
+
);
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Handle OAuth callback response using MCPClientManager configuration
|
|
1072
|
+
* @param result OAuth callback result
|
|
1073
|
+
* @param request The original request (needed for base URL)
|
|
1074
|
+
* @returns Response for the OAuth callback
|
|
1075
|
+
*/
|
|
1076
|
+
handleOAuthCallbackResponse(result, request) {
|
|
1077
|
+
const config = this.mcp.getOAuthCallbackConfig();
|
|
1078
|
+
if (config?.customHandler) {
|
|
1079
|
+
return config.customHandler(result);
|
|
1080
|
+
}
|
|
1081
|
+
if (config?.successRedirect && result.authSuccess) {
|
|
1082
|
+
return Response.redirect(config.successRedirect);
|
|
1083
|
+
}
|
|
1084
|
+
if (config?.errorRedirect && !result.authSuccess) {
|
|
1085
|
+
return Response.redirect(
|
|
1086
|
+
`${config.errorRedirect}?error=${encodeURIComponent(result.authError || "Unknown error")}`
|
|
1087
|
+
);
|
|
1088
|
+
}
|
|
1089
|
+
const baseUrl = new URL(request.url).origin;
|
|
1090
|
+
return Response.redirect(baseUrl);
|
|
1091
|
+
}
|
|
1021
1092
|
};
|
|
1022
1093
|
/**
|
|
1023
1094
|
* Agent configuration options
|
|
@@ -1028,6 +1099,7 @@ _Agent.options = {
|
|
|
1028
1099
|
// default to hibernate
|
|
1029
1100
|
};
|
|
1030
1101
|
var Agent = _Agent;
|
|
1102
|
+
var wrappedClasses = /* @__PURE__ */ new Set();
|
|
1031
1103
|
async function routeAgentRequest(request, env, options) {
|
|
1032
1104
|
const corsHeaders = options?.cors === true ? {
|
|
1033
1105
|
"Access-Control-Allow-Credentials": "true",
|
|
@@ -1206,7 +1278,7 @@ var StreamingResponse = class {
|
|
|
1206
1278
|
id: this._id,
|
|
1207
1279
|
result: chunk,
|
|
1208
1280
|
success: true,
|
|
1209
|
-
type: "rpc"
|
|
1281
|
+
type: "rpc" /* RPC */
|
|
1210
1282
|
};
|
|
1211
1283
|
this._connection.send(JSON.stringify(response));
|
|
1212
1284
|
}
|
|
@@ -1224,7 +1296,7 @@ var StreamingResponse = class {
|
|
|
1224
1296
|
id: this._id,
|
|
1225
1297
|
result: finalChunk,
|
|
1226
1298
|
success: true,
|
|
1227
|
-
type: "rpc"
|
|
1299
|
+
type: "rpc" /* RPC */
|
|
1228
1300
|
};
|
|
1229
1301
|
this._connection.send(JSON.stringify(response));
|
|
1230
1302
|
}
|
|
@@ -1256,6 +1328,7 @@ function isLocalMode() {
|
|
|
1256
1328
|
|
|
1257
1329
|
export {
|
|
1258
1330
|
genericObservability,
|
|
1331
|
+
callable,
|
|
1259
1332
|
unstable_callable,
|
|
1260
1333
|
getCurrentAgent,
|
|
1261
1334
|
Agent,
|
|
@@ -1267,4 +1340,4 @@ export {
|
|
|
1267
1340
|
getAgentByName,
|
|
1268
1341
|
StreamingResponse
|
|
1269
1342
|
};
|
|
1270
|
-
//# sourceMappingURL=chunk-
|
|
1343
|
+
//# sourceMappingURL=chunk-254F4GDT.js.map
|