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