agents 0.0.0-8bc0470 → 0.0.0-8bf3250
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 +31 -5
- package/dist/ai-chat-agent.js +119 -115
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +16 -4
- package/dist/ai-react.js +27 -29
- package/dist/ai-react.js.map +1 -1
- package/dist/{chunk-JR3NW4A7.js → chunk-4CIGD73X.js} +295 -125
- package/dist/chunk-4CIGD73X.js.map +1 -0
- package/dist/chunk-767EASBA.js +106 -0
- package/dist/chunk-767EASBA.js.map +1 -0
- package/dist/{chunk-7VFQNJFK.js → chunk-E3LCYPCB.js} +42 -35
- package/dist/chunk-E3LCYPCB.js.map +1 -0
- package/dist/chunk-NKZZ66QY.js +116 -0
- package/dist/chunk-NKZZ66QY.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 +103 -9
- package/dist/index.js +4 -3
- package/dist/mcp/client.d.ts +293 -23
- 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 +16 -7
- package/dist/mcp/index.js +134 -167
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +85 -5
- package/dist/react.js +20 -8
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +2 -2
- package/dist/schedule.js +4 -6
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/package.json +70 -71
- package/src/index.ts +378 -77
- 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-4CIGD73X.js";
|
|
4
|
+
import "../chunk-E3LCYPCB.js";
|
|
5
|
+
import "../chunk-767EASBA.js";
|
|
6
|
+
import "../chunk-NKZZ66QY.js";
|
|
11
7
|
|
|
12
8
|
// src/mcp/index.ts
|
|
13
9
|
import { DurableObject } from "cloudflare:workers";
|
|
@@ -20,40 +16,41 @@ import {
|
|
|
20
16
|
JSONRPCMessageSchema
|
|
21
17
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
22
18
|
var MAXIMUM_MESSAGE_SIZE_BYTES = 4 * 1024 * 1024;
|
|
23
|
-
function corsHeaders(
|
|
19
|
+
function corsHeaders(_request, corsOptions = {}) {
|
|
24
20
|
const origin = "*";
|
|
25
21
|
return {
|
|
26
|
-
"Access-Control-Allow-
|
|
22
|
+
"Access-Control-Allow-Headers": corsOptions.headers || "Content-Type, mcp-session-id, mcp-protocol-version",
|
|
27
23
|
"Access-Control-Allow-Methods": corsOptions.methods || "GET, POST, OPTIONS",
|
|
28
|
-
"Access-Control-Allow-
|
|
29
|
-
"Access-Control-
|
|
30
|
-
"Access-Control-
|
|
24
|
+
"Access-Control-Allow-Origin": corsOptions.origin || origin,
|
|
25
|
+
"Access-Control-Expose-Headers": corsOptions.exposeHeaders || "mcp-session-id",
|
|
26
|
+
"Access-Control-Max-Age": (corsOptions.maxAge || 86400).toString()
|
|
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,27 @@ 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
|
}
|
|
144
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: overriden later
|
|
171
145
|
onStateUpdate(state, source) {
|
|
172
146
|
}
|
|
173
147
|
async onStart() {
|
|
174
148
|
var _a;
|
|
175
149
|
const self = this;
|
|
176
|
-
|
|
150
|
+
this._agent = new (_a = class extends Agent {
|
|
177
151
|
constructor() {
|
|
178
152
|
super(...arguments);
|
|
179
153
|
this.initialState = self.initialState;
|
|
@@ -186,22 +160,22 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
186
160
|
}
|
|
187
161
|
}, _a.options = {
|
|
188
162
|
hibernate: true
|
|
189
|
-
}, _a)(this.ctx, this.env)
|
|
163
|
+
}, _a)(this.ctx, this.env);
|
|
190
164
|
this.props = await this.ctx.storage.get("props");
|
|
191
|
-
|
|
165
|
+
this._transportType = await this.ctx.storage.get(
|
|
192
166
|
"transportType"
|
|
193
|
-
)
|
|
167
|
+
);
|
|
194
168
|
await this._init(this.props);
|
|
195
169
|
const server = await this.server;
|
|
196
|
-
if (
|
|
197
|
-
|
|
198
|
-
await server.connect(
|
|
199
|
-
} else if (
|
|
200
|
-
|
|
170
|
+
if (this._transportType === "sse") {
|
|
171
|
+
this._transport = new McpSSETransport(() => this.getWebSocket());
|
|
172
|
+
await server.connect(this._transport);
|
|
173
|
+
} else if (this._transportType === "streamable-http") {
|
|
174
|
+
this._transport = new McpStreamableHttpTransport(
|
|
201
175
|
(id) => this.getWebSocketForResponseID(id),
|
|
202
|
-
(id) =>
|
|
203
|
-
)
|
|
204
|
-
await server.connect(
|
|
176
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
177
|
+
);
|
|
178
|
+
await server.connect(this._transport);
|
|
205
179
|
}
|
|
206
180
|
}
|
|
207
181
|
async _init(props) {
|
|
@@ -221,10 +195,17 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
221
195
|
async isInitialized() {
|
|
222
196
|
return await this.ctx.storage.get("initialized") === true;
|
|
223
197
|
}
|
|
198
|
+
async _initialize() {
|
|
199
|
+
await this.ctx.blockConcurrencyWhile(async () => {
|
|
200
|
+
this._status = "starting";
|
|
201
|
+
await this.onStart();
|
|
202
|
+
this._status = "started";
|
|
203
|
+
});
|
|
204
|
+
}
|
|
224
205
|
// Allow the worker to fetch a websocket connection to the agent
|
|
225
206
|
async fetch(request) {
|
|
226
|
-
if (
|
|
227
|
-
await
|
|
207
|
+
if (this._status !== "started") {
|
|
208
|
+
await this._initialize();
|
|
228
209
|
}
|
|
229
210
|
if (request.headers.get("Upgrade") !== "websocket") {
|
|
230
211
|
return new Response("Expected WebSocket Upgrade request", {
|
|
@@ -241,24 +222,24 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
241
222
|
return new Response("Websocket already connected", { status: 400 });
|
|
242
223
|
}
|
|
243
224
|
await this.ctx.storage.put("transportType", "sse");
|
|
244
|
-
|
|
245
|
-
if (!
|
|
246
|
-
|
|
247
|
-
await server.connect(
|
|
225
|
+
this._transportType = "sse";
|
|
226
|
+
if (!this._transport) {
|
|
227
|
+
this._transport = new McpSSETransport(() => this.getWebSocket());
|
|
228
|
+
await server.connect(this._transport);
|
|
248
229
|
}
|
|
249
|
-
return
|
|
230
|
+
return this._agent.fetch(request);
|
|
250
231
|
}
|
|
251
232
|
case "/streamable-http": {
|
|
252
|
-
if (!
|
|
253
|
-
|
|
233
|
+
if (!this._transport) {
|
|
234
|
+
this._transport = new McpStreamableHttpTransport(
|
|
254
235
|
(id) => this.getWebSocketForResponseID(id),
|
|
255
|
-
(id) =>
|
|
256
|
-
)
|
|
257
|
-
await server.connect(
|
|
236
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
237
|
+
);
|
|
238
|
+
await server.connect(this._transport);
|
|
258
239
|
}
|
|
259
240
|
await this.ctx.storage.put("transportType", "streamable-http");
|
|
260
|
-
|
|
261
|
-
return
|
|
241
|
+
this._transportType = "streamable-http";
|
|
242
|
+
return this._agent.fetch(request);
|
|
262
243
|
}
|
|
263
244
|
default:
|
|
264
245
|
return new Response(
|
|
@@ -277,19 +258,19 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
277
258
|
return websockets[0];
|
|
278
259
|
}
|
|
279
260
|
getWebSocketForResponseID(id) {
|
|
280
|
-
const connectionId =
|
|
261
|
+
const connectionId = this._requestIdToConnectionId.get(id);
|
|
281
262
|
if (connectionId === void 0) {
|
|
282
263
|
return null;
|
|
283
264
|
}
|
|
284
|
-
return
|
|
265
|
+
return this._agent.getConnection(connectionId) ?? null;
|
|
285
266
|
}
|
|
286
267
|
// All messages received here. This is currently never called
|
|
287
268
|
async onMessage(connection, event) {
|
|
288
|
-
if (
|
|
269
|
+
if (this._transportType !== "streamable-http") {
|
|
289
270
|
const err = new Error(
|
|
290
271
|
"Internal Server Error: Expected streamable-http protocol"
|
|
291
272
|
);
|
|
292
|
-
|
|
273
|
+
this._transport?.onerror?.(err);
|
|
293
274
|
return;
|
|
294
275
|
}
|
|
295
276
|
let message;
|
|
@@ -297,21 +278,21 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
297
278
|
const data = typeof event === "string" ? event : new TextDecoder().decode(event);
|
|
298
279
|
message = JSONRPCMessageSchema.parse(JSON.parse(data));
|
|
299
280
|
} catch (error) {
|
|
300
|
-
|
|
281
|
+
this._transport?.onerror?.(error);
|
|
301
282
|
return;
|
|
302
283
|
}
|
|
303
284
|
if (isJSONRPCRequest(message)) {
|
|
304
|
-
|
|
285
|
+
this._requestIdToConnectionId.set(message.id.toString(), connection.id);
|
|
305
286
|
}
|
|
306
|
-
|
|
287
|
+
this._transport?.onmessage?.(message);
|
|
307
288
|
}
|
|
308
289
|
// All messages received over SSE after the initial connection has been established
|
|
309
290
|
// will be passed here
|
|
310
|
-
async onSSEMcpMessage(
|
|
311
|
-
if (
|
|
312
|
-
await
|
|
291
|
+
async onSSEMcpMessage(_sessionId, request) {
|
|
292
|
+
if (this._status !== "started") {
|
|
293
|
+
await this._initialize();
|
|
313
294
|
}
|
|
314
|
-
if (
|
|
295
|
+
if (this._transportType !== "sse") {
|
|
315
296
|
return new Error("Internal Server Error: Expected SSE protocol");
|
|
316
297
|
}
|
|
317
298
|
try {
|
|
@@ -320,36 +301,36 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
320
301
|
try {
|
|
321
302
|
parsedMessage = JSONRPCMessageSchema.parse(message);
|
|
322
303
|
} catch (error) {
|
|
323
|
-
|
|
304
|
+
this._transport?.onerror?.(error);
|
|
324
305
|
throw error;
|
|
325
306
|
}
|
|
326
|
-
|
|
307
|
+
this._transport?.onmessage?.(parsedMessage);
|
|
327
308
|
return null;
|
|
328
309
|
} catch (error) {
|
|
329
310
|
console.error("Error forwarding message to SSE:", error);
|
|
330
|
-
|
|
311
|
+
this._transport?.onerror?.(error);
|
|
331
312
|
return error;
|
|
332
313
|
}
|
|
333
314
|
}
|
|
334
315
|
// Delegate all websocket events to the underlying agent
|
|
335
316
|
async webSocketMessage(ws, event) {
|
|
336
|
-
if (
|
|
337
|
-
await
|
|
317
|
+
if (this._status !== "started") {
|
|
318
|
+
await this._initialize();
|
|
338
319
|
}
|
|
339
|
-
return await
|
|
320
|
+
return await this._agent.webSocketMessage(ws, event);
|
|
340
321
|
}
|
|
341
322
|
// WebSocket event handlers for hibernation support
|
|
342
323
|
async webSocketError(ws, error) {
|
|
343
|
-
if (
|
|
344
|
-
await
|
|
324
|
+
if (this._status !== "started") {
|
|
325
|
+
await this._initialize();
|
|
345
326
|
}
|
|
346
|
-
return await
|
|
327
|
+
return await this._agent.webSocketError(ws, error);
|
|
347
328
|
}
|
|
348
329
|
async webSocketClose(ws, code, reason, wasClean) {
|
|
349
|
-
if (
|
|
350
|
-
await
|
|
330
|
+
if (this._status !== "started") {
|
|
331
|
+
await this._initialize();
|
|
351
332
|
}
|
|
352
|
-
return await
|
|
333
|
+
return await this._agent.webSocketClose(ws, code, reason, wasClean);
|
|
353
334
|
}
|
|
354
335
|
static mount(path, {
|
|
355
336
|
binding = "MCP_OBJECT",
|
|
@@ -379,7 +360,7 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
379
360
|
);
|
|
380
361
|
return new Response("Invalid binding", { status: 500 });
|
|
381
362
|
}
|
|
382
|
-
if (bindingValue
|
|
363
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
383
364
|
return new Response("Invalid binding", { status: 500 });
|
|
384
365
|
}
|
|
385
366
|
const namespace = bindingValue;
|
|
@@ -440,10 +421,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
440
421
|
onMessage(event).catch(console.error);
|
|
441
422
|
});
|
|
442
423
|
ws.addEventListener("error", (error) => {
|
|
443
|
-
async function onError(
|
|
424
|
+
async function onError(_error) {
|
|
444
425
|
try {
|
|
445
426
|
await writer.close();
|
|
446
|
-
} catch (
|
|
427
|
+
} catch (_e) {
|
|
447
428
|
}
|
|
448
429
|
}
|
|
449
430
|
onError(error).catch(console.error);
|
|
@@ -460,9 +441,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
460
441
|
});
|
|
461
442
|
return new Response(readable, {
|
|
462
443
|
headers: {
|
|
463
|
-
"Content-Type": "text/event-stream",
|
|
464
444
|
"Cache-Control": "no-cache",
|
|
465
445
|
Connection: "keep-alive",
|
|
446
|
+
"Content-Type": "text/event-stream",
|
|
466
447
|
...corsHeaders(request, corsOptions)
|
|
467
448
|
}
|
|
468
449
|
});
|
|
@@ -498,23 +479,23 @@ data: ${JSON.stringify(result.data)}
|
|
|
498
479
|
const error = await doStub.onSSEMcpMessage(sessionId, request);
|
|
499
480
|
if (error) {
|
|
500
481
|
return new Response(error.message, {
|
|
501
|
-
status: 400,
|
|
502
482
|
headers: {
|
|
503
|
-
"Content-Type": "text/event-stream",
|
|
504
483
|
"Cache-Control": "no-cache",
|
|
505
484
|
Connection: "keep-alive",
|
|
485
|
+
"Content-Type": "text/event-stream",
|
|
506
486
|
...corsHeaders(request, corsOptions)
|
|
507
|
-
}
|
|
487
|
+
},
|
|
488
|
+
status: 400
|
|
508
489
|
});
|
|
509
490
|
}
|
|
510
491
|
return new Response("Accepted", {
|
|
511
|
-
status: 202,
|
|
512
492
|
headers: {
|
|
513
|
-
"Content-Type": "text/event-stream",
|
|
514
493
|
"Cache-Control": "no-cache",
|
|
515
494
|
Connection: "keep-alive",
|
|
495
|
+
"Content-Type": "text/event-stream",
|
|
516
496
|
...corsHeaders(request, corsOptions)
|
|
517
|
-
}
|
|
497
|
+
},
|
|
498
|
+
status: 202
|
|
518
499
|
});
|
|
519
500
|
}
|
|
520
501
|
return new Response("Not Found", { status: 404 });
|
|
@@ -544,7 +525,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
544
525
|
);
|
|
545
526
|
return new Response("Invalid binding", { status: 500 });
|
|
546
527
|
}
|
|
547
|
-
if (bindingValue
|
|
528
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
548
529
|
return new Response("Invalid binding", { status: 500 });
|
|
549
530
|
}
|
|
550
531
|
const namespace = bindingValue;
|
|
@@ -552,24 +533,24 @@ data: ${JSON.stringify(result.data)}
|
|
|
552
533
|
const acceptHeader = request.headers.get("accept");
|
|
553
534
|
if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
|
|
554
535
|
const body2 = JSON.stringify({
|
|
555
|
-
jsonrpc: "2.0",
|
|
556
536
|
error: {
|
|
557
537
|
code: -32e3,
|
|
558
538
|
message: "Not Acceptable: Client must accept both application/json and text/event-stream"
|
|
559
539
|
},
|
|
560
|
-
id: null
|
|
540
|
+
id: null,
|
|
541
|
+
jsonrpc: "2.0"
|
|
561
542
|
});
|
|
562
543
|
return new Response(body2, { status: 406 });
|
|
563
544
|
}
|
|
564
545
|
const ct = request.headers.get("content-type");
|
|
565
546
|
if (!ct || !ct.includes("application/json")) {
|
|
566
547
|
const body2 = JSON.stringify({
|
|
567
|
-
jsonrpc: "2.0",
|
|
568
548
|
error: {
|
|
569
549
|
code: -32e3,
|
|
570
550
|
message: "Unsupported Media Type: Content-Type must be application/json"
|
|
571
551
|
},
|
|
572
|
-
id: null
|
|
552
|
+
id: null,
|
|
553
|
+
jsonrpc: "2.0"
|
|
573
554
|
});
|
|
574
555
|
return new Response(body2, { status: 415 });
|
|
575
556
|
}
|
|
@@ -579,12 +560,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
579
560
|
);
|
|
580
561
|
if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) {
|
|
581
562
|
const body2 = JSON.stringify({
|
|
582
|
-
jsonrpc: "2.0",
|
|
583
563
|
error: {
|
|
584
564
|
code: -32e3,
|
|
585
565
|
message: `Request body too large. Maximum size is ${MAXIMUM_MESSAGE_SIZE_BYTES} bytes`
|
|
586
566
|
},
|
|
587
|
-
id: null
|
|
567
|
+
id: null,
|
|
568
|
+
jsonrpc: "2.0"
|
|
588
569
|
});
|
|
589
570
|
return new Response(body2, { status: 413 });
|
|
590
571
|
}
|
|
@@ -592,14 +573,14 @@ data: ${JSON.stringify(result.data)}
|
|
|
592
573
|
let rawMessage;
|
|
593
574
|
try {
|
|
594
575
|
rawMessage = await request.json();
|
|
595
|
-
} catch (
|
|
576
|
+
} catch (_error) {
|
|
596
577
|
const body2 = JSON.stringify({
|
|
597
|
-
jsonrpc: "2.0",
|
|
598
578
|
error: {
|
|
599
579
|
code: -32700,
|
|
600
580
|
message: "Parse error: Invalid JSON"
|
|
601
581
|
},
|
|
602
|
-
id: null
|
|
582
|
+
id: null,
|
|
583
|
+
jsonrpc: "2.0"
|
|
603
584
|
});
|
|
604
585
|
return new Response(body2, { status: 400 });
|
|
605
586
|
}
|
|
@@ -613,12 +594,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
613
594
|
for (const msg of arrayMessage) {
|
|
614
595
|
if (!JSONRPCMessageSchema.safeParse(msg).success) {
|
|
615
596
|
const body2 = JSON.stringify({
|
|
616
|
-
jsonrpc: "2.0",
|
|
617
597
|
error: {
|
|
618
598
|
code: -32700,
|
|
619
599
|
message: "Parse error: Invalid JSON-RPC message"
|
|
620
600
|
},
|
|
621
|
-
id: null
|
|
601
|
+
id: null,
|
|
602
|
+
jsonrpc: "2.0"
|
|
622
603
|
});
|
|
623
604
|
return new Response(body2, { status: 400 });
|
|
624
605
|
}
|
|
@@ -629,34 +610,34 @@ data: ${JSON.stringify(result.data)}
|
|
|
629
610
|
);
|
|
630
611
|
if (isInitializationRequest && sessionId) {
|
|
631
612
|
const body2 = JSON.stringify({
|
|
632
|
-
jsonrpc: "2.0",
|
|
633
613
|
error: {
|
|
634
614
|
code: -32600,
|
|
635
615
|
message: "Invalid Request: Initialization requests must not include a sessionId"
|
|
636
616
|
},
|
|
637
|
-
id: null
|
|
617
|
+
id: null,
|
|
618
|
+
jsonrpc: "2.0"
|
|
638
619
|
});
|
|
639
620
|
return new Response(body2, { status: 400 });
|
|
640
621
|
}
|
|
641
622
|
if (isInitializationRequest && messages.length > 1) {
|
|
642
623
|
const body2 = JSON.stringify({
|
|
643
|
-
jsonrpc: "2.0",
|
|
644
624
|
error: {
|
|
645
625
|
code: -32600,
|
|
646
626
|
message: "Invalid Request: Only one initialization request is allowed"
|
|
647
627
|
},
|
|
648
|
-
id: null
|
|
628
|
+
id: null,
|
|
629
|
+
jsonrpc: "2.0"
|
|
649
630
|
});
|
|
650
631
|
return new Response(body2, { status: 400 });
|
|
651
632
|
}
|
|
652
633
|
if (!isInitializationRequest && !sessionId) {
|
|
653
634
|
const body2 = JSON.stringify({
|
|
654
|
-
jsonrpc: "2.0",
|
|
655
635
|
error: {
|
|
656
636
|
code: -32e3,
|
|
657
637
|
message: "Bad Request: Mcp-Session-Id header is required"
|
|
658
638
|
},
|
|
659
|
-
id: null
|
|
639
|
+
id: null,
|
|
640
|
+
jsonrpc: "2.0"
|
|
660
641
|
});
|
|
661
642
|
return new Response(body2, { status: 400 });
|
|
662
643
|
}
|
|
@@ -669,12 +650,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
669
650
|
await doStub.setInitialized();
|
|
670
651
|
} else if (!isInitialized) {
|
|
671
652
|
const body2 = JSON.stringify({
|
|
672
|
-
jsonrpc: "2.0",
|
|
673
653
|
error: {
|
|
674
654
|
code: -32001,
|
|
675
655
|
message: "Session not found"
|
|
676
656
|
},
|
|
677
|
-
id: null
|
|
657
|
+
id: null,
|
|
658
|
+
jsonrpc: "2.0"
|
|
678
659
|
});
|
|
679
660
|
return new Response(body2, { status: 404 });
|
|
680
661
|
}
|
|
@@ -697,12 +678,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
697
678
|
console.error("Failed to establish WebSocket connection");
|
|
698
679
|
await writer.close();
|
|
699
680
|
const body2 = JSON.stringify({
|
|
700
|
-
jsonrpc: "2.0",
|
|
701
681
|
error: {
|
|
702
682
|
code: -32001,
|
|
703
683
|
message: "Failed to establish WebSocket connection"
|
|
704
684
|
},
|
|
705
|
-
id: null
|
|
685
|
+
id: null,
|
|
686
|
+
jsonrpc: "2.0"
|
|
706
687
|
});
|
|
707
688
|
return new Response(body2, { status: 500 });
|
|
708
689
|
}
|
|
@@ -735,10 +716,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
735
716
|
onMessage(event).catch(console.error);
|
|
736
717
|
});
|
|
737
718
|
ws.addEventListener("error", (error) => {
|
|
738
|
-
async function onError(
|
|
719
|
+
async function onError(_error) {
|
|
739
720
|
try {
|
|
740
721
|
await writer.close();
|
|
741
|
-
} catch (
|
|
722
|
+
} catch (_e) {
|
|
742
723
|
}
|
|
743
724
|
}
|
|
744
725
|
onError(error).catch(console.error);
|
|
@@ -762,8 +743,8 @@ data: ${JSON.stringify(result.data)}
|
|
|
762
743
|
}
|
|
763
744
|
ws.close();
|
|
764
745
|
return new Response(null, {
|
|
765
|
-
|
|
766
|
-
|
|
746
|
+
headers: corsHeaders(request, corsOptions),
|
|
747
|
+
status: 202
|
|
767
748
|
});
|
|
768
749
|
}
|
|
769
750
|
for (const message of messages) {
|
|
@@ -774,9 +755,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
774
755
|
}
|
|
775
756
|
return new Response(readable, {
|
|
776
757
|
headers: {
|
|
777
|
-
"Content-Type": "text/event-stream",
|
|
778
758
|
"Cache-Control": "no-cache",
|
|
779
759
|
Connection: "keep-alive",
|
|
760
|
+
"Content-Type": "text/event-stream",
|
|
780
761
|
"mcp-session-id": sessionId,
|
|
781
762
|
...corsHeaders(request, corsOptions)
|
|
782
763
|
},
|
|
@@ -784,32 +765,18 @@ data: ${JSON.stringify(result.data)}
|
|
|
784
765
|
});
|
|
785
766
|
}
|
|
786
767
|
const body = JSON.stringify({
|
|
787
|
-
jsonrpc: "2.0",
|
|
788
768
|
error: {
|
|
789
769
|
code: -32e3,
|
|
790
770
|
message: "Method not allowed"
|
|
791
771
|
},
|
|
792
|
-
id: null
|
|
772
|
+
id: null,
|
|
773
|
+
jsonrpc: "2.0"
|
|
793
774
|
});
|
|
794
775
|
return new Response(body, { status: 405 });
|
|
795
776
|
}
|
|
796
777
|
};
|
|
797
778
|
}
|
|
798
779
|
};
|
|
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
780
|
export {
|
|
814
781
|
McpAgent
|
|
815
782
|
};
|