agents 0.0.79 → 0.0.81

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.
@@ -1,5 +1,5 @@
1
1
  import * as zod from 'zod';
2
- import { ClientCapabilities, Tool, Prompt, Resource, ResourceTemplate, ServerCapabilities, CallToolRequest, CallToolResultSchema, CompatibilityCallToolResultSchema, ReadResourceRequest, GetPromptRequest } from '@modelcontextprotocol/sdk/types.js';
2
+ import { Tool, Prompt, Resource, ResourceTemplate, ServerCapabilities, CallToolRequest, CallToolResultSchema, CompatibilityCallToolResultSchema, ReadResourceRequest, GetPromptRequest } from '@modelcontextprotocol/sdk/types.js';
3
3
  import { Client } from '@modelcontextprotocol/sdk/client/index.js';
4
4
  import { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
5
5
  import { AgentsOAuthProvider } from './do-oauth-client-provider.js';
@@ -15,7 +15,6 @@ declare class MCPClientConnection {
15
15
  authProvider?: AgentsOAuthProvider;
16
16
  };
17
17
  client: ConstructorParameters<typeof Client>[1];
18
- capabilities: ClientCapabilities;
19
18
  };
20
19
  client: Client;
21
20
  connectionState: "authenticating" | "connecting" | "ready" | "discovering" | "failed";
@@ -30,7 +29,6 @@ declare class MCPClientConnection {
30
29
  authProvider?: AgentsOAuthProvider;
31
30
  };
32
31
  client: ConstructorParameters<typeof Client>[1];
33
- capabilities: ClientCapabilities;
34
32
  });
35
33
  /**
36
34
  * Initialize a client connection
@@ -38,7 +36,7 @@ declare class MCPClientConnection {
38
36
  * @param code Optional OAuth code to initialize the connection with if auth hasn't been initialized
39
37
  * @returns
40
38
  */
41
- init(code?: string, clientId?: string): Promise<void>;
39
+ init(code?: string): Promise<void>;
42
40
  /**
43
41
  * Notification handler registration
44
42
  */
@@ -68,10 +66,10 @@ declare class MCPClientConnection {
68
66
  }[]>;
69
67
  fetchResources(): Promise<{
70
68
  [x: string]: unknown;
71
- uri: string;
72
69
  name: string;
73
- mimeType?: string | undefined;
70
+ uri: string;
74
71
  description?: string | undefined;
72
+ mimeType?: string | undefined;
75
73
  }[]>;
76
74
  fetchPrompts(): Promise<{
77
75
  [x: string]: unknown;
@@ -88,8 +86,8 @@ declare class MCPClientConnection {
88
86
  [x: string]: unknown;
89
87
  name: string;
90
88
  uriTemplate: string;
91
- mimeType?: string | undefined;
92
89
  description?: string | undefined;
90
+ mimeType?: string | undefined;
93
91
  }[]>;
94
92
  }
95
93
 
