agents 0.0.0-eede2bd → 0.0.0-f31397c
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 +22 -22
- package/dist/ai-chat-agent.d.ts +32 -5
- package/dist/ai-chat-agent.js +149 -115
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +18 -5
- package/dist/ai-react.js +27 -29
- package/dist/ai-react.js.map +1 -1
- package/dist/chunk-KUH345EY.js +116 -0
- package/dist/chunk-KUH345EY.js.map +1 -0
- package/dist/chunk-MFNGQLFL.js +1260 -0
- package/dist/chunk-MFNGQLFL.js.map +1 -0
- package/dist/{chunk-Q5ZBHY4Z.js → chunk-MW5BQ2FW.js} +49 -36
- package/dist/chunk-MW5BQ2FW.js.map +1 -0
- package/dist/chunk-PVQZBKN7.js +106 -0
- package/dist/chunk-PVQZBKN7.js.map +1 -0
- package/dist/client.d.ts +16 -2
- package/dist/client.js +6 -126
- package/dist/client.js.map +1 -1
- package/dist/index-BIJvkfYt.d.ts +614 -0
- package/dist/index.d.ts +33 -308
- package/dist/index.js +10 -3
- package/dist/mcp/client.d.ts +301 -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 +20 -10
- package/dist/mcp/index.js +148 -175
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +12 -0
- package/dist/observability/index.js +10 -0
- 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 +6 -6
- 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/dist/serializable.js.map +1 -0
- package/package.json +76 -69
- package/src/index.ts +1059 -137
- package/dist/chunk-5W7ZWKOP.js +0 -617
- package/dist/chunk-5W7ZWKOP.js.map +0 -1
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-Q5ZBHY4Z.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → observability/index.js.map} +0 -0
package/dist/mcp/index.js
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
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-MFNGQLFL.js";
|
|
4
|
+
import "../chunk-MW5BQ2FW.js";
|
|
5
|
+
import "../chunk-PVQZBKN7.js";
|
|
6
|
+
import "../chunk-KUH345EY.js";
|
|
11
7
|
|
|
12
8
|
// src/mcp/index.ts
|
|
13
9
|
import { DurableObject } from "cloudflare:workers";
|
|
14
10
|
import {
|
|
15
11
|
InitializeRequestSchema,
|
|
12
|
+
JSONRPCMessageSchema,
|
|
16
13
|
isJSONRPCError,
|
|
17
14
|
isJSONRPCNotification,
|
|
18
15
|
isJSONRPCRequest,
|
|
19
|
-
isJSONRPCResponse
|
|
20
|
-
JSONRPCMessageSchema
|
|
16
|
+
isJSONRPCResponse
|
|
21
17
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
22
18
|
var MAXIMUM_MESSAGE_SIZE_BYTES = 4 * 1024 * 1024;
|
|
23
|
-
function
|
|
24
|
-
const origin =
|
|
25
|
-
|
|
26
|
-
"Access-Control-Allow-
|
|
27
|
-
"Access-Control-Allow-Methods": corsOptions
|
|
28
|
-
"Access-Control-Allow-
|
|
29
|
-
"Access-Control-
|
|
19
|
+
function corsHeaders(_request, corsOptions = {}) {
|
|
20
|
+
const origin = "*";
|
|
21
|
+
return {
|
|
22
|
+
"Access-Control-Allow-Headers": corsOptions.headers || "Content-Type, mcp-session-id, mcp-protocol-version",
|
|
23
|
+
"Access-Control-Allow-Methods": corsOptions.methods || "GET, POST, OPTIONS",
|
|
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()
|
|
30
27
|
};
|
|
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
|
+
}
|
|
32
|
+
function handleCORS(request, corsOptions) {
|
|
31
33
|
if (request.method === "OPTIONS") {
|
|
32
|
-
return new Response(null, { headers: corsHeaders });
|
|
34
|
+
return new Response(null, { headers: corsHeaders(request, corsOptions) });
|
|
33
35
|
}
|
|
34
36
|
return null;
|
|
35
37
|
}
|
|
36
|
-
var _getWebSocket, _started;
|
|
37
38
|
var McpSSETransport = class {
|
|
38
39
|
constructor(getWebSocket) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
__privateSet(this, _getWebSocket, getWebSocket);
|
|
40
|
+
this._started = false;
|
|
41
|
+
this._getWebSocket = getWebSocket;
|
|
42
42
|
}
|
|
43
43
|
async start() {
|
|
44
|
-
if (
|
|
44
|
+
if (this._started) {
|
|
45
45
|
throw new Error("Transport already started");
|
|
46
46
|
}
|
|
47
|
-
|
|
47
|
+
this._started = true;
|
|
48
48
|
}
|
|
49
49
|
async send(message) {
|
|
50
|
-
if (!
|
|
50
|
+
if (!this._started) {
|
|
51
51
|
throw new Error("Transport not started");
|
|
52
52
|
}
|
|
53
|
-
const websocket =
|
|
53
|
+
const websocket = this._getWebSocket();
|
|
54
54
|
if (!websocket) {
|
|
55
55
|
throw new Error("WebSocket not connected");
|
|
56
56
|
}
|
|
@@ -65,52 +65,40 @@ var McpSSETransport = class {
|
|
|
65
65
|
this.onclose?.();
|
|
66
66
|
}
|
|
67
67
|
};
|
|
68
|
-
_getWebSocket = new WeakMap();
|
|
69
|
-
_started = new WeakMap();
|
|
70
|
-
var _getWebSocketForGetRequest, _getWebSocketForMessageID, _notifyResponseIdSent, _started2;
|
|
71
68
|
var McpStreamableHttpTransport = class {
|
|
72
69
|
constructor(getWebSocketForMessageID, notifyResponseIdSent) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
__privateAdd(this, _getWebSocketForMessageID);
|
|
78
|
-
// Notify the server that a response has been sent for a given message id
|
|
79
|
-
// so that it may clean up it's mapping of message ids to connections
|
|
80
|
-
// once they are no longer needed
|
|
81
|
-
__privateAdd(this, _notifyResponseIdSent);
|
|
82
|
-
__privateAdd(this, _started2, false);
|
|
83
|
-
__privateSet(this, _getWebSocketForMessageID, getWebSocketForMessageID);
|
|
84
|
-
__privateSet(this, _notifyResponseIdSent, notifyResponseIdSent);
|
|
85
|
-
__privateSet(this, _getWebSocketForGetRequest, () => null);
|
|
70
|
+
this._started = false;
|
|
71
|
+
this._getWebSocketForMessageID = getWebSocketForMessageID;
|
|
72
|
+
this._notifyResponseIdSent = notifyResponseIdSent;
|
|
73
|
+
this._getWebSocketForGetRequest = () => null;
|
|
86
74
|
}
|
|
87
75
|
async start() {
|
|
88
|
-
if (
|
|
76
|
+
if (this._started) {
|
|
89
77
|
throw new Error("Transport already started");
|
|
90
78
|
}
|
|
91
|
-
|
|
79
|
+
this._started = true;
|
|
92
80
|
}
|
|
93
81
|
async send(message) {
|
|
94
|
-
if (!
|
|
82
|
+
if (!this._started) {
|
|
95
83
|
throw new Error("Transport not started");
|
|
96
84
|
}
|
|
97
85
|
let websocket = null;
|
|
98
86
|
if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
|
|
99
|
-
websocket =
|
|
87
|
+
websocket = this._getWebSocketForMessageID(message.id.toString());
|
|
100
88
|
if (!websocket) {
|
|
101
89
|
throw new Error(
|
|
102
90
|
`Could not find WebSocket for message id: ${message.id}`
|
|
103
91
|
);
|
|
104
92
|
}
|
|
105
93
|
} else if (isJSONRPCRequest(message)) {
|
|
106
|
-
websocket =
|
|
94
|
+
websocket = this._getWebSocketForGetRequest();
|
|
107
95
|
} else if (isJSONRPCNotification(message)) {
|
|
108
96
|
websocket = null;
|
|
109
97
|
}
|
|
110
98
|
try {
|
|
111
99
|
websocket?.send(JSON.stringify(message));
|
|
112
100
|
if (isJSONRPCResponse(message)) {
|
|
113
|
-
|
|
101
|
+
this._notifyResponseIdSent(message.id.toString());
|
|
114
102
|
}
|
|
115
103
|
} catch (error) {
|
|
116
104
|
this.onerror?.(error);
|
|
@@ -121,28 +109,16 @@ var McpStreamableHttpTransport = class {
|
|
|
121
109
|
this.onclose?.();
|
|
122
110
|
}
|
|
123
111
|
};
|
|
124
|
-
|
|
125
|
-
_getWebSocketForMessageID = new WeakMap();
|
|
126
|
-
_notifyResponseIdSent = new WeakMap();
|
|
127
|
-
_started2 = new WeakMap();
|
|
128
|
-
var _status, _transport, _transportType, _requestIdToConnectionId, _agent, _McpAgent_instances, initialize_fn;
|
|
129
|
-
var _McpAgent = class _McpAgent extends DurableObject {
|
|
112
|
+
var McpAgent = class _McpAgent extends DurableObject {
|
|
130
113
|
constructor(ctx, env) {
|
|
131
114
|
var _a;
|
|
132
115
|
super(ctx, env);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
__privateAdd(this, _transportType, "unset");
|
|
137
|
-
__privateAdd(this, _requestIdToConnectionId, /* @__PURE__ */ new Map());
|
|
138
|
-
/**
|
|
139
|
-
* Since McpAgent's _aren't_ yet real "Agents", let's only expose a couple of the methods
|
|
140
|
-
* to the outer class: initialState/state/setState/onStateUpdate/sql
|
|
141
|
-
*/
|
|
142
|
-
__privateAdd(this, _agent);
|
|
116
|
+
this._status = "zero";
|
|
117
|
+
this._transportType = "unset";
|
|
118
|
+
this._requestIdToConnectionId = /* @__PURE__ */ new Map();
|
|
143
119
|
this.initRun = false;
|
|
144
120
|
const self = this;
|
|
145
|
-
|
|
121
|
+
this._agent = new (_a = class extends Agent {
|
|
146
122
|
onStateUpdate(state, source) {
|
|
147
123
|
return self.onStateUpdate(state, source);
|
|
148
124
|
}
|
|
@@ -151,26 +127,27 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
151
127
|
}
|
|
152
128
|
}, _a.options = {
|
|
153
129
|
hibernate: true
|
|
154
|
-
}, _a)(ctx, env)
|
|
130
|
+
}, _a)(ctx, env);
|
|
155
131
|
}
|
|
156
132
|
get mcp() {
|
|
157
|
-
return
|
|
133
|
+
return this._agent.mcp;
|
|
158
134
|
}
|
|
159
135
|
get state() {
|
|
160
|
-
return
|
|
136
|
+
return this._agent.state;
|
|
161
137
|
}
|
|
162
138
|
sql(strings, ...values) {
|
|
163
|
-
return
|
|
139
|
+
return this._agent.sql(strings, ...values);
|
|
164
140
|
}
|
|
165
141
|
setState(state) {
|
|
166
|
-
return
|
|
142
|
+
return this._agent.setState(state);
|
|
167
143
|
}
|
|
144
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: overriden later
|
|
168
145
|
onStateUpdate(state, source) {
|
|
169
146
|
}
|
|
170
147
|
async onStart() {
|
|
171
148
|
var _a;
|
|
172
149
|
const self = this;
|
|
173
|
-
|
|
150
|
+
this._agent = new (_a = class extends Agent {
|
|
174
151
|
constructor() {
|
|
175
152
|
super(...arguments);
|
|
176
153
|
this.initialState = self.initialState;
|
|
@@ -183,22 +160,22 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
183
160
|
}
|
|
184
161
|
}, _a.options = {
|
|
185
162
|
hibernate: true
|
|
186
|
-
}, _a)(this.ctx, this.env)
|
|
163
|
+
}, _a)(this.ctx, this.env);
|
|
187
164
|
this.props = await this.ctx.storage.get("props");
|
|
188
|
-
|
|
165
|
+
this._transportType = await this.ctx.storage.get(
|
|
189
166
|
"transportType"
|
|
190
|
-
)
|
|
167
|
+
);
|
|
191
168
|
await this._init(this.props);
|
|
192
169
|
const server = await this.server;
|
|
193
|
-
if (
|
|
194
|
-
|
|
195
|
-
await server.connect(
|
|
196
|
-
} else if (
|
|
197
|
-
|
|
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(
|
|
198
175
|
(id) => this.getWebSocketForResponseID(id),
|
|
199
|
-
(id) =>
|
|
200
|
-
)
|
|
201
|
-
await server.connect(
|
|
176
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
177
|
+
);
|
|
178
|
+
await server.connect(this._transport);
|
|
202
179
|
}
|
|
203
180
|
}
|
|
204
181
|
async _init(props) {
|
|
@@ -218,10 +195,17 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
218
195
|
async isInitialized() {
|
|
219
196
|
return await this.ctx.storage.get("initialized") === true;
|
|
220
197
|
}
|
|
198
|
+
async _initialize() {
|
|
199
|
+
await this.ctx.blockConcurrencyWhile(async () => {
|
|
200
|
+
this._status = "starting";
|
|
201
|
+
await this.onStart();
|
|
202
|
+
this._status = "started";
|
|
203
|
+
});
|
|
204
|
+
}
|
|
221
205
|
// Allow the worker to fetch a websocket connection to the agent
|
|
222
206
|
async fetch(request) {
|
|
223
|
-
if (
|
|
224
|
-
await
|
|
207
|
+
if (this._status !== "started") {
|
|
208
|
+
await this._initialize();
|
|
225
209
|
}
|
|
226
210
|
if (request.headers.get("Upgrade") !== "websocket") {
|
|
227
211
|
return new Response("Expected WebSocket Upgrade request", {
|
|
@@ -238,24 +222,24 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
238
222
|
return new Response("Websocket already connected", { status: 400 });
|
|
239
223
|
}
|
|
240
224
|
await this.ctx.storage.put("transportType", "sse");
|
|
241
|
-
|
|
242
|
-
if (!
|
|
243
|
-
|
|
244
|
-
await server.connect(
|
|
225
|
+
this._transportType = "sse";
|
|
226
|
+
if (!this._transport) {
|
|
227
|
+
this._transport = new McpSSETransport(() => this.getWebSocket());
|
|
228
|
+
await server.connect(this._transport);
|
|
245
229
|
}
|
|
246
|
-
return
|
|
230
|
+
return this._agent.fetch(request);
|
|
247
231
|
}
|
|
248
232
|
case "/streamable-http": {
|
|
249
|
-
if (!
|
|
250
|
-
|
|
233
|
+
if (!this._transport) {
|
|
234
|
+
this._transport = new McpStreamableHttpTransport(
|
|
251
235
|
(id) => this.getWebSocketForResponseID(id),
|
|
252
|
-
(id) =>
|
|
253
|
-
)
|
|
254
|
-
await server.connect(
|
|
236
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
237
|
+
);
|
|
238
|
+
await server.connect(this._transport);
|
|
255
239
|
}
|
|
256
240
|
await this.ctx.storage.put("transportType", "streamable-http");
|
|
257
|
-
|
|
258
|
-
return
|
|
241
|
+
this._transportType = "streamable-http";
|
|
242
|
+
return this._agent.fetch(request);
|
|
259
243
|
}
|
|
260
244
|
default:
|
|
261
245
|
return new Response(
|
|
@@ -274,19 +258,19 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
274
258
|
return websockets[0];
|
|
275
259
|
}
|
|
276
260
|
getWebSocketForResponseID(id) {
|
|
277
|
-
const connectionId =
|
|
261
|
+
const connectionId = this._requestIdToConnectionId.get(id);
|
|
278
262
|
if (connectionId === void 0) {
|
|
279
263
|
return null;
|
|
280
264
|
}
|
|
281
|
-
return
|
|
265
|
+
return this._agent.getConnection(connectionId) ?? null;
|
|
282
266
|
}
|
|
283
267
|
// All messages received here. This is currently never called
|
|
284
268
|
async onMessage(connection, event) {
|
|
285
|
-
if (
|
|
269
|
+
if (this._transportType !== "streamable-http") {
|
|
286
270
|
const err = new Error(
|
|
287
271
|
"Internal Server Error: Expected streamable-http protocol"
|
|
288
272
|
);
|
|
289
|
-
|
|
273
|
+
this._transport?.onerror?.(err);
|
|
290
274
|
return;
|
|
291
275
|
}
|
|
292
276
|
let message;
|
|
@@ -294,21 +278,21 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
294
278
|
const data = typeof event === "string" ? event : new TextDecoder().decode(event);
|
|
295
279
|
message = JSONRPCMessageSchema.parse(JSON.parse(data));
|
|
296
280
|
} catch (error) {
|
|
297
|
-
|
|
281
|
+
this._transport?.onerror?.(error);
|
|
298
282
|
return;
|
|
299
283
|
}
|
|
300
284
|
if (isJSONRPCRequest(message)) {
|
|
301
|
-
|
|
285
|
+
this._requestIdToConnectionId.set(message.id.toString(), connection.id);
|
|
302
286
|
}
|
|
303
|
-
|
|
287
|
+
this._transport?.onmessage?.(message);
|
|
304
288
|
}
|
|
305
289
|
// All messages received over SSE after the initial connection has been established
|
|
306
290
|
// will be passed here
|
|
307
|
-
async onSSEMcpMessage(
|
|
308
|
-
if (
|
|
309
|
-
await
|
|
291
|
+
async onSSEMcpMessage(_sessionId, request) {
|
|
292
|
+
if (this._status !== "started") {
|
|
293
|
+
await this._initialize();
|
|
310
294
|
}
|
|
311
|
-
if (
|
|
295
|
+
if (this._transportType !== "sse") {
|
|
312
296
|
return new Error("Internal Server Error: Expected SSE protocol");
|
|
313
297
|
}
|
|
314
298
|
try {
|
|
@@ -317,36 +301,36 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
317
301
|
try {
|
|
318
302
|
parsedMessage = JSONRPCMessageSchema.parse(message);
|
|
319
303
|
} catch (error) {
|
|
320
|
-
|
|
304
|
+
this._transport?.onerror?.(error);
|
|
321
305
|
throw error;
|
|
322
306
|
}
|
|
323
|
-
|
|
307
|
+
this._transport?.onmessage?.(parsedMessage);
|
|
324
308
|
return null;
|
|
325
309
|
} catch (error) {
|
|
326
310
|
console.error("Error forwarding message to SSE:", error);
|
|
327
|
-
|
|
311
|
+
this._transport?.onerror?.(error);
|
|
328
312
|
return error;
|
|
329
313
|
}
|
|
330
314
|
}
|
|
331
315
|
// Delegate all websocket events to the underlying agent
|
|
332
316
|
async webSocketMessage(ws, event) {
|
|
333
|
-
if (
|
|
334
|
-
await
|
|
317
|
+
if (this._status !== "started") {
|
|
318
|
+
await this._initialize();
|
|
335
319
|
}
|
|
336
|
-
return await
|
|
320
|
+
return await this._agent.webSocketMessage(ws, event);
|
|
337
321
|
}
|
|
338
322
|
// WebSocket event handlers for hibernation support
|
|
339
323
|
async webSocketError(ws, error) {
|
|
340
|
-
if (
|
|
341
|
-
await
|
|
324
|
+
if (this._status !== "started") {
|
|
325
|
+
await this._initialize();
|
|
342
326
|
}
|
|
343
|
-
return await
|
|
327
|
+
return await this._agent.webSocketError(ws, error);
|
|
344
328
|
}
|
|
345
329
|
async webSocketClose(ws, code, reason, wasClean) {
|
|
346
|
-
if (
|
|
347
|
-
await
|
|
330
|
+
if (this._status !== "started") {
|
|
331
|
+
await this._initialize();
|
|
348
332
|
}
|
|
349
|
-
return await
|
|
333
|
+
return await this._agent.webSocketClose(ws, code, reason, wasClean);
|
|
350
334
|
}
|
|
351
335
|
static mount(path, {
|
|
352
336
|
binding = "MCP_OBJECT",
|
|
@@ -376,7 +360,7 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
376
360
|
);
|
|
377
361
|
return new Response("Invalid binding", { status: 500 });
|
|
378
362
|
}
|
|
379
|
-
if (bindingValue
|
|
363
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
380
364
|
return new Response("Invalid binding", { status: 500 });
|
|
381
365
|
}
|
|
382
366
|
const namespace = bindingValue;
|
|
@@ -437,10 +421,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
437
421
|
onMessage(event).catch(console.error);
|
|
438
422
|
});
|
|
439
423
|
ws.addEventListener("error", (error) => {
|
|
440
|
-
async function onError(
|
|
424
|
+
async function onError(_error) {
|
|
441
425
|
try {
|
|
442
426
|
await writer.close();
|
|
443
|
-
} catch (
|
|
427
|
+
} catch (_e) {
|
|
444
428
|
}
|
|
445
429
|
}
|
|
446
430
|
onError(error).catch(console.error);
|
|
@@ -457,10 +441,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
457
441
|
});
|
|
458
442
|
return new Response(readable, {
|
|
459
443
|
headers: {
|
|
460
|
-
"Content-Type": "text/event-stream",
|
|
461
444
|
"Cache-Control": "no-cache",
|
|
462
445
|
Connection: "keep-alive",
|
|
463
|
-
"
|
|
446
|
+
"Content-Type": "text/event-stream",
|
|
447
|
+
...corsHeaders(request, corsOptions)
|
|
464
448
|
}
|
|
465
449
|
});
|
|
466
450
|
}
|
|
@@ -495,23 +479,23 @@ data: ${JSON.stringify(result.data)}
|
|
|
495
479
|
const error = await doStub.onSSEMcpMessage(sessionId, request);
|
|
496
480
|
if (error) {
|
|
497
481
|
return new Response(error.message, {
|
|
498
|
-
status: 400,
|
|
499
482
|
headers: {
|
|
500
|
-
"Content-Type": "text/event-stream",
|
|
501
483
|
"Cache-Control": "no-cache",
|
|
502
484
|
Connection: "keep-alive",
|
|
503
|
-
"
|
|
504
|
-
|
|
485
|
+
"Content-Type": "text/event-stream",
|
|
486
|
+
...corsHeaders(request, corsOptions)
|
|
487
|
+
},
|
|
488
|
+
status: 400
|
|
505
489
|
});
|
|
506
490
|
}
|
|
507
491
|
return new Response("Accepted", {
|
|
508
|
-
status: 202,
|
|
509
492
|
headers: {
|
|
510
|
-
"Content-Type": "text/event-stream",
|
|
511
493
|
"Cache-Control": "no-cache",
|
|
512
494
|
Connection: "keep-alive",
|
|
513
|
-
"
|
|
514
|
-
|
|
495
|
+
"Content-Type": "text/event-stream",
|
|
496
|
+
...corsHeaders(request, corsOptions)
|
|
497
|
+
},
|
|
498
|
+
status: 202
|
|
515
499
|
});
|
|
516
500
|
}
|
|
517
501
|
return new Response("Not Found", { status: 404 });
|
|
@@ -541,7 +525,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
541
525
|
);
|
|
542
526
|
return new Response("Invalid binding", { status: 500 });
|
|
543
527
|
}
|
|
544
|
-
if (bindingValue
|
|
528
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
545
529
|
return new Response("Invalid binding", { status: 500 });
|
|
546
530
|
}
|
|
547
531
|
const namespace = bindingValue;
|
|
@@ -549,24 +533,24 @@ data: ${JSON.stringify(result.data)}
|
|
|
549
533
|
const acceptHeader = request.headers.get("accept");
|
|
550
534
|
if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
|
|
551
535
|
const body2 = JSON.stringify({
|
|
552
|
-
jsonrpc: "2.0",
|
|
553
536
|
error: {
|
|
554
537
|
code: -32e3,
|
|
555
538
|
message: "Not Acceptable: Client must accept both application/json and text/event-stream"
|
|
556
539
|
},
|
|
557
|
-
id: null
|
|
540
|
+
id: null,
|
|
541
|
+
jsonrpc: "2.0"
|
|
558
542
|
});
|
|
559
543
|
return new Response(body2, { status: 406 });
|
|
560
544
|
}
|
|
561
545
|
const ct = request.headers.get("content-type");
|
|
562
546
|
if (!ct || !ct.includes("application/json")) {
|
|
563
547
|
const body2 = JSON.stringify({
|
|
564
|
-
jsonrpc: "2.0",
|
|
565
548
|
error: {
|
|
566
549
|
code: -32e3,
|
|
567
550
|
message: "Unsupported Media Type: Content-Type must be application/json"
|
|
568
551
|
},
|
|
569
|
-
id: null
|
|
552
|
+
id: null,
|
|
553
|
+
jsonrpc: "2.0"
|
|
570
554
|
});
|
|
571
555
|
return new Response(body2, { status: 415 });
|
|
572
556
|
}
|
|
@@ -576,12 +560,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
576
560
|
);
|
|
577
561
|
if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) {
|
|
578
562
|
const body2 = JSON.stringify({
|
|
579
|
-
jsonrpc: "2.0",
|
|
580
563
|
error: {
|
|
581
564
|
code: -32e3,
|
|
582
565
|
message: `Request body too large. Maximum size is ${MAXIMUM_MESSAGE_SIZE_BYTES} bytes`
|
|
583
566
|
},
|
|
584
|
-
id: null
|
|
567
|
+
id: null,
|
|
568
|
+
jsonrpc: "2.0"
|
|
585
569
|
});
|
|
586
570
|
return new Response(body2, { status: 413 });
|
|
587
571
|
}
|
|
@@ -589,14 +573,14 @@ data: ${JSON.stringify(result.data)}
|
|
|
589
573
|
let rawMessage;
|
|
590
574
|
try {
|
|
591
575
|
rawMessage = await request.json();
|
|
592
|
-
} catch (
|
|
576
|
+
} catch (_error) {
|
|
593
577
|
const body2 = JSON.stringify({
|
|
594
|
-
jsonrpc: "2.0",
|
|
595
578
|
error: {
|
|
596
579
|
code: -32700,
|
|
597
580
|
message: "Parse error: Invalid JSON"
|
|
598
581
|
},
|
|
599
|
-
id: null
|
|
582
|
+
id: null,
|
|
583
|
+
jsonrpc: "2.0"
|
|
600
584
|
});
|
|
601
585
|
return new Response(body2, { status: 400 });
|
|
602
586
|
}
|
|
@@ -610,12 +594,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
610
594
|
for (const msg of arrayMessage) {
|
|
611
595
|
if (!JSONRPCMessageSchema.safeParse(msg).success) {
|
|
612
596
|
const body2 = JSON.stringify({
|
|
613
|
-
jsonrpc: "2.0",
|
|
614
597
|
error: {
|
|
615
598
|
code: -32700,
|
|
616
599
|
message: "Parse error: Invalid JSON-RPC message"
|
|
617
600
|
},
|
|
618
|
-
id: null
|
|
601
|
+
id: null,
|
|
602
|
+
jsonrpc: "2.0"
|
|
619
603
|
});
|
|
620
604
|
return new Response(body2, { status: 400 });
|
|
621
605
|
}
|
|
@@ -626,34 +610,34 @@ data: ${JSON.stringify(result.data)}
|
|
|
626
610
|
);
|
|
627
611
|
if (isInitializationRequest && sessionId) {
|
|
628
612
|
const body2 = JSON.stringify({
|
|
629
|
-
jsonrpc: "2.0",
|
|
630
613
|
error: {
|
|
631
614
|
code: -32600,
|
|
632
615
|
message: "Invalid Request: Initialization requests must not include a sessionId"
|
|
633
616
|
},
|
|
634
|
-
id: null
|
|
617
|
+
id: null,
|
|
618
|
+
jsonrpc: "2.0"
|
|
635
619
|
});
|
|
636
620
|
return new Response(body2, { status: 400 });
|
|
637
621
|
}
|
|
638
622
|
if (isInitializationRequest && messages.length > 1) {
|
|
639
623
|
const body2 = JSON.stringify({
|
|
640
|
-
jsonrpc: "2.0",
|
|
641
624
|
error: {
|
|
642
625
|
code: -32600,
|
|
643
626
|
message: "Invalid Request: Only one initialization request is allowed"
|
|
644
627
|
},
|
|
645
|
-
id: null
|
|
628
|
+
id: null,
|
|
629
|
+
jsonrpc: "2.0"
|
|
646
630
|
});
|
|
647
631
|
return new Response(body2, { status: 400 });
|
|
648
632
|
}
|
|
649
633
|
if (!isInitializationRequest && !sessionId) {
|
|
650
634
|
const body2 = JSON.stringify({
|
|
651
|
-
jsonrpc: "2.0",
|
|
652
635
|
error: {
|
|
653
636
|
code: -32e3,
|
|
654
637
|
message: "Bad Request: Mcp-Session-Id header is required"
|
|
655
638
|
},
|
|
656
|
-
id: null
|
|
639
|
+
id: null,
|
|
640
|
+
jsonrpc: "2.0"
|
|
657
641
|
});
|
|
658
642
|
return new Response(body2, { status: 400 });
|
|
659
643
|
}
|
|
@@ -666,12 +650,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
666
650
|
await doStub.setInitialized();
|
|
667
651
|
} else if (!isInitialized) {
|
|
668
652
|
const body2 = JSON.stringify({
|
|
669
|
-
jsonrpc: "2.0",
|
|
670
653
|
error: {
|
|
671
654
|
code: -32001,
|
|
672
655
|
message: "Session not found"
|
|
673
656
|
},
|
|
674
|
-
id: null
|
|
657
|
+
id: null,
|
|
658
|
+
jsonrpc: "2.0"
|
|
675
659
|
});
|
|
676
660
|
return new Response(body2, { status: 404 });
|
|
677
661
|
}
|
|
@@ -694,12 +678,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
694
678
|
console.error("Failed to establish WebSocket connection");
|
|
695
679
|
await writer.close();
|
|
696
680
|
const body2 = JSON.stringify({
|
|
697
|
-
jsonrpc: "2.0",
|
|
698
681
|
error: {
|
|
699
682
|
code: -32001,
|
|
700
683
|
message: "Failed to establish WebSocket connection"
|
|
701
684
|
},
|
|
702
|
-
id: null
|
|
685
|
+
id: null,
|
|
686
|
+
jsonrpc: "2.0"
|
|
703
687
|
});
|
|
704
688
|
return new Response(body2, { status: 500 });
|
|
705
689
|
}
|
|
@@ -732,10 +716,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
732
716
|
onMessage(event).catch(console.error);
|
|
733
717
|
});
|
|
734
718
|
ws.addEventListener("error", (error) => {
|
|
735
|
-
async function onError(
|
|
719
|
+
async function onError(_error) {
|
|
736
720
|
try {
|
|
737
721
|
await writer.close();
|
|
738
|
-
} catch (
|
|
722
|
+
} catch (_e) {
|
|
739
723
|
}
|
|
740
724
|
}
|
|
741
725
|
onError(error).catch(console.error);
|
|
@@ -758,7 +742,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
758
742
|
ws.send(JSON.stringify(message));
|
|
759
743
|
}
|
|
760
744
|
ws.close();
|
|
761
|
-
return new Response(null, {
|
|
745
|
+
return new Response(null, {
|
|
746
|
+
headers: corsHeaders(request, corsOptions),
|
|
747
|
+
status: 202
|
|
748
|
+
});
|
|
762
749
|
}
|
|
763
750
|
for (const message of messages) {
|
|
764
751
|
if (isJSONRPCRequest(message)) {
|
|
@@ -768,42 +755,28 @@ data: ${JSON.stringify(result.data)}
|
|
|
768
755
|
}
|
|
769
756
|
return new Response(readable, {
|
|
770
757
|
headers: {
|
|
771
|
-
"Content-Type": "text/event-stream",
|
|
772
758
|
"Cache-Control": "no-cache",
|
|
773
759
|
Connection: "keep-alive",
|
|
760
|
+
"Content-Type": "text/event-stream",
|
|
774
761
|
"mcp-session-id": sessionId,
|
|
775
|
-
|
|
762
|
+
...corsHeaders(request, corsOptions)
|
|
776
763
|
},
|
|
777
764
|
status: 200
|
|
778
765
|
});
|
|
779
766
|
}
|
|
780
767
|
const body = JSON.stringify({
|
|
781
|
-
jsonrpc: "2.0",
|
|
782
768
|
error: {
|
|
783
769
|
code: -32e3,
|
|
784
770
|
message: "Method not allowed"
|
|
785
771
|
},
|
|
786
|
-
id: null
|
|
772
|
+
id: null,
|
|
773
|
+
jsonrpc: "2.0"
|
|
787
774
|
});
|
|
788
775
|
return new Response(body, { status: 405 });
|
|
789
776
|
}
|
|
790
777
|
};
|
|
791
778
|
}
|
|
792
779
|
};
|
|
793
|
-
_status = new WeakMap();
|
|
794
|
-
_transport = new WeakMap();
|
|
795
|
-
_transportType = new WeakMap();
|
|
796
|
-
_requestIdToConnectionId = new WeakMap();
|
|
797
|
-
_agent = new WeakMap();
|
|
798
|
-
_McpAgent_instances = new WeakSet();
|
|
799
|
-
initialize_fn = async function() {
|
|
800
|
-
await this.ctx.blockConcurrencyWhile(async () => {
|
|
801
|
-
__privateSet(this, _status, "starting");
|
|
802
|
-
await this.onStart();
|
|
803
|
-
__privateSet(this, _status, "started");
|
|
804
|
-
});
|
|
805
|
-
};
|
|
806
|
-
var McpAgent = _McpAgent;
|
|
807
780
|
export {
|
|
808
781
|
McpAgent
|
|
809
782
|
};
|