agents 0.0.0-8a1eb98 → 0.0.0-8a4558c

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 (40) hide show
  1. package/dist/ai-chat-agent.d.ts +28 -3
  2. package/dist/ai-chat-agent.js +99 -103
  3. package/dist/ai-chat-agent.js.map +1 -1
  4. package/dist/ai-react.d.ts +13 -0
  5. package/dist/ai-react.js +3 -3
  6. package/dist/ai-react.js.map +1 -1
  7. package/dist/{chunk-AV3OMRR4.js → chunk-6RPGDIE2.js} +329 -140
  8. package/dist/chunk-6RPGDIE2.js.map +1 -0
  9. package/dist/chunk-BZXOAZUX.js +106 -0
  10. package/dist/chunk-BZXOAZUX.js.map +1 -0
  11. package/dist/{chunk-YZNSS675.js → chunk-OYJXQRRH.js} +56 -26
  12. package/dist/chunk-OYJXQRRH.js.map +1 -0
  13. package/dist/chunk-VCSB47AK.js +116 -0
  14. package/dist/chunk-VCSB47AK.js.map +1 -0
  15. package/dist/client.d.ts +15 -1
  16. package/dist/client.js +6 -126
  17. package/dist/client.js.map +1 -1
  18. package/dist/index.d.ts +111 -13
  19. package/dist/index.js +8 -7
  20. package/dist/mcp/client.d.ts +30 -15
  21. package/dist/mcp/client.js +1 -2
  22. package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
  23. package/dist/mcp/do-oauth-client-provider.js +3 -103
  24. package/dist/mcp/do-oauth-client-provider.js.map +1 -1
  25. package/dist/mcp/index.d.ts +16 -5
  26. package/dist/mcp/index.js +113 -137
  27. package/dist/mcp/index.js.map +1 -1
  28. package/dist/react.d.ts +85 -5
  29. package/dist/react.js +14 -2
  30. package/dist/react.js.map +1 -1
  31. package/dist/schedule.js +0 -2
  32. package/dist/schedule.js.map +1 -1
  33. package/dist/serializable.d.ts +32 -0
  34. package/dist/serializable.js +1 -0
  35. package/package.json +10 -7
  36. package/src/index.ts +393 -56
  37. package/dist/chunk-AV3OMRR4.js.map +0 -1
  38. package/dist/chunk-HMLY7DHA.js +0 -16
  39. package/dist/chunk-YZNSS675.js.map +0 -1
  40. /package/dist/{chunk-HMLY7DHA.js.map → serializable.js.map} +0 -0
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/mcp/do-oauth-client-provider.ts"],"sourcesContent":["import type { OAuthClientProvider } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport type {\n OAuthTokens,\n OAuthClientMetadata,\n OAuthClientInformation,\n OAuthClientInformationFull,\n} from \"@modelcontextprotocol/sdk/shared/auth.js\";\n\n// A slight extension to the standard OAuthClientProvider interface because `redirectToAuthorization` doesn't give us the interface we need\n// This allows us to track authentication for a specific server and associated dynamic client registration\nexport interface AgentsOAuthProvider extends OAuthClientProvider {\n authUrl: string | undefined;\n clientId: string | undefined;\n serverId: string | undefined;\n}\n\nexport class DurableObjectOAuthClientProvider implements AgentsOAuthProvider {\n private authUrl_: string | undefined;\n private serverId_: string | undefined;\n private clientId_: string | undefined;\n\n constructor(\n public storage: DurableObjectStorage,\n public clientName: string,\n public baseRedirectUrl: string\n ) {}\n\n get clientMetadata(): OAuthClientMetadata {\n return {\n redirect_uris: [this.redirectUrl],\n token_endpoint_auth_method: \"none\",\n grant_types: [\"authorization_code\", \"refresh_token\"],\n response_types: [\"code\"],\n client_name: this.clientName,\n client_uri: \"example.com\",\n };\n }\n\n get redirectUrl() {\n return `${this.baseRedirectUrl}/${this.serverId}`;\n }\n\n get clientId() {\n if (!this.clientId_) {\n throw new Error(\"Trying to access clientId before it was set\");\n }\n return this.clientId_;\n }\n\n set clientId(clientId_: string) {\n this.clientId_ = clientId_;\n }\n\n get serverId() {\n if (!this.serverId_) {\n throw new Error(\"Trying to access serverId before it was set\");\n }\n return this.serverId_;\n }\n\n set serverId(serverId_: string) {\n this.serverId_ = serverId_;\n }\n\n keyPrefix(clientId: string) {\n return `/${this.clientName}/${this.serverId}/${clientId}`;\n }\n\n clientInfoKey(clientId: string) {\n return `${this.keyPrefix(clientId)}/client_info/`;\n }\n\n async clientInformation(): Promise<OAuthClientInformation | undefined> {\n if (!this.clientId_) {\n return undefined;\n }\n return (\n (await this.storage.get<OAuthClientInformation>(\n this.clientInfoKey(this.clientId)\n )) ?? undefined\n );\n }\n\n async saveClientInformation(\n clientInformation: OAuthClientInformationFull\n ): Promise<void> {\n await this.storage.put(\n this.clientInfoKey(clientInformation.client_id),\n clientInformation\n );\n this.clientId = clientInformation.client_id;\n }\n\n tokenKey(clientId: string) {\n return `${this.keyPrefix(clientId)}/token`;\n }\n\n async tokens(): Promise<OAuthTokens | undefined> {\n if (!this.clientId_) {\n return undefined;\n }\n return (\n (await this.storage.get<OAuthTokens>(this.tokenKey(this.clientId))) ??\n undefined\n );\n }\n\n async saveTokens(tokens: OAuthTokens): Promise<void> {\n await this.storage.put(this.tokenKey(this.clientId), tokens);\n }\n\n get authUrl() {\n return this.authUrl_;\n }\n\n /**\n * Because this operates on the server side (but we need browser auth), we send this url back to the user\n * and require user interact to initiate the redirect flow\n */\n async redirectToAuthorization(authUrl: URL): Promise<void> {\n // We want to track the client ID in state here because the typescript SSE client sometimes does\n // a dynamic client registration AFTER generating this redirect URL.\n const client_id = authUrl.searchParams.get(\"client_id\");\n if (client_id) {\n authUrl.searchParams.append(\"state\", client_id);\n }\n this.authUrl_ = authUrl.toString();\n }\n\n codeVerifierKey(clientId: string) {\n return `${this.keyPrefix(clientId)}/code_verifier`;\n }\n\n async saveCodeVerifier(verifier: string): Promise<void> {\n await this.storage.put(this.codeVerifierKey(this.clientId), verifier);\n }\n\n async codeVerifier(): Promise<string> {\n const codeVerifier = await this.storage.get<string>(\n this.codeVerifierKey(this.clientId)\n );\n if (!codeVerifier) {\n throw new Error(\"No code verifier found\");\n }\n return codeVerifier;\n }\n}\n"],"mappings":";;;AAgBO,IAAM,mCAAN,MAAsE;AAAA,EAK3E,YACS,SACA,YACA,iBACP;AAHO;AACA;AACA;AAAA,EACN;AAAA,EAEH,IAAI,iBAAsC;AACxC,WAAO;AAAA,MACL,eAAe,CAAC,KAAK,WAAW;AAAA,MAChC,4BAA4B;AAAA,MAC5B,aAAa,CAAC,sBAAsB,eAAe;AAAA,MACnD,gBAAgB,CAAC,MAAM;AAAA,MACvB,aAAa,KAAK;AAAA,MAClB,YAAY;AAAA,IACd;AAAA,EACF;AAAA,EAEA,IAAI,cAAc;AAChB,WAAO,GAAG,KAAK,eAAe,IAAI,KAAK,QAAQ;AAAA,EACjD;AAAA,EAEA,IAAI,WAAW;AACb,QAAI,CAAC,KAAK,WAAW;AACnB,YAAM,IAAI,MAAM,6CAA6C;AAAA,IAC/D;AACA,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,SAAS,WAAmB;AAC9B,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,IAAI,WAAW;AACb,QAAI,CAAC,KAAK,WAAW;AACnB,YAAM,IAAI,MAAM,6CAA6C;AAAA,IAC/D;AACA,WAAO,KAAK;AAAA,EACd;AAAA,EAEA,IAAI,SAAS,WAAmB;AAC9B,SAAK,YAAY;AAAA,EACnB;AAAA,EAEA,UAAU,UAAkB;AAC1B,WAAO,IAAI,KAAK,UAAU,IAAI,KAAK,QAAQ,IAAI,QAAQ;AAAA,EACzD;AAAA,EAEA,cAAc,UAAkB;AAC9B,WAAO,GAAG,KAAK,UAAU,QAAQ,CAAC;AAAA,EACpC;AAAA,EAEA,MAAM,oBAAiE;AACrE,QAAI,CAAC,KAAK,WAAW;AACnB,aAAO;AAAA,IACT;AACA,WACG,MAAM,KAAK,QAAQ;AAAA,MAClB,KAAK,cAAc,KAAK,QAAQ;AAAA,IAClC,KAAM;AAAA,EAEV;AAAA,EAEA,MAAM,sBACJ,mBACe;AACf,UAAM,KAAK,QAAQ;AAAA,MACjB,KAAK,cAAc,kBAAkB,SAAS;AAAA,MAC9C;AAAA,IACF;AACA,SAAK,WAAW,kBAAkB;AAAA,EACpC;AAAA,EAEA,SAAS,UAAkB;AACzB,WAAO,GAAG,KAAK,UAAU,QAAQ,CAAC;AAAA,EACpC;AAAA,EAEA,MAAM,SAA2C;AAC/C,QAAI,CAAC,KAAK,WAAW;AACnB,aAAO;AAAA,IACT;AACA,WACG,MAAM,KAAK,QAAQ,IAAiB,KAAK,SAAS,KAAK,QAAQ,CAAC,KACjE;AAAA,EAEJ;AAAA,EAEA,MAAM,WAAW,QAAoC;AACnD,UAAM,KAAK,QAAQ,IAAI,KAAK,SAAS,KAAK,QAAQ,GAAG,MAAM;AAAA,EAC7D;AAAA,EAEA,IAAI,UAAU;AACZ,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,wBAAwB,SAA6B;AAGzD,UAAM,YAAY,QAAQ,aAAa,IAAI,WAAW;AACtD,QAAI,WAAW;AACb,cAAQ,aAAa,OAAO,SAAS,SAAS;AAAA,IAChD;AACA,SAAK,WAAW,QAAQ,SAAS;AAAA,EACnC;AAAA,EAEA,gBAAgB,UAAkB;AAChC,WAAO,GAAG,KAAK,UAAU,QAAQ,CAAC;AAAA,EACpC;AAAA,EAEA,MAAM,iBAAiB,UAAiC;AACtD,UAAM,KAAK,QAAQ,IAAI,KAAK,gBAAgB,KAAK,QAAQ,GAAG,QAAQ;AAAA,EACtE;AAAA,EAEA,MAAM,eAAgC;AACpC,UAAM,eAAe,MAAM,KAAK,QAAQ;AAAA,MACtC,KAAK,gBAAgB,KAAK,QAAQ;AAAA,IACpC;AACA,QAAI,CAAC,cAAc;AACjB,YAAM,IAAI,MAAM,wBAAwB;AAAA,IAC1C;AACA,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -18,9 +18,19 @@ interface CORSOptions {
18
18
  methods?: string;