@@ -97,16 +95,16 @@ declare class MCPClientConnection {
97
95
  * Utility class that aggregates multiple MCP clients into one
98
96
  */
99
97
  declare class MCPClientManager {
100
- private name;
101
- private version;
98
+ private _name;
99
+ private _version;
102
100
  mcpConnections: Record<string, MCPClientConnection>;
103
- private callbackUrls;
101
+ private _callbackUrls;
104
102
  /**
105
- * @param name Name of the MCP client
106
- * @param version Version of the MCP Client
103
+ * @param _name Name of the MCP client
104
+ * @param _version Version of the MCP Client
107
105
  * @param auth Auth paramters if being used to create a DurableObjectOAuthClientProvider
108
106
  */
109
- constructor(name: string, version: string);
107
+ constructor(_name: string, _version: string);
110
108
  /**
111
109
  * Connect to and register an MCP server
112
110
  *
@@ -124,10 +122,10 @@ declare class MCPClientManager {
124
122
  authProvider?: AgentsOAuthProvider;
125
123
  };
126
124
  client?: ConstructorParameters<typeof Client>[1];
127
- capabilities?: ClientCapabilities;
128
125
  }): Promise<{
129
126
  id: string;
130
- authUrl: string | undefined;
127
+ authUrl?: string;
128
+ clientId?: string;
131
129
  }>;
132
130
  isCallbackRequest(req: Request): boolean;
133
131
  handleCallbackRequest(req: Request): Promise<{
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  MCPClientManager,
3
3
  getNamespacedData
4
- } from "../chunk-7VFQNJFK.js";
5
- import "../chunk-HMLY7DHA.js";
4
+ } from "../chunk-Y67CHZBI.js";
5
+ import "../chunk-NOUFNU2O.js";
6
6
  export {
7
7
  MCPClientManager,
8
8
  getNamespacedData
@@ -10,9 +10,9 @@ declare class DurableObjectOAuthClientProvider implements AgentsOAuthProvider {
10
10
  storage: DurableObjectStorage;
11
11
  clientName: string;
12
12
  baseRedirectUrl: string;
13
- private authUrl_;
14
- private serverId_;
15
- private clientId_;
13
+ private _authUrl_;
14
+ private _serverId_;
15
+ private _clientId_;
16
16
  constructor(storage: DurableObjectStorage, clientName: string, baseRedirectUrl: string);
17
17
  get clientMetadata(): OAuthClientMetadata;
18
18
  get redirectUrl(): string;
@@ -1,106 +1,7 @@
1
- import "../chunk-HMLY7DHA.js";
2
-
3
- // src/mcp/do-oauth-client-provider.ts
4
- var DurableObjectOAuthClientProvider = class {
5
- constructor(storage, clientName, baseRedirectUrl) {
6
- this.storage = storage;
7
- this.clientName = clientName;
8
- this.baseRedirectUrl = baseRedirectUrl;
9
- }
10
- get clientMetadata() {
11
- return {
12
- redirect_uris: [this.redirectUrl],
13
- token_endpoint_auth_method: "none",
14
- grant_types: ["authorization_code", "refresh_token"],
15
- response_types: ["code"],
16
- client_name: this.clientName,
17
- client_uri: "example.com"
18
- };
19
- }
20
- get redirectUrl() {
21
- return `${this.baseRedirectUrl}/${this.serverId}`;
22
- }
23
- get clientId() {
24
- if (!this.clientId_) {
25
- throw new Error("Trying to access clientId before it was set");
26
- }
27
- return this.clientId_;
28
- }
29
- set clientId(clientId_) {
30
- this.clientId_ = clientId_;
31
- }
32
- get serverId() {
33
- if (!this.serverId_) {
34
- throw new Error("Trying to access serverId before it was set");
35
- }
36
- return this.serverId_;
37
- }
38
- set serverId(serverId_) {
39
- this.serverId_ = serverId_;
40
- }
41
- keyPrefix(clientId) {
42
- return `/${this.clientName}/${this.serverId}/${clientId}`;
43
- }
44
- clientInfoKey(clientId) {
45
- return `${this.keyPrefix(clientId)}/client_info/`;
46
- }
47
- async clientInformation() {
48
- if (!this.clientId_) {
49
- return void 0;
50
- }
51
- return await this.storage.get(
52
- this.clientInfoKey(this.clientId)
53
- ) ?? void 0;
54
- }
55
- async saveClientInformation(clientInformation) {
56
- await this.storage.put(
57
- this.clientInfoKey(clientInformation.client_id),
58
- clientInformation
59
- );
60
- this.clientId = clientInformation.client_id;
61
- }
62
- tokenKey(clientId) {
63
- return `${this.keyPrefix(clientId)}/token`;
64
- }
65
- async tokens() {
66
- if (!this.clientId_) {
67
- return void 0;
68
- }
69
- return await this.storage.get(this.tokenKey(this.clientId)) ?? void 0;
70
- }
71
- async saveTokens(tokens) {
72
- await this.storage.put(this.tokenKey(this.clientId), tokens);
73
- }
74
- get authUrl() {
75
- return this.authUrl_;
76
- }
77
- /**
78
- * Because this operates on the server side (but we need browser auth), we send this url back to the user
79
- * and require user interact to initiate the redirect flow
80
- */
81
- async redirectToAuthorization(authUrl) {
82
- const client_id = authUrl.searchParams.get("client_id");
83
- if (client_id) {
84
- authUrl.searchParams.append("state", client_id);
85
- }
86
- this.authUrl_ = authUrl.toString();
87
- }
88
- codeVerifierKey(clientId) {
89
- return `${this.keyPrefix(clientId)}/code_verifier`;
90
- }
91
- async saveCodeVerifier(verifier) {
92
- await this.storage.put(this.codeVerifierKey(this.clientId), verifier);
93
- }
94
- async codeVerifier() {
95
- const codeVerifier = await this.storage.get(
96
- this.codeVerifierKey(this.clientId)
97
- );
98
- if (!codeVerifier) {
99
- throw new Error("No code verifier found");
100
- }
101
- return codeVerifier;
102
- }
103
- };
1
+ import {
2
+ DurableObjectOAuthClientProvider
3
+ } from "../chunk-BZXOAZUX.js";
4
+ import "../chunk-NOUFNU2O.js";
104
5
  export {
105
6
  DurableObjectOAuthClientProvider
106
7
  };
@@ -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":[]}
@@ -22,7 +22,15 @@ interface CORSOptions {
22
22
  }
23
23
  type MaybePromise<T> = T | Promise<T>;
24
24
  declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Record<string, unknown> = Record<string, unknown>> extends DurableObject<Env> {
25
- #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;
26
34
  get mcp(): MCPClientManager;
27
35
  protected constructor(ctx: DurableObjectState, env: Env);
28
36
  /**
@@ -44,6 +52,7 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
44
52
  _init(props: Props): Promise<void>;
45
53
  setInitialized(): Promise<void>;
46
54
  isInitialized(): Promise<boolean>;
55
+ private _initialize;
47
56
  fetch(request: Request): Promise<Response>;
48
57
  getWebSocket(): WebSocket | null;
49
58
  getWebSocketForResponseID(id: string): WebSocket | null;
package/dist/mcp/index.js CHANGED
@@ -1,13 +1,10 @@
1
1
  import {
2
2
  Agent
3
- } from "../chunk-JR3NW4A7.js";
4
- import "../chunk-7VFQNJFK.js";
5
- import {
6
- __privateAdd,
7
- __privateGet,
8
- __privateMethod,
9
- __privateSet
10
- } from "../chunk-HMLY7DHA.js";
3
+ } from "../chunk-NPGUKHFR.js";
4
+ import "../chunk-BZXOAZUX.js";
5
+ import "../chunk-QSGN3REV.js";
6
+ import "../chunk-Y67CHZBI.js";
7
+ import "../chunk-NOUFNU2O.js";
11
8
 
12
9
  // src/mcp/index.ts
13
10
  import { DurableObject } from "cloudflare:workers";
@@ -36,24 +33,22 @@ function handleCORS(request, corsOptions) {
36
33
  }
37
34
  return null;
38
35
  }
39
- var _getWebSocket, _started;
40
36
  var McpSSETransport = class {
41
37
  constructor(getWebSocket) {
42
- __privateAdd(this, _getWebSocket);
43
- __privateAdd(this, _started, false);
44
- __privateSet(this, _getWebSocket, getWebSocket);
38
+ this._started = false;
39
+ this._getWebSocket = getWebSocket;
45
40
  }
46
41
  async start() {
47
- if (__privateGet(this, _started)) {
42
+ if (this._started) {
48
43
  throw new Error("Transport already started");
49
44
  }
50
- __privateSet(this, _started, true);
45
+ this._started = true;
51
46
  }
52
47
  async send(message) {
53
- if (!__privateGet(this, _started)) {
48
+ if (!this._started) {
54
49
  throw new Error("Transport not started");
55
50
  }
56
- const websocket = __privateGet(this, _getWebSocket).call(this);
51
+ const websocket = this._getWebSocket();
57
52
  if (!websocket) {
58
53
  throw new Error("WebSocket not connected");
59
54
  }
@@ -68,52 +63,40 @@ var McpSSETransport = class {
68
63
  this.onclose?.();
69
64
  }
70
65
  };
71
- _getWebSocket = new WeakMap();
72
- _started = new WeakMap();
73
- var _getWebSocketForGetRequest, _getWebSocketForMessageID, _notifyResponseIdSent, _started2;
74
66
  var McpStreamableHttpTransport = class {
75
67
  constructor(getWebSocketForMessageID, notifyResponseIdSent) {
76
- // TODO: If there is an open connection to send server-initiated messages
77
- // back, we should use that connection
78
- __privateAdd(this, _getWebSocketForGetRequest);
79
- // Get the appropriate websocket connection for a given message id
80
- __privateAdd(this, _getWebSocketForMessageID);
81
- // Notify the server that a response has been sent for a given message id
82
- // so that it may clean up it's mapping of message ids to connections
83
- // once they are no longer needed
84
- __privateAdd(this, _notifyResponseIdSent);
85
- __privateAdd(this, _started2, false);
86
- __privateSet(this, _getWebSocketForMessageID, getWebSocketForMessageID);
87
- __privateSet(this, _notifyResponseIdSent, notifyResponseIdSent);
88
- __privateSet(this, _getWebSocketForGetRequest, () => null);
68
+ this._started = false;
69
+ this._getWebSocketForMessageID = getWebSocketForMessageID;
70
+ this._notifyResponseIdSent = notifyResponseIdSent;
71
+ this._getWebSocketForGetRequest = () => null;
89
72
  }
90
73
  async start() {
91
- if (__privateGet(this, _started2)) {
74
+ if (this._started) {
92
75
  throw new Error("Transport already started");
93
76
  }
94
- __privateSet(this, _started2, true);
77
+ this._started = true;
95
78
  }
96
79
  async send(message) {
97
- if (!__privateGet(this, _started2)) {
80
+ if (!this._started) {
98
81
  throw new Error("Transport not started");
99
82
  }
100
83
  let websocket = null;
101
84
  if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
102
- websocket = __privateGet(this, _getWebSocketForMessageID).call(this, message.id.toString());
85
+ websocket = this._getWebSocketForMessageID(message.id.toString());
103
86
  if (!websocket) {
104
87
  throw new Error(
105
88
  `Could not find WebSocket for message id: ${message.id}`
106
89
  );
107
90
  }
108
91
  } else if (isJSONRPCRequest(message)) {
109
- websocket = __privateGet(this, _getWebSocketForGetRequest).call(this);
92
+ websocket = this._getWebSocketForGetRequest();
110
93
  } else if (isJSONRPCNotification(message)) {
111
94
  websocket = null;
112
95
  }
113
96
  try {
114
97
  websocket?.send(JSON.stringify(message));
115
98
  if (isJSONRPCResponse(message)) {
116
- __privateGet(this, _notifyResponseIdSent).call(this, message.id.toString());
99
+ this._notifyResponseIdSent(message.id.toString());
117
100
  }
118
101
  } catch (error) {
119
102
  this.onerror?.(error);
@@ -124,28 +107,16 @@ var McpStreamableHttpTransport = class {
124
107
  this.onclose?.();
125
108
  }
126
109
  };
127
- _getWebSocketForGetRequest = new WeakMap();
128
- _getWebSocketForMessageID = new WeakMap();
129
- _notifyResponseIdSent = new WeakMap();
130
- _started2 = new WeakMap();
131
- var _status, _transport, _transportType, _requestIdToConnectionId, _agent, _McpAgent_instances, initialize_fn;
132
- var _McpAgent = class _McpAgent extends DurableObject {
110
+ var McpAgent = class _McpAgent extends DurableObject {
133
111
  constructor(ctx, env) {
134
112
  var _a;
135
113
  super(ctx, env);
136
- __privateAdd(this, _McpAgent_instances);
137
- __privateAdd(this, _status, "zero");
138
- __privateAdd(this, _transport);
139
- __privateAdd(this, _transportType, "unset");
140
- __privateAdd(this, _requestIdToConnectionId, /* @__PURE__ */ new Map());
141
- /**
142
- * Since McpAgent's _aren't_ yet real "Agents", let's only expose a couple of the methods
143
- * to the outer class: initialState/state/setState/onStateUpdate/sql
144
- */
145
- __privateAdd(this, _agent);
114
+ this._status = "zero";
115
+ this._transportType = "unset";
116
+ this._requestIdToConnectionId = /* @__PURE__ */ new Map();
146
117
  this.initRun = false;
147
118
  const self = this;
148
- __privateSet(this, _agent, new (_a = class extends Agent {
119
+ this._agent = new (_a = class extends Agent {
149
120
  onStateUpdate(state, source) {
150
121
  return self.onStateUpdate(state, source);
151
122
  }
@@ -154,26 +125,26 @@ var _McpAgent = class _McpAgent extends DurableObject {
154
125
  }
155
126
  }, _a.options = {
156
127
  hibernate: true
157
- }, _a)(ctx, env));
128
+ }, _a)(ctx, env);
158
129
  }
