agents 0.0.0-1cb6383 → 0.0.0-1dc8321
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 +11 -8
- package/dist/ai-chat-agent.js +466 -66
- 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 +246 -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-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-PVQZBKN7.js → chunk-C2OEBJZ2.js} +14 -7
- package/dist/chunk-C2OEBJZ2.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-JXN5WZFQ.js → chunk-XGMKNUJA.js} +173 -117
- package/dist/chunk-XGMKNUJA.js.map +1 -0
- package/dist/{chunk-HY7ZLHJB.js → chunk-ZMMHNOMZ.js} +415 -71
- package/dist/chunk-ZMMHNOMZ.js.map +1 -0
- package/dist/{client-DgyzBU_8.d.ts → client-DVoPb3-C.d.ts} +555 -36
- package/dist/client.js +3 -1
- package/dist/codemode/ai.d.ts +25 -0
- package/dist/codemode/ai.js +5200 -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 -83
- package/dist/mcp/index.js +904 -760
- 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 +34 -9
- package/src/index.ts +251 -137
- package/dist/chunk-HY7ZLHJB.js.map +0 -1
- package/dist/chunk-JXN5WZFQ.js.map +0 -1
- package/dist/chunk-KUH345EY.js.map +0 -1
- package/dist/chunk-PVQZBKN7.js.map +0 -1
- package/dist/index-BCJclX6q.d.ts +0 -615
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
+
DisposableStore,
|
|
2
3
|
MCPClientManager
|
|
3
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-ZMMHNOMZ.js";
|
|
4
5
|
import {
|
|
5
6
|
DurableObjectOAuthClientProvider
|
|
6
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-C2OEBJZ2.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,
|
|
@@ -327,13 +356,15 @@ var _Agent = class _Agent extends Server {
|
|
|
327
356
|
const servers = this.sql`
|
|
328
357
|
SELECT id, name, server_url, client_id, auth_url, callback_url, server_options FROM cf_agents_mcp_servers;
|
|
329
358
|
`;
|
|
330
|
-
this.
|
|
331
|
-
JSON.stringify({
|
|
332
|
-
mcp: this.getMcpServers(),
|
|
333
|
-
type: "cf_agent_mcp_servers"
|
|
334
|
-
})
|
|
335
|
-
);
|
|
359
|
+
this.broadcastMcpServers();
|
|
336
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
|
+
});
|
|
337
368
|
servers.forEach((server) => {
|
|
338
369
|
this._connectToMcpServerInternal(
|
|
339
370
|
server.name,
|
|
@@ -345,27 +376,17 @@ var _Agent = class _Agent extends Server {
|
|
|
345
376
|
oauthClientId: server.client_id ?? void 0
|
|
346
377
|
}
|
|
347
378
|
).then(() => {
|
|
348
|
-
this.
|
|
349
|
-
JSON.stringify({
|
|
350
|
-
mcp: this.getMcpServers(),
|
|
351
|
-
type: "cf_agent_mcp_servers"
|
|
352
|
-
})
|
|
353
|
-
);
|
|
379
|
+
this.broadcastMcpServers();
|
|
354
380
|
}).catch((error) => {
|
|
355
381
|
console.error(
|
|
356
382
|
`Error connecting to MCP server: ${server.name} (${server.server_url})`,
|
|
357
383
|
error
|
|
358
384
|
);
|
|
359
|
-
this.
|
|
360
|
-
JSON.stringify({
|
|
361
|
-
mcp: this.getMcpServers(),
|
|
362
|
-
type: "cf_agent_mcp_servers"
|
|
363
|
-
})
|
|
364
|
-
);
|
|
385
|
+
this.broadcastMcpServers();
|
|
365
386
|
});
|
|
366
387
|
});
|
|
367
388
|
}
|
|
368
|
-
return _onStart();
|
|
389
|
+
return _onStart(props);
|
|
369
390
|
});
|
|
370
391
|
}
|
|
371
392
|
);
|
|
@@ -417,7 +438,6 @@ var _Agent = class _Agent extends Server {
|
|
|
417
438
|
}
|
|
418
439
|
}
|
|
419
440
|
_setStateInternal(state, source = "server") {
|
|
420
|
-
const previousState = this._state;
|
|
421
441
|
this._state = state;
|
|
422
442
|
this.sql`
|
|
423
443
|
INSERT OR REPLACE INTO cf_agents_state (id, state)
|
|
@@ -430,7 +450,7 @@ var _Agent = class _Agent extends Server {
|
|
|
430
450
|
this.broadcast(
|
|
431
451
|
JSON.stringify({
|
|
432
452
|
state,
|
|
433
|
-
type: "cf_agent_state"
|
|
453
|
+
type: "cf_agent_state" /* CF_AGENT_STATE */
|
|
434
454
|
}),
|
|
435
455
|
source !== "server" ? [source.id] : []
|
|
436
456
|
);
|
|
@@ -443,10 +463,7 @@ var _Agent = class _Agent extends Server {
|
|
|
443
463
|
{
|
|
444
464
|
displayMessage: "State updated",
|
|
445
465
|
id: nanoid(),
|
|
446
|
-
payload: {
|
|
447
|
-
previousState,
|
|
448
|
-
state
|
|
449
|
-
},
|
|
466
|
+
payload: {},
|
|
450
467
|
timestamp: Date.now(),
|
|
451
468
|
type: "state:update"
|
|
452
469
|
},
|
|
@@ -563,28 +580,22 @@ var _Agent = class _Agent extends Server {
|
|
|
563
580
|
while (proto && proto !== Object.prototype && depth < 10) {
|
|
564
581
|
const methodNames = Object.getOwnPropertyNames(proto);
|
|
565
582
|
for (const methodName of methodNames) {
|
|
566
|
-
|
|
583
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, methodName);
|
|
584
|
+
if (baseMethods.has(methodName) || methodName.startsWith("_") || !descriptor || !!descriptor.get || typeof descriptor.value !== "function") {
|
|
567
585
|
continue;
|
|
568
586
|
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
wrappedFunction,
|
|
580
|
-
callableMetadata.get(
|
|
581
|
-
this[methodName]
|
|
582
|
-
)
|
|
583
|
-
);
|
|
584
|
-
}
|
|
585
|
-
this.constructor.prototype[methodName] = wrappedFunction;
|
|
586
|
-
}
|
|
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
|
+
);
|
|
587
597
|
}
|
|
598
|
+
this.constructor.prototype[methodName] = wrappedFunction;
|
|
588
599
|
}
|
|
589
600
|
proto = Object.getPrototypeOf(proto);
|
|
590
601
|
depth++;
|
|
@@ -731,7 +742,10 @@ var _Agent = class _Agent extends Server {
|
|
|
731
742
|
{
|
|
732
743
|
displayMessage: `Schedule ${schedule.id} created`,
|
|
733
744
|
id: nanoid(),
|
|
734
|
-
payload:
|
|
745
|
+
payload: {
|
|
746
|
+
callback,
|
|
747
|
+
id
|
|
748
|
+
},
|
|
735
749
|
timestamp: Date.now(),
|
|
736
750
|
type: "schedule:create"
|
|
737
751
|
},
|
|
@@ -866,7 +880,10 @@ var _Agent = class _Agent extends Server {
|
|
|
866
880
|
{
|
|
867
881
|
displayMessage: `Schedule ${id} cancelled`,
|
|
868
882
|
id: nanoid(),
|
|
869
|
-
payload:
|
|
883
|
+
payload: {
|
|
884
|
+
callback: schedule.callback,
|
|
885
|
+
id: schedule.id
|
|
886
|
+
},
|
|
870
887
|
timestamp: Date.now(),
|
|
871
888
|
type: "schedule:cancel"
|
|
872
889
|
},
|
|
@@ -900,6 +917,8 @@ var _Agent = class _Agent extends Server {
|
|
|
900
917
|
this.sql`DROP TABLE IF EXISTS cf_agents_queues`;
|
|
901
918
|
await this.ctx.storage.deleteAlarm();
|
|
902
919
|
await this.ctx.storage.deleteAll();
|
|
920
|
+
this._disposables.dispose();
|
|
921
|
+
await this.mcp.dispose?.();
|
|
903
922
|
this.ctx.abort("destroyed");
|
|
904
923
|
this.observability?.emit(
|
|
905
924
|
{
|
|
@@ -922,14 +941,26 @@ var _Agent = class _Agent extends Server {
|
|
|
922
941
|
/**
|
|
923
942
|
* Connect to a new MCP Server
|
|
924
943
|
*
|
|
944
|
+
* @param serverName Name of the MCP server
|
|
925
945
|
* @param url MCP Server SSE URL
|
|
926
|
-
* @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.
|
|
927
947
|
* @param agentsPrefix agents routing prefix if not using `agents`
|
|
928
|
-
* @param options MCP client and transport
|
|
948
|
+
* @param options MCP client and transport options
|
|
929
949
|
* @returns authUrl
|
|
930
950
|
*/
|
|
931
951
|
async addMcpServer(serverName, url, callbackHost, agentsPrefix = "agents", options) {
|
|
932
|
-
|
|
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`;
|
|
933
964
|
const result = await this._connectToMcpServerInternal(
|
|
934
965
|
serverName,
|
|
935
966
|
url,
|
|
@@ -949,12 +980,7 @@ var _Agent = class _Agent extends Server {
|
|
|
949
980
|
${options ? JSON.stringify(options) : null}
|
|
950
981
|
);
|
|
951
982
|
`;
|
|
952
|
-
this.
|
|
953
|
-
JSON.stringify({
|
|
954
|
-
mcp: this.getMcpServers(),
|
|
955
|
-
type: "cf_agent_mcp_servers"
|
|
956
|
-
})
|
|
957
|
-
);
|
|
983
|
+
this.broadcastMcpServers();
|
|
958
984
|
return result;
|
|
959
985
|
}
|
|
960
986
|
async _connectToMcpServerInternal(_serverName, url, callbackUrl, options, reconnect) {
|
|
@@ -969,6 +995,7 @@ var _Agent = class _Agent extends Server {
|
|
|
969
995
|
authProvider.clientId = reconnect.oauthClientId;
|
|
970
996
|
}
|
|
971
997
|
}
|
|
998
|
+
const transportType = options?.transport?.type ?? "auto";
|
|
972
999
|
let headerTransportOpts = {};
|
|
973
1000
|
if (options?.transport?.headers) {
|
|
974
1001
|
headerTransportOpts = {
|
|
@@ -988,7 +1015,8 @@ var _Agent = class _Agent extends Server {
|
|
|
988
1015
|
reconnect,
|
|
989
1016
|
transport: {
|
|
990
1017
|
...headerTransportOpts,
|
|
991
|
-
authProvider
|
|
1018
|
+
authProvider,
|
|
1019
|
+
type: transportType
|
|
992
1020
|
}
|
|
993
1021
|
});
|
|
994
1022
|
return {
|
|
@@ -999,15 +1027,11 @@ var _Agent = class _Agent extends Server {
|
|
|
999
1027
|
}
|
|
1000
1028
|
async removeMcpServer(id) {
|
|
1001
1029
|
this.mcp.closeConnection(id);
|
|
1030
|
+
this.mcp.unregisterCallbackUrl(id);
|
|
1002
1031
|
this.sql`
|
|
1003
1032
|
DELETE FROM cf_agents_mcp_servers WHERE id = ${id};
|
|
1004
1033
|
`;
|
|
1005
|
-
this.
|
|
1006
|
-
JSON.stringify({
|
|
1007
|
-
mcp: this.getMcpServers(),
|
|
1008
|
-
type: "cf_agent_mcp_servers"
|
|
1009
|
-
})
|
|
1010
|
-
);
|
|
1034
|
+
this.broadcastMcpServers();
|
|
1011
1035
|
}
|
|
1012
1036
|
getMcpServers() {
|
|
1013
1037
|
const mcpState = {
|
|
@@ -1035,6 +1059,36 @@ var _Agent = class _Agent extends Server {
|
|
|
1035
1059
|
}
|
|
1036
1060
|
return mcpState;
|
|
1037
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
|
+
}
|
|
1038
1092
|
};
|
|
1039
1093
|
/**
|
|
1040
1094
|
* Agent configuration options
|
|
@@ -1045,6 +1099,7 @@ _Agent.options = {
|
|
|
1045
1099
|
// default to hibernate
|
|
1046
1100
|
};
|
|
1047
1101
|
var Agent = _Agent;
|
|
1102
|
+
var wrappedClasses = /* @__PURE__ */ new Set();
|
|
1048
1103
|
async function routeAgentRequest(request, env, options) {
|
|
1049
1104
|
const corsHeaders = options?.cors === true ? {
|
|
1050
1105
|
"Access-Control-Allow-Credentials": "true",
|
|
@@ -1223,7 +1278,7 @@ var StreamingResponse = class {
|
|
|
1223
1278
|
id: this._id,
|
|
1224
1279
|
result: chunk,
|
|
1225
1280
|
success: true,
|
|
1226
|
-
type: "rpc"
|
|
1281
|
+
type: "rpc" /* RPC */
|
|
1227
1282
|
};
|
|
1228
1283
|
this._connection.send(JSON.stringify(response));
|
|
1229
1284
|
}
|
|
@@ -1241,7 +1296,7 @@ var StreamingResponse = class {
|
|
|
1241
1296
|
id: this._id,
|
|
1242
1297
|
result: finalChunk,
|
|
1243
1298
|
success: true,
|
|
1244
|
-
type: "rpc"
|
|
1299
|
+
type: "rpc" /* RPC */
|
|
1245
1300
|
};
|
|
1246
1301
|
this._connection.send(JSON.stringify(response));
|
|
1247
1302
|
}
|
|
@@ -1273,6 +1328,7 @@ function isLocalMode() {
|
|
|
1273
1328
|
|
|
1274
1329
|
export {
|
|
1275
1330
|
genericObservability,
|
|
1331
|
+
callable,
|
|
1276
1332
|
unstable_callable,
|
|
1277
1333
|
getCurrentAgent,
|
|
1278
1334
|
Agent,
|
|
@@ -1284,4 +1340,4 @@ export {
|
|
|
1284
1340
|
getAgentByName,
|
|
1285
1341
|
StreamingResponse
|
|
1286
1342
|
};
|
|
1287
|
-
//# sourceMappingURL=chunk-
|
|
1343
|
+
//# sourceMappingURL=chunk-XGMKNUJA.js.map
|