agents 0.0.0-c5e3a32 → 0.0.0-c69f616
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 +106 -0
- package/dist/ai-chat-agent.d.ts +5 -3
- package/dist/ai-chat-agent.js +2 -2
- package/dist/ai-react.d.ts +5 -3
- package/dist/{chunk-MW5BQ2FW.js → chunk-HY7ZLHJB.js} +146 -17
- package/dist/chunk-HY7ZLHJB.js.map +1 -0
- package/dist/{chunk-EDUDXISR.js → chunk-OJFA7RKX.js} +136 -14
- package/dist/chunk-OJFA7RKX.js.map +1 -0
- package/dist/client-CH-eFIfq.d.ts +4457 -0
- package/dist/{index-DukU3sIa.d.ts → index-BVoermIz.d.ts} +46 -2
- package/dist/index.d.ts +6 -3
- package/dist/index.js +2 -2
- package/dist/mcp/client.d.ts +9 -1053
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/index.d.ts +21 -4
- package/dist/mcp/index.js +132 -7
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +5 -3
- package/dist/observability/index.js +2 -2
- package/dist/react.d.ts +5 -3
- package/dist/schedule.d.ts +4 -4
- package/package.json +2 -2
- package/src/index.ts +184 -21
- package/dist/chunk-EDUDXISR.js.map +0 -1
- package/dist/chunk-MW5BQ2FW.js.map +0 -1
package/README.md
CHANGED
|
@@ -393,6 +393,112 @@ This creates:
|
|
|
393
393
|
- Intuitive input handling
|
|
394
394
|
- Easy conversation reset
|
|
395
395
|
|
|
396
|
+
### 🔗 MCP (Model Context Protocol) Integration
|
|
397
|
+
|
|
398
|
+
Agents can seamlessly integrate with the Model Context Protocol, allowing them to act as both MCP servers (providing tools to AI assistants) and MCP clients (using tools from other services).
|
|
399
|
+
|
|
400
|
+
#### Creating an MCP Server
|
|
401
|
+
|
|
402
|
+
```typescript
|
|
403
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
404
|
+
import { McpAgent } from "agents/mcp";
|
|
405
|
+
import { z } from "zod";
|
|
406
|
+
|
|
407
|
+
type Env = {
|
|
408
|
+
MyMCP: DurableObjectNamespace<MyMCP>;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
type State = { counter: number };
|
|
412
|
+
|
|
413
|
+
export class MyMCP extends McpAgent<Env, State, {}> {
|
|
414
|
+
server = new McpServer({
|
|
415
|
+
name: "Demo",
|
|
416
|
+
version: "1.0.0"
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
initialState: State = {
|
|
420
|
+
counter: 1
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
async init() {
|
|
424
|
+
this.server.resource("counter", "mcp://resource/counter", (uri) => {
|
|
425
|
+
return {
|
|
426
|
+
contents: [{ text: String(this.state.counter), uri: uri.href }]
|
|
427
|
+
};
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
this.server.tool(
|
|
431
|
+
"add",
|
|
432
|
+
"Add to the counter, stored in the MCP",
|
|
433
|
+
{ a: z.number() },
|
|
434
|
+
async ({ a }) => {
|
|
435
|
+
this.setState({ ...this.state, counter: this.state.counter + a });
|
|
436
|
+
|
|
437
|
+
return {
|
|
438
|
+
content: [
|
|
439
|
+
{
|
|
440
|
+
text: String(`Added ${a}, total is now ${this.state.counter}`),
|
|
441
|
+
type: "text"
|
|
442
|
+
}
|
|
443
|
+
]
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
onStateUpdate(state: State) {
|
|
450
|
+
console.log({ stateUpdate: state });
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// HTTP Streamable transport (recommended)
|
|
455
|
+
export default MyMCP.serve("/mcp", {
|
|
456
|
+
binding: "MyMCP"
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
// Or SSE transport for legacy compatibility
|
|
460
|
+
// export default MyMCP.serveSSE("/mcp", { binding: "MyMCP" });
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
#### Using MCP Tools
|
|
464
|
+
|
|
465
|
+
```typescript
|
|
466
|
+
import { MCPClientManager } from "agents/mcp";
|
|
467
|
+
|
|
468
|
+
const client = new MCPClientManager("my-app", "1.0.0");
|
|
469
|
+
|
|
470
|
+
// Connect to an MCP server
|
|
471
|
+
await client.connect("https://weather-service.com/mcp", {
|
|
472
|
+
transport: { type: "streamable-http" }
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
// Use tools from the server
|
|
476
|
+
const weather = await client.callTool({
|
|
477
|
+
serverId: "weather-service",
|
|
478
|
+
name: "getWeather",
|
|
479
|
+
arguments: { location: "San Francisco" }
|
|
480
|
+
});
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
#### AI SDK Integration
|
|
484
|
+
|
|
485
|
+
```typescript
|
|
486
|
+
import { generateText } from "ai";
|
|
487
|
+
|
|
488
|
+
// Convert MCP tools for AI use
|
|
489
|
+
const result = await generateText({
|
|
490
|
+
model: openai("gpt-4"),
|
|
491
|
+
tools: client.unstable_getAITools(),
|
|
492
|
+
prompt: "What's the weather in Tokyo?"
|
|
493
|
+
});
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
**Transport Options:**
|
|
497
|
+
|
|
498
|
+
- **Auto**: Automatically determine the correct transport
|
|
499
|
+
- **HTTP Streamable**: Best performance, batch requests, session management
|
|
500
|
+
- **SSE**: Simple setup, legacy compatibility
|
|
501
|
+
|
|
396
502
|
### 💬 The Path Forward
|
|
397
503
|
|
|
398
504
|
We're developing new dimensions of agent capability:
|
package/dist/ai-chat-agent.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { Message, StreamTextOnFinishCallback, ToolSet } from "ai";
|
|
2
|
-
import { A as Agent, a as AgentContext } from "./index-
|
|
2
|
+
import { A as Agent, a as AgentContext } from "./index-BVoermIz.js";
|
|
3
3
|
import { Connection, WSMessage } from "partyserver";
|
|
4
|
+
import "cloudflare:workers";
|
|
4
5
|
import "@modelcontextprotocol/sdk/client/index.js";
|
|
5
6
|
import "@modelcontextprotocol/sdk/types.js";
|
|
6
|
-
import "./
|
|
7
|
+
import "./client-CH-eFIfq.js";
|
|
7
8
|
import "zod";
|
|
8
|
-
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
9
9
|
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
10
|
+
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
11
|
+
import "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
10
12
|
import "./mcp/do-oauth-client-provider.js";
|
|
11
13
|
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
12
14
|
import "@modelcontextprotocol/sdk/shared/auth.js";
|
package/dist/ai-chat-agent.js
CHANGED
package/dist/ai-react.d.ts
CHANGED
|
@@ -4,14 +4,16 @@ import { useChat } from "@ai-sdk/react";
|
|
|
4
4
|
import { useAgent } from "./react.js";
|
|
5
5
|
import "partysocket";
|
|
6
6
|
import "partysocket/react";
|
|
7
|
-
import "./index-
|
|
7
|
+
import "./index-BVoermIz.js";
|
|
8
|
+
import "cloudflare:workers";
|
|
8
9
|
import "@modelcontextprotocol/sdk/client/index.js";
|
|
9
10
|
import "@modelcontextprotocol/sdk/types.js";
|
|
10
11
|
import "partyserver";
|
|
11
|
-
import "./
|
|
12
|
+
import "./client-CH-eFIfq.js";
|
|
12
13
|
import "zod";
|
|
13
|
-
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
14
14
|
import "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
15
|
+
import "@modelcontextprotocol/sdk/client/sse.js";
|
|
16
|
+
import "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
15
17
|
import "./mcp/do-oauth-client-provider.js";
|
|
16
18
|
import "@modelcontextprotocol/sdk/client/auth.js";
|
|
17
19
|
import "@modelcontextprotocol/sdk/shared/auth.js";
|
|
@@ -7,7 +7,8 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
|
7
7
|
import {
|
|
8
8
|
PromptListChangedNotificationSchema,
|
|
9
9
|
ResourceListChangedNotificationSchema,
|
|
10
|
-
ToolListChangedNotificationSchema
|
|
10
|
+
ToolListChangedNotificationSchema,
|
|
11
|
+
ElicitRequestSchema
|
|
11
12
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
12
13
|
|
|
13
14
|
// src/mcp/sse-edge.ts
|
|
@@ -57,6 +58,56 @@ var SSEEdgeClientTransport = class extends SSEClientTransport {
|
|
|
57
58
|
}
|
|
58
59
|
};
|
|
59
60
|
|
|
61
|
+
// src/mcp/streamable-http-edge.ts
|
|
62
|
+
import {
|
|
63
|
+
StreamableHTTPClientTransport
|
|
64
|
+
} from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
65
|
+
var StreamableHTTPEdgeClientTransport = class extends StreamableHTTPClientTransport {
|
|
66
|
+
/**
|
|
67
|
+
* Creates a new StreamableHTTPEdgeClientTransport, which overrides fetch to be compatible with the CF workers environment
|
|
68
|
+
*/
|
|
69
|
+
constructor(url, options) {
|
|
70
|
+
const fetchOverride = async (fetchUrl, fetchInit = {}) => {
|
|
71
|
+
const headers = await this.authHeaders();
|
|
72
|
+
const workerOptions = {
|
|
73
|
+
...fetchInit,
|
|
74
|
+
headers: {
|
|
75
|
+
...options.requestInit?.headers,
|
|
76
|
+
...fetchInit?.headers,
|
|
77
|
+
...headers
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
delete workerOptions.mode;
|
|
81
|
+
return (
|
|
82
|
+
// @ts-expect-error Custom fetch function for Cloudflare Workers compatibility
|
|
83
|
+
options.requestInit?.fetch?.(
|
|
84
|
+
fetchUrl,
|
|
85
|
+
workerOptions
|
|
86
|
+
) || fetch(fetchUrl, workerOptions)
|
|
87
|
+
);
|
|
88
|
+
};
|
|
89
|
+
super(url, {
|
|
90
|
+
...options,
|
|
91
|
+
requestInit: {
|
|
92
|
+
...options.requestInit,
|
|
93
|
+
// @ts-expect-error Custom fetch override for Cloudflare Workers
|
|
94
|
+
fetch: fetchOverride
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
this.authProvider = options.authProvider;
|
|
98
|
+
}
|
|
99
|
+
async authHeaders() {
|
|
100
|
+
if (this.authProvider) {
|
|
101
|
+
const tokens = await this.authProvider.tokens();
|
|
102
|
+
if (tokens) {
|
|
103
|
+
return {
|
|
104
|
+
Authorization: `Bearer ${tokens.access_token}`
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
60
111
|
// src/mcp/client-connection.ts
|
|
61
112
|
var MCPClientConnection = class {
|
|
62
113
|
constructor(url, info, options = { client: {}, transport: {} }) {
|
|
@@ -67,7 +118,14 @@ var MCPClientConnection = class {
|
|
|
67
118
|
this.prompts = [];
|
|
68
119
|
this.resources = [];
|
|
69
120
|
this.resourceTemplates = [];
|
|
70
|
-
|
|
121
|
+
const clientOptions = {
|
|
122
|
+
...options.client,
|
|
123
|
+
capabilities: {
|
|
124
|
+
...options.client?.capabilities,
|
|
125
|
+
elicitation: {}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
this.client = new Client(info, clientOptions);
|
|
71
129
|
}
|
|
72
130
|
/**
|
|
73
131
|
* Initialize a client connection
|
|
@@ -77,14 +135,8 @@ var MCPClientConnection = class {
|
|
|
77
135
|
*/
|
|
78
136
|
async init(code) {
|
|
79
137
|
try {
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
this.options.transport
|
|
83
|
-
);
|
|
84
|
-
if (code) {
|
|
85
|
-
await transport.finishAuth(code);
|
|
86
|
-
}
|
|
87
|
-
await this.client.connect(transport);
|
|
138
|
+
const transportType = this.options.transport.type || "streamable-http";
|
|
139
|
+
await this.tryConnect(transportType, code);
|
|
88
140
|
} catch (e) {
|
|
89
141
|
if (e.toString().includes("Unauthorized")) {
|
|
90
142
|
this.connectionState = "authenticating";
|
|
@@ -98,18 +150,36 @@ var MCPClientConnection = class {
|
|
|
98
150
|
if (!this.serverCapabilities) {
|
|
99
151
|
throw new Error("The MCP Server failed to return server capabilities");
|
|
100
152
|
}
|
|
101
|
-
const [
|
|
153
|
+
const [
|
|
154
|
+
instructionsResult,
|
|
155
|
+
toolsResult,
|
|
156
|
+
resourcesResult,
|
|
157
|
+
promptsResult,
|
|
158
|
+
resourceTemplatesResult
|
|
159
|
+
] = await Promise.allSettled([
|
|
102
160
|
this.client.getInstructions(),
|
|
103
161
|
this.registerTools(),
|
|
104
162
|
this.registerResources(),
|
|
105
163
|
this.registerPrompts(),
|
|
106
164
|
this.registerResourceTemplates()
|
|
107
165
|
]);
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
166
|
+
const operations = [
|
|
167
|
+
{ name: "instructions", result: instructionsResult },
|
|
168
|
+
{ name: "tools", result: toolsResult },
|
|
169
|
+
{ name: "resources", result: resourcesResult },
|
|
170
|
+
{ name: "prompts", result: promptsResult },
|
|
171
|
+
{ name: "resource templates", result: resourceTemplatesResult }
|
|
172
|
+
];
|
|
173
|
+
for (const { name, result } of operations) {
|
|
174
|
+
if (result.status === "rejected") {
|
|
175
|
+
console.error(`Failed to initialize ${name}:`, result.reason);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
this.instructions = instructionsResult.status === "fulfilled" ? instructionsResult.value : void 0;
|
|
179
|
+
this.tools = toolsResult.status === "fulfilled" ? toolsResult.value : [];
|
|
180
|
+
this.resources = resourcesResult.status === "fulfilled" ? resourcesResult.value : [];
|
|
181
|
+
this.prompts = promptsResult.status === "fulfilled" ? promptsResult.value : [];
|
|
182
|
+
this.resourceTemplates = resourceTemplatesResult.status === "fulfilled" ? resourceTemplatesResult.value : [];
|
|
113
183
|
this.connectionState = "ready";
|
|
114
184
|
}
|
|
115
185
|
/**
|
|
@@ -214,6 +284,63 @@ var MCPClientConnection = class {
|
|
|
214
284
|
} while (templatesResult.nextCursor);
|
|
215
285
|
return templatesAgg;
|
|
216
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* Handle elicitation request from server
|
|
289
|
+
* Automatically uses the Agent's built-in elicitation handling if available
|
|
290
|
+
*/
|
|
291
|
+
async handleElicitationRequest(_request) {
|
|
292
|
+
throw new Error(
|
|
293
|
+
"Elicitation handler must be implemented for your platform. Override handleElicitationRequest method."
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Get the transport for the client
|
|
298
|
+
* @param transportType - The transport type to get
|
|
299
|
+
* @returns The transport for the client
|
|
300
|
+
*/
|
|
301
|
+
getTransport(transportType) {
|
|
302
|
+
switch (transportType) {
|
|
303
|
+
case "streamable-http":
|
|
304
|
+
return new StreamableHTTPEdgeClientTransport(
|
|
305
|
+
this.url,
|
|
306
|
+
this.options.transport
|
|
307
|
+
);
|
|
308
|
+
case "sse":
|
|
309
|
+
return new SSEEdgeClientTransport(
|
|
310
|
+
this.url,
|
|
311
|
+
this.options.transport
|
|
312
|
+
);
|
|
313
|
+
default:
|
|
314
|
+
throw new Error(`Unsupported transport type: ${transportType}`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async tryConnect(transportType, code) {
|
|
318
|
+
const transports = transportType === "auto" ? ["streamable-http", "sse"] : [transportType];
|
|
319
|
+
for (const currentTransportType of transports) {
|
|
320
|
+
const isLastTransport = currentTransportType === transports[transports.length - 1];
|
|
321
|
+
const hasFallback = transportType === "auto" && currentTransportType === "streamable-http" && !isLastTransport;
|
|
322
|
+
const transport = await this.getTransport(currentTransportType);
|
|
323
|
+
if (code) {
|
|
324
|
+
await transport.finishAuth(code);
|
|
325
|
+
}
|
|
326
|
+
try {
|
|
327
|
+
await this.client.connect(transport);
|
|
328
|
+
break;
|
|
329
|
+
} catch (e) {
|
|
330
|
+
const error = e instanceof Error ? e : new Error(String(e));
|
|
331
|
+
if (hasFallback && (error.message.includes("404") || error.message.includes("405"))) {
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
throw e;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
this.client.setRequestHandler(
|
|
338
|
+
ElicitRequestSchema,
|
|
339
|
+
async (request) => {
|
|
340
|
+
return await this.handleElicitationRequest(request);
|
|
341
|
+
}
|
|
342
|
+
);
|
|
343
|
+
}
|
|
217
344
|
};
|
|
218
345
|
function capabilityErrorHandler(empty, method) {
|
|
219
346
|
return (e) => {
|
|
@@ -463,7 +590,9 @@ function getNamespacedData(mcpClients, type) {
|
|
|
463
590
|
}
|
|
464
591
|
|
|
465
592
|
export {
|
|
593
|
+
SSEEdgeClientTransport,
|
|
594
|
+
StreamableHTTPEdgeClientTransport,
|
|
466
595
|
MCPClientManager,
|
|
467
596
|
getNamespacedData
|
|
468
597
|
};
|
|
469
|
-
//# sourceMappingURL=chunk-
|
|
598
|
+
//# sourceMappingURL=chunk-HY7ZLHJB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/mcp/client.ts","../src/mcp/client-connection.ts","../src/mcp/sse-edge.ts","../src/mcp/streamable-http-edge.ts"],"sourcesContent":["import type { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport type { RequestOptions } from \"@modelcontextprotocol/sdk/shared/protocol.js\";\nimport type {\n CallToolRequest,\n CallToolResultSchema,\n CompatibilityCallToolResultSchema,\n GetPromptRequest,\n Prompt,\n ReadResourceRequest,\n Resource,\n ResourceTemplate,\n Tool\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { type ToolSet, jsonSchema } from \"ai\";\nimport { nanoid } from \"nanoid\";\nimport {\n MCPClientConnection,\n type MCPTransportOptions\n} from \"./client-connection\";\n\n/**\n * Utility class that aggregates multiple MCP clients into one\n */\nexport class MCPClientManager {\n public mcpConnections: Record<string, MCPClientConnection> = {};\n private _callbackUrls: string[] = [];\n\n /**\n * @param _name Name of the MCP client\n * @param _version Version of the MCP Client\n * @param auth Auth paramters if being used to create a DurableObjectOAuthClientProvider\n */\n constructor(\n private _name: string,\n private _version: string\n ) {}\n\n /**\n * Connect to and register an MCP server\n *\n * @param transportConfig Transport config\n * @param clientConfig Client config\n * @param capabilities Client capabilities (i.e. if the client supports roots/sampling)\n */\n async connect(\n url: string,\n options: {\n // Allows you to reconnect to a server (in the case of an auth reconnect)\n reconnect?: {\n // server id\n id: string;\n oauthClientId?: string;\n oauthCode?: string;\n };\n // we're overriding authProvider here because we want to be able to access the auth URL\n transport?: MCPTransportOptions;\n client?: ConstructorParameters<typeof Client>[1];\n } = {}\n ): Promise<{\n id: string;\n authUrl?: string;\n clientId?: string;\n }> {\n const id = options.reconnect?.id ?? nanoid(8);\n\n if (!options.transport?.authProvider) {\n console.warn(\n \"No authProvider provided in the transport options. This client will only support unauthenticated remote MCP Servers\"\n );\n } else {\n options.transport.authProvider.serverId = id;\n // reconnect with auth\n if (options.reconnect?.oauthClientId) {\n options.transport.authProvider.clientId =\n options.reconnect?.oauthClientId;\n }\n }\n\n this.mcpConnections[id] = new MCPClientConnection(\n new URL(url),\n {\n name: this._name,\n version: this._version\n },\n {\n client: options.client ?? {},\n transport: options.transport ?? {}\n }\n );\n\n await this.mcpConnections[id].init(options.reconnect?.oauthCode);\n\n const authUrl = options.transport?.authProvider?.authUrl;\n if (authUrl && options.transport?.authProvider?.redirectUrl) {\n this._callbackUrls.push(\n options.transport.authProvider.redirectUrl.toString()\n );\n return {\n authUrl,\n clientId: options.transport?.authProvider?.clientId,\n id\n };\n }\n\n return {\n id\n };\n }\n\n isCallbackRequest(req: Request): boolean {\n return (\n req.method === \"GET\" &&\n !!this._callbackUrls.find((url) => {\n return req.url.startsWith(url);\n })\n );\n }\n\n async handleCallbackRequest(req: Request) {\n const url = new URL(req.url);\n const urlMatch = this._callbackUrls.find((url) => {\n return req.url.startsWith(url);\n });\n if (!urlMatch) {\n throw new Error(\n `No callback URI match found for the request url: ${req.url}. Was the request matched with \\`isCallbackRequest()\\`?`\n );\n }\n const code = url.searchParams.get(\"code\");\n const clientId = url.searchParams.get(\"state\");\n const urlParams = urlMatch.split(\"/\");\n const serverId = urlParams[urlParams.length - 1];\n if (!code) {\n throw new Error(\"Unauthorized: no code provided\");\n }\n if (!clientId) {\n throw new Error(\"Unauthorized: no state provided\");\n }\n\n if (this.mcpConnections[serverId] === undefined) {\n throw new Error(`Could not find serverId: ${serverId}`);\n }\n\n if (this.mcpConnections[serverId].connectionState !== \"authenticating\") {\n throw new Error(\n \"Failed to authenticate: the client isn't in the `authenticating` state\"\n );\n }\n\n const conn = this.mcpConnections[serverId];\n if (!conn.options.transport.authProvider) {\n throw new Error(\n \"Trying to finalize authentication for a server connection without an authProvider\"\n );\n }\n\n conn.options.transport.authProvider.clientId = clientId;\n conn.options.transport.authProvider.serverId = serverId;\n\n // reconnect to server with authorization\n const serverUrl = conn.url.toString();\n await this.connect(serverUrl, {\n reconnect: {\n id: serverId,\n oauthClientId: clientId,\n oauthCode: code\n },\n ...conn.options\n });\n\n if (this.mcpConnections[serverId].connectionState === \"authenticating\") {\n throw new Error(\"Failed to authenticate: client failed to initialize\");\n }\n\n return { serverId };\n }\n\n /**\n * @returns namespaced list of tools\n */\n listTools(): NamespacedData[\"tools\"] {\n return getNamespacedData(this.mcpConnections, \"tools\");\n }\n\n /**\n * @returns a set of tools that you can use with the AI SDK\n */\n unstable_getAITools(): ToolSet {\n return Object.fromEntries(\n getNamespacedData(this.mcpConnections, \"tools\").map((tool) => {\n return [\n `${tool.serverId}_${tool.name}`,\n {\n description: tool.description,\n execute: async (args) => {\n const result = await this.callTool({\n arguments: args,\n name: tool.name,\n serverId: tool.serverId\n });\n if (result.isError) {\n // @ts-expect-error TODO we should fix this\n throw new Error(result.content[0].text);\n }\n return result;\n },\n parameters: jsonSchema(tool.inputSchema)\n }\n ];\n })\n );\n }\n\n /**\n * Closes all connections to MCP servers\n */\n async closeAllConnections() {\n return Promise.all(\n Object.values(this.mcpConnections).map(async (connection) => {\n await connection.client.close();\n })\n );\n }\n\n /**\n * Closes a connection to an MCP server\n * @param id The id of the connection to close\n */\n async closeConnection(id: string) {\n if (!this.mcpConnections[id]) {\n throw new Error(`Connection with id \"${id}\" does not exist.`);\n }\n await this.mcpConnections[id].client.close();\n delete this.mcpConnections[id];\n }\n\n /**\n * @returns namespaced list of prompts\n */\n listPrompts(): NamespacedData[\"prompts\"] {\n return getNamespacedData(this.mcpConnections, \"prompts\");\n }\n\n /**\n * @returns namespaced list of tools\n */\n listResources(): NamespacedData[\"resources\"] {\n return getNamespacedData(this.mcpConnections, \"resources\");\n }\n\n /**\n * @returns namespaced list of resource templates\n */\n listResourceTemplates(): NamespacedData[\"resourceTemplates\"] {\n return getNamespacedData(this.mcpConnections, \"resourceTemplates\");\n }\n\n /**\n * Namespaced version of callTool\n */\n callTool(\n params: CallToolRequest[\"params\"] & { serverId: string },\n resultSchema?:\n | typeof CallToolResultSchema\n | typeof CompatibilityCallToolResultSchema,\n options?: RequestOptions\n ) {\n const unqualifiedName = params.name.replace(`${params.serverId}.`, \"\");\n return this.mcpConnections[params.serverId].client.callTool(\n {\n ...params,\n name: unqualifiedName\n },\n resultSchema,\n options\n );\n }\n\n /**\n * Namespaced version of readResource\n */\n readResource(\n params: ReadResourceRequest[\"params\"] & { serverId: string },\n options: RequestOptions\n ) {\n return this.mcpConnections[params.serverId].client.readResource(\n params,\n options\n );\n }\n\n /**\n * Namespaced version of getPrompt\n */\n getPrompt(\n params: GetPromptRequest[\"params\"] & { serverId: string },\n options: RequestOptions\n ) {\n return this.mcpConnections[params.serverId].client.getPrompt(\n params,\n options\n );\n }\n}\n\ntype NamespacedData = {\n tools: (Tool & { serverId: string })[];\n prompts: (Prompt & { serverId: string })[];\n resources: (Resource & { serverId: string })[];\n resourceTemplates: (ResourceTemplate & { serverId: string })[];\n};\n\nexport function getNamespacedData<T extends keyof NamespacedData>(\n mcpClients: Record<string, MCPClientConnection>,\n type: T\n): NamespacedData[T] {\n const sets = Object.entries(mcpClients).map(([name, conn]) => {\n return { data: conn[type], name };\n });\n\n const namespacedData = sets.flatMap(({ name: serverId, data }) => {\n return data.map((item) => {\n return {\n ...item,\n // we add a serverId so we can easily pull it out and send the tool call to the right server\n serverId\n };\n });\n });\n\n return namespacedData as NamespacedData[T]; // Type assertion needed due to TS limitations with conditional return types\n}\n","import { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport type { SSEClientTransportOptions } from \"@modelcontextprotocol/sdk/client/sse.js\";\nimport type { StreamableHTTPClientTransportOptions } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport {\n type ClientCapabilities,\n type ListPromptsResult,\n type ListResourceTemplatesResult,\n type ListResourcesResult,\n type ListToolsResult,\n type Prompt,\n PromptListChangedNotificationSchema,\n type Resource,\n ResourceListChangedNotificationSchema,\n type ResourceTemplate,\n type ServerCapabilities,\n type Tool,\n ToolListChangedNotificationSchema,\n ElicitRequestSchema,\n type ElicitRequest,\n type ElicitResult\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport type { AgentsOAuthProvider } from \"./do-oauth-client-provider\";\nimport { SSEEdgeClientTransport } from \"./sse-edge\";\nimport { StreamableHTTPEdgeClientTransport } from \"./streamable-http-edge\";\n\nexport type MCPTransportOptions = (\n | SSEClientTransportOptions\n | StreamableHTTPClientTransportOptions\n) & {\n authProvider?: AgentsOAuthProvider;\n type?: \"sse\" | \"streamable-http\" | \"auto\";\n};\n\ntype TransportType = Exclude<MCPTransportOptions[\"type\"], \"auto\">;\n\nexport class MCPClientConnection {\n client: Client;\n connectionState:\n | \"authenticating\"\n | \"connecting\"\n | \"ready\"\n | \"discovering\"\n | \"failed\" = \"connecting\";\n instructions?: string;\n tools: Tool[] = [];\n prompts: Prompt[] = [];\n resources: Resource[] = [];\n resourceTemplates: ResourceTemplate[] = [];\n serverCapabilities: ServerCapabilities | undefined;\n\n constructor(\n public url: URL,\n info: ConstructorParameters<typeof Client>[0],\n public options: {\n transport: MCPTransportOptions;\n client: ConstructorParameters<typeof Client>[1];\n } = { client: {}, transport: {} }\n ) {\n const clientOptions = {\n ...options.client,\n capabilities: {\n ...options.client?.capabilities,\n elicitation: {}\n } as ClientCapabilities\n };\n\n this.client = new Client(info, clientOptions);\n }\n\n /**\n * Initialize a client connection\n *\n * @param code Optional OAuth code to initialize the connection with if auth hasn't been initialized\n * @returns\n */\n async init(code?: string) {\n try {\n const transportType = this.options.transport.type || \"streamable-http\";\n await this.tryConnect(transportType, code);\n // biome-ignore lint/suspicious/noExplicitAny: allow for the error check here\n } catch (e: any) {\n if (e.toString().includes(\"Unauthorized\")) {\n // unauthorized, we should wait for the user to authenticate\n this.connectionState = \"authenticating\";\n return;\n }\n this.connectionState = \"failed\";\n throw e;\n }\n\n this.connectionState = \"discovering\";\n\n this.serverCapabilities = await this.client.getServerCapabilities();\n if (!this.serverCapabilities) {\n throw new Error(\"The MCP Server failed to return server capabilities\");\n }\n\n const [\n instructionsResult,\n toolsResult,\n resourcesResult,\n promptsResult,\n resourceTemplatesResult\n ] = await Promise.allSettled([\n this.client.getInstructions(),\n this.registerTools(),\n this.registerResources(),\n this.registerPrompts(),\n this.registerResourceTemplates()\n ]);\n\n const operations = [\n { name: \"instructions\", result: instructionsResult },\n { name: \"tools\", result: toolsResult },\n { name: \"resources\", result: resourcesResult },\n { name: \"prompts\", result: promptsResult },\n { name: \"resource templates\", result: resourceTemplatesResult }\n ];\n\n for (const { name, result } of operations) {\n if (result.status === \"rejected\") {\n console.error(`Failed to initialize ${name}:`, result.reason);\n }\n }\n\n this.instructions =\n instructionsResult.status === \"fulfilled\"\n ? instructionsResult.value\n : undefined;\n this.tools = toolsResult.status === \"fulfilled\" ? toolsResult.value : [];\n this.resources =\n resourcesResult.status === \"fulfilled\" ? resourcesResult.value : [];\n this.prompts =\n promptsResult.status === \"fulfilled\" ? promptsResult.value : [];\n this.resourceTemplates =\n resourceTemplatesResult.status === \"fulfilled\"\n ? resourceTemplatesResult.value\n : [];\n\n this.connectionState = \"ready\";\n }\n\n /**\n * Notification handler registration\n */\n async registerTools(): Promise<Tool[]> {\n if (!this.serverCapabilities || !this.serverCapabilities.tools) {\n return [];\n }\n\n if (this.serverCapabilities.tools.listChanged) {\n this.client.setNotificationHandler(\n ToolListChangedNotificationSchema,\n async (_notification) => {\n this.tools = await this.fetchTools();\n }\n );\n }\n\n return this.fetchTools();\n }\n\n async registerResources(): Promise<Resource[]> {\n if (!this.serverCapabilities || !this.serverCapabilities.resources) {\n return [];\n }\n\n if (this.serverCapabilities.resources.listChanged) {\n this.client.setNotificationHandler(\n ResourceListChangedNotificationSchema,\n async (_notification) => {\n this.resources = await this.fetchResources();\n }\n );\n }\n\n return this.fetchResources();\n }\n\n async registerPrompts(): Promise<Prompt[]> {\n if (!this.serverCapabilities || !this.serverCapabilities.prompts) {\n return [];\n }\n\n if (this.serverCapabilities.prompts.listChanged) {\n this.client.setNotificationHandler(\n PromptListChangedNotificationSchema,\n async (_notification) => {\n this.prompts = await this.fetchPrompts();\n }\n );\n }\n\n return this.fetchPrompts();\n }\n\n async registerResourceTemplates(): Promise<ResourceTemplate[]> {\n if (!this.serverCapabilities || !this.serverCapabilities.resources) {\n return [];\n }\n\n return this.fetchResourceTemplates();\n }\n\n async fetchTools() {\n let toolsAgg: Tool[] = [];\n let toolsResult: ListToolsResult = { tools: [] };\n do {\n toolsResult = await this.client\n .listTools({\n cursor: toolsResult.nextCursor\n })\n .catch(capabilityErrorHandler({ tools: [] }, \"tools/list\"));\n toolsAgg = toolsAgg.concat(toolsResult.tools);\n } while (toolsResult.nextCursor);\n return toolsAgg;\n }\n\n async fetchResources() {\n let resourcesAgg: Resource[] = [];\n let resourcesResult: ListResourcesResult = { resources: [] };\n do {\n resourcesResult = await this.client\n .listResources({\n cursor: resourcesResult.nextCursor\n })\n .catch(capabilityErrorHandler({ resources: [] }, \"resources/list\"));\n resourcesAgg = resourcesAgg.concat(resourcesResult.resources);\n } while (resourcesResult.nextCursor);\n return resourcesAgg;\n }\n\n async fetchPrompts() {\n let promptsAgg: Prompt[] = [];\n let promptsResult: ListPromptsResult = { prompts: [] };\n do {\n promptsResult = await this.client\n .listPrompts({\n cursor: promptsResult.nextCursor\n })\n .catch(capabilityErrorHandler({ prompts: [] }, \"prompts/list\"));\n promptsAgg = promptsAgg.concat(promptsResult.prompts);\n } while (promptsResult.nextCursor);\n return promptsAgg;\n }\n\n async fetchResourceTemplates() {\n let templatesAgg: ResourceTemplate[] = [];\n let templatesResult: ListResourceTemplatesResult = {\n resourceTemplates: []\n };\n do {\n templatesResult = await this.client\n .listResourceTemplates({\n cursor: templatesResult.nextCursor\n })\n .catch(\n capabilityErrorHandler(\n { resourceTemplates: [] },\n \"resources/templates/list\"\n )\n );\n templatesAgg = templatesAgg.concat(templatesResult.resourceTemplates);\n } while (templatesResult.nextCursor);\n return templatesAgg;\n }\n\n /**\n * Handle elicitation request from server\n * Automatically uses the Agent's built-in elicitation handling if available\n */\n async handleElicitationRequest(\n _request: ElicitRequest\n ): Promise<ElicitResult> {\n // Elicitation handling must be implemented by the platform\n // For MCP servers, this should be handled by McpAgent.elicitInput()\n throw new Error(\n \"Elicitation handler must be implemented for your platform. Override handleElicitationRequest method.\"\n );\n }\n /**\n * Get the transport for the client\n * @param transportType - The transport type to get\n * @returns The transport for the client\n */\n getTransport(transportType: TransportType) {\n switch (transportType) {\n case \"streamable-http\":\n return new StreamableHTTPEdgeClientTransport(\n this.url,\n this.options.transport as StreamableHTTPClientTransportOptions\n );\n case \"sse\":\n return new SSEEdgeClientTransport(\n this.url,\n this.options.transport as SSEClientTransportOptions\n );\n default:\n throw new Error(`Unsupported transport type: ${transportType}`);\n }\n }\n\n async tryConnect(transportType: MCPTransportOptions[\"type\"], code?: string) {\n const transports: TransportType[] =\n transportType === \"auto\" ? [\"streamable-http\", \"sse\"] : [transportType];\n\n for (const currentTransportType of transports) {\n const isLastTransport =\n currentTransportType === transports[transports.length - 1];\n const hasFallback =\n transportType === \"auto\" &&\n currentTransportType === \"streamable-http\" &&\n !isLastTransport;\n\n const transport = await this.getTransport(currentTransportType);\n\n if (code) {\n await transport.finishAuth(code);\n }\n\n try {\n await this.client.connect(transport);\n break;\n } catch (e) {\n const error = e instanceof Error ? e : new Error(String(e));\n\n if (\n hasFallback &&\n (error.message.includes(\"404\") || error.message.includes(\"405\"))\n ) {\n // try the next transport if we have a fallback\n continue;\n }\n\n throw e;\n }\n }\n\n // Set up elicitation request handler\n this.client.setRequestHandler(\n ElicitRequestSchema,\n async (request: ElicitRequest) => {\n return await this.handleElicitationRequest(request);\n }\n );\n }\n}\n\nfunction capabilityErrorHandler<T>(empty: T, method: string) {\n return (e: { code: number }) => {\n // server is badly behaved and returning invalid capabilities. This commonly occurs for resource templates\n if (e.code === -32601) {\n console.error(\n `The server advertised support for the capability ${method.split(\"/\")[0]}, but returned \"Method not found\" for '${method}'.`\n );\n return empty;\n }\n throw e;\n };\n}\n","import type { OAuthClientProvider } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport {\n SSEClientTransport,\n type SSEClientTransportOptions\n} from \"@modelcontextprotocol/sdk/client/sse.js\";\n\nexport class SSEEdgeClientTransport extends SSEClientTransport {\n private authProvider: OAuthClientProvider | undefined;\n /**\n * Creates a new EdgeSSEClientTransport, which overrides fetch to be compatible with the CF workers environment\n */\n constructor(url: URL, options: SSEClientTransportOptions) {\n const fetchOverride: typeof fetch = async (\n fetchUrl: RequestInfo | URL,\n fetchInit: RequestInit = {}\n ) => {\n // add auth headers\n const headers = await this.authHeaders();\n const workerOptions = {\n ...fetchInit,\n headers: {\n ...options.requestInit?.headers,\n ...fetchInit?.headers,\n ...headers\n }\n };\n\n // Remove unsupported properties\n delete workerOptions.mode;\n\n // Call the original fetch with fixed options\n return (\n (options.eventSourceInit?.fetch?.(\n fetchUrl as URL | string,\n // @ts-expect-error Expects FetchLikeInit from EventSource but is compatible with RequestInit\n workerOptions\n ) as Promise<Response>) || fetch(fetchUrl, workerOptions)\n );\n };\n\n super(url, {\n ...options,\n eventSourceInit: {\n ...options.eventSourceInit,\n fetch: fetchOverride\n }\n });\n this.authProvider = options.authProvider;\n }\n\n async authHeaders() {\n if (this.authProvider) {\n const tokens = await this.authProvider.tokens();\n if (tokens) {\n return {\n Authorization: `Bearer ${tokens.access_token}`\n };\n }\n }\n }\n}\n","import type { OAuthClientProvider } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport {\n StreamableHTTPClientTransport,\n type StreamableHTTPClientTransportOptions\n} from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\n\nexport class StreamableHTTPEdgeClientTransport extends StreamableHTTPClientTransport {\n private authProvider: OAuthClientProvider | undefined;\n\n /**\n * Creates a new StreamableHTTPEdgeClientTransport, which overrides fetch to be compatible with the CF workers environment\n */\n constructor(url: URL, options: StreamableHTTPClientTransportOptions) {\n const fetchOverride: typeof fetch = async (\n fetchUrl: RequestInfo | URL,\n fetchInit: RequestInit = {}\n ) => {\n // add auth headers\n const headers = await this.authHeaders();\n const workerOptions = {\n ...fetchInit,\n headers: {\n ...options.requestInit?.headers,\n ...fetchInit?.headers,\n ...headers\n }\n };\n\n // Remove unsupported properties\n delete workerOptions.mode;\n\n // Call the original fetch with fixed options\n return (\n // @ts-expect-error Custom fetch function for Cloudflare Workers compatibility\n (options.requestInit?.fetch?.(\n fetchUrl as URL | string,\n workerOptions\n ) as Promise<Response>) || fetch(fetchUrl, workerOptions)\n );\n };\n\n super(url, {\n ...options,\n requestInit: {\n ...options.requestInit,\n // @ts-expect-error Custom fetch override for Cloudflare Workers\n fetch: fetchOverride\n }\n });\n this.authProvider = options.authProvider;\n }\n\n async authHeaders() {\n if (this.authProvider) {\n const tokens = await this.authProvider.tokens();\n if (tokens) {\n return {\n Authorization: `Bearer ${tokens.access_token}`\n };\n }\n }\n }\n}\n"],"mappings":";AAaA,SAAuB,kBAAkB;AACzC,SAAS,cAAc;;;ACdvB,SAAS,cAAc;AAGvB;AAAA,EAOE;AAAA,EAEA;AAAA,EAIA;AAAA,EACA;AAAA,OAGK;;;ACnBP;AAAA,EACE;AAAA,OAEK;AAEA,IAAM,yBAAN,cAAqC,mBAAmB;AAAA;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A,MACF;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAED,SAAO;AACT;","names":["url"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MCPClientManager
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-HY7ZLHJB.js";
|
|
4
4
|
import {
|
|
5
5
|
DurableObjectOAuthClientProvider
|
|
6
6
|
} from "./chunk-PVQZBKN7.js";
|
|
@@ -76,6 +76,7 @@ var _Agent = class _Agent extends Server {
|
|
|
76
76
|
* The observability implementation to use for the Agent
|
|
77
77
|
*/
|
|
78
78
|
this.observability = genericObservability;
|
|
79
|
+
this._flushingQueue = false;
|
|
79
80
|
/**
|
|
80
81
|
* Method called when an alarm fires.
|
|
81
82
|
* Executes any scheduled tasks that are due.
|
|
@@ -136,13 +137,21 @@ var _Agent = class _Agent extends Server {
|
|
|
136
137
|
}
|
|
137
138
|
await this._scheduleNextAlarm();
|
|
138
139
|
};
|
|
140
|
+
this._autoWrapCustomMethods();
|
|
139
141
|
this.sql`
|
|
140
142
|
CREATE TABLE IF NOT EXISTS cf_agents_state (
|
|
141
143
|
id TEXT PRIMARY KEY NOT NULL,
|
|
142
144
|
state TEXT
|
|
143
145
|
)
|
|
144
146
|
`;
|
|
145
|
-
this.
|
|
147
|
+
this.sql`
|
|
148
|
+
CREATE TABLE IF NOT EXISTS cf_agents_queues (
|
|
149
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
150
|
+
payload TEXT,
|
|
151
|
+
callback TEXT,
|
|
152
|
+
created_at INTEGER DEFAULT (unixepoch())
|
|
153
|
+
)
|
|
154
|
+
`;
|
|
146
155
|
void this.ctx.blockConcurrencyWhile(async () => {
|
|
147
156
|
return this._tryCatch(async () => {
|
|
148
157
|
this.sql`
|
|
@@ -589,6 +598,108 @@ var _Agent = class _Agent extends Server {
|
|
|
589
598
|
render() {
|
|
590
599
|
throw new Error("Not implemented");
|
|
591
600
|
}
|
|
601
|
+
/**
|
|
602
|
+
* Queue a task to be executed in the future
|
|
603
|
+
* @param payload Payload to pass to the callback
|
|
604
|
+
* @param callback Name of the method to call
|
|
605
|
+
* @returns The ID of the queued task
|
|
606
|
+
*/
|
|
607
|
+
async queue(callback, payload) {
|
|
608
|
+
const id = nanoid(9);
|
|
609
|
+
if (typeof callback !== "string") {
|
|
610
|
+
throw new Error("Callback must be a string");
|
|
611
|
+
}
|
|
612
|
+
if (typeof this[callback] !== "function") {
|
|
613
|
+
throw new Error(`this.${callback} is not a function`);
|
|
614
|
+
}
|
|
615
|
+
this.sql`
|
|
616
|
+
INSERT OR REPLACE INTO cf_agents_queues (id, payload, callback)
|
|
617
|
+
VALUES (${id}, ${JSON.stringify(payload)}, ${callback})
|
|
618
|
+
`;
|
|
619
|
+
void this._flushQueue().catch((e) => {
|
|
620
|
+
console.error("Error flushing queue:", e);
|
|
621
|
+
});
|
|
622
|
+
return id;
|
|
623
|
+
}
|
|
624
|
+
async _flushQueue() {
|
|
625
|
+
if (this._flushingQueue) {
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
this._flushingQueue = true;
|
|
629
|
+
while (true) {
|
|
630
|
+
const result = this.sql`
|
|
631
|
+
SELECT * FROM cf_agents_queues
|
|
632
|
+
ORDER BY created_at ASC
|
|
633
|
+
`;
|
|
634
|
+
if (!result || result.length === 0) {
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
for (const row of result || []) {
|
|
638
|
+
const callback = this[row.callback];
|
|
639
|
+
if (!callback) {
|
|
640
|
+
console.error(`callback ${row.callback} not found`);
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
const { connection, request, email } = agentContext.getStore() || {};
|
|
644
|
+
await agentContext.run(
|
|
645
|
+
{
|
|
646
|
+
agent: this,
|
|
647
|
+
connection,
|
|
648
|
+
request,
|
|
649
|
+
email
|
|
650
|
+
},
|
|
651
|
+
async () => {
|
|
652
|
+
await callback.bind(this)(JSON.parse(row.payload), row);
|
|
653
|
+
await this.dequeue(row.id);
|
|
654
|
+
}
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
this._flushingQueue = false;
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Dequeue a task by ID
|
|
662
|
+
* @param id ID of the task to dequeue
|
|
663
|
+
*/
|
|
664
|
+
async dequeue(id) {
|
|
665
|
+
this.sql`DELETE FROM cf_agents_queues WHERE id = ${id}`;
|
|
666
|
+
}
|
|
667
|
+
/**
|
|
668
|
+
* Dequeue all tasks
|
|
669
|
+
*/
|
|
670
|
+
async dequeueAll() {
|
|
671
|
+
this.sql`DELETE FROM cf_agents_queues`;
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Dequeue all tasks by callback
|
|
675
|
+
* @param callback Name of the callback to dequeue
|
|
676
|
+
*/
|
|
677
|
+
async dequeueAllByCallback(callback) {
|
|
678
|
+
this.sql`DELETE FROM cf_agents_queues WHERE callback = ${callback}`;
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Get a queued task by ID
|
|
682
|
+
* @param id ID of the task to get
|
|
683
|
+
* @returns The task or undefined if not found
|
|
684
|
+
*/
|
|
685
|
+
async getQueue(id) {
|
|
686
|
+
const result = this.sql`
|
|
687
|
+
SELECT * FROM cf_agents_queues WHERE id = ${id}
|
|
688
|
+
`;
|
|
689
|
+
return result ? { ...result[0], payload: JSON.parse(result[0].payload) } : void 0;
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Get all queues by key and value
|
|
693
|
+
* @param key Key to filter by
|
|
694
|
+
* @param value Value to filter by
|
|
695
|
+
* @returns Array of matching QueueItem objects
|
|
696
|
+
*/
|
|
697
|
+
async getQueues(key, value) {
|
|
698
|
+
const result = this.sql`
|
|
699
|
+
SELECT * FROM cf_agents_queues
|
|
700
|
+
`;
|
|
701
|
+
return result.filter((row) => JSON.parse(row.payload)[key] === value);
|
|
702
|
+
}
|
|
592
703
|
/**
|
|
593
704
|
* Schedule a task to be executed in the future
|
|
594
705
|
* @template T Type of the payload data
|
|
@@ -751,9 +862,9 @@ var _Agent = class _Agent extends Server {
|
|
|
751
862
|
}
|
|
752
863
|
async _scheduleNextAlarm() {
|
|
753
864
|
const result = this.sql`
|
|
754
|
-
SELECT time FROM cf_agents_schedules
|
|
865
|
+
SELECT time FROM cf_agents_schedules
|
|
755
866
|
WHERE time > ${Math.floor(Date.now() / 1e3)}
|
|
756
|
-
ORDER BY time ASC
|
|
867
|
+
ORDER BY time ASC
|
|
757
868
|
LIMIT 1
|
|
758
869
|
`;
|
|
759
870
|
if (!result) return;
|
|
@@ -769,6 +880,7 @@ var _Agent = class _Agent extends Server {
|
|
|
769
880
|
this.sql`DROP TABLE IF EXISTS cf_agents_state`;
|
|
770
881
|
this.sql`DROP TABLE IF EXISTS cf_agents_schedules`;
|
|
771
882
|
this.sql`DROP TABLE IF EXISTS cf_agents_mcp_servers`;
|
|
883
|
+
this.sql`DROP TABLE IF EXISTS cf_agents_queues`;
|
|
772
884
|
await this.ctx.storage.deleteAlarm();
|
|
773
885
|
await this.ctx.storage.deleteAll();
|
|
774
886
|
this.ctx.abort("destroyed");
|
|
@@ -1004,25 +1116,35 @@ function createAddressBasedEmailResolver(defaultAgentName) {
|
|
|
1004
1116
|
function createCatchAllEmailResolver(agentName, agentId) {
|
|
1005
1117
|
return async () => ({ agentName, agentId });
|
|
1006
1118
|
}
|
|
1119
|
+
var agentMapCache = /* @__PURE__ */ new WeakMap();
|
|
1007
1120
|
async function routeAgentEmail(email, env, options) {
|
|
1008
1121
|
const routingInfo = await options.resolver(email, env);
|
|
1009
1122
|
if (!routingInfo) {
|
|
1010
1123
|
console.warn("No routing information found for email, dropping message");
|
|
1011
1124
|
return;
|
|
1012
1125
|
}
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1126
|
+
if (!agentMapCache.has(env)) {
|
|
1127
|
+
const map = {};
|
|
1128
|
+
for (const [key, value] of Object.entries(env)) {
|
|
1129
|
+
if (value && typeof value === "object" && "idFromName" in value && typeof value.idFromName === "function") {
|
|
1130
|
+
map[key] = value;
|
|
1131
|
+
map[camelCaseToKebabCase(key)] = value;
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
agentMapCache.set(env, map);
|
|
1018
1135
|
}
|
|
1019
|
-
|
|
1136
|
+
const agentMap = agentMapCache.get(env);
|
|
1137
|
+
const namespace = agentMap[routingInfo.agentName];
|
|
1138
|
+
if (!namespace) {
|
|
1139
|
+
const availableAgents = Object.keys(agentMap).filter((key) => !key.includes("-")).join(", ");
|
|
1020
1140
|
throw new Error(
|
|
1021
|
-
`
|
|
1141
|
+
`Agent namespace '${routingInfo.agentName}' not found in environment. Available agents: ${availableAgents}`
|
|
1022
1142
|
);
|
|
1023
1143
|
}
|
|
1024
|
-
const
|
|
1025
|
-
|
|
1144
|
+
const agent = await getAgentByName(
|
|
1145
|
+
namespace,
|
|
1146
|
+
routingInfo.agentId
|
|
1147
|
+
);
|
|
1026
1148
|
const serialisableEmail = {
|
|
1027
1149
|
getRaw: async () => {
|
|
1028
1150
|
const reader = email.raw.getReader();
|
|
@@ -1145,4 +1267,4 @@ export {
|
|
|
1145
1267
|
getAgentByName,
|
|
1146
1268
|
StreamingResponse
|
|
1147
1269
|
};
|
|
1148
|
-
//# sourceMappingURL=chunk-
|
|
1270
|
+
//# sourceMappingURL=chunk-OJFA7RKX.js.map
|