agents 0.0.0-aa5f972 → 0.0.0-aacf837
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-chat-agent.d.ts +27 -2
- package/dist/ai-chat-agent.js +99 -103
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +13 -0
- package/dist/ai-react.js +3 -3
- package/dist/ai-react.js.map +1 -1
- package/dist/chunk-BZXOAZUX.js +106 -0
- package/dist/chunk-BZXOAZUX.js.map +1 -0
- package/dist/{chunk-AV3OMRR4.js → chunk-DVT5CDUP.js} +323 -137
- package/dist/chunk-DVT5CDUP.js.map +1 -0
- package/dist/{chunk-YZNSS675.js → chunk-IFXSRTKF.js} +56 -26
- package/dist/chunk-IFXSRTKF.js.map +1 -0
- package/dist/chunk-VCSB47AK.js +116 -0
- package/dist/chunk-VCSB47AK.js.map +1 -0
- package/dist/client.d.ts +15 -1
- package/dist/client.js +6 -126
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +111 -13
- package/dist/index.js +8 -7
- package/dist/mcp/client.d.ts +30 -15
- package/dist/mcp/client.js +1 -2
- package/dist/mcp/do-oauth-client-provider.d.ts +3 -3
- package/dist/mcp/do-oauth-client-provider.js +3 -103
- package/dist/mcp/do-oauth-client-provider.js.map +1 -1
- package/dist/mcp/index.d.ts +16 -5
- package/dist/mcp/index.js +108 -135
- package/dist/mcp/index.js.map +1 -1
- package/dist/react.d.ts +85 -5
- package/dist/react.js +14 -2
- package/dist/react.js.map +1 -1
- package/dist/schedule.js +0 -2
- package/dist/schedule.js.map +1 -1
- package/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/package.json +10 -7
- package/src/index.ts +385 -53
- package/dist/chunk-AV3OMRR4.js.map +0 -1
- package/dist/chunk-HMLY7DHA.js +0 -16
- package/dist/chunk-YZNSS675.js.map +0 -1
- /package/dist/{chunk-HMLY7DHA.js.map → serializable.js.map} +0 -0
package/dist/mcp/index.d.ts
CHANGED
|
@@ -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
|
-
|
|
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-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
__privateGet,
|
|
8
|
-
__privateMethod,
|
|
9
|
-
__privateSet
|
|
10
|
-
} from "../chunk-HMLY7DHA.js";
|
|
3
|
+
} from "../chunk-DVT5CDUP.js";
|
|
4
|
+
import "../chunk-IFXSRTKF.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,38 @@ import {
|
|
|
20
16
|
JSONRPCMessageSchema
|
|
21
17
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
22
18
|
var MAXIMUM_MESSAGE_SIZE_BYTES = 4 * 1024 * 1024;
|
|
23
|
-
function
|
|
24
|
-
const origin =
|
|
25
|
-
|
|
26
|
-
"Access-Control-Allow-Origin": corsOptions
|
|
27
|
-
"Access-Control-Allow-Methods": corsOptions
|
|
28
|
-
"Access-Control-Allow-Headers": corsOptions
|
|
29
|
-
"Access-Control-Max-Age": (corsOptions
|
|
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 handleCORS(request, corsOptions) {
|
|
31
30
|
if (request.method === "OPTIONS") {
|
|
32
|
-
return new Response(null, { headers: corsHeaders });
|
|
31
|
+
return new Response(null, { headers: corsHeaders(request, corsOptions) });
|
|
33
32
|
}
|
|
34
33
|
return null;
|
|
35
34
|
}
|
|
36
|
-
var _getWebSocket, _started;
|
|
37
35
|
var McpSSETransport = class {
|
|
38
36
|
constructor(getWebSocket) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
__privateSet(this, _getWebSocket, getWebSocket);
|
|
37
|
+
this._started = false;
|
|
38
|
+
this._getWebSocket = getWebSocket;
|
|
42
39
|
}
|
|
43
40
|
async start() {
|
|
44
|
-
if (
|
|
41
|
+
if (this._started) {
|
|
45
42
|
throw new Error("Transport already started");
|
|
46
43
|
}
|
|
47
|
-
|
|
44
|
+
this._started = true;
|
|
48
45
|
}
|
|
49
46
|
async send(message) {
|
|
50
|
-
if (!
|
|
47
|
+
if (!this._started) {
|
|
51
48
|
throw new Error("Transport not started");
|
|
52
49
|
}
|
|
53
|
-
const websocket =
|
|
50
|
+
const websocket = this._getWebSocket();
|
|
54
51
|
if (!websocket) {
|
|
55
52
|
throw new Error("WebSocket not connected");
|
|
56
53
|
}
|
|
@@ -65,52 +62,40 @@ var McpSSETransport = class {
|
|
|
65
62
|
this.onclose?.();
|
|
66
63
|
}
|
|
67
64
|
};
|
|
68
|
-
_getWebSocket = new WeakMap();
|
|
69
|
-
_started = new WeakMap();
|
|
70
|
-
var _getWebSocketForGetRequest, _getWebSocketForMessageID, _notifyResponseIdSent, _started2;
|
|
71
65
|
var McpStreamableHttpTransport = class {
|
|
72
66
|
constructor(getWebSocketForMessageID, notifyResponseIdSent) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
__privateAdd(this, _getWebSocketForMessageID);
|
|
78
|
-
// Notify the server that a response has been sent for a given message id
|
|
79
|
-
// so that it may clean up it's mapping of message ids to connections
|
|
80
|
-
// once they are no longer needed
|
|
81
|
-
__privateAdd(this, _notifyResponseIdSent);
|
|
82
|
-
__privateAdd(this, _started2, false);
|
|
83
|
-
__privateSet(this, _getWebSocketForMessageID, getWebSocketForMessageID);
|
|
84
|
-
__privateSet(this, _notifyResponseIdSent, notifyResponseIdSent);
|
|
85
|
-
__privateSet(this, _getWebSocketForGetRequest, () => null);
|
|
67
|
+
this._started = false;
|
|
68
|
+
this._getWebSocketForMessageID = getWebSocketForMessageID;
|
|
69
|
+
this._notifyResponseIdSent = notifyResponseIdSent;
|
|
70
|
+
this._getWebSocketForGetRequest = () => null;
|
|
86
71
|
}
|
|
87
72
|
async start() {
|
|
88
|
-
if (
|
|
73
|
+
if (this._started) {
|
|
89
74
|
throw new Error("Transport already started");
|
|
90
75
|
}
|
|
91
|
-
|
|
76
|
+
this._started = true;
|
|
92
77
|
}
|
|
93
78
|
async send(message) {
|
|
94
|
-
if (!
|
|
79
|
+
if (!this._started) {
|
|
95
80
|
throw new Error("Transport not started");
|
|
96
81
|
}
|
|
97
82
|
let websocket = null;
|
|
98
83
|
if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
|
|
99
|
-
websocket =
|
|
84
|
+
websocket = this._getWebSocketForMessageID(message.id.toString());
|
|
100
85
|
if (!websocket) {
|
|
101
86
|
throw new Error(
|
|
102
87
|
`Could not find WebSocket for message id: ${message.id}`
|
|
103
88
|
);
|
|
104
89
|
}
|
|
105
90
|
} else if (isJSONRPCRequest(message)) {
|
|
106
|
-
websocket =
|
|
91
|
+
websocket = this._getWebSocketForGetRequest();
|
|
107
92
|
} else if (isJSONRPCNotification(message)) {
|
|
108
93
|
websocket = null;
|
|
109
94
|
}
|
|
110
95
|
try {
|
|
111
96
|
websocket?.send(JSON.stringify(message));
|
|
112
97
|
if (isJSONRPCResponse(message)) {
|
|
113
|
-
|
|
98
|
+
this._notifyResponseIdSent(message.id.toString());
|
|
114
99
|
}
|
|
115
100
|
} catch (error) {
|
|
116
101
|
this.onerror?.(error);
|
|
@@ -121,28 +106,16 @@ var McpStreamableHttpTransport = class {
|
|
|
121
106
|
this.onclose?.();
|
|
122
107
|
}
|
|
123
108
|
};
|
|
124
|
-
|
|
125
|
-
_getWebSocketForMessageID = new WeakMap();
|
|
126
|
-
_notifyResponseIdSent = new WeakMap();
|
|
127
|
-
_started2 = new WeakMap();
|
|
128
|
-
var _status, _transport, _transportType, _requestIdToConnectionId, _agent, _McpAgent_instances, initialize_fn;
|
|
129
|
-
var _McpAgent = class _McpAgent extends DurableObject {
|
|
109
|
+
var McpAgent = class _McpAgent extends DurableObject {
|
|
130
110
|
constructor(ctx, env) {
|
|
131
111
|
var _a;
|
|
132
112
|
super(ctx, env);
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
__privateAdd(this, _transportType, "unset");
|
|
137
|
-
__privateAdd(this, _requestIdToConnectionId, /* @__PURE__ */ new Map());
|
|
138
|
-
/**
|
|
139
|
-
* Since McpAgent's _aren't_ yet real "Agents", let's only expose a couple of the methods
|
|
140
|
-
* to the outer class: initialState/state/setState/onStateUpdate/sql
|
|
141
|
-
*/
|
|
142
|
-
__privateAdd(this, _agent);
|
|
113
|
+
this._status = "zero";
|
|
114
|
+
this._transportType = "unset";
|
|
115
|
+
this._requestIdToConnectionId = /* @__PURE__ */ new Map();
|
|
143
116
|
this.initRun = false;
|
|
144
117
|
const self = this;
|
|
145
|
-
|
|
118
|
+
this._agent = new (_a = class extends Agent {
|
|
146
119
|
onStateUpdate(state, source) {
|
|
147
120
|
return self.onStateUpdate(state, source);
|
|
148
121
|
}
|
|
@@ -151,26 +124,26 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
151
124
|
}
|
|
152
125
|
}, _a.options = {
|
|
153
126
|
hibernate: true
|
|
154
|
-
}, _a)(ctx, env)
|
|
127
|
+
}, _a)(ctx, env);
|
|
155
128
|
}
|
|
156
129
|
get mcp() {
|
|
157
|
-
return
|
|
130
|
+
return this._agent.mcp;
|
|
158
131
|
}
|
|
159
132
|
get state() {
|
|
160
|
-
return
|
|
133
|
+
return this._agent.state;
|
|
161
134
|
}
|
|
162
135
|
sql(strings, ...values) {
|
|
163
|
-
return
|
|
136
|
+
return this._agent.sql(strings, ...values);
|
|
164
137
|
}
|
|
165
138
|
setState(state) {
|
|
166
|
-
return
|
|
139
|
+
return this._agent.setState(state);
|
|
167
140
|
}
|
|
168
141
|
onStateUpdate(state, source) {
|
|
169
142
|
}
|
|
170
143
|
async onStart() {
|
|
171
144
|
var _a;
|
|
172
145
|
const self = this;
|
|
173
|
-
|
|
146
|
+
this._agent = new (_a = class extends Agent {
|
|
174
147
|
constructor() {
|
|
175
148
|
super(...arguments);
|
|
176
149
|
this.initialState = self.initialState;
|
|
@@ -183,21 +156,22 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
183
156
|
}
|
|
184
157
|
}, _a.options = {
|
|
185
158
|
hibernate: true
|
|
186
|
-
}, _a)(this.ctx, this.env)
|
|
159
|
+
}, _a)(this.ctx, this.env);
|
|
187
160
|
this.props = await this.ctx.storage.get("props");
|
|
188
|
-
|
|
161
|
+
this._transportType = await this.ctx.storage.get(
|
|
189
162
|
"transportType"
|
|
190
|
-
)
|
|
163
|
+
);
|
|
191
164
|
await this._init(this.props);
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
165
|
+
const server = await this.server;
|
|
166
|
+
if (this._transportType === "sse") {
|
|
167
|
+
this._transport = new McpSSETransport(() => this.getWebSocket());
|
|
168
|
+
await server.connect(this._transport);
|
|
169
|
+
} else if (this._transportType === "streamable-http") {
|
|
170
|
+
this._transport = new McpStreamableHttpTransport(
|
|
197
171
|
(id) => this.getWebSocketForResponseID(id),
|
|
198
|
-
(id) =>
|
|
199
|
-
)
|
|
200
|
-
await
|
|
172
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
173
|
+
);
|
|
174
|
+
await server.connect(this._transport);
|
|
201
175
|
}
|
|
202
176
|
}
|
|
203
177
|
async _init(props) {
|
|
@@ -217,10 +191,17 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
217
191
|
async isInitialized() {
|
|
218
192
|
return await this.ctx.storage.get("initialized") === true;
|
|
219
193
|
}
|
|
194
|
+
async _initialize() {
|
|
195
|
+
await this.ctx.blockConcurrencyWhile(async () => {
|
|
196
|
+
this._status = "starting";
|
|
197
|
+
await this.onStart();
|
|
198
|
+
this._status = "started";
|
|
199
|
+
});
|
|
200
|
+
}
|
|
220
201
|
// Allow the worker to fetch a websocket connection to the agent
|
|
221
202
|
async fetch(request) {
|
|
222
|
-
if (
|
|
223
|
-
await
|
|
203
|
+
if (this._status !== "started") {
|
|
204
|
+
await this._initialize();
|
|
224
205
|
}
|
|
225
206
|
if (request.headers.get("Upgrade") !== "websocket") {
|
|
226
207
|
return new Response("Expected WebSocket Upgrade request", {
|
|
@@ -229,6 +210,7 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
229
210
|
}
|
|
230
211
|
const url = new URL(request.url);
|
|
231
212
|
const path = url.pathname;
|
|
213
|
+
const server = await this.server;
|
|
232
214
|
switch (path) {
|
|
233
215
|
case "/sse": {
|
|
234
216
|
const websockets = this.ctx.getWebSockets();
|
|
@@ -236,24 +218,24 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
236
218
|
return new Response("Websocket already connected", { status: 400 });
|
|
237
219
|
}
|
|
238
220
|
await this.ctx.storage.put("transportType", "sse");
|
|
239
|
-
|
|
240
|
-
if (!
|
|
241
|
-
|
|
242
|
-
await
|
|
221
|
+
this._transportType = "sse";
|
|
222
|
+
if (!this._transport) {
|
|
223
|
+
this._transport = new McpSSETransport(() => this.getWebSocket());
|
|
224
|
+
await server.connect(this._transport);
|
|
243
225
|
}
|
|
244
|
-
return
|
|
226
|
+
return this._agent.fetch(request);
|
|
245
227
|
}
|
|
246
228
|
case "/streamable-http": {
|
|
247
|
-
if (!
|
|
248
|
-
|
|
229
|
+
if (!this._transport) {
|
|
230
|
+
this._transport = new McpStreamableHttpTransport(
|
|
249
231
|
(id) => this.getWebSocketForResponseID(id),
|
|
250
|
-
(id) =>
|
|
251
|
-
)
|
|
252
|
-
await
|
|
232
|
+
(id) => this._requestIdToConnectionId.delete(id)
|
|
233
|
+
);
|
|
234
|
+
await server.connect(this._transport);
|
|
253
235
|
}
|
|
254
236
|
await this.ctx.storage.put("transportType", "streamable-http");
|
|
255
|
-
|
|
256
|
-
return
|
|
237
|
+
this._transportType = "streamable-http";
|
|
238
|
+
return this._agent.fetch(request);
|
|
257
239
|
}
|
|
258
240
|
default:
|
|
259
241
|
return new Response(
|
|
@@ -272,19 +254,19 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
272
254
|
return websockets[0];
|
|
273
255
|
}
|
|
274
256
|
getWebSocketForResponseID(id) {
|
|
275
|
-
const connectionId =
|
|
257
|
+
const connectionId = this._requestIdToConnectionId.get(id);
|
|
276
258
|
if (connectionId === void 0) {
|
|
277
259
|
return null;
|
|
278
260
|
}
|
|
279
|
-
return
|
|
261
|
+
return this._agent.getConnection(connectionId) ?? null;
|
|
280
262
|
}
|
|
281
263
|
// All messages received here. This is currently never called
|
|
282
264
|
async onMessage(connection, event) {
|
|
283
|
-
if (
|
|
265
|
+
if (this._transportType !== "streamable-http") {
|
|
284
266
|
const err = new Error(
|
|
285
267
|
"Internal Server Error: Expected streamable-http protocol"
|
|
286
268
|
);
|
|
287
|
-
|
|
269
|
+
this._transport?.onerror?.(err);
|
|
288
270
|
return;
|
|
289
271
|
}
|
|
290
272
|
let message;
|
|
@@ -292,21 +274,21 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
292
274
|
const data = typeof event === "string" ? event : new TextDecoder().decode(event);
|
|
293
275
|
message = JSONRPCMessageSchema.parse(JSON.parse(data));
|
|
294
276
|
} catch (error) {
|
|
295
|
-
|
|
277
|
+
this._transport?.onerror?.(error);
|
|
296
278
|
return;
|
|
297
279
|
}
|
|
298
280
|
if (isJSONRPCRequest(message)) {
|
|
299
|
-
|
|
281
|
+
this._requestIdToConnectionId.set(message.id.toString(), connection.id);
|
|
300
282
|
}
|
|
301
|
-
|
|
283
|
+
this._transport?.onmessage?.(message);
|
|
302
284
|
}
|
|
303
285
|
// All messages received over SSE after the initial connection has been established
|
|
304
286
|
// will be passed here
|
|
305
287
|
async onSSEMcpMessage(sessionId, request) {
|
|
306
|
-
if (
|
|
307
|
-
await
|
|
288
|
+
if (this._status !== "started") {
|
|
289
|
+
await this._initialize();
|
|
308
290
|
}
|
|
309
|
-
if (
|
|
291
|
+
if (this._transportType !== "sse") {
|
|
310
292
|
return new Error("Internal Server Error: Expected SSE protocol");
|
|
311
293
|
}
|
|
312
294
|
try {
|
|
@@ -315,35 +297,36 @@ var _McpAgent = class _McpAgent extends DurableObject {
|
|
|
315
297
|
try {
|
|
316
298
|
parsedMessage = JSONRPCMessageSchema.parse(message);
|
|
317
299
|
} catch (error) {
|
|
318
|
-
|
|
300
|
+
this._transport?.onerror?.(error);
|
|
319
301
|
throw error;
|
|
320
302
|
}
|
|
321
|
-
|
|
303
|
+
this._transport?.onmessage?.(parsedMessage);
|
|
322
304
|
return null;
|
|
323
305
|
} catch (error) {
|
|
324
|
-
|
|
306
|
+
console.error("Error forwarding message to SSE:", error);
|
|
307
|
+
this._transport?.onerror?.(error);
|
|
325
308
|
return error;
|
|
326
309
|
}
|
|
327
310
|
}
|
|
328
311
|
// Delegate all websocket events to the underlying agent
|
|
329
312
|
async webSocketMessage(ws, event) {
|
|
330
|
-
if (
|
|
331
|
-
await
|
|
313
|
+
if (this._status !== "started") {
|
|
314
|
+
await this._initialize();
|
|
332
315
|
}
|
|
333
|
-
return await
|
|
316
|
+
return await this._agent.webSocketMessage(ws, event);
|
|
334
317
|
}
|
|
335
318
|
// WebSocket event handlers for hibernation support
|
|
336
319
|
async webSocketError(ws, error) {
|
|
337
|
-
if (
|
|
338
|
-
await
|
|
320
|
+
if (this._status !== "started") {
|
|
321
|
+
await this._initialize();
|
|
339
322
|
}
|
|
340
|
-
return await
|
|
323
|
+
return await this._agent.webSocketError(ws, error);
|
|
341
324
|
}
|
|
342
325
|
async webSocketClose(ws, code, reason, wasClean) {
|
|
343
|
-
if (
|
|
344
|
-
await
|
|
326
|
+
if (this._status !== "started") {
|
|
327
|
+
await this._initialize();
|
|
345
328
|
}
|
|
346
|
-
return await
|
|
329
|
+
return await this._agent.webSocketClose(ws, code, reason, wasClean);
|
|
347
330
|
}
|
|
348
331
|
static mount(path, {
|
|
349
332
|
binding = "MCP_OBJECT",
|
|
@@ -457,7 +440,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
457
440
|
"Content-Type": "text/event-stream",
|
|
458
441
|
"Cache-Control": "no-cache",
|
|
459
442
|
Connection: "keep-alive",
|
|
460
|
-
|
|
443
|
+
...corsHeaders(request, corsOptions)
|
|
461
444
|
}
|
|
462
445
|
});
|
|
463
446
|
}
|
|
@@ -497,7 +480,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
497
480
|
"Content-Type": "text/event-stream",
|
|
498
481
|
"Cache-Control": "no-cache",
|
|
499
482
|
Connection: "keep-alive",
|
|
500
|
-
|
|
483
|
+
...corsHeaders(request, corsOptions)
|
|
501
484
|
}
|
|
502
485
|
});
|
|
503
486
|
}
|
|
@@ -507,7 +490,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
507
490
|
"Content-Type": "text/event-stream",
|
|
508
491
|
"Cache-Control": "no-cache",
|
|
509
492
|
Connection: "keep-alive",
|
|
510
|
-
|
|
493
|
+
...corsHeaders(request, corsOptions)
|
|
511
494
|
}
|
|
512
495
|
});
|
|
513
496
|
}
|
|
@@ -659,6 +642,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
659
642
|
const doStub = namespace.get(id);
|
|
660
643
|
const isInitialized = await doStub.isInitialized();
|
|
661
644
|
if (isInitializationRequest) {
|
|
645
|
+
await doStub._init(ctx.props);
|
|
662
646
|
await doStub.setInitialized();
|
|
663
647
|
} else if (!isInitialized) {
|
|
664
648
|
const body2 = JSON.stringify({
|
|
@@ -754,7 +738,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
754
738
|
ws.send(JSON.stringify(message));
|
|
755
739
|
}
|
|
756
740
|
ws.close();
|
|
757
|
-
return new Response(null, {
|
|
741
|
+
return new Response(null, {
|
|
742
|
+
status: 202,
|
|
743
|
+
headers: corsHeaders(request, corsOptions)
|
|
744
|
+
});
|
|
758
745
|
}
|
|
759
746
|
for (const message of messages) {
|
|
760
747
|
if (isJSONRPCRequest(message)) {
|
|
@@ -768,7 +755,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
768
755
|
"Cache-Control": "no-cache",
|
|
769
756
|
Connection: "keep-alive",
|
|
770
757
|
"mcp-session-id": sessionId,
|
|
771
|
-
|
|
758
|
+
...corsHeaders(request, corsOptions)
|
|
772
759
|
},
|
|
773
760
|
status: 200
|
|
774
761
|
});
|
|
@@ -786,20 +773,6 @@ data: ${JSON.stringify(result.data)}
|
|
|
786
773
|
};
|
|
787
774
|
}
|
|
788
775
|
};
|
|
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
776
|
export {
|
|
804
777
|
McpAgent
|
|
805
778
|
};
|