agents 0.0.0-e416962 → 0.0.0-e48e5f9
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 +28 -29
- package/dist/ai-react.js.map +1 -1
- package/dist/chunk-5YIRLLUX.js +1264 -0
- package/dist/chunk-5YIRLLUX.js.map +1 -0
- package/dist/chunk-KUH345EY.js +116 -0
- package/dist/chunk-KUH345EY.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 +23 -13
- package/dist/mcp/index.js +150 -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 -68
- package/src/index.ts +1078 -140
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-HN5JVKAZ.js +0 -606
- package/dist/chunk-HN5JVKAZ.js.map +0 -1
- 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-5YIRLLUX.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,35 +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);
|
|
311
|
+
this._transport?.onerror?.(error);
|
|
327
312
|
return error;
|
|
328
313
|
}
|
|
329
314
|
}
|
|
330
315
|
// Delegate all websocket events to the underlying agent
|
|
331
316
|
async webSocketMessage(ws, event) {
|
|
332
|
-
if (
|
|
333
|
-
await
|
|
317
|
+
if (this._status !== "started") {
|
|
318
|
+
await this._initialize();
|
|
334
319
|
}
|
|
335
|
-
return await
|
|
320
|
+
return await this._agent.webSocketMessage(ws, event);
|
|
336
321
|
}
|
|
337
322
|
// WebSocket event handlers for hibernation support
|
|
338
323
|
async webSocketError(ws, error) {
|
|
339
|
-
if (
|
|
340
|
-
await
|
|
324
|
+
if (this._status !== "started") {
|
|
325
|
+
await this._initialize();
|
|
341
326
|
}
|
|
342
|
-
return await
|
|
327
|
+
return await this._agent.webSocketError(ws, error);
|
|
343
328
|
}
|
|
344
329
|
async webSocketClose(ws, code, reason, wasClean) {
|
|
345
|
-
if (
|
|
346
|
-
await
|
|
330
|
+
if (this._status !== "started") {
|
|
331
|
+
await this._initialize();
|
|
347
332
|
}
|
|
348
|
-
return await
|
|
333
|
+
return await this._agent.webSocketClose(ws, code, reason, wasClean);
|
|
349
334
|
}
|
|
350
335
|
static mount(path, {
|
|
351
336
|
binding = "MCP_OBJECT",
|
|
@@ -375,7 +360,7 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
375
360
|
);
|
|
376
361
|
return new Response("Invalid binding", { status: 500 });
|
|
377
362
|
}
|
|
378
|
-
if (bindingValue
|
|
363
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
379
364
|
return new Response("Invalid binding", { status: 500 });
|
|
380
365
|
}
|
|
381
366
|
const namespace = bindingValue;
|
|
@@ -436,10 +421,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
436
421
|
onMessage(event).catch(console.error);
|
|
437
422
|
});
|
|
438
423
|
ws.addEventListener("error", (error) => {
|
|
439
|
-
async function onError(
|
|
424
|
+
async function onError(_error) {
|
|
440
425
|
try {
|
|
441
426
|
await writer.close();
|
|
442
|
-
} catch (
|
|
427
|
+
} catch (_e) {
|
|
443
428
|
}
|
|
444
429
|
}
|
|
445
430
|
onError(error).catch(console.error);
|
|
@@ -456,10 +441,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
456
441
|
});
|
|
457
442
|
return new Response(readable, {
|
|
458
443
|
headers: {
|
|
459
|
-
"Content-Type": "text/event-stream",
|
|
460
444
|
"Cache-Control": "no-cache",
|
|
461
445
|
Connection: "keep-alive",
|
|
462
|
-
"
|
|
446
|
+
"Content-Type": "text/event-stream",
|
|
447
|
+
...corsHeaders(request, corsOptions)
|
|
463
448
|
}
|
|
464
449
|
});
|
|
465
450
|
}
|
|
@@ -494,23 +479,23 @@ data: ${JSON.stringify(result.data)}
|
|
|
494
479
|
const error = await doStub.onSSEMcpMessage(sessionId, request);
|
|
495
480
|
if (error) {
|
|
496
481
|
return new Response(error.message, {
|
|
497
|
-
status: 400,
|
|
498
482
|
headers: {
|
|
499
|
-
"Content-Type": "text/event-stream",
|
|
500
483
|
"Cache-Control": "no-cache",
|
|
501
484
|
Connection: "keep-alive",
|
|
502
|
-
"
|
|
503
|
-
|
|
485
|
+
"Content-Type": "text/event-stream",
|
|
486
|
+
...corsHeaders(request, corsOptions)
|
|
487
|
+
},
|
|
488
|
+
status: 400
|
|
504
489
|
});
|
|
505
490
|
}
|
|
506
491
|
return new Response("Accepted", {
|
|
507
|
-
status: 202,
|
|
508
492
|
headers: {
|
|
509
|
-
"Content-Type": "text/event-stream",
|
|
510
493
|
"Cache-Control": "no-cache",
|
|
511
494
|
Connection: "keep-alive",
|
|
512
|
-
"
|
|
513
|
-
|
|
495
|
+
"Content-Type": "text/event-stream",
|
|
496
|
+
...corsHeaders(request, corsOptions)
|
|
497
|
+
},
|
|
498
|
+
status: 202
|
|
514
499
|
});
|
|
515
500
|
}
|
|
516
501
|
return new Response("Not Found", { status: 404 });
|
|
@@ -540,7 +525,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
540
525
|
);
|
|
541
526
|
return new Response("Invalid binding", { status: 500 });
|
|
542
527
|
}
|
|
543
|
-
if (bindingValue
|
|
528
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
544
529
|
return new Response("Invalid binding", { status: 500 });
|
|
545
530
|
}
|
|
546
531
|
const namespace = bindingValue;
|
|
@@ -548,24 +533,24 @@ data: ${JSON.stringify(result.data)}
|
|
|
548
533
|
const acceptHeader = request.headers.get("accept");
|
|
549
534
|
if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
|
|
550
535
|
const body2 = JSON.stringify({
|
|
551
|
-
jsonrpc: "2.0",
|
|
552
536
|
error: {
|
|
553
537
|
code: -32e3,
|
|
554
538
|
message: "Not Acceptable: Client must accept both application/json and text/event-stream"
|
|
555
539
|
},
|
|
556
|
-
id: null
|
|
540
|
+
id: null,
|
|
541
|
+
jsonrpc: "2.0"
|
|
557
542
|
});
|
|
558
543
|
return new Response(body2, { status: 406 });
|
|
559
544
|
}
|
|
560
545
|
const ct = request.headers.get("content-type");
|
|
561
546
|
if (!ct || !ct.includes("application/json")) {
|
|
562
547
|
const body2 = JSON.stringify({
|
|
563
|
-
jsonrpc: "2.0",
|
|
564
548
|
error: {
|
|
565
549
|
code: -32e3,
|
|
566
550
|
message: "Unsupported Media Type: Content-Type must be application/json"
|
|
567
551
|
},
|
|
568
|
-
id: null
|
|
552
|
+
id: null,
|
|
553
|
+
jsonrpc: "2.0"
|
|
569
554
|
});
|
|
570
555
|
return new Response(body2, { status: 415 });
|
|
571
556
|
}
|
|
@@ -575,12 +560,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
575
560
|
);
|
|
576
561
|
if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) {
|
|
577
562
|
const body2 = JSON.stringify({
|
|
578
|
-
jsonrpc: "2.0",
|
|
579
563
|
error: {
|
|
580
564
|
code: -32e3,
|
|
581
565
|
message: `Request body too large. Maximum size is ${MAXIMUM_MESSAGE_SIZE_BYTES} bytes`
|
|
582
566
|
},
|
|
583
|
-
id: null
|
|
567
|
+
id: null,
|
|
568
|
+
jsonrpc: "2.0"
|
|
584
569
|
});
|
|
585
570
|
return new Response(body2, { status: 413 });
|
|
586
571
|
}
|
|
@@ -588,14 +573,14 @@ data: ${JSON.stringify(result.data)}
|
|
|
588
573
|
let rawMessage;
|
|
589
574
|
try {
|
|
590
575
|
rawMessage = await request.json();
|
|
591
|
-
} catch (
|
|
576
|
+
} catch (_error) {
|
|
592
577
|
const body2 = JSON.stringify({
|
|
593
|
-
jsonrpc: "2.0",
|
|
594
578
|
error: {
|
|
595
579
|
code: -32700,
|
|
596
580
|
message: "Parse error: Invalid JSON"
|
|
597
581
|
},
|
|
598
|
-
id: null
|
|
582
|
+
id: null,
|
|
583
|
+
jsonrpc: "2.0"
|
|
599
584
|
});
|
|
600
585
|
return new Response(body2, { status: 400 });
|
|
601
586
|
}
|
|
@@ -609,12 +594,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
609
594
|
for (const msg of arrayMessage) {
|
|
610
595
|
if (!JSONRPCMessageSchema.safeParse(msg).success) {
|
|
611
596
|
const body2 = JSON.stringify({
|
|
612
|
-
jsonrpc: "2.0",
|
|
613
597
|
error: {
|
|
614
598
|
code: -32700,
|
|
615
599
|
message: "Parse error: Invalid JSON-RPC message"
|
|
616
600
|
},
|
|
617
|
-
id: null
|
|
601
|
+
id: null,
|
|
602
|
+
jsonrpc: "2.0"
|
|
618
603
|
});
|
|
619
604
|
return new Response(body2, { status: 400 });
|
|
620
605
|
}
|
|
@@ -625,34 +610,34 @@ data: ${JSON.stringify(result.data)}
|
|
|
625
610
|
);
|
|
626
611
|
if (isInitializationRequest && sessionId) {
|
|
627
612
|
const body2 = JSON.stringify({
|
|
628
|
-
jsonrpc: "2.0",
|
|
629
613
|
error: {
|
|
630
614
|
code: -32600,
|
|
631
615
|
message: "Invalid Request: Initialization requests must not include a sessionId"
|
|
632
616
|
},
|
|
633
|
-
id: null
|
|
617
|
+
id: null,
|
|
618
|
+
jsonrpc: "2.0"
|
|
634
619
|
});
|
|
635
620
|
return new Response(body2, { status: 400 });
|
|
636
621
|
}
|
|
637
622
|
if (isInitializationRequest && messages.length > 1) {
|
|
638
623
|
const body2 = JSON.stringify({
|
|
639
|
-
jsonrpc: "2.0",
|
|
640
624
|
error: {
|
|
641
625
|
code: -32600,
|
|
642
626
|
message: "Invalid Request: Only one initialization request is allowed"
|
|
643
627
|
},
|
|
644
|
-
id: null
|
|
628
|
+
id: null,
|
|
629
|
+
jsonrpc: "2.0"
|
|
645
630
|
});
|
|
646
631
|
return new Response(body2, { status: 400 });
|
|
647
632
|
}
|
|
648
633
|
if (!isInitializationRequest && !sessionId) {
|
|
649
634
|
const body2 = JSON.stringify({
|
|
650
|
-
jsonrpc: "2.0",
|
|
651
635
|
error: {
|
|
652
636
|
code: -32e3,
|
|
653
637
|
message: "Bad Request: Mcp-Session-Id header is required"
|
|
654
638
|
},
|
|
655
|
-
id: null
|
|
639
|
+
id: null,
|
|
640
|
+
jsonrpc: "2.0"
|
|
656
641
|
});
|
|
657
642
|
return new Response(body2, { status: 400 });
|
|
658
643
|
}
|
|
@@ -661,15 +646,16 @@ data: ${JSON.stringify(result.data)}
|
|
|
661
646
|
const doStub = namespace.get(id);
|
|
662
647
|
const isInitialized = await doStub.isInitialized();
|
|
663
648
|
if (isInitializationRequest) {
|
|
649
|
+
await doStub._init(ctx.props);
|
|
664
650
|
await doStub.setInitialized();
|
|
665
651
|
} else if (!isInitialized) {
|
|
666
652
|
const body2 = JSON.stringify({
|
|
667
|
-
jsonrpc: "2.0",
|
|
668
653
|
error: {
|
|
669
654
|
code: -32001,
|
|
670
655
|
message: "Session not found"
|
|
671
656
|
},
|
|
672
|
-
id: null
|
|
657
|
+
id: null,
|
|
658
|
+
jsonrpc: "2.0"
|
|
673
659
|
});
|
|
674
660
|
return new Response(body2, { status: 404 });
|
|
675
661
|
}
|
|
@@ -692,12 +678,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
692
678
|
console.error("Failed to establish WebSocket connection");
|
|
693
679
|
await writer.close();
|
|
694
680
|
const body2 = JSON.stringify({
|
|
695
|
-
jsonrpc: "2.0",
|
|
696
681
|
error: {
|
|
697
682
|
code: -32001,
|
|
698
683
|
message: "Failed to establish WebSocket connection"
|
|
699
684
|
},
|
|
700
|
-
id: null
|
|
685
|
+
id: null,
|
|
686
|
+
jsonrpc: "2.0"
|
|
701
687
|
});
|
|
702
688
|
return new Response(body2, { status: 500 });
|
|
703
689
|
}
|
|
@@ -730,10 +716,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
730
716
|
onMessage(event).catch(console.error);
|
|
731
717
|
});
|
|
732
718
|
ws.addEventListener("error", (error) => {
|
|
733
|
-
async function onError(
|
|
719
|
+
async function onError(_error) {
|
|
734
720
|
try {
|
|
735
721
|
await writer.close();
|
|
736
|
-
} catch (
|
|
722
|
+
} catch (_e) {
|
|
737
723
|
}
|
|
738
724
|
}
|
|
739
725
|
onError(error).catch(console.error);
|
|
@@ -756,7 +742,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
756
742
|
ws.send(JSON.stringify(message));
|
|
757
743
|
}
|
|
758
744
|
ws.close();
|
|
759
|
-
return new Response(null, {
|
|
745
|
+
return new Response(null, {
|
|
746
|
+
headers: corsHeaders(request, corsOptions),
|
|
747
|
+
status: 202
|
|
748
|
+
});
|
|
760
749
|
}
|
|
761
750
|
for (const message of messages) {
|
|
762
751
|
if (isJSONRPCRequest(message)) {
|
|
@@ -766,42 +755,28 @@ data: ${JSON.stringify(result.data)}
|
|
|
766
755
|
}
|
|
767
756
|
return new Response(readable, {
|
|
768
757
|
headers: {
|
|
769
|
-
"Content-Type": "text/event-stream",
|
|
770
758
|
"Cache-Control": "no-cache",
|
|
771
759
|
Connection: "keep-alive",
|
|
760
|
+
"Content-Type": "text/event-stream",
|
|
772
761
|
"mcp-session-id": sessionId,
|
|
773
|
-
|
|
762
|
+
...corsHeaders(request, corsOptions)
|
|
774
763
|
},
|
|
775
764
|
status: 200
|
|
776
765
|
});
|
|
777
766
|
}
|
|
778
767
|
const body = JSON.stringify({
|
|
779
|
-
jsonrpc: "2.0",
|
|
780
768
|
error: {
|
|
781
769
|
code: -32e3,
|
|
782
770
|
message: "Method not allowed"
|
|
783
771
|
},
|
|
784
|
-
id: null
|
|
772
|
+
id: null,
|
|
773
|
+
jsonrpc: "2.0"
|
|
785
774
|
});
|
|
786
775
|
return new Response(body, { status: 405 });
|
|
787
776
|
}
|
|
788
777
|
};
|
|
789
778
|
}
|
|
790
779
|
};
|
|
791
|
-
_status = new WeakMap();
|
|
792
|
-
_transport = new WeakMap();
|
|
793
|
-
_transportType = new WeakMap();
|
|
794
|
-
_requestIdToConnectionId = new WeakMap();
|
|
795
|
-
_agent = new WeakMap();
|
|
796
|
-
_McpAgent_instances = new WeakSet();
|
|
797
|
-
initialize_fn = async function() {
|
|
798
|
-
await this.ctx.blockConcurrencyWhile(async () => {
|
|
799
|
-
__privateSet(this, _status, "starting");
|
|
800
|
-
await this.onStart();
|
|
801
|
-
__privateSet(this, _status, "started");
|
|
802
|
-
});
|
|
803
|
-
};
|
|
804
|
-
var McpAgent = _McpAgent;
|
|
805
780
|
export {
|
|
806
781
|
McpAgent
|
|
807
782
|
};
|