159
130
  get mcp() {
160
- return __privateGet(this, _agent).mcp;
131
+ return this._agent.mcp;
161
132
  }
162
133
  get state() {
163
- return __privateGet(this, _agent).state;
134
+ return this._agent.state;
164
135
  }
165
136
  sql(strings, ...values) {
166
- return __privateGet(this, _agent).sql(strings, ...values);
137
+ return this._agent.sql(strings, ...values);
167
138
  }
168
139
  setState(state) {
169
- return __privateGet(this, _agent).setState(state);
140
+ return this._agent.setState(state);
170
141
  }
171
142
  onStateUpdate(state, source) {
172
143
  }
173
144
  async onStart() {
174
145
  var _a;
175
146
  const self = this;
176
- __privateSet(this, _agent, new (_a = class extends Agent {
147
+ this._agent = new (_a = class extends Agent {
177
148
  constructor() {
178
149
  super(...arguments);
179
150
  this.initialState = self.initialState;
@@ -186,22 +157,22 @@ var _McpAgent = class _McpAgent extends DurableObject {
186
157
  }
187
158
  }, _a.options = {
188
159
  hibernate: true
189
- }, _a)(this.ctx, this.env));
160
+ }, _a)(this.ctx, this.env);
190
161
  this.props = await this.ctx.storage.get("props");
191
- __privateSet(this, _transportType, await this.ctx.storage.get(
162
+ this._transportType = await this.ctx.storage.get(
192
163
  "transportType"
193
- ));
164
+ );
194
165
  await this._init(this.props);
195
166
  const server = await this.server;
196
- if (__privateGet(this, _transportType) === "sse") {
197
- __privateSet(this, _transport, new McpSSETransport(() => this.getWebSocket()));
198
- await server.connect(__privateGet(this, _transport));
199
- } else if (__privateGet(this, _transportType) === "streamable-http") {
200
- __privateSet(this, _transport, new McpStreamableHttpTransport(
167
+ if (this._transportType === "sse") {
168
+ this._transport = new McpSSETransport(() => this.getWebSocket());
169
+ await server.connect(this._transport);
170
+ } else if (this._transportType === "streamable-http") {
171
+ this._transport = new McpStreamableHttpTransport(
201
172
  (id) => this.getWebSocketForResponseID(id),
202
- (id) => __privateGet(this, _requestIdToConnectionId).delete(id)
203
- ));
204
- await server.connect(__privateGet(this, _transport));
173
+ (id) => this._requestIdToConnectionId.delete(id)
174
+ );
175
+ await server.connect(this._transport);
205
176
  }
206
177
  }
207
178
  async _init(props) {
@@ -221,10 +192,17 @@ var _McpAgent = class _McpAgent extends DurableObject {
221
192
  async isInitialized() {
222
193
  return await this.ctx.storage.get("initialized") === true;
223
194
  }
195
+ async _initialize() {
196
+ await this.ctx.blockConcurrencyWhile(async () => {
197
+ this._status = "starting";
198
+ await this.onStart();
199
+ this._status = "started";
200
+ });
201
+ }
224
202
  // Allow the worker to fetch a websocket connection to the agent
225
203
  async fetch(request) {
226
- if (__privateGet(this, _status) !== "started") {
227
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
204
+ if (this._status !== "started") {
205
+ await this._initialize();
228
206
  }
229
207
  if (request.headers.get("Upgrade") !== "websocket") {
230
208
  return new Response("Expected WebSocket Upgrade request", {
@@ -241,24 +219,24 @@ var _McpAgent = class _McpAgent extends DurableObject {
241
219
  return new Response("Websocket already connected", { status: 400 });
242
220
  }
243
221
  await this.ctx.storage.put("transportType", "sse");
244
- __privateSet(this, _transportType, "sse");
245
- if (!__privateGet(this, _transport)) {
246
- __privateSet(this, _transport, new McpSSETransport(() => this.getWebSocket()));
247
- await server.connect(__privateGet(this, _transport));
222
+ this._transportType = "sse";
223
+ if (!this._transport) {
224
+ this._transport = new McpSSETransport(() => this.getWebSocket());
225
+ await server.connect(this._transport);
248
226
  }
249
- return __privateGet(this, _agent).fetch(request);
227
+ return this._agent.fetch(request);
250
228
  }
251
229
  case "/streamable-http": {
252
- if (!__privateGet(this, _transport)) {
253
- __privateSet(this, _transport, new McpStreamableHttpTransport(
230
+ if (!this._transport) {
231
+ this._transport = new McpStreamableHttpTransport(
254
232
  (id) => this.getWebSocketForResponseID(id),
255
- (id) => __privateGet(this, _requestIdToConnectionId).delete(id)
256
- ));
257
- await server.connect(__privateGet(this, _transport));
233
+ (id) => this._requestIdToConnectionId.delete(id)
234
+ );
235
+ await server.connect(this._transport);
258
236
  }
259
237
  await this.ctx.storage.put("transportType", "streamable-http");
260
- __privateSet(this, _transportType, "streamable-http");
261
- return __privateGet(this, _agent).fetch(request);
238
+ this._transportType = "streamable-http";
239
+ return this._agent.fetch(request);
262
240
  }
263
241
  default:
264
242
  return new Response(
@@ -277,19 +255,19 @@ var _McpAgent = class _McpAgent extends DurableObject {
277
255
  return websockets[0];
278
256
  }
279
257
  getWebSocketForResponseID(id) {
280
- const connectionId = __privateGet(this, _requestIdToConnectionId).get(id);
258
+ const connectionId = this._requestIdToConnectionId.get(id);
281
259
  if (connectionId === void 0) {
282
260
  return null;
283
261
  }
284
- return __privateGet(this, _agent).getConnection(connectionId) ?? null;
262
+ return this._agent.getConnection(connectionId) ?? null;
285
263
  }
286
264
  // All messages received here. This is currently never called
287
265
  async onMessage(connection, event) {
288
- if (__privateGet(this, _transportType) !== "streamable-http") {
266
+ if (this._transportType !== "streamable-http") {
289
267
  const err = new Error(
290
268
  "Internal Server Error: Expected streamable-http protocol"
291
269
  );
292
- __privateGet(this, _transport)?.onerror?.(err);
270
+ this._transport?.onerror?.(err);
293
271
  return;
294
272
  }
295
273
  let message;
@@ -297,21 +275,21 @@ var _McpAgent = class _McpAgent extends DurableObject {
297
275
  const data = typeof event === "string" ? event : new TextDecoder().decode(event);
298
276
  message = JSONRPCMessageSchema.parse(JSON.parse(data));
299
277
  } catch (error) {
300
- __privateGet(this, _transport)?.onerror?.(error);
278
+ this._transport?.onerror?.(error);
301
279
  return;
302
280
  }
303
281
  if (isJSONRPCRequest(message)) {
304
- __privateGet(this, _requestIdToConnectionId).set(message.id.toString(), connection.id);
282
+ this._requestIdToConnectionId.set(message.id.toString(), connection.id);
305
283
  }
306
- __privateGet(this, _transport)?.onmessage?.(message);
284
+ this._transport?.onmessage?.(message);
307
285
  }
308
286
  // All messages received over SSE after the initial connection has been established
309
287
  // will be passed here
310
288
  async onSSEMcpMessage(sessionId, request) {
311
- if (__privateGet(this, _status) !== "started") {
312
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
289
+ if (this._status !== "started") {
290
+ await this._initialize();
313
291
  }
314
- if (__privateGet(this, _transportType) !== "sse") {
292
+ if (this._transportType !== "sse") {
315
293
  return new Error("Internal Server Error: Expected SSE protocol");
316
294
  }
317
295
  try {
@@ -320,36 +298,36 @@ var _McpAgent = class _McpAgent extends DurableObject {
320
298
  try {
321
299
  parsedMessage = JSONRPCMessageSchema.parse(message);
322
300
  } catch (error) {
323
- __privateGet(this, _transport)?.onerror?.(error);
301
+ this._transport?.onerror?.(error);
324
302
  throw error;
325
303
  }
326
- __privateGet(this, _transport)?.onmessage?.(parsedMessage);
304
+ this._transport?.onmessage?.(parsedMessage);
327
305
  return null;
328
306
  } catch (error) {
329
307
  console.error("Error forwarding message to SSE:", error);
330
- __privateGet(this, _transport)?.onerror?.(error);
308
+ this._transport?.onerror?.(error);
331
309
  return error;
332
310
  }
333
311
  }
334
312
  // Delegate all websocket events to the underlying agent
335
313
  async webSocketMessage(ws, event) {
336
- if (__privateGet(this, _status) !== "started") {
337
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
314
+ if (this._status !== "started") {
315
+ await this._initialize();
338
316
  }
339
- return await __privateGet(this, _agent).webSocketMessage(ws, event);
317
+ return await this._agent.webSocketMessage(ws, event);
340
318
  }
341
319
  // WebSocket event handlers for hibernation support
342
320
  async webSocketError(ws, error) {
343
- if (__privateGet(this, _status) !== "started") {
344
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
321
+ if (this._status !== "started") {
322
+ await this._initialize();
345
323
  }
346
- return await __privateGet(this, _agent).webSocketError(ws, error);
324
+ return await this._agent.webSocketError(ws, error);
347
325
  }
348
326
  async webSocketClose(ws, code, reason, wasClean) {
349
- if (__privateGet(this, _status) !== "started") {
350
- await __privateMethod(this, _McpAgent_instances, initialize_fn).call(this);
327
+ if (this._status !== "started") {
328
+ await this._initialize();
351
329
  }
352
- return await __privateGet(this, _agent).webSocketClose(ws, code, reason, wasClean);
330
+ return await this._agent.webSocketClose(ws, code, reason, wasClean);
353
331
  }
354
332
  static mount(path, {
355
333
  binding = "MCP_OBJECT",
@@ -796,20 +774,6 @@ data: ${JSON.stringify(result.data)}
796
774
  };
797
775
  }
798
776
  };
799
- _status = new WeakMap();
800
- _transport = new WeakMap();
801
- _transportType = new WeakMap();
802
- _requestIdToConnectionId = new WeakMap();
803
- _agent = new WeakMap();
804
- _McpAgent_instances = new WeakSet();
805
- initialize_fn = async function() {
806
- await this.ctx.blockConcurrencyWhile(async () => {
807
- __privateSet(this, _status, "starting");
808
- await this.onStart();
809
- __privateSet(this, _status, "started");
810
- });
811
- };
812
- var McpAgent = _McpAgent;
813
777
  export {
814
778
  McpAgent
815
779
  };