agents 0.0.0-881f11e → 0.0.0-89b649a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-chat-agent.d.ts +28 -2
- package/dist/ai-chat-agent.js +99 -103
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +12 -0
- package/dist/ai-react.js +0 -2
- package/dist/ai-react.js.map +1 -1
- package/dist/chunk-BZXOAZUX.js +106 -0
- package/dist/chunk-BZXOAZUX.js.map +1 -0
- package/dist/{chunk-JR3NW4A7.js → chunk-MXJNY43J.js} +265 -101
- package/dist/chunk-MXJNY43J.js.map +1 -0
- package/dist/{chunk-7VFQNJFK.js → chunk-OYJXQRRH.js} +26 -23
- package/dist/chunk-OYJXQRRH.js.map +1 -0
- package/dist/chunk-VCSB47AK.js +116 -0
- package/dist/chunk-VCSB47AK.js.map +1 -0
- package/dist/client.d.ts +15 -1
- package/dist/client.js +6 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +97 -4
- package/dist/index.js +4 -3
- package/dist/mcp/client.d.ts +13 -15
- package/dist/mcp/client.js +1 -2
- package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
- package/dist/mcp/do-oauth-client-provider.js +3 -103
- package/dist/mcp/do-oauth-client-provider.js.map +1 -1
- package/dist/mcp/index.d.ts +10 -1
- package/dist/mcp/index.js +90 -124
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +85 -5
- package/dist/react.js +14 -2
- package/dist/react.js.map +1 -1
- package/dist/schedule.js +0 -2
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/package.json +8 -8
- package/src/index.ts +343 -45
- package/dist/chunk-7VFQNJFK.js.map +0 -1
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-JR3NW4A7.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → serializable.js.map} +0 -0
package/dist/mcp/index.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
__privateGet,
|
|
8
|
-
__privateMethod,
|
|
9
|
-
__privateSet
|
|
10
|
-
} from "../chunk-HMLY7DHA.js";
|
|
3
|
+
} from "../chunk-MXJNY43J.js";
|
|
4
|
+
import "../chunk-OYJXQRRH.js";
|
|
5
|
+
import "../chunk-BZXOAZUX.js";
|
|
6
|
+
import "../chunk-VCSB47AK.js";
|
|
11
7
|
|
|
12
8
|
// src/mcp/index.ts
|
|
13
9
|
import { DurableObject } from "cloudflare:workers";
|
|
@@ -30,30 +26,31 @@ function corsHeaders(request, corsOptions = {}) {
|
|
|
30
26
|
"Access-Control-Expose-Headers": corsOptions.exposeHeaders || "mcp-session-id"
|
|
31
27
|
};
|
|
32
28
|
}
|
|
29
|
+
function isDurableObjectNamespace(namespace) {
|
|
30
|
+
return typeof namespace === "object" && namespace !== null && "newUniqueId" in namespace && typeof namespace.newUniqueId === "function" && "idFromName" in namespace && typeof namespace.idFromName === "function";
|
|
31
|
+
}
|
|
33
32
|
function handleCORS(request, corsOptions) {
|
|
34
33
|
if (request.method === "OPTIONS") {
|
|
35
34
|
return new Response(null, { headers: corsHeaders(request, corsOptions) });
|
|
36
35
|
}
|
|
37
36
|
return null;
|
|
38
37
|
}
|
|
39
|
-
var _getWebSocket, _started;
|
|
40
38
|
var McpSSETransport = class {
|
|
41
39
|
constructor(getWebSocket) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
__privateSet(this, _getWebSocket, getWebSocket);
|
|
40
|
+
this._started = false;
|
|
41
|
+
this._getWebSocket = getWebSocket;
|
|
45
42
|
}
|
|
46
43
|
async start() {
|
|
47
|
-
if (
|
|
44
|
+
if (this._started) {
|
|
48
45
|
throw new Error("Transport already started");
|
|
49
46
|
}
|
|
50
|
-
|
|
47
|
+
this._started = true;
|
|
51
48
|
}
|
|
52
49
|
async send(message) {
|
|
53
|
-
if (!
|
|
50
|
+
if (!this._started) {
|
|
54
51
|
throw new Error("Transport not started");
|
|
55
52
|
}
|
|
56
|
-
const websocket =
|
|
53
|
+
const websocket = this._getWebSocket();
|
|
57
54
|
if (!websocket) {
|
|
58
55
|
throw new Error("WebSocket not connected");
|
|
59
56
|
}
|
|
@@ -68,52 +65,40 @@ var McpSSETransport = class {
|
|
|
68
65
|
this.onclose?.();
|
|
69
66
|
}
|
|
70
67
|
};
|
|
71
|
-
_getWebSocket = new WeakMap();
|
|
72
|
-
_started = new WeakMap();
|
|
73
|
-
var _getWebSocketForGetRequest, _getWebSocketForMessageID, _notifyResponseIdSent, _started2;
|
|
74
68
|
var McpStreamableHttpTransport = class {
|
|
75
69
|
constructor(getWebSocketForMessageID, notifyResponseIdSent) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
__privateAdd(this, _getWebSocketForMessageID);
|
|
81
|
-
// Notify the server that a response has been sent for a given message id
|
|
82
|
-
// so that it may clean up it's mapping of message ids to connections
|
|
83
|
-
// once they are no longer needed
|
|
84
|
-
__privateAdd(this, _notifyResponseIdSent);
|
|
85
|
-
__privateAdd(this, _started2, false);
|
|
86
|
-
__privateSet(this, _getWebSocketForMessageID, getWebSocketForMessageID);
|
|
87
|
-
__privateSet(this, _notifyResponseIdSent, notifyResponseIdSent);
|
|
88
|
-
__privateSet(this, _getWebSocketForGetRequest, () => null);
|
|
70
|
+
this._started = false;
|
|
71
|
+
this._getWebSocketForMessageID = getWebSocketForMessageID;
|
|
72
|
+
this._notifyResponseIdSent = notifyResponseIdSent;
|
|
73
|
+
this._getWebSocketForGetRequest = () => null;
|
|
89
74
|
}
|
|
90
75
|
async start() {
|
|
91
|
-
if (
|
|
76
|
+
if (this._started) {
|
|
92
77
|
throw new Error("Transport already started");
|
|
93
78
|
}
|
|
94
|
-
|
|
79
|
+
this._started = true;
|
|
95
80
|
}
|
|
96
81
|
async send(message) {
|
|
97
|
-
if (!
|
|
82
|
+
if (!this._started) {
|
|
98
83
|
throw new Error("Transport not started");
|
|
99
84
|
}
|
|
100
85
|
let websocket = null;
|
|
101
86
|
if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
|
|
102
|
-
websocket =
|
|
87
|
+
websocket = this._getWebSocketForMessageID(message.id.toString());
|
|
103
88
|
if (!websocket) {
|
|
104
89
|
throw new Error(
|
|
105
90
|
`Could not find WebSocket for message id: ${message.id}`
|
|
106
91
|
);
|
|
107
92
|
}
|
|
108
93
|
} else if (isJSONRPCRequest(message)) {
|
|
109
|
-
websocket =
|
|
94
|
+
websocket = this._getWebSocketForGetRequest();
|
|
110
95
|
} else if (isJSONRPCNotification(message)) {
|
|
111
96
|
websocket = null;
|
|
112
97
|
}
|
|
113
98
|
try {
|
|
114
99
|
websocket?.send(JSON.stringify(message));
|
|
115
100
|
if (isJSONRPCResponse(message)) {
|
|
116
|
-
|
|
101
|
+
this._notifyResponseIdSent(message.id.toString());
|
|
117
102
|
}
|
|
118
103
|
} catch (error) {
|
|
119
104
|
this.onerror?.(error);
|
|
@@ -124,28 +109,16 @@ var McpStreamableHttpTransport = class {
|
|
|
124
109
|
this.onclose?.();
|
|
125
110
|
}
|
|
126
111
|
};
|
|
127
|
-
|
|
128
|
-
_getWebSocketForMessageID = new WeakMap();
|
|
129
|
-
_notifyResponseIdSent = new WeakMap();
|
|
130
|
-
_started2 = new WeakMap();
|
|
131
|
-
var _status, _transport, _transportType, _requestIdToConnectionId, _agent, _McpAgent_instances, initialize_fn;
|
|
132
|
-
var _McpAgent = class _McpAgent extends DurableObject {
|
|
112
|
+
var McpAgent = class _McpAgent extends DurableObject {
|
|
133
113
|
constructor(ctx, env) {
|
|
134
114
|
var _a;
|
|
135
115
|
super(ctx, env);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
__privateAdd(this, _transportType, "unset");
|
|
140
|
-
__privateAdd(this, _requestIdToConnectionId, /* @__PURE__ */ new Map());
|
|
141
|
-
/**
|
|
142
|
-
* Since McpAgent's _aren't_ yet real "Agents", let's only expose a couple of the methods
|
|
143
|
-
* to the outer class: initialState/state/setState/onStateUpdate/sql
|
|
144
|
-
*/
|
|
145
|
-
__privateAdd(this, _agent);
|
|
116
|
+
this._status = "zero";
|
|
117
|
+
this._transportType = "unset";
|
|
118
|
+
this._requestIdToConnectionId = /* @__PURE__ */ new Map();
|
|
146
119
|
this.initRun = false;
|
|
147
120
|
const self = this;
|
|
148
|
-
|
|
121
|
+
this._agent = new (_a = class extends Agent {
|
|
149
122
|
onStateUpdate(state, source) {
|
|
150
123
|
return self.onStateUpdate(state, source);
|
|
151
124
|
}
|
|
@@ -154,26 +127,26 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
154
127
|
}
|
|
155
128
|
}, _a.options = {
|
|
156
129
|
hibernate: true
|
|
157
|
-
}, _a)(ctx, env)
|
|
130
|
+
}, _a)(ctx, env);
|
|
158
131
|
}
|
|
159
132
|
get mcp() {
|
|
160
|
-
return
|
|
133
|
+
return this._agent.mcp;
|
|
161
134
|
}
|
|
162
135
|
get state() {
|
|
163
|
-
return
|
|
136
|
+
return this._agent.state;
|
|
164
137
|
}
|
|
165
138
|
sql(strings, ...values) {
|
|
166
|
-
return
|
|
139
|
+
return this._agent.sql(strings, ...values);
|
|
167
140
|
}
|
|
168
141
|
setState(state) {
|
|
169
|
-
return
|
|
142
|
+
return this._agent.setState(state);
|
|
170
143
|
}
|
|
171
144
|
onStateUpdate(state, source) {
|
|
172
145
|
}
|
|
173
146
|
async onStart() {
|
|
174
147
|
var _a;
|
|
175
148
|
const self = this;
|
|
176
|
-
|
|
149
|
+
this._agent = new (_a = class extends Agent {
|
|
177
150
|
constructor() {
|
|
178
151
|
super(...arguments);
|
|
179
152
|
this.initialState = self.initialState;
|
|
@@ -186,22 +159,22 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
186
159
|
}
|
|
187
160
|
}, _a.options = {
|
|
188
161
|
hibernate: true
|
|
189
|
-
}, _a)(this.ctx, this.env)
|
|
162
|
+
}, _a)(this.ctx, this.env);
|
|
190
163
|
this.props = await this.ctx.storage.get("props");
|
|
191
|
-
|
|
164
|
+
this._transportType = await this.ctx.storage.get(
|
|
192
165
|
"transportType"
|
|
193
|
-
)
|
|
166
|
+
);
|
|
194
167
|
await this._init(this.props);
|
|
195
168
|
const server = await this.server;
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
await server.connect(
|
|
199
|
-
} else if (
|
|
200
|
-
|
|
169
|
+
if (this._transportType === "sse") {
|
|
170
|
+
this._transport = new McpSSETransport(() => this.getWebSocket());
|
|
171
|
+
await server.connect(this._transport);
|
|
172
|
+
} else if (this._transportType === "streamable-http") {
|
|
173
|
+
this._transport = new McpStreamableHttpTransport(
|
|
201
174
|
(id) => this.getWebSocketForResponseID(id),
|
|
202
|
-
(id) =>
|
|
203
|
-
)
|
|
204
|
-
await server.connect(
|
|
175
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
176
|
+
);
|
|
177
|
+
await server.connect(this._transport);
|
|
205
178
|
}
|
|
206
179
|
}
|
|
207
180
|
async _init(props) {
|
|
@@ -221,10 +194,17 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
221
194
|
async isInitialized() {
|
|
222
195
|
return await this.ctx.storage.get("initialized") === true;
|
|
223
196
|
}
|
|
197
|
+
async _initialize() {
|
|
198
|
+
await this.ctx.blockConcurrencyWhile(async () => {
|
|
199
|
+
this._status = "starting";
|
|
200
|
+
await this.onStart();
|
|
201
|
+
this._status = "started";
|
|
202
|
+
});
|
|
203
|
+
}
|
|
224
204
|
// Allow the worker to fetch a websocket connection to the agent
|
|
225
205
|
async fetch(request) {
|
|
226
|
-
if (
|
|
227
|
-
await
|
|
206
|
+
if (this._status !== "started") {
|
|
207
|
+
await this._initialize();
|
|
228
208
|
}
|
|
229
209
|
if (request.headers.get("Upgrade") !== "websocket") {
|
|
230
210
|
return new Response("Expected WebSocket Upgrade request", {
|
|
@@ -241,24 +221,24 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
241
221
|
return new Response("Websocket already connected", { status: 400 });
|
|
242
222
|
}
|
|
243
223
|
await this.ctx.storage.put("transportType", "sse");
|
|
244
|
-
|
|
245
|
-
if (!
|
|
246
|
-
|
|
247
|
-
await server.connect(
|
|
224
|
+
this._transportType = "sse";
|
|
225
|
+
if (!this._transport) {
|
|
226
|
+
this._transport = new McpSSETransport(() => this.getWebSocket());
|
|
227
|
+
await server.connect(this._transport);
|
|
248
228
|
}
|
|
249
|
-
return
|
|
229
|
+
return this._agent.fetch(request);
|
|
250
230
|
}
|
|
251
231
|
case "/streamable-http": {
|
|
252
|
-
if (!
|
|
253
|
-
|
|
232
|
+
if (!this._transport) {
|
|
233
|
+
this._transport = new McpStreamableHttpTransport(
|
|
254
234
|
(id) => this.getWebSocketForResponseID(id),
|
|
255
|
-
(id) =>
|
|
256
|
-
)
|
|
257
|
-
await server.connect(
|
|
235
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
236
|
+
);
|
|
237
|
+
await server.connect(this._transport);
|
|
258
238
|
}
|
|
259
239
|
await this.ctx.storage.put("transportType", "streamable-http");
|
|
260
|
-
|
|
261
|
-
return
|
|
240
|
+
this._transportType = "streamable-http";
|
|
241
|
+
return this._agent.fetch(request);
|
|
262
242
|
}
|
|
263
243
|
default:
|
|
264
244
|
return new Response(
|
|
@@ -277,19 +257,19 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
277
257
|
return websockets[0];
|
|
278
258
|
}
|
|
279
259
|
getWebSocketForResponseID(id) {
|
|
280
|
-
const connectionId =
|
|
260
|
+
const connectionId = this._requestIdToConnectionId.get(id);
|
|
281
261
|
if (connectionId === void 0) {
|
|
282
262
|
return null;
|
|
283
263
|
}
|
|
284
|
-
return
|
|
264
|
+
return this._agent.getConnection(connectionId) ?? null;
|
|
285
265
|
}
|
|
286
266
|
// All messages received here. This is currently never called
|
|
287
267
|
async onMessage(connection, event) {
|
|
288
|
-
if (
|
|
268
|
+
if (this._transportType !== "streamable-http") {
|
|
289
269
|
const err = new Error(
|
|
290
270
|
"Internal Server Error: Expected streamable-http protocol"
|
|
291
271
|
);
|
|
292
|
-
|
|
272
|
+
this._transport?.onerror?.(err);
|
|
293
273
|
return;
|
|
294
274
|
}
|
|
295
275
|
let message;
|
|
@@ -297,21 +277,21 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
297
277
|
const data = typeof event === "string" ? event : new TextDecoder().decode(event);
|
|
298
278
|
message = JSONRPCMessageSchema.parse(JSON.parse(data));
|
|
299
279
|
} catch (error) {
|
|
300
|
-
|
|
280
|
+
this._transport?.onerror?.(error);
|
|
301
281
|
return;
|
|
302
282
|
}
|
|
303
283
|
if (isJSONRPCRequest(message)) {
|
|
304
|
-
|
|
284
|
+
this._requestIdToConnectionId.set(message.id.toString(), connection.id);
|
|
305
285
|
}
|
|
306
|
-
|
|
286
|
+
this._transport?.onmessage?.(message);
|
|
307
287
|
}
|
|
308
288
|
// All messages received over SSE after the initial connection has been established
|
|
309
289
|
// will be passed here
|
|
310
290
|
async onSSEMcpMessage(sessionId, request) {
|
|
311
|
-
if (
|
|
312
|
-
await
|
|
291
|
+
if (this._status !== "started") {
|
|
292
|
+
await this._initialize();
|
|
313
293
|
}
|
|
314
|
-
if (
|
|
294
|
+
if (this._transportType !== "sse") {
|
|
315
295
|
return new Error("Internal Server Error: Expected SSE protocol");
|
|
316
296
|
}
|
|
317
297
|
try {
|
|
@@ -320,36 +300,36 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
320
300
|
try {
|
|
321
301
|
parsedMessage = JSONRPCMessageSchema.parse(message);
|
|
322
302
|
} catch (error) {
|
|
323
|
-
|
|
303
|
+
this._transport?.onerror?.(error);
|
|
324
304
|
throw error;
|
|
325
305
|
}
|
|
326
|
-
|
|
306
|
+
this._transport?.onmessage?.(parsedMessage);
|
|
327
307
|
return null;
|
|
328
308
|
} catch (error) {
|
|
329
309
|
console.error("Error forwarding message to SSE:", error);
|
|
330
|
-
|
|
310
|
+
this._transport?.onerror?.(error);
|
|
331
311
|
return error;
|
|
332
312
|
}
|
|
333
313
|
}
|
|
334
314
|
// Delegate all websocket events to the underlying agent
|
|
335
315
|
async webSocketMessage(ws, event) {
|
|
336
|
-
if (
|
|
337
|
-
await
|
|
316
|
+
if (this._status !== "started") {
|
|
317
|
+
await this._initialize();
|
|
338
318
|
}
|
|
339
|
-
return await
|
|
319
|
+
return await this._agent.webSocketMessage(ws, event);
|
|
340
320
|
}
|
|
341
321
|
// WebSocket event handlers for hibernation support
|
|
342
322
|
async webSocketError(ws, error) {
|
|
343
|
-
if (
|
|
344
|
-
await
|
|
323
|
+
if (this._status !== "started") {
|
|
324
|
+
await this._initialize();
|
|
345
325
|
}
|
|
346
|
-
return await
|
|
326
|
+
return await this._agent.webSocketError(ws, error);
|
|
347
327
|
}
|
|
348
328
|
async webSocketClose(ws, code, reason, wasClean) {
|
|
349
|
-
if (
|
|
350
|
-
await
|
|
329
|
+
if (this._status !== "started") {
|
|
330
|
+
await this._initialize();
|
|
351
331
|
}
|
|
352
|
-
return await
|
|
332
|
+
return await this._agent.webSocketClose(ws, code, reason, wasClean);
|
|
353
333
|
}
|
|
354
334
|
static mount(path, {
|
|
355
335
|
binding = "MCP_OBJECT",
|
|
@@ -379,7 +359,7 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
379
359
|
);
|
|
380
360
|
return new Response("Invalid binding", { status: 500 });
|
|
381
361
|
}
|
|
382
|
-
if (bindingValue
|
|
362
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
383
363
|
return new Response("Invalid binding", { status: 500 });
|
|
384
364
|
}
|
|
385
365
|
const namespace = bindingValue;
|
|
@@ -544,7 +524,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
544
524
|
);
|
|
545
525
|
return new Response("Invalid binding", { status: 500 });
|
|
546
526
|
}
|
|
547
|
-
if (bindingValue
|
|
527
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
548
528
|
return new Response("Invalid binding", { status: 500 });
|
|
549
529
|
}
|
|
550
530
|
const namespace = bindingValue;
|
|
@@ -796,20 +776,6 @@ data: ${JSON.stringify(result.data)}
|
|
|
796
776
|
};
|
|
797
777
|
}
|
|
798
778
|
};
|
|
799
|
-
_status = new WeakMap();
|
|
800
|
-
_transport = new WeakMap();
|
|
801
|
-
_transportType = new WeakMap();
|
|
802
|
-
_requestIdToConnectionId = new WeakMap();
|
|
803
|
-
_agent = new WeakMap();
|
|
804
|
-
_McpAgent_instances = new WeakSet();
|
|
805
|
-
initialize_fn = async function() {
|
|
806
|
-
await this.ctx.blockConcurrencyWhile(async () => {
|
|
807
|
-
__privateSet(this, _status, "starting");
|
|
808
|
-
await this.onStart();
|
|
809
|
-
__privateSet(this, _status, "started");
|
|
810
|
-
});
|
|
811
|
-
};
|
|
812
|
-
var McpAgent = _McpAgent;
|
|
813
779
|
export {
|
|
814
780
|
McpAgent
|
|
815
781
|
};
|