19
19
  headers?: string;
20
20
  maxAge?: number;
21
+ exposeHeaders?: string;
21
22
  }
23
+ type MaybePromise<T> = T | Promise<T>;
22
24
  declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Record<string, unknown> = Record<string, unknown>> extends DurableObject<Env> {
23
- #private;
25
+ private _status;
26
+ private _transport?;
27
+ private _transportType;
28
+ private _requestIdToConnectionId;
29
+ /**
30
+ * Since McpAgent's _aren't_ yet real "Agents", let's only expose a couple of the methods
31
+ * to the outer class: initialState/state/setState/onStateUpdate/sql
32
+ */
33
+ private _agent;
24
34
  get mcp(): MCPClientManager;
25
35
  protected constructor(ctx: DurableObjectState, env: Env);
26
36
  /**
@@ -35,13 +45,14 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
35
45
  /**
36
46
  * McpAgent API
37
47
  */
38
- abstract server: McpServer | Server;
48
+ abstract server: MaybePromise<McpServer | Server>;
39
49
  props: Props;
40
50
  initRun: boolean;
41
51
  abstract init(): Promise<void>;
42
52
  _init(props: Props): Promise<void>;
43
53
  setInitialized(): Promise<void>;
44
54
  isInitialized(): Promise<boolean>;
55
+ private _initialize;
45
56
  fetch(request: Request): Promise<Response>;
46
57
  getWebSocket(): WebSocket | null;
47
58
  getWebSocketForResponseID(id: string): WebSocket | null;
@@ -54,19 +65,19 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
54
65
  binding?: string;
55
66
  corsOptions?: CORSOptions;
56
67
  }): {
57
- fetch<Env>(request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
68
+ fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
58
69
  };
59
70
  static serveSSE(path: string, { binding, corsOptions, }?: {
60
71
  binding?: string;
61
72
  corsOptions?: CORSOptions;
62
73
  }): {
63
- fetch<Env>(request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
74
+ fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
64
75
  };
65
76
  static serve(path: string, { binding, corsOptions, }?: {
66
77
  binding?: string;
67
78
  corsOptions?: CORSOptions;
68
79
  }): {
69
- fetch<Env>(request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
80
+ fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
70
81
  };
71
82
  }
72
83
 
package/dist/mcp/index.js CHANGED
@@ -1,13 +1,9 @@
1
1
  import {
2
2
  Agent
3
- } from "../chunk-AV3OMRR4.js";
4
- import "../chunk-YZNSS675.js";
5
- import {
6
- __privateAdd,
7
- __privateGet,
8
- __privateMethod,
9
- __privateSet
10
- } from "../chunk-HMLY7DHA.js";
3
+ } from "../chunk-6RPGDIE2.js";
4
+ import "../chunk-OYJXQRRH.js";
5
+ import "../chunk-BZXOAZUX.js";
6
+ import "../chunk-VCSB47AK.js";
11
7
 
12
8
  // src/mcp/index.ts
13
9
  import { DurableObject } from "cloudflare:workers";
@@ -20,37 +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 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()
19
+ function corsHeaders(request, corsOptions = {}) {
20
+ const origin = "*";
21
+ return {
22
+ "Access-Control-Allow-Origin": corsOptions.origin || origin,
23
+ "Access-Control-Allow-Methods": corsOptions.methods || "GET, POST, OPTIONS",
24
+ "Access-Control-Allow-Headers": corsOptions.headers || "Content-Type, mcp-session-id",
25
+ "Access-Control-Max-Age": (corsOptions.maxAge || 86400).toString(),
26
+ "Access-Control-Expose-Headers": corsOptions.exposeHeaders || "mcp-session-id"
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
- __privateAdd(this, _getWebSocket);
40
- __privateAdd(this, _started, false);
41
- __privateSet(this, _getWebSocket, getWebSocket);
40
+ this._started = false;
41
+ this._getWebSocket = getWebSocket;
42
42
  }
43
43
  async start() {
44
- if (__privateGet(this, _started)) {
44
+ if (this._started) {
45
45
  throw new Error("Transport already started");
46
46
  }
47
- __privateSet(this, _started, true);
47
+ this._started = true;
48
48
  }
49
49
  async send(message) {
50
- if (!__privateGet(this, _started)) {
50
+ if (!this._started) {
51
51
  throw new Error("Transport not started");
52
52
  }
53
- const websocket = __privateGet(this, _getWebSocket).call(this);
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
- // 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);
70
+ this._started = false;
71
+ this._getWebSocketForMessageID = getWebSocketForMessageID;
72
+ this._notifyResponseIdSent = notifyResponseIdSent;
73
+ this._getWebSocketForGetRequest = () => null;
86
74
  }
87
75
  async start() {
88
- if (__privateGet(this, _started2)) {
76
+ if (this._started) {
89
77
  throw new Error("Transport already started");
90
78
  }
91
- __privateSet(this, _started2, true);
79
+ this._started = true;
92
80
  }
93
81
  async send(message) {
94
- if (!__privateGet(this, _started2)) {
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 = __privateGet(this, _getWebSocketForMessageID).call(this, message.id.toString());
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 = __privateGet(this, _getWebSocketForGetRequest).call(this);
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
- __privateGet(this, _notifyResponseIdSent).call(this, message.id.toString());
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
- _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 {
112
+ var McpAgent = class _McpAgent extends DurableObject {
130
113
  constructor(ctx, env) {
131
114
  var _a;
132
115
  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);
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
- __privateSet(this, _agent, new (_a = class extends Agent {
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,26 @@ 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 __privateGet(this, _agent).mcp;
133
+ return this._agent.mcp;
158
134
  }
159
135
  get state() {
160
- return __privateGet(this, _agent).state;
136
+ return this._agent.state;
161
137
  }
162
138
  sql(strings, ...values) {
163
- return __privateGet(this, _agent).sql(strings, ...values);
139
+ return this._agent.sql(strings, ...values);
164
140
  }
165
141
  setState(state) {
166
- return __privateGet(this, _agent).setState(state);
142
+ return this._agent.setState(state);
167
143
  }
168
144
  onStateUpdate(state, source) {
169
145
  }
170
146
  async onStart() {
171
147
  var _a;
172
148
  const self = this;
173
- __privateSet(this, _agent, new (_a = class extends Agent {
149
+ this._agent = new (_a = class extends Agent {
174
150
  constructor() {
175
151
  super(...arguments);
176
152
  this.initialState = self.initialState;
@@ -183,21 +159,22 @@ var _McpAgent = class _McpAgent extends DurableObject {
183
159
  }
184
160
  }, _a.options = {
185
161
  hibernate: true
186
- }, _a)(this.ctx, this.env));
162
+ }, _a)(this.ctx, this.env);
187
163
  this.props = await this.ctx.storage.get("props");
188
- __privateSet(this, _transportType, await this.ctx.storage.get(
164
+ this._transportType = await this.ctx.storage.get(
189
165
  "transportType"
190
- ));
166
+ );
191
167
  await this._init(this.props);
192
- if (__privateGet(this, _transportType) === "sse") {
193
- __privateSet(this, _transport, new McpSSETransport(() => this.getWebSocket()));
194
- await this.server.connect(__privateGet(this, _transport));
195
- } else if (__privateGet(this, _transportType) === "streamable-http") {
196
- __privateSet(this, _transport, new McpStreamableHttpTransport(
168
+ const server = await this.server;
169
+ if (this._transportType === "sse") {
170
+ this._transport = new McpSSETransport(() => this.getWebSocket());
171
+ await server.connect(this._transport);
172
+ } else if (this._transportType === "streamable-http") {
173
+ this._transport = new McpStreamableHttpTransport(
197
174
  (id) => this.getWebSocketForResponseID(id),
198
- (id) => __privateGet(this, _requestIdToConnectionId).delete(id)
199
- ));
200
- await this.server.connect(__privateGet(this, _transport));
175
+ (id) => this._requestIdToConnectionId.delete(id)
176
+ );
177
+ await server.connect(this._transport);
201
178
  }
202
179
  }
203
180
  async _init(props) {
@@ -217,10 +194,17 @@ var _McpAgent = class _McpAgent extends DurableObject {
217
194
  async isInitialized() {
218
195
  return await this.ctx.storage.get("initialized") === true;
219
196
  }
197
+ async _initialize() {
198
+ await this.ctx.blockConcurrencyWhile(async () => {
199
+ this._status = "starting";
200
+ await this.onStart();
201
+ this._status = "started";
202
+ });
203
+ }
220
204
  // Allow the worker to fetch a websocket connection to the agent
221
205
  async fetch(request) {
222
- if (__privateGet(this, _status) !== "started") {
223
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
206
+ if (this._status !== "started") {
207
+ await this._initialize();
224
208
  }
225
209
  if (request.headers.get("Upgrade") !== "websocket") {
226
210
  return new Response("Expected WebSocket Upgrade request", {
@@ -229,6 +213,7 @@ var _McpAgent = class _McpAgent extends DurableObject {
229
213
  }
230
214
  const url = new URL(request.url);
231
215
  const path = url.pathname;
216
+ const server = await this.server;
232
217
  switch (path) {
233
218
  case "/sse": {
234
219
  const websockets = this.ctx.getWebSockets();
@@ -236,24 +221,24 @@ var _McpAgent = class _McpAgent extends DurableObject {
236
221
  return new Response("Websocket already connected", { status: 400 });
237
222
  }
238
223
  await this.ctx.storage.put("transportType", "sse");
239
- __privateSet(this, _transportType, "sse");
240
- if (!__privateGet(this, _transport)) {
241
- __privateSet(this, _transport, new McpSSETransport(() => this.getWebSocket()));
242
- await this.server.connect(__privateGet(this, _transport));
224
+ this._transportType = "sse";
225
+ if (!this._transport) {
226
+ this._transport = new McpSSETransport(() => this.getWebSocket());
227
+ await server.connect(this._transport);
243
228
  }
244
- return __privateGet(this, _agent).fetch(request);
229
+ return this._agent.fetch(request);
245
230
  }
246
231
  case "/streamable-http": {
247
- if (!__privateGet(this, _transport)) {
248
- __privateSet(this, _transport, new McpStreamableHttpTransport(
232
+ if (!this._transport) {
233
+ this._transport = new McpStreamableHttpTransport(
249
234
  (id) => this.getWebSocketForResponseID(id),
250
- (id) => __privateGet(this, _requestIdToConnectionId).delete(id)
251
- ));
252
- await this.server.connect(__privateGet(this, _transport));
235
+ (id) => this._requestIdToConnectionId.delete(id)
236
+ );
237
+ await server.connect(this._transport);
253
238
  }
254
239
  await this.ctx.storage.put("transportType", "streamable-http");
255
- __privateSet(this, _transportType, "streamable-http");
256
- return __privateGet(this, _agent).fetch(request);
240
+ this._transportType = "streamable-http";
241
+ return this._agent.fetch(request);
257
242
  }
258
243
  default:
259
244
  return new Response(
@@ -272,19 +257,19 @@ var _McpAgent = class _McpAgent extends DurableObject {
272
257
  return websockets[0];
273
258
  }
274
259
  getWebSocketForResponseID(id) {
275
- const connectionId = __privateGet(this, _requestIdToConnectionId).get(id);
260
+ const connectionId = this._requestIdToConnectionId.get(id);
276
261
  if (connectionId === void 0) {
277
262
  return null;
278
263
  }
279
- return __privateGet(this, _agent).getConnection(connectionId) ?? null;
264
+ return this._agent.getConnection(connectionId) ?? null;
280
265
  }
281
266
  // All messages received here. This is currently never called
282
267
  async onMessage(connection, event) {
283
- if (__privateGet(this, _transportType) !== "streamable-http") {
268
+ if (this._transportType !== "streamable-http") {
284
269
  const err = new Error(
285
270
  "Internal Server Error: Expected streamable-http protocol"
286
271
  );
287
- __privateGet(this, _transport)?.onerror?.(err);
272
+ this._transport?.onerror?.(err);
288
273
  return;
289
274
  }
290
275
  let message;
@@ -292,21 +277,21 @@ var _McpAgent = class _McpAgent extends DurableObject {
292
277
  const data = typeof event === "string" ? event : new TextDecoder().decode(event);
293
278
  message = JSONRPCMessageSchema.parse(JSON.parse(data));
294
279
  } catch (error) {
295
- __privateGet(this, _transport)?.onerror?.(error);
280
+ this._transport?.onerror?.(error);
296
281
  return;
297
282
  }
298
283
  if (isJSONRPCRequest(message)) {
299
- __privateGet(this, _requestIdToConnectionId).set(message.id.toString(), connection.id);
284
+ this._requestIdToConnectionId.set(message.id.toString(), connection.id);
300
285
  }
301
- __privateGet(this, _transport)?.onmessage?.(message);
286
+ this._transport?.onmessage?.(message);
302
287
  }
303
288
  // All messages received over SSE after the initial connection has been established
304
289
  // will be passed here
305
290
  async onSSEMcpMessage(sessionId, request) {
306
- if (__privateGet(this, _status) !== "started") {
307
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
291
+ if (this._status !== "started") {
292
+ await this._initialize();
308
293
  }
309
- if (__privateGet(this, _transportType) !== "sse") {
294
+ if (this._transportType !== "sse") {
310
295
  return new Error("Internal Server Error: Expected SSE protocol");
311
296
  }
312
297
  try {
@@ -315,35 +300,36 @@ var _McpAgent = class _McpAgent extends DurableObject {
315
300
  try {
316
301
  parsedMessage = JSONRPCMessageSchema.parse(message);
317
302
  } catch (error) {
318
- __privateGet(this, _transport)?.onerror?.(error);
303
+ this._transport?.onerror?.(error);
319
304
  throw error;
320
305
  }
321
- __privateGet(this, _transport)?.onmessage?.(parsedMessage);
306
+ this._transport?.onmessage?.(parsedMessage);
322
307
  return null;
323
308
  } catch (error) {
324
- __privateGet(this, _transport)?.onerror?.(error);
309
+ console.error("Error forwarding message to SSE:", error);
310
+ this._transport?.onerror?.(error);
325
311
  return error;
326
312
  }
327
313
  }
328
314
  // Delegate all websocket events to the underlying agent
329
315
  async webSocketMessage(ws, event) {
330
- if (__privateGet(this, _status) !== "started") {
331
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
316
+ if (this._status !== "started") {
317
+ await this._initialize();
332
318
  }
333
- return await __privateGet(this, _agent).webSocketMessage(ws, event);
319
+ return await this._agent.webSocketMessage(ws, event);
334
320
  }
335
321
  // WebSocket event handlers for hibernation support
336
322
  async webSocketError(ws, error) {
337
- if (__privateGet(this, _status) !== "started") {
338
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
323
+ if (this._status !== "started") {
324
+ await this._initialize();
339
325
  }
340
- return await __privateGet(this, _agent).webSocketError(ws, error);
326
+ return await this._agent.webSocketError(ws, error);
341
327
  }
342
328
  async webSocketClose(ws, code, reason, wasClean) {
343
- if (__privateGet(this, _status) !== "started") {
344
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
329
+ if (this._status !== "started") {
330
+ await this._initialize();
345
331
  }
346
- return await __privateGet(this, _agent).webSocketClose(ws, code, reason, wasClean);
332
+ return await this._agent.webSocketClose(ws, code, reason, wasClean);
347
333
  }
348
334
  static mount(path, {
349
335
  binding = "MCP_OBJECT",
@@ -373,7 +359,7 @@ var _McpAgent = class _McpAgent extends DurableObject {
373
359
  );
374
360
  return new Response("Invalid binding", { status: 500 });
375
361
  }
376
- if (bindingValue.toString() !== "[object DurableObjectNamespace]") {
362
+ if (!isDurableObjectNamespace(bindingValue)) {
377
363
  return new Response("Invalid binding", { status: 500 });
378
364
  }
379
365
  const namespace = bindingValue;
@@ -457,7 +443,7 @@ data: ${JSON.stringify(result.data)}
457
443
  "Content-Type": "text/event-stream",
458
444
  "Cache-Control": "no-cache",
459
445
  Connection: "keep-alive",
460
- "Access-Control-Allow-Origin": corsOptions?.origin || "*"
446
+ ...corsHeaders(request, corsOptions)
461
447
  }
462
448
  });
463
449
  }
@@ -497,7 +483,7 @@ data: ${JSON.stringify(result.data)}
497
483
  "Content-Type": "text/event-stream",
498
484
  "Cache-Control": "no-cache",
499
485
  Connection: "keep-alive",
500
- "Access-Control-Allow-Origin": corsOptions?.origin || "*"
486
+ ...corsHeaders(request, corsOptions)
501
487
  }
502
488
  });
503
489
  }
@@ -507,7 +493,7 @@ data: ${JSON.stringify(result.data)}
507
493
  "Content-Type": "text/event-stream",
508
494
  "Cache-Control": "no-cache",
509
495
  Connection: "keep-alive",
510
- "Access-Control-Allow-Origin": corsOptions?.origin || "*"
496
+ ...corsHeaders(request, corsOptions)
511
497
  }
512
498
  });
513
499
  }
@@ -538,7 +524,7 @@ data: ${JSON.stringify(result.data)}
538
524
  );
539
525
  return new Response("Invalid binding", { status: 500 });
540
526
  }
541
- if (bindingValue.toString() !== "[object DurableObjectNamespace]") {
527
+ if (!isDurableObjectNamespace(bindingValue)) {
542
528
  return new Response("Invalid binding", { status: 500 });
543
529
  }
544
530
  const namespace = bindingValue;
@@ -659,6 +645,7 @@ data: ${JSON.stringify(result.data)}
659
645
  const doStub = namespace.get(id);
660
646
  const isInitialized = await doStub.isInitialized();
661
647
  if (isInitializationRequest) {
648
+ await doStub._init(ctx.props);
662
649
  await doStub.setInitialized();
663
650
  } else if (!isInitialized) {
664
651
  const body2 = JSON.stringify({
@@ -754,7 +741,10 @@ data: ${JSON.stringify(result.data)}
754
741
  ws.send(JSON.stringify(message));
755
742
  }
756
743
  ws.close();
757
- return new Response(null, { status: 202 });
744
+ return new Response(null, {
745
+ status: 202,
746
+ headers: corsHeaders(request, corsOptions)
747
+ });
758
748
  }
759
749
  for (const message of messages) {
760
750
  if (isJSONRPCRequest(message)) {
@@ -768,7 +758,7 @@ data: ${JSON.stringify(result.data)}
768
758
  "Cache-Control": "no-cache",
769
759
  Connection: "keep-alive",
770
760
  "mcp-session-id": sessionId,
771
- "Access-Control-Allow-Origin": corsOptions?.origin || "*"
761
+ ...corsHeaders(request, corsOptions)
772
762
  },
773
763
  status: 200
774
764
  });
@@ -786,20 +776,6 @@ data: ${JSON.stringify(result.data)}
786
776
  };
787
777
  }
788
778
  };
789
- _status = new WeakMap();
790
- _transport = new WeakMap();
791
- _transportType = new WeakMap();
792
- _requestIdToConnectionId = new WeakMap();
793
- _agent = new WeakMap();
794
- _McpAgent_instances = new WeakSet();
795
- initialize_fn = async function() {
796
- await this.ctx.blockConcurrencyWhile(async () => {
797
- __privateSet(this, _status, "starting");
798
- await this.onStart();
799
- __privateSet(this, _status, "started");
800
- });
801
- };
802
- var McpAgent = _McpAgent;
803
779
  export {
804
780
  McpAgent
805
781
  };