agents 0.0.0-b57e1d9 → 0.0.0-b63b4a6
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 +127 -22
- package/dist/ai-chat-agent.d.ts +6 -3
- package/dist/ai-chat-agent.js +64 -26
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +11 -9
- package/dist/ai-react.js +27 -27
- package/dist/ai-react.js.map +1 -1
- package/dist/{chunk-MXJNY43J.js → chunk-DBXG3DYU.js} +614 -129
- package/dist/chunk-DBXG3DYU.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/{chunk-OYJXQRRH.js → chunk-UYXBO2TX.js} +127 -26
- package/dist/chunk-UYXBO2TX.js.map +1 -0
- package/dist/client.d.ts +2 -2
- package/dist/client.js +1 -1
- package/dist/index-CLW1aEBr.d.ts +615 -0
- package/dist/index.d.ts +35 -405
- package/dist/index.js +10 -4
- package/dist/mcp/client.d.ts +295 -19
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/do-oauth-client-provider.js +1 -1
- package/dist/mcp/index.d.ts +49 -11
- package/dist/mcp/index.js +176 -50
- 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 +10 -8
- 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 +801 -160
- package/dist/chunk-BZXOAZUX.js.map +0 -1
- package/dist/chunk-MXJNY43J.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,41 @@
|
|
|
1
1
|
import { MCPClientManager } from './client.js';
|
|
2
2
|
import { DurableObject } from 'cloudflare:workers';
|
|
3
|
-
import { Connection, WSMessage } from 'partyserver';
|
|
4
|
-
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
3
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
4
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
|
+
import { ElicitResult } from '@modelcontextprotocol/sdk/types.js';
|
|
6
|
+
export { ElicitRequest, ElicitRequestSchema, ElicitResult } from '@modelcontextprotocol/sdk/types.js';
|
|
7
|
+
import { Connection, WSMessage } from 'partyserver';
|
|
8
|
+
import { SSEClientTransport, SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
9
|
+
import { StreamableHTTPClientTransport, StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
6
10
|
import 'zod';
|
|
7
|
-
import '@modelcontextprotocol/sdk/types.js';
|
|
8
11
|
import '@modelcontextprotocol/sdk/client/index.js';
|
|
9
|
-
import '@modelcontextprotocol/sdk/
|
|
12
|
+
import '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
13
|
+
import 'ai';
|
|
10
14
|
import './do-oauth-client-provider.js';
|
|
11
15
|
import '@modelcontextprotocol/sdk/client/auth.js';
|
|
12
16
|
import '@modelcontextprotocol/sdk/shared/auth.js';
|
|
13
|
-
|
|
14
|
-
|
|
17
|
+
|
|
18
|
+
declare class SSEEdgeClientTransport extends SSEClientTransport {
|
|
19
|
+
private authProvider;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new EdgeSSEClientTransport, which overrides fetch to be compatible with the CF workers environment
|
|
22
|
+
*/
|
|
23
|
+
constructor(url: URL, options: SSEClientTransportOptions);
|
|
24
|
+
authHeaders(): Promise<{
|
|
25
|
+
Authorization: string;
|
|
26
|
+
} | undefined>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare class StreamableHTTPEdgeClientTransport extends StreamableHTTPClientTransport {
|
|
30
|
+
private authProvider;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new StreamableHTTPEdgeClientTransport, which overrides fetch to be compatible with the CF workers environment
|
|
33
|
+
*/
|
|
34
|
+
constructor(url: URL, options: StreamableHTTPClientTransportOptions);
|
|
35
|
+
authHeaders(): Promise<{
|
|
36
|
+
Authorization: string;
|
|
37
|
+
} | undefined>;
|
|
38
|
+
}
|
|
15
39
|
|
|
16
40
|
interface CORSOptions {
|
|
17
41
|
origin?: string;
|
|
@@ -40,6 +64,13 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
|
|
|
40
64
|
get state(): State;
|
|
41
65
|
sql<T = Record<string, string | number | boolean | null>>(strings: TemplateStringsArray, ...values: (string | number | boolean | null)[]): T[];
|
|
42
66
|
setState(state: State): void;
|
|
67
|
+
/**
|
|
68
|
+
* Elicit user input with a message and schema
|
|
69
|
+
*/
|
|
70
|
+
elicitInput(params: {
|
|
71
|
+
message: string;
|
|
72
|
+
requestedSchema: unknown;
|
|
73
|
+
}): Promise<ElicitResult>;
|
|
43
74
|
onStateUpdate(state: State | undefined, source: Connection | "server"): void;
|
|
44
75
|
onStart(): Promise<void>;
|
|
45
76
|
/**
|
|
@@ -57,23 +88,30 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
|
|
|
57
88
|
getWebSocket(): WebSocket | null;
|
|
58
89
|
getWebSocketForResponseID(id: string): WebSocket | null;
|
|
59
90
|
onMessage(connection: Connection, event: WSMessage): Promise<void>;
|
|
60
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Wait for elicitation response through storage polling
|
|
93
|
+
*/
|
|
94
|
+
private _waitForElicitationResponse;
|
|
95
|
+
/**
|
|
96
|
+
* Handle elicitation responses */
|
|
97
|
+
private _handleElicitationResponse;
|
|
98
|
+
onSSEMcpMessage(_sessionId: string, request: Request): Promise<Error | null>;
|
|
61
99
|
webSocketMessage(ws: WebSocket, event: ArrayBuffer | string): Promise<void>;
|
|
62
100
|
webSocketError(ws: WebSocket, error: unknown): Promise<void>;
|
|
63
101
|
webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): Promise<void>;
|
|
64
|
-
static mount(path: string, { binding, corsOptions
|
|
102
|
+
static mount(path: string, { binding, corsOptions }?: {
|
|
65
103
|
binding?: string;
|
|
66
104
|
corsOptions?: CORSOptions;
|
|
67
105
|
}): {
|
|
68
106
|
fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
|
|
69
107
|
};
|
|
70
|
-
static serveSSE(path: string, { binding, corsOptions
|
|
108
|
+
static serveSSE(path: string, { binding, corsOptions }?: {
|
|
71
109
|
binding?: string;
|
|
72
110
|
corsOptions?: CORSOptions;
|
|
73
111
|
}): {
|
|
74
112
|
fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
|
|
75
113
|
};
|
|
76
|
-
static serve(path: string, { binding, corsOptions
|
|
114
|
+
static serve(path: string, { binding, corsOptions }?: {
|
|
77
115
|
binding?: string;
|
|
78
116
|
corsOptions?: CORSOptions;
|
|
79
117
|
}): {
|
|
@@ -81,4 +119,4 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
|
|
|
81
119
|
};
|
|
82
120
|
}
|
|
83
121
|
|
|
84
|
-
export { McpAgent };
|
|
122
|
+
export { McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport };
|
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-DBXG3DYU.js";
|
|
4
|
+
import {
|
|
5
|
+
SSEEdgeClientTransport,
|
|
6
|
+
StreamableHTTPEdgeClientTransport
|
|
7
|
+
} from "../chunk-UYXBO2TX.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() {
|
|
@@ -280,14 +326,78 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
280
326
|
this._transport?.onerror?.(error);
|
|
281
327
|
return;
|
|
282
328
|
}
|
|
329
|
+
if (await this._handleElicitationResponse(message)) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
283
332
|
if (isJSONRPCRequest(message)) {
|
|
284
333
|
this._requestIdToConnectionId.set(message.id.toString(), connection.id);
|
|
285
334
|
}
|
|
286
335
|
this._transport?.onmessage?.(message);
|
|
287
336
|
}
|
|
337
|
+
/**
|
|
338
|
+
* Wait for elicitation response through storage polling
|
|
339
|
+
*/
|
|
340
|
+
async _waitForElicitationResponse(requestId) {
|
|
341
|
+
const startTime = Date.now();
|
|
342
|
+
const timeout = 6e4;
|
|
343
|
+
try {
|
|
344
|
+
while (Date.now() - startTime < timeout) {
|
|
345
|
+
const response = await this.ctx.storage.get(
|
|
346
|
+
`elicitation:response:${requestId}`
|
|
347
|
+
);
|
|
348
|
+
if (response) {
|
|
349
|
+
await this.ctx.storage.delete(`elicitation:${requestId}`);
|
|
350
|
+
await this.ctx.storage.delete(`elicitation:response:${requestId}`);
|
|
351
|
+
return response;
|
|
352
|
+
}
|
|
353
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
354
|
+
}
|
|
355
|
+
throw new Error("Elicitation request timed out");
|
|
356
|
+
} finally {
|
|
357
|
+
await this.ctx.storage.delete(`elicitation:${requestId}`);
|
|
358
|
+
await this.ctx.storage.delete(`elicitation:response:${requestId}`);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Handle elicitation responses */
|
|
363
|
+
async _handleElicitationResponse(message) {
|
|
364
|
+
if (isJSONRPCResponse(message) && message.result) {
|
|
365
|
+
const requestId = message.id?.toString();
|
|
366
|
+
if (!requestId || !requestId.startsWith("elicit_")) return false;
|
|
367
|
+
const pendingRequest = await this.ctx.storage.get(
|
|
368
|
+
`elicitation:${requestId}`
|
|
369
|
+
);
|
|
370
|
+
if (!pendingRequest) return false;
|
|
371
|
+
await this.ctx.storage.put(
|
|
372
|
+
`elicitation:response:${requestId}`,
|
|
373
|
+
message.result
|
|
374
|
+
);
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
if (isJSONRPCError(message)) {
|
|
378
|
+
const requestId = message.id?.toString();
|
|
379
|
+
if (!requestId || !requestId.startsWith("elicit_")) return false;
|
|
380
|
+
const pendingRequest = await this.ctx.storage.get(
|
|
381
|
+
`elicitation:${requestId}`
|
|
382
|
+
);
|
|
383
|
+
if (!pendingRequest) return false;
|
|
384
|
+
const errorResult = {
|
|
385
|
+
action: "cancel",
|
|
386
|
+
content: {
|
|
387
|
+
error: message.error.message || "Elicitation request failed"
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
await this.ctx.storage.put(
|
|
391
|
+
`elicitation:response:${requestId}`,
|
|
392
|
+
errorResult
|
|
393
|
+
);
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
288
398
|
// All messages received over SSE after the initial connection has been established
|
|
289
399
|
// will be passed here
|
|
290
|
-
async onSSEMcpMessage(
|
|
400
|
+
async onSSEMcpMessage(_sessionId, request) {
|
|
291
401
|
if (this._status !== "started") {
|
|
292
402
|
await this._initialize();
|
|
293
403
|
}
|
|
@@ -303,6 +413,9 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
303
413
|
this._transport?.onerror?.(error);
|
|
304
414
|
throw error;
|
|
305
415
|
}
|
|
416
|
+
if (await this._handleElicitationResponse(parsedMessage)) {
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
306
419
|
this._transport?.onmessage?.(parsedMessage);
|
|
307
420
|
return null;
|
|
308
421
|
} catch (error) {
|
|
@@ -382,9 +495,14 @@ data: ${relativeUrlWithSession}
|
|
|
382
495
|
await doStub._init(ctx.props);
|
|
383
496
|
const upgradeUrl = new URL(request.url);
|
|
384
497
|
upgradeUrl.pathname = "/sse";
|
|
498
|
+
const existingHeaders = {};
|
|
499
|
+
request.headers.forEach((value, key) => {
|
|
500
|
+
existingHeaders[key] = value;
|
|
501
|
+
});
|
|
385
502
|
const response = await doStub.fetch(
|
|
386
503
|
new Request(upgradeUrl, {
|
|
387
504
|
headers: {
|
|
505
|
+
...existingHeaders,
|
|
388
506
|
Upgrade: "websocket",
|
|
389
507
|
// Required by PartyServer
|
|
390
508
|
"x-partykit-room": sessionId
|
|
@@ -420,10 +538,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
420
538
|
onMessage(event).catch(console.error);
|
|
421
539
|
});
|
|
422
540
|
ws.addEventListener("error", (error) => {
|
|
423
|
-
async function onError(
|
|
541
|
+
async function onError(_error) {
|
|
424
542
|
try {
|
|
425
543
|
await writer.close();
|
|
426
|
-
} catch (
|
|
544
|
+
} catch (_e) {
|
|
427
545
|
}
|
|
428
546
|
}
|
|
429
547
|
onError(error).catch(console.error);
|
|
@@ -440,9 +558,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
440
558
|
});
|
|
441
559
|
return new Response(readable, {
|
|
442
560
|
headers: {
|
|
443
|
-
"Content-Type": "text/event-stream",
|
|
444
561
|
"Cache-Control": "no-cache",
|
|
445
562
|
Connection: "keep-alive",
|
|
563
|
+
"Content-Type": "text/event-stream",
|
|
446
564
|
...corsHeaders(request, corsOptions)
|
|
447
565
|
}
|
|
448
566
|
});
|
|
@@ -478,23 +596,23 @@ data: ${JSON.stringify(result.data)}
|
|
|
478
596
|
const error = await doStub.onSSEMcpMessage(sessionId, request);
|
|
479
597
|
if (error) {
|
|
480
598
|
return new Response(error.message, {
|
|
481
|
-
status: 400,
|
|
482
599
|
headers: {
|
|
483
|
-
"Content-Type": "text/event-stream",
|
|
484
600
|
"Cache-Control": "no-cache",
|
|
485
601
|
Connection: "keep-alive",
|
|
602
|
+
"Content-Type": "text/event-stream",
|
|
486
603
|
...corsHeaders(request, corsOptions)
|
|
487
|
-
}
|
|
604
|
+
},
|
|
605
|
+
status: 400
|
|
488
606
|
});
|
|
489
607
|
}
|
|
490
608
|
return new Response("Accepted", {
|
|
491
|
-
status: 202,
|
|
492
609
|
headers: {
|
|
493
|
-
"Content-Type": "text/event-stream",
|
|
494
610
|
"Cache-Control": "no-cache",
|
|
495
611
|
Connection: "keep-alive",
|
|
612
|
+
"Content-Type": "text/event-stream",
|
|
496
613
|
...corsHeaders(request, corsOptions)
|
|
497
|
-
}
|
|
614
|
+
},
|
|
615
|
+
status: 202
|
|
498
616
|
});
|
|
499
617
|
}
|
|
500
618
|
return new Response("Not Found", { status: 404 });
|
|
@@ -532,24 +650,24 @@ data: ${JSON.stringify(result.data)}
|
|
|
532
650
|
const acceptHeader = request.headers.get("accept");
|
|
533
651
|
if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
|
|
534
652
|
const body2 = JSON.stringify({
|
|
535
|
-
jsonrpc: "2.0",
|
|
536
653
|
error: {
|
|
537
654
|
code: -32e3,
|
|
538
655
|
message: "Not Acceptable: Client must accept both application/json and text/event-stream"
|
|
539
656
|
},
|
|
540
|
-
id: null
|
|
657
|
+
id: null,
|
|
658
|
+
jsonrpc: "2.0"
|
|
541
659
|
});
|
|
542
660
|
return new Response(body2, { status: 406 });
|
|
543
661
|
}
|
|
544
662
|
const ct = request.headers.get("content-type");
|
|
545
663
|
if (!ct || !ct.includes("application/json")) {
|
|
546
664
|
const body2 = JSON.stringify({
|
|
547
|
-
jsonrpc: "2.0",
|
|
548
665
|
error: {
|
|
549
666
|
code: -32e3,
|
|
550
667
|
message: "Unsupported Media Type: Content-Type must be application/json"
|
|
551
668
|
},
|
|
552
|
-
id: null
|
|
669
|
+
id: null,
|
|
670
|
+
jsonrpc: "2.0"
|
|
553
671
|
});
|
|
554
672
|
return new Response(body2, { status: 415 });
|
|
555
673
|
}
|
|
@@ -559,12 +677,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
559
677
|
);
|
|
560
678
|
if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) {
|
|
561
679
|
const body2 = JSON.stringify({
|
|
562
|
-
jsonrpc: "2.0",
|
|
563
680
|
error: {
|
|
564
681
|
code: -32e3,
|
|
565
682
|
message: `Request body too large. Maximum size is ${MAXIMUM_MESSAGE_SIZE_BYTES} bytes`
|
|
566
683
|
},
|
|
567
|
-
id: null
|
|
684
|
+
id: null,
|
|
685
|
+
jsonrpc: "2.0"
|
|
568
686
|
});
|
|
569
687
|
return new Response(body2, { status: 413 });
|
|
570
688
|
}
|
|
@@ -572,14 +690,14 @@ data: ${JSON.stringify(result.data)}
|
|
|
572
690
|
let rawMessage;
|
|
573
691
|
try {
|
|
574
692
|
rawMessage = await request.json();
|
|
575
|
-
} catch (
|
|
693
|
+
} catch (_error) {
|
|
576
694
|
const body2 = JSON.stringify({
|
|
577
|
-
jsonrpc: "2.0",
|
|
578
695
|
error: {
|
|
579
696
|
code: -32700,
|
|
580
697
|
message: "Parse error: Invalid JSON"
|
|
581
698
|
},
|
|
582
|
-
id: null
|
|
699
|
+
id: null,
|
|
700
|
+
jsonrpc: "2.0"
|
|
583
701
|
});
|
|
584
702
|
return new Response(body2, { status: 400 });
|
|
585
703
|
}
|
|
@@ -593,12 +711,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
593
711
|
for (const msg of arrayMessage) {
|
|
594
712
|
if (!JSONRPCMessageSchema.safeParse(msg).success) {
|
|
595
713
|
const body2 = JSON.stringify({
|
|
596
|
-
jsonrpc: "2.0",
|
|
597
714
|
error: {
|
|
598
715
|
code: -32700,
|
|
599
716
|
message: "Parse error: Invalid JSON-RPC message"
|
|
600
717
|
},
|
|
601
|
-
id: null
|
|
718
|
+
id: null,
|
|
719
|
+
jsonrpc: "2.0"
|
|
602
720
|
});
|
|
603
721
|
return new Response(body2, { status: 400 });
|
|
604
722
|
}
|
|
@@ -609,34 +727,34 @@ data: ${JSON.stringify(result.data)}
|
|
|
609
727
|
);
|
|
610
728
|
if (isInitializationRequest && sessionId) {
|
|
611
729
|
const body2 = JSON.stringify({
|
|
612
|
-
jsonrpc: "2.0",
|
|
613
730
|
error: {
|
|
614
731
|
code: -32600,
|
|
615
732
|
message: "Invalid Request: Initialization requests must not include a sessionId"
|
|
616
733
|
},
|
|
617
|
-
id: null
|
|
734
|
+
id: null,
|
|
735
|
+
jsonrpc: "2.0"
|
|
618
736
|
});
|
|
619
737
|
return new Response(body2, { status: 400 });
|
|
620
738
|
}
|
|
621
739
|
if (isInitializationRequest && messages.length > 1) {
|
|
622
740
|
const body2 = JSON.stringify({
|
|
623
|
-
jsonrpc: "2.0",
|
|
624
741
|
error: {
|
|
625
742
|
code: -32600,
|
|
626
743
|
message: "Invalid Request: Only one initialization request is allowed"
|
|
627
744
|
},
|
|
628
|
-
id: null
|
|
745
|
+
id: null,
|
|
746
|
+
jsonrpc: "2.0"
|
|
629
747
|
});
|
|
630
748
|
return new Response(body2, { status: 400 });
|
|
631
749
|
}
|
|
632
750
|
if (!isInitializationRequest && !sessionId) {
|
|
633
751
|
const body2 = JSON.stringify({
|
|
634
|
-
jsonrpc: "2.0",
|
|
635
752
|
error: {
|
|
636
753
|
code: -32e3,
|
|
637
754
|
message: "Bad Request: Mcp-Session-Id header is required"
|
|
638
755
|
},
|
|
639
|
-
id: null
|
|
756
|
+
id: null,
|
|
757
|
+
jsonrpc: "2.0"
|
|
640
758
|
});
|
|
641
759
|
return new Response(body2, { status: 400 });
|
|
642
760
|
}
|
|
@@ -649,12 +767,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
649
767
|
await doStub.setInitialized();
|
|
650
768
|
} else if (!isInitialized) {
|
|
651
769
|
const body2 = JSON.stringify({
|
|
652
|
-
jsonrpc: "2.0",
|
|
653
770
|
error: {
|
|
654
771
|
code: -32001,
|
|
655
772
|
message: "Session not found"
|
|
656
773
|
},
|
|
657
|
-
id: null
|
|
774
|
+
id: null,
|
|
775
|
+
jsonrpc: "2.0"
|
|
658
776
|
});
|
|
659
777
|
return new Response(body2, { status: 404 });
|
|
660
778
|
}
|
|
@@ -663,9 +781,14 @@ data: ${JSON.stringify(result.data)}
|
|
|
663
781
|
const encoder = new TextEncoder();
|
|
664
782
|
const upgradeUrl = new URL(request.url);
|
|
665
783
|
upgradeUrl.pathname = "/streamable-http";
|
|
784
|
+
const existingHeaders = {};
|
|
785
|
+
request.headers.forEach((value, key) => {
|
|
786
|
+
existingHeaders[key] = value;
|
|
787
|
+
});
|
|
666
788
|
const response = await doStub.fetch(
|
|
667
789
|
new Request(upgradeUrl, {
|
|
668
790
|
headers: {
|
|
791
|
+
...existingHeaders,
|
|
669
792
|
Upgrade: "websocket",
|
|
670
793
|
// Required by PartyServer
|
|
671
794
|
"x-partykit-room": sessionId
|
|
@@ -677,12 +800,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
677
800
|
console.error("Failed to establish WebSocket connection");
|
|
678
801
|
await writer.close();
|
|
679
802
|
const body2 = JSON.stringify({
|
|
680
|
-
jsonrpc: "2.0",
|
|
681
803
|
error: {
|
|
682
804
|
code: -32001,
|
|
683
805
|
message: "Failed to establish WebSocket connection"
|
|
684
806
|
},
|
|
685
|
-
id: null
|
|
807
|
+
id: null,
|
|
808
|
+
jsonrpc: "2.0"
|
|
686
809
|
});
|
|
687
810
|
return new Response(body2, { status: 500 });
|
|
688
811
|
}
|
|
@@ -715,10 +838,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
715
838
|
onMessage(event).catch(console.error);
|
|
716
839
|
});
|
|
717
840
|
ws.addEventListener("error", (error) => {
|
|
718
|
-
async function onError(
|
|
841
|
+
async function onError(_error) {
|
|
719
842
|
try {
|
|
720
843
|
await writer.close();
|
|
721
|
-
} catch (
|
|
844
|
+
} catch (_e) {
|
|
722
845
|
}
|
|
723
846
|
}
|
|
724
847
|
onError(error).catch(console.error);
|
|
@@ -742,8 +865,8 @@ data: ${JSON.stringify(result.data)}
|
|
|
742
865
|
}
|
|
743
866
|
ws.close();
|
|
744
867
|
return new Response(null, {
|
|
745
|
-
|
|
746
|
-
|
|
868
|
+
headers: corsHeaders(request, corsOptions),
|
|
869
|
+
status: 202
|
|
747
870
|
});
|
|
748
871
|
}
|
|
749
872
|
for (const message of messages) {
|
|
@@ -754,9 +877,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
754
877
|
}
|
|
755
878
|
return new Response(readable, {
|
|
756
879
|
headers: {
|
|
757
|
-
"Content-Type": "text/event-stream",
|
|
758
880
|
"Cache-Control": "no-cache",
|
|
759
881
|
Connection: "keep-alive",
|
|
882
|
+
"Content-Type": "text/event-stream",
|
|
760
883
|
"mcp-session-id": sessionId,
|
|
761
884
|
...corsHeaders(request, corsOptions)
|
|
762
885
|
},
|
|
@@ -764,12 +887,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
764
887
|
});
|
|
765
888
|
}
|
|
766
889
|
const body = JSON.stringify({
|
|
767
|
-
jsonrpc: "2.0",
|
|
768
890
|
error: {
|
|
769
891
|
code: -32e3,
|
|
770
892
|
message: "Method not allowed"
|
|
771
893
|
},
|
|
772
|
-
id: null
|
|
894
|
+
id: null,
|
|
895
|
+
jsonrpc: "2.0"
|
|
773
896
|
});
|
|
774
897
|
return new Response(body, { status: 405 });
|
|
775
898
|
}
|
|
@@ -777,6 +900,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
777
900
|
}
|
|
778
901
|
};
|
|
779
902
|
export {
|
|
780
|
-
|
|
903
|
+
ElicitRequestSchema,
|
|
904
|
+
McpAgent,
|
|
905
|
+
SSEEdgeClientTransport,
|
|
906
|
+
StreamableHTTPEdgeClientTransport
|
|
781
907
|
};
|
|
782
908
|
//# sourceMappingURL=index.js.map
|