agents 0.0.0-96a8138 → 0.0.0-9ae8ee3

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