agents 0.0.0-398c7f5 → 0.0.0-3bcb134
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +128 -22
- package/dist/ai-chat-agent.d.ts +7 -4
- package/dist/ai-chat-agent.js +54 -29
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +12 -10
- package/dist/ai-react.js +27 -27
- package/dist/ai-react.js.map +1 -1
- package/dist/{chunk-OYJXQRRH.js → chunk-HY7ZLHJB.js} +166 -33
- package/dist/chunk-HY7ZLHJB.js.map +1 -0
- package/dist/{chunk-6RPGDIE2.js → chunk-JXN5WZFQ.js} +633 -132
- package/dist/chunk-JXN5WZFQ.js.map +1 -0
- package/dist/{chunk-VCSB47AK.js → chunk-KUH345EY.js} +8 -8
- package/dist/chunk-KUH345EY.js.map +1 -0
- package/dist/{chunk-BZXOAZUX.js → chunk-PVQZBKN7.js} +5 -5
- package/dist/chunk-PVQZBKN7.js.map +1 -0
- package/dist/client-DgyzBU_8.d.ts +4601 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.js +1 -1
- package/dist/index-BCJclX6q.d.ts +615 -0
- package/dist/index.d.ts +35 -405
- package/dist/index.js +10 -4
- package/dist/mcp/client.d.ts +9 -781
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/do-oauth-client-provider.js +1 -1
- package/dist/mcp/index.d.ts +38 -10
- package/dist/mcp/index.js +232 -59
- package/dist/mcp/index.js.map +1 -1
- package/dist/observability/index.d.ts +14 -0
- package/dist/observability/index.js +10 -0
- package/dist/observability/index.js.map +1 -0
- package/dist/react.d.ts +11 -9
- package/dist/react.js +7 -7
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +10 -10
- package/dist/schedule.js +4 -4
- package/dist/schedule.js.map +1 -1
- package/package.json +76 -71
- package/src/index.ts +828 -166
- package/dist/chunk-6RPGDIE2.js.map +0 -1
- package/dist/chunk-BZXOAZUX.js.map +0 -1
- package/dist/chunk-OYJXQRRH.js.map +0 -1
- package/dist/chunk-VCSB47AK.js.map +0 -1
package/dist/mcp/client.js
CHANGED
package/dist/mcp/index.d.ts
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
import { MCPClientManager } from '
|
|
1
|
+
import { M as MCPClientManager } from '../client-DgyzBU_8.js';
|
|
2
|
+
export { S as SSEEdgeClientTransport, a as StreamableHTTPEdgeClientTransport } from '../client-DgyzBU_8.js';
|
|
2
3
|
import { DurableObject } from 'cloudflare:workers';
|
|
3
|
-
import { Connection, WSMessage } from 'partyserver';
|
|
4
|
-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
4
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
5
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
6
|
+
import { ElicitResult } from '@modelcontextprotocol/sdk/types.js';
|
|
7
|
+
export { ElicitRequest, ElicitRequestSchema, ElicitResult } from '@modelcontextprotocol/sdk/types.js';
|
|
8
|
+
import { Connection, WSMessage } from 'partyserver';
|
|
6
9
|
import 'zod';
|
|
7
|
-
import '@modelcontextprotocol/sdk/types.js';
|
|
8
10
|
import '@modelcontextprotocol/sdk/client/index.js';
|
|
11
|
+
import '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
12
|
+
import 'ai';
|
|
9
13
|
import '@modelcontextprotocol/sdk/client/sse.js';
|
|
14
|
+
import '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
10
15
|
import './do-oauth-client-provider.js';
|
|
11
16
|
import '@modelcontextprotocol/sdk/client/auth.js';
|
|
12
17
|
import '@modelcontextprotocol/sdk/shared/auth.js';
|
|
13
|
-
import '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
14
|
-
import 'ai';
|
|
15
18
|
|
|
16
19
|
interface CORSOptions {
|
|
17
20
|
origin?: string;
|
|
@@ -40,6 +43,13 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
|
|
|
40
43
|
get state(): State;
|
|
41
44
|
sql<T = Record<string, string | number | boolean | null>>(strings: TemplateStringsArray, ...values: (string | number | boolean | null)[]): T[];
|
|
42
45
|
setState(state: State): void;
|
|
46
|
+
/**
|
|
47
|
+
* Elicit user input with a message and schema
|
|
48
|
+
*/
|
|
49
|
+
elicitInput(params: {
|
|
50
|
+
message: string;
|
|
51
|
+
requestedSchema: unknown;
|
|
52
|
+
}): Promise<ElicitResult>;
|
|
43
53
|
onStateUpdate(state: State | undefined, source: Connection | "server"): void;
|
|
44
54
|
onStart(): Promise<void>;
|
|
45
55
|
/**
|
|
@@ -49,31 +59,49 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
|
|
|
49
59
|
props: Props;
|
|
50
60
|
initRun: boolean;
|
|
51
61
|
abstract init(): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Handle errors that occur during initialization or operation.
|
|
64
|
+
* Override this method to provide custom error handling.
|
|
65
|
+
* @param error - The error that occurred
|
|
66
|
+
* @returns An error response object with status code and message
|
|
67
|
+
*/
|
|
68
|
+
onError(error: Error): {
|
|
69
|
+
status: number;
|
|
70
|
+
message: string;
|
|
71
|
+
};
|
|
52
72
|
_init(props: Props): Promise<void>;
|
|
53
73
|
setInitialized(): Promise<void>;
|
|
54
74
|
isInitialized(): Promise<boolean>;
|
|
75
|
+
updateProps(props: Props): Promise<void>;
|
|
55
76
|
private _initialize;
|
|
56
77
|
fetch(request: Request): Promise<Response>;
|
|
57
78
|
getWebSocket(): WebSocket | null;
|
|
58
79
|
getWebSocketForResponseID(id: string): WebSocket | null;
|
|
59
80
|
onMessage(connection: Connection, event: WSMessage): Promise<void>;
|
|
60
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Wait for elicitation response through storage polling
|
|
83
|
+
*/
|
|
84
|
+
private _waitForElicitationResponse;
|
|
85
|
+
/**
|
|
86
|
+
* Handle elicitation responses */
|
|
87
|
+
private _handleElicitationResponse;
|
|
88
|
+
onSSEMcpMessage(_sessionId: string, messageBody: unknown): Promise<Error | null>;
|
|
61
89
|
webSocketMessage(ws: WebSocket, event: ArrayBuffer | string): Promise<void>;
|
|
62
90
|
webSocketError(ws: WebSocket, error: unknown): Promise<void>;
|
|
63
91
|
webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): Promise<void>;
|
|
64
|
-
static mount(path: string, { binding, corsOptions
|
|
92
|
+
static mount(path: string, { binding, corsOptions }?: {
|
|
65
93
|
binding?: string;
|
|
66
94
|
corsOptions?: CORSOptions;
|
|
67
95
|
}): {
|
|
68
96
|
fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
|
|
69
97
|
};
|
|
70
|
-
static serveSSE(path: string, { binding, corsOptions
|
|
98
|
+
static serveSSE(path: string, { binding, corsOptions }?: {
|
|
71
99
|
binding?: string;
|
|
72
100
|
corsOptions?: CORSOptions;
|
|
73
101
|
}): {
|
|
74
102
|
fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
|
|
75
103
|
};
|
|
76
|
-
static serve(path: string, { binding, corsOptions
|
|
104
|
+
static serve(path: string, { binding, corsOptions }?: {
|
|
77
105
|
binding?: string;
|
|
78
106
|
corsOptions?: CORSOptions;
|
|
79
107
|
}): {
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,29 +1,35 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
} from "../chunk-JXN5WZFQ.js";
|
|
4
|
+
import {
|
|
5
|
+
SSEEdgeClientTransport,
|
|
6
|
+
StreamableHTTPEdgeClientTransport
|
|
7
|
+
} from "../chunk-HY7ZLHJB.js";
|
|
8
|
+
import "../chunk-PVQZBKN7.js";
|
|
9
|
+
import "../chunk-KUH345EY.js";
|
|
7
10
|
|
|
8
11
|
// src/mcp/index.ts
|
|
9
12
|
import { DurableObject } from "cloudflare:workers";
|
|
10
13
|
import {
|
|
11
14
|
InitializeRequestSchema,
|
|
15
|
+
JSONRPCMessageSchema,
|
|
12
16
|
isJSONRPCError,
|
|
13
17
|
isJSONRPCNotification,
|
|
14
18
|
isJSONRPCRequest,
|
|
15
|
-
isJSONRPCResponse
|
|
16
|
-
|
|
19
|
+
isJSONRPCResponse
|
|
20
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
21
|
+
import {
|
|
22
|
+
ElicitRequestSchema
|
|
17
23
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
18
24
|
var MAXIMUM_MESSAGE_SIZE_BYTES = 4 * 1024 * 1024;
|
|
19
|
-
function corsHeaders(
|
|
25
|
+
function corsHeaders(_request, corsOptions = {}) {
|
|
20
26
|
const origin = "*";
|
|
21
27
|
return {
|
|
22
|
-
"Access-Control-Allow-
|
|
28
|
+
"Access-Control-Allow-Headers": corsOptions.headers || "Content-Type, mcp-session-id, mcp-protocol-version",
|
|
23
29
|
"Access-Control-Allow-Methods": corsOptions.methods || "GET, POST, OPTIONS",
|
|
24
|
-
"Access-Control-Allow-
|
|
25
|
-
"Access-Control-
|
|
26
|
-
"Access-Control-
|
|
30
|
+
"Access-Control-Allow-Origin": corsOptions.origin || origin,
|
|
31
|
+
"Access-Control-Expose-Headers": corsOptions.exposeHeaders || "mcp-session-id",
|
|
32
|
+
"Access-Control-Max-Age": (corsOptions.maxAge || 86400).toString()
|
|
27
33
|
};
|
|
28
34
|
}
|
|
29
35
|
function isDurableObjectNamespace(namespace) {
|
|
@@ -141,6 +147,46 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
141
147
|
setState(state) {
|
|
142
148
|
return this._agent.setState(state);
|
|
143
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* Elicit user input with a message and schema
|
|
152
|
+
*/
|
|
153
|
+
async elicitInput(params) {
|
|
154
|
+
const requestId = `elicit_${Math.random().toString(36).substring(2, 11)}`;
|
|
155
|
+
await this.ctx.storage.put(`elicitation:${requestId}`, {
|
|
156
|
+
message: params.message,
|
|
157
|
+
requestedSchema: params.requestedSchema,
|
|
158
|
+
timestamp: Date.now()
|
|
159
|
+
});
|
|
160
|
+
const elicitRequest = {
|
|
161
|
+
jsonrpc: "2.0",
|
|
162
|
+
id: requestId,
|
|
163
|
+
method: "elicitation/create",
|
|
164
|
+
params: {
|
|
165
|
+
message: params.message,
|
|
166
|
+
requestedSchema: params.requestedSchema
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
if (this._transport) {
|
|
170
|
+
await this._transport.send(elicitRequest);
|
|
171
|
+
} else {
|
|
172
|
+
const connections = this._agent?.getConnections();
|
|
173
|
+
if (!connections || Array.from(connections).length === 0) {
|
|
174
|
+
await this.ctx.storage.delete(`elicitation:${requestId}`);
|
|
175
|
+
throw new Error("No active connections available for elicitation");
|
|
176
|
+
}
|
|
177
|
+
const connectionList = Array.from(connections);
|
|
178
|
+
for (const connection of connectionList) {
|
|
179
|
+
try {
|
|
180
|
+
connection.send(JSON.stringify(elicitRequest));
|
|
181
|
+
} catch (error) {
|
|
182
|
+
console.error("Failed to send elicitation request:", error);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return this._waitForElicitationResponse(requestId);
|
|
187
|
+
}
|
|
188
|
+
// we leave the variables as unused for autocomplete purposes
|
|
189
|
+
// biome-ignore lint/correctness/noUnusedFunctionParameters: overriden later
|
|
144
190
|
onStateUpdate(state, source) {
|
|
145
191
|
}
|
|
146
192
|
async onStart() {
|
|
@@ -177,15 +223,32 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
177
223
|
await server.connect(this._transport);
|
|
178
224
|
}
|
|
179
225
|
}
|
|
226
|
+
/**
|
|
227
|
+
* Handle errors that occur during initialization or operation.
|
|
228
|
+
* Override this method to provide custom error handling.
|
|
229
|
+
* @param error - The error that occurred
|
|
230
|
+
* @returns An error response object with status code and message
|
|
231
|
+
*/
|
|
232
|
+
onError(error) {
|
|
233
|
+
console.error("McpAgent error:", error);
|
|
234
|
+
return {
|
|
235
|
+
status: 500,
|
|
236
|
+
message: error.message || "An unexpected error occurred during initialization"
|
|
237
|
+
};
|
|
238
|
+
}
|
|
180
239
|
async _init(props) {
|
|
181
|
-
await this.
|
|
240
|
+
await this.updateProps(props);
|
|
182
241
|
if (!this.ctx.storage.get("transportType")) {
|
|
183
242
|
await this.ctx.storage.put("transportType", "unset");
|
|
184
243
|
}
|
|
185
|
-
this.props = props;
|
|
186
244
|
if (!this.initRun) {
|
|
187
245
|
this.initRun = true;
|
|
188
|
-
|
|
246
|
+
try {
|
|
247
|
+
await this.init();
|
|
248
|
+
} catch (error) {
|
|
249
|
+
const errorResponse = this.onError(error);
|
|
250
|
+
throw new Error(`Initialization failed: ${errorResponse.message}`);
|
|
251
|
+
}
|
|
189
252
|
}
|
|
190
253
|
}
|
|
191
254
|
async setInitialized() {
|
|
@@ -194,6 +257,10 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
194
257
|
async isInitialized() {
|
|
195
258
|
return await this.ctx.storage.get("initialized") === true;
|
|
196
259
|
}
|
|
260
|
+
async updateProps(props) {
|
|
261
|
+
await this.ctx.storage.put("props", props ?? {});
|
|
262
|
+
this.props = props;
|
|
263
|
+
}
|
|
197
264
|
async _initialize() {
|
|
198
265
|
await this.ctx.blockConcurrencyWhile(async () => {
|
|
199
266
|
this._status = "starting";
|
|
@@ -280,14 +347,78 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
280
347
|
this._transport?.onerror?.(error);
|
|
281
348
|
return;
|
|
282
349
|
}
|
|
350
|
+
if (await this._handleElicitationResponse(message)) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
283
353
|
if (isJSONRPCRequest(message)) {
|
|
284
354
|
this._requestIdToConnectionId.set(message.id.toString(), connection.id);
|
|
285
355
|
}
|
|
286
356
|
this._transport?.onmessage?.(message);
|
|
287
357
|
}
|
|
358
|
+
/**
|
|
359
|
+
* Wait for elicitation response through storage polling
|
|
360
|
+
*/
|
|
361
|
+
async _waitForElicitationResponse(requestId) {
|
|
362
|
+
const startTime = Date.now();
|
|
363
|
+
const timeout = 6e4;
|
|
364
|
+
try {
|
|
365
|
+
while (Date.now() - startTime < timeout) {
|
|
366
|
+
const response = await this.ctx.storage.get(
|
|
367
|
+
`elicitation:response:${requestId}`
|
|
368
|
+
);
|
|
369
|
+
if (response) {
|
|
370
|
+
await this.ctx.storage.delete(`elicitation:${requestId}`);
|
|
371
|
+
await this.ctx.storage.delete(`elicitation:response:${requestId}`);
|
|
372
|
+
return response;
|
|
373
|
+
}
|
|
374
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
375
|
+
}
|
|
376
|
+
throw new Error("Elicitation request timed out");
|
|
377
|
+
} finally {
|
|
378
|
+
await this.ctx.storage.delete(`elicitation:${requestId}`);
|
|
379
|
+
await this.ctx.storage.delete(`elicitation:response:${requestId}`);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Handle elicitation responses */
|
|
384
|
+
async _handleElicitationResponse(message) {
|
|
385
|
+
if (isJSONRPCResponse(message) && message.result) {
|
|
386
|
+
const requestId = message.id?.toString();
|
|
387
|
+
if (!requestId || !requestId.startsWith("elicit_")) return false;
|
|
388
|
+
const pendingRequest = await this.ctx.storage.get(
|
|
389
|
+
`elicitation:${requestId}`
|
|
390
|
+
);
|
|
391
|
+
if (!pendingRequest) return false;
|
|
392
|
+
await this.ctx.storage.put(
|
|
393
|
+
`elicitation:response:${requestId}`,
|
|
394
|
+
message.result
|
|
395
|
+
);
|
|
396
|
+
return true;
|
|
397
|
+
}
|
|
398
|
+
if (isJSONRPCError(message)) {
|
|
399
|
+
const requestId = message.id?.toString();
|
|
400
|
+
if (!requestId || !requestId.startsWith("elicit_")) return false;
|
|
401
|
+
const pendingRequest = await this.ctx.storage.get(
|
|
402
|
+
`elicitation:${requestId}`
|
|
403
|
+
);
|
|
404
|
+
if (!pendingRequest) return false;
|
|
405
|
+
const errorResult = {
|
|
406
|
+
action: "cancel",
|
|
407
|
+
content: {
|
|
408
|
+
error: message.error.message || "Elicitation request failed"
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
await this.ctx.storage.put(
|
|
412
|
+
`elicitation:response:${requestId}`,
|
|
413
|
+
errorResult
|
|
414
|
+
);
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
288
419
|
// All messages received over SSE after the initial connection has been established
|
|
289
420
|
// will be passed here
|
|
290
|
-
async onSSEMcpMessage(
|
|
421
|
+
async onSSEMcpMessage(_sessionId, messageBody) {
|
|
291
422
|
if (this._status !== "started") {
|
|
292
423
|
await this._initialize();
|
|
293
424
|
}
|
|
@@ -295,14 +426,16 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
295
426
|
return new Error("Internal Server Error: Expected SSE protocol");
|
|
296
427
|
}
|
|
297
428
|
try {
|
|
298
|
-
const message = await request.json();
|
|
299
429
|
let parsedMessage;
|
|
300
430
|
try {
|
|
301
|
-
parsedMessage = JSONRPCMessageSchema.parse(
|
|
431
|
+
parsedMessage = JSONRPCMessageSchema.parse(messageBody);
|
|
302
432
|
} catch (error) {
|
|
303
433
|
this._transport?.onerror?.(error);
|
|
304
434
|
throw error;
|
|
305
435
|
}
|
|
436
|
+
if (await this._handleElicitationResponse(parsedMessage)) {
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
306
439
|
this._transport?.onmessage?.(parsedMessage);
|
|
307
440
|
return null;
|
|
308
441
|
} catch (error) {
|
|
@@ -379,12 +512,26 @@ data: ${relativeUrlWithSession}
|
|
|
379
512
|
writer.write(encoder.encode(endpointMessage));
|
|
380
513
|
const id = namespace.idFromName(`sse:${sessionId}`);
|
|
381
514
|
const doStub = namespace.get(id);
|
|
382
|
-
|
|
515
|
+
try {
|
|
516
|
+
await doStub._init(ctx.props);
|
|
517
|
+
} catch (error) {
|
|
518
|
+
console.error("Failed to initialize McpAgent:", error);
|
|
519
|
+
await writer.close();
|
|
520
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
521
|
+
return new Response(`Initialization failed: ${errorMessage}`, {
|
|
522
|
+
status: 500
|
|
523
|
+
});
|
|
524
|
+
}
|
|
383
525
|
const upgradeUrl = new URL(request.url);
|
|
384
526
|
upgradeUrl.pathname = "/sse";
|
|
527
|
+
const existingHeaders = {};
|
|
528
|
+
request.headers.forEach((value, key) => {
|
|
529
|
+
existingHeaders[key] = value;
|
|
530
|
+
});
|
|
385
531
|
const response = await doStub.fetch(
|
|
386
532
|
new Request(upgradeUrl, {
|
|
387
533
|
headers: {
|
|
534
|
+
...existingHeaders,
|
|
388
535
|
Upgrade: "websocket",
|
|
389
536
|
// Required by PartyServer
|
|
390
537
|
"x-partykit-room": sessionId
|
|
@@ -420,10 +567,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
420
567
|
onMessage(event).catch(console.error);
|
|
421
568
|
});
|
|
422
569
|
ws.addEventListener("error", (error) => {
|
|
423
|
-
async function onError(
|
|
570
|
+
async function onError(_error) {
|
|
424
571
|
try {
|
|
425
572
|
await writer.close();
|
|
426
|
-
} catch (
|
|
573
|
+
} catch (_e) {
|
|
427
574
|
}
|
|
428
575
|
}
|
|
429
576
|
onError(error).catch(console.error);
|
|
@@ -440,9 +587,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
440
587
|
});
|
|
441
588
|
return new Response(readable, {
|
|
442
589
|
headers: {
|
|
443
|
-
"Content-Type": "text/event-stream",
|
|
444
590
|
"Cache-Control": "no-cache",
|
|
445
591
|
Connection: "keep-alive",
|
|
592
|
+
"Content-Type": "text/event-stream",
|
|
446
593
|
...corsHeaders(request, corsOptions)
|
|
447
594
|
}
|
|
448
595
|
});
|
|
@@ -475,26 +622,28 @@ data: ${JSON.stringify(result.data)}
|
|
|
475
622
|
}
|
|
476
623
|
const id = namespace.idFromName(`sse:${sessionId}`);
|
|
477
624
|
const doStub = namespace.get(id);
|
|
478
|
-
const
|
|
625
|
+
const messageBody = await request.json();
|
|
626
|
+
await doStub.updateProps(ctx.props);
|
|
627
|
+
const error = await doStub.onSSEMcpMessage(sessionId, messageBody);
|
|
479
628
|
if (error) {
|
|
480
629
|
return new Response(error.message, {
|
|
481
|
-
status: 400,
|
|
482
630
|
headers: {
|
|
483
|
-
"Content-Type": "text/event-stream",
|
|
484
631
|
"Cache-Control": "no-cache",
|
|
485
632
|
Connection: "keep-alive",
|
|
633
|
+
"Content-Type": "text/event-stream",
|
|
486
634
|
...corsHeaders(request, corsOptions)
|
|
487
|
-
}
|
|
635
|
+
},
|
|
636
|
+
status: 400
|
|
488
637
|
});
|
|
489
638
|
}
|
|
490
639
|
return new Response("Accepted", {
|
|
491
|
-
status: 202,
|
|
492
640
|
headers: {
|
|
493
|
-
"Content-Type": "text/event-stream",
|
|
494
641
|
"Cache-Control": "no-cache",
|
|
495
642
|
Connection: "keep-alive",
|
|
643
|
+
"Content-Type": "text/event-stream",
|
|
496
644
|
...corsHeaders(request, corsOptions)
|
|
497
|
-
}
|
|
645
|
+
},
|
|
646
|
+
status: 202
|
|
498
647
|
});
|
|
499
648
|
}
|
|
500
649
|
return new Response("Not Found", { status: 404 });
|
|
@@ -532,24 +681,24 @@ data: ${JSON.stringify(result.data)}
|
|
|
532
681
|
const acceptHeader = request.headers.get("accept");
|
|
533
682
|
if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
|
|
534
683
|
const body2 = JSON.stringify({
|
|
535
|
-
jsonrpc: "2.0",
|
|
536
684
|
error: {
|
|
537
685
|
code: -32e3,
|
|
538
686
|
message: "Not Acceptable: Client must accept both application/json and text/event-stream"
|
|
539
687
|
},
|
|
540
|
-
id: null
|
|
688
|
+
id: null,
|
|
689
|
+
jsonrpc: "2.0"
|
|
541
690
|
});
|
|
542
691
|
return new Response(body2, { status: 406 });
|
|
543
692
|
}
|
|
544
693
|
const ct = request.headers.get("content-type");
|
|
545
694
|
if (!ct || !ct.includes("application/json")) {
|
|
546
695
|
const body2 = JSON.stringify({
|
|
547
|
-
jsonrpc: "2.0",
|
|
548
696
|
error: {
|
|
549
697
|
code: -32e3,
|
|
550
698
|
message: "Unsupported Media Type: Content-Type must be application/json"
|
|
551
699
|
},
|
|
552
|
-
id: null
|
|
700
|
+
id: null,
|
|
701
|
+
jsonrpc: "2.0"
|
|
553
702
|
});
|
|
554
703
|
return new Response(body2, { status: 415 });
|
|
555
704
|
}
|
|
@@ -559,12 +708,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
559
708
|
);
|
|
560
709
|
if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) {
|
|
561
710
|
const body2 = JSON.stringify({
|
|
562
|
-
jsonrpc: "2.0",
|
|
563
711
|
error: {
|
|
564
712
|
code: -32e3,
|
|
565
713
|
message: `Request body too large. Maximum size is ${MAXIMUM_MESSAGE_SIZE_BYTES} bytes`
|
|
566
714
|
},
|
|
567
|
-
id: null
|
|
715
|
+
id: null,
|
|
716
|
+
jsonrpc: "2.0"
|
|
568
717
|
});
|
|
569
718
|
return new Response(body2, { status: 413 });
|
|
570
719
|
}
|
|
@@ -572,14 +721,14 @@ data: ${JSON.stringify(result.data)}
|
|
|
572
721
|
let rawMessage;
|
|
573
722
|
try {
|
|
574
723
|
rawMessage = await request.json();
|
|
575
|
-
} catch (
|
|
724
|
+
} catch (_error) {
|
|
576
725
|
const body2 = JSON.stringify({
|
|
577
|
-
jsonrpc: "2.0",
|
|
578
726
|
error: {
|
|
579
727
|
code: -32700,
|
|
580
728
|
message: "Parse error: Invalid JSON"
|
|
581
729
|
},
|
|
582
|
-
id: null
|
|
730
|
+
id: null,
|
|
731
|
+
jsonrpc: "2.0"
|
|
583
732
|
});
|
|
584
733
|
return new Response(body2, { status: 400 });
|
|
585
734
|
}
|
|
@@ -593,12 +742,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
593
742
|
for (const msg of arrayMessage) {
|
|
594
743
|
if (!JSONRPCMessageSchema.safeParse(msg).success) {
|
|
595
744
|
const body2 = JSON.stringify({
|
|
596
|
-
jsonrpc: "2.0",
|
|
597
745
|
error: {
|
|
598
746
|
code: -32700,
|
|
599
747
|
message: "Parse error: Invalid JSON-RPC message"
|
|
600
748
|
},
|
|
601
|
-
id: null
|
|
749
|
+
id: null,
|
|
750
|
+
jsonrpc: "2.0"
|
|
602
751
|
});
|
|
603
752
|
return new Response(body2, { status: 400 });
|
|
604
753
|
}
|
|
@@ -609,34 +758,34 @@ data: ${JSON.stringify(result.data)}
|
|
|
609
758
|
);
|
|
610
759
|
if (isInitializationRequest && sessionId) {
|
|
611
760
|
const body2 = JSON.stringify({
|
|
612
|
-
jsonrpc: "2.0",
|
|
613
761
|
error: {
|
|
614
762
|
code: -32600,
|
|
615
763
|
message: "Invalid Request: Initialization requests must not include a sessionId"
|
|
616
764
|
},
|
|
617
|
-
id: null
|
|
765
|
+
id: null,
|
|
766
|
+
jsonrpc: "2.0"
|
|
618
767
|
});
|
|
619
768
|
return new Response(body2, { status: 400 });
|
|
620
769
|
}
|
|
621
770
|
if (isInitializationRequest && messages.length > 1) {
|
|
622
771
|
const body2 = JSON.stringify({
|
|
623
|
-
jsonrpc: "2.0",
|
|
624
772
|
error: {
|
|
625
773
|
code: -32600,
|
|
626
774
|
message: "Invalid Request: Only one initialization request is allowed"
|
|
627
775
|
},
|
|
628
|
-
id: null
|
|
776
|
+
id: null,
|
|
777
|
+
jsonrpc: "2.0"
|
|
629
778
|
});
|
|
630
779
|
return new Response(body2, { status: 400 });
|
|
631
780
|
}
|
|
632
781
|
if (!isInitializationRequest && !sessionId) {
|
|
633
782
|
const body2 = JSON.stringify({
|
|
634
|
-
jsonrpc: "2.0",
|
|
635
783
|
error: {
|
|
636
784
|
code: -32e3,
|
|
637
785
|
message: "Bad Request: Mcp-Session-Id header is required"
|
|
638
786
|
},
|
|
639
|
-
id: null
|
|
787
|
+
id: null,
|
|
788
|
+
jsonrpc: "2.0"
|
|
640
789
|
});
|
|
641
790
|
return new Response(body2, { status: 400 });
|
|
642
791
|
}
|
|
@@ -645,27 +794,48 @@ data: ${JSON.stringify(result.data)}
|
|
|
645
794
|
const doStub = namespace.get(id);
|
|
646
795
|
const isInitialized = await doStub.isInitialized();
|
|
647
796
|
if (isInitializationRequest) {
|
|
648
|
-
|
|
649
|
-
|
|
797
|
+
try {
|
|
798
|
+
await doStub._init(ctx.props);
|
|
799
|
+
await doStub.setInitialized();
|
|
800
|
+
} catch (error) {
|
|
801
|
+
console.error("Failed to initialize McpAgent:", error);
|
|
802
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
803
|
+
const body2 = JSON.stringify({
|
|
804
|
+
error: {
|
|
805
|
+
code: -32001,
|
|
806
|
+
message: `Initialization failed: ${errorMessage}`
|
|
807
|
+
},
|
|
808
|
+
id: null,
|
|
809
|
+
jsonrpc: "2.0"
|
|
810
|
+
});
|
|
811
|
+
return new Response(body2, { status: 500 });
|
|
812
|
+
}
|
|
650
813
|
} else if (!isInitialized) {
|
|
651
814
|
const body2 = JSON.stringify({
|
|
652
|
-
jsonrpc: "2.0",
|
|
653
815
|
error: {
|
|
654
816
|
code: -32001,
|
|
655
817
|
message: "Session not found"
|
|
656
818
|
},
|
|
657
|
-
id: null
|
|
819
|
+
id: null,
|
|
820
|
+
jsonrpc: "2.0"
|
|
658
821
|
});
|
|
659
822
|
return new Response(body2, { status: 404 });
|
|
823
|
+
} else {
|
|
824
|
+
await doStub.updateProps(ctx.props);
|
|
660
825
|
}
|
|
661
826
|
const { readable, writable } = new TransformStream();
|
|
662
827
|
const writer = writable.getWriter();
|
|
663
828
|
const encoder = new TextEncoder();
|
|
664
829
|
const upgradeUrl = new URL(request.url);
|
|
665
830
|
upgradeUrl.pathname = "/streamable-http";
|
|
831
|
+
const existingHeaders = {};
|
|
832
|
+
request.headers.forEach((value, key) => {
|
|
833
|
+
existingHeaders[key] = value;
|
|
834
|
+
});
|
|
666
835
|
const response = await doStub.fetch(
|
|
667
836
|
new Request(upgradeUrl, {
|
|
668
837
|
headers: {
|
|
838
|
+
...existingHeaders,
|
|
669
839
|
Upgrade: "websocket",
|
|
670
840
|
// Required by PartyServer
|
|
671
841
|
"x-partykit-room": sessionId
|
|
@@ -677,12 +847,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
677
847
|
console.error("Failed to establish WebSocket connection");
|
|
678
848
|
await writer.close();
|
|
679
849
|
const body2 = JSON.stringify({
|
|
680
|
-
jsonrpc: "2.0",
|
|
681
850
|
error: {
|
|
682
851
|
code: -32001,
|
|
683
852
|
message: "Failed to establish WebSocket connection"
|
|
684
853
|
},
|
|
685
|
-
id: null
|
|
854
|
+
id: null,
|
|
855
|
+
jsonrpc: "2.0"
|
|
686
856
|
});
|
|
687
857
|
return new Response(body2, { status: 500 });
|
|
688
858
|
}
|
|
@@ -715,10 +885,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
715
885
|
onMessage(event).catch(console.error);
|
|
716
886
|
});
|
|
717
887
|
ws.addEventListener("error", (error) => {
|
|
718
|
-
async function onError(
|
|
888
|
+
async function onError(_error) {
|
|
719
889
|
try {
|
|
720
890
|
await writer.close();
|
|
721
|
-
} catch (
|
|
891
|
+
} catch (_e) {
|
|
722
892
|
}
|
|
723
893
|
}
|
|
724
894
|
onError(error).catch(console.error);
|
|
@@ -742,8 +912,8 @@ data: ${JSON.stringify(result.data)}
|
|
|
742
912
|
}
|
|
743
913
|
ws.close();
|
|
744
914
|
return new Response(null, {
|
|
745
|
-
|
|
746
|
-
|
|
915
|
+
headers: corsHeaders(request, corsOptions),
|
|
916
|
+
status: 202
|
|
747
917
|
});
|
|
748
918
|
}
|
|
749
919
|
for (const message of messages) {
|
|
@@ -754,9 +924,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
754
924
|
}
|
|
755
925
|
return new Response(readable, {
|
|
756
926
|
headers: {
|
|
757
|
-
"Content-Type": "text/event-stream",
|
|
758
927
|
"Cache-Control": "no-cache",
|
|
759
928
|
Connection: "keep-alive",
|
|
929
|
+
"Content-Type": "text/event-stream",
|
|
760
930
|
"mcp-session-id": sessionId,
|
|
761
931
|
...corsHeaders(request, corsOptions)
|
|
762
932
|
},
|
|
@@ -764,12 +934,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
764
934
|
});
|
|
765
935
|
}
|
|
766
936
|
const body = JSON.stringify({
|
|
767
|
-
jsonrpc: "2.0",
|
|
768
937
|
error: {
|
|
769
938
|
code: -32e3,
|
|
770
939
|
message: "Method not allowed"
|
|
771
940
|
},
|
|
772
|
-
id: null
|
|
941
|
+
id: null,
|
|
942
|
+
jsonrpc: "2.0"
|
|
773
943
|
});
|
|
774
944
|
return new Response(body, { status: 405 });
|
|
775
945
|
}
|
|
@@ -777,6 +947,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
777
947
|
}
|
|
778
948
|
};
|
|
779
949
|
export {
|
|
780
|
-
|
|
950
|
+
ElicitRequestSchema,
|
|
951
|
+
McpAgent,
|
|
952
|
+
SSEEdgeClientTransport,
|
|
953
|
+
StreamableHTTPEdgeClientTransport
|
|
781
954
|
};
|
|
782
955
|
//# sourceMappingURL=index.js.map
|