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