agents 0.0.0-d1f6c02 → 0.0.0-d2872f3
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 +8 -5
- package/dist/ai-chat-agent.js +64 -26
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +13 -10
- package/dist/ai-react.js +27 -27
- package/dist/ai-react.js.map +1 -1
- package/dist/{chunk-Y67CHZBI.js → chunk-HY7ZLHJB.js} +167 -33
- package/dist/chunk-HY7ZLHJB.js.map +1 -0
- package/dist/{chunk-AXSPGBHI.js → chunk-JXN5WZFQ.js} +639 -135
- package/dist/chunk-JXN5WZFQ.js.map +1 -0
- package/dist/{chunk-QSGN3REV.js → chunk-KUH345EY.js} +8 -15
- 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 +8 -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 +28 -10
- package/dist/mcp/index.js +192 -57
- 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 +59 -39
- package/dist/react.js +16 -6
- 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/dist/serializable.d.ts +32 -0
- package/dist/serializable.js +1 -0
- package/dist/serializable.js.map +1 -0
- package/package.json +76 -72
- package/src/index.ts +848 -181
- package/dist/chunk-AXSPGBHI.js.map +0 -1
- package/dist/chunk-BZXOAZUX.js.map +0 -1
- package/dist/chunk-QSGN3REV.js.map +0 -1
- package/dist/chunk-Y67CHZBI.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
|
/**
|
|
@@ -52,28 +62,36 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
|
|
|
52
62
|
_init(props: Props): Promise<void>;
|
|
53
63
|
setInitialized(): Promise<void>;
|
|
54
64
|
isInitialized(): Promise<boolean>;
|
|
65
|
+
updateProps(props: Props): Promise<void>;
|
|
55
66
|
private _initialize;
|
|
56
67
|
fetch(request: Request): Promise<Response>;
|
|
57
68
|
getWebSocket(): WebSocket | null;
|
|
58
69
|
getWebSocketForResponseID(id: string): WebSocket | null;
|
|
59
70
|
onMessage(connection: Connection, event: WSMessage): Promise<void>;
|
|
60
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Wait for elicitation response through storage polling
|
|
73
|
+
*/
|
|
74
|
+
private _waitForElicitationResponse;
|
|
75
|
+
/**
|
|
76
|
+
* Handle elicitation responses */
|
|
77
|
+
private _handleElicitationResponse;
|
|
78
|
+
onSSEMcpMessage(_sessionId: string, messageBody: unknown): Promise<Error | null>;
|
|
61
79
|
webSocketMessage(ws: WebSocket, event: ArrayBuffer | string): Promise<void>;
|
|
62
80
|
webSocketError(ws: WebSocket, error: unknown): Promise<void>;
|
|
63
81
|
webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): Promise<void>;
|
|
64
|
-
static mount(path: string, { binding, corsOptions
|
|
82
|
+
static mount(path: string, { binding, corsOptions }?: {
|
|
65
83
|
binding?: string;
|
|
66
84
|
corsOptions?: CORSOptions;
|
|
67
85
|
}): {
|
|
68
86
|
fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
|
|
69
87
|
};
|
|
70
|
-
static serveSSE(path: string, { binding, corsOptions
|
|
88
|
+
static serveSSE(path: string, { binding, corsOptions }?: {
|
|
71
89
|
binding?: string;
|
|
72
90
|
corsOptions?: CORSOptions;
|
|
73
91
|
}): {
|
|
74
92
|
fetch<Env>(this: void, request: Request, env: Env, ctx: ExecutionContext): Promise<Response>;
|
|
75
93
|
};
|
|
76
|
-
static serve(path: string, { binding, corsOptions
|
|
94
|
+
static serve(path: string, { binding, corsOptions }?: {
|
|
77
95
|
binding?: string;
|
|
78
96
|
corsOptions?: CORSOptions;
|
|
79
97
|
}): {
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,31 +1,40 @@
|
|
|
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
|
}
|
|
35
|
+
function isDurableObjectNamespace(namespace) {
|
|
36
|
+
return typeof namespace === "object" && namespace !== null && "newUniqueId" in namespace && typeof namespace.newUniqueId === "function" && "idFromName" in namespace && typeof namespace.idFromName === "function";
|
|
37
|
+
}
|
|
29
38
|
function handleCORS(request, corsOptions) {
|
|
30
39
|
if (request.method === "OPTIONS") {
|
|
31
40
|
return new Response(null, { headers: corsHeaders(request, corsOptions) });
|
|
@@ -138,6 +147,46 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
138
147
|
setState(state) {
|
|
139
148
|
return this._agent.setState(state);
|
|
140
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
|
|
141
190
|
onStateUpdate(state, source) {
|
|
142
191
|
}
|
|
143
192
|
async onStart() {
|
|
@@ -175,11 +224,10 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
175
224
|
}
|
|
176
225
|
}
|
|
177
226
|
async _init(props) {
|
|
178
|
-
await this.
|
|
227
|
+
await this.updateProps(props);
|
|
179
228
|
if (!this.ctx.storage.get("transportType")) {
|
|
180
229
|
await this.ctx.storage.put("transportType", "unset");
|
|
181
230
|
}
|
|
182
|
-
this.props = props;
|
|
183
231
|
if (!this.initRun) {
|
|
184
232
|
this.initRun = true;
|
|
185
233
|
await this.init();
|
|
@@ -191,6 +239,10 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
191
239
|
async isInitialized() {
|
|
192
240
|
return await this.ctx.storage.get("initialized") === true;
|
|
193
241
|
}
|
|
242
|
+
async updateProps(props) {
|
|
243
|
+
await this.ctx.storage.put("props", props ?? {});
|
|
244
|
+
this.props = props;
|
|
245
|
+
}
|
|
194
246
|
async _initialize() {
|
|
195
247
|
await this.ctx.blockConcurrencyWhile(async () => {
|
|
196
248
|
this._status = "starting";
|
|
@@ -277,14 +329,78 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
277
329
|
this._transport?.onerror?.(error);
|
|
278
330
|
return;
|
|
279
331
|
}
|
|
332
|
+
if (await this._handleElicitationResponse(message)) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
280
335
|
if (isJSONRPCRequest(message)) {
|
|
281
336
|
this._requestIdToConnectionId.set(message.id.toString(), connection.id);
|
|
282
337
|
}
|
|
283
338
|
this._transport?.onmessage?.(message);
|
|
284
339
|
}
|
|
340
|
+
/**
|
|
341
|
+
* Wait for elicitation response through storage polling
|
|
342
|
+
*/
|
|
343
|
+
async _waitForElicitationResponse(requestId) {
|
|
344
|
+
const startTime = Date.now();
|
|
345
|
+
const timeout = 6e4;
|
|
346
|
+
try {
|
|
347
|
+
while (Date.now() - startTime < timeout) {
|
|
348
|
+
const response = await this.ctx.storage.get(
|
|
349
|
+
`elicitation:response:${requestId}`
|
|
350
|
+
);
|
|
351
|
+
if (response) {
|
|
352
|
+
await this.ctx.storage.delete(`elicitation:${requestId}`);
|
|
353
|
+
await this.ctx.storage.delete(`elicitation:response:${requestId}`);
|
|
354
|
+
return response;
|
|
355
|
+
}
|
|
356
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
357
|
+
}
|
|
358
|
+
throw new Error("Elicitation request timed out");
|
|
359
|
+
} finally {
|
|
360
|
+
await this.ctx.storage.delete(`elicitation:${requestId}`);
|
|
361
|
+
await this.ctx.storage.delete(`elicitation:response:${requestId}`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Handle elicitation responses */
|
|
366
|
+
async _handleElicitationResponse(message) {
|
|
367
|
+
if (isJSONRPCResponse(message) && message.result) {
|
|
368
|
+
const requestId = message.id?.toString();
|
|
369
|
+
if (!requestId || !requestId.startsWith("elicit_")) return false;
|
|
370
|
+
const pendingRequest = await this.ctx.storage.get(
|
|
371
|
+
`elicitation:${requestId}`
|
|
372
|
+
);
|
|
373
|
+
if (!pendingRequest) return false;
|
|
374
|
+
await this.ctx.storage.put(
|
|
375
|
+
`elicitation:response:${requestId}`,
|
|
376
|
+
message.result
|
|
377
|
+
);
|
|
378
|
+
return true;
|
|
379
|
+
}
|
|
380
|
+
if (isJSONRPCError(message)) {
|
|
381
|
+
const requestId = message.id?.toString();
|
|
382
|
+
if (!requestId || !requestId.startsWith("elicit_")) return false;
|
|
383
|
+
const pendingRequest = await this.ctx.storage.get(
|
|
384
|
+
`elicitation:${requestId}`
|
|
385
|
+
);
|
|
386
|
+
if (!pendingRequest) return false;
|
|
387
|
+
const errorResult = {
|
|
388
|
+
action: "cancel",
|
|
389
|
+
content: {
|
|
390
|
+
error: message.error.message || "Elicitation request failed"
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
await this.ctx.storage.put(
|
|
394
|
+
`elicitation:response:${requestId}`,
|
|
395
|
+
errorResult
|
|
396
|
+
);
|
|
397
|
+
return true;
|
|
398
|
+
}
|
|
399
|
+
return false;
|
|
400
|
+
}
|
|
285
401
|
// All messages received over SSE after the initial connection has been established
|
|
286
402
|
// will be passed here
|
|
287
|
-
async onSSEMcpMessage(
|
|
403
|
+
async onSSEMcpMessage(_sessionId, messageBody) {
|
|
288
404
|
if (this._status !== "started") {
|
|
289
405
|
await this._initialize();
|
|
290
406
|
}
|
|
@@ -292,14 +408,16 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
292
408
|
return new Error("Internal Server Error: Expected SSE protocol");
|
|
293
409
|
}
|
|
294
410
|
try {
|
|
295
|
-
const message = await request.json();
|
|
296
411
|
let parsedMessage;
|
|
297
412
|
try {
|
|
298
|
-
parsedMessage = JSONRPCMessageSchema.parse(
|
|
413
|
+
parsedMessage = JSONRPCMessageSchema.parse(messageBody);
|
|
299
414
|
} catch (error) {
|
|
300
415
|
this._transport?.onerror?.(error);
|
|
301
416
|
throw error;
|
|
302
417
|
}
|
|
418
|
+
if (await this._handleElicitationResponse(parsedMessage)) {
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
303
421
|
this._transport?.onmessage?.(parsedMessage);
|
|
304
422
|
return null;
|
|
305
423
|
} catch (error) {
|
|
@@ -356,7 +474,7 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
356
474
|
);
|
|
357
475
|
return new Response("Invalid binding", { status: 500 });
|
|
358
476
|
}
|
|
359
|
-
if (bindingValue
|
|
477
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
360
478
|
return new Response("Invalid binding", { status: 500 });
|
|
361
479
|
}
|
|
362
480
|
const namespace = bindingValue;
|
|
@@ -379,9 +497,14 @@ data: ${relativeUrlWithSession}
|
|
|
379
497
|
await doStub._init(ctx.props);
|
|
380
498
|
const upgradeUrl = new URL(request.url);
|
|
381
499
|
upgradeUrl.pathname = "/sse";
|
|
500
|
+
const existingHeaders = {};
|
|
501
|
+
request.headers.forEach((value, key) => {
|
|
502
|
+
existingHeaders[key] = value;
|
|
503
|
+
});
|
|
382
504
|
const response = await doStub.fetch(
|
|
383
505
|
new Request(upgradeUrl, {
|
|
384
506
|
headers: {
|
|
507
|
+
...existingHeaders,
|
|
385
508
|
Upgrade: "websocket",
|
|
386
509
|
// Required by PartyServer
|
|
387
510
|
"x-partykit-room": sessionId
|
|
@@ -417,10 +540,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
417
540
|
onMessage(event).catch(console.error);
|
|
418
541
|
});
|
|
419
542
|
ws.addEventListener("error", (error) => {
|
|
420
|
-
async function onError(
|
|
543
|
+
async function onError(_error) {
|
|
421
544
|
try {
|
|
422
545
|
await writer.close();
|
|
423
|
-
} catch (
|
|
546
|
+
} catch (_e) {
|
|
424
547
|
}
|
|
425
548
|
}
|
|
426
549
|
onError(error).catch(console.error);
|
|
@@ -437,9 +560,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
437
560
|
});
|
|
438
561
|
return new Response(readable, {
|
|
439
562
|
headers: {
|
|
440
|
-
"Content-Type": "text/event-stream",
|
|
441
563
|
"Cache-Control": "no-cache",
|
|
442
564
|
Connection: "keep-alive",
|
|
565
|
+
"Content-Type": "text/event-stream",
|
|
443
566
|
...corsHeaders(request, corsOptions)
|
|
444
567
|
}
|
|
445
568
|
});
|
|
@@ -472,26 +595,28 @@ data: ${JSON.stringify(result.data)}
|
|
|
472
595
|
}
|
|
473
596
|
const id = namespace.idFromName(`sse:${sessionId}`);
|
|
474
597
|
const doStub = namespace.get(id);
|
|
475
|
-
const
|
|
598
|
+
const messageBody = await request.json();
|
|
599
|
+
await doStub.updateProps(ctx.props);
|
|
600
|
+
const error = await doStub.onSSEMcpMessage(sessionId, messageBody);
|
|
476
601
|
if (error) {
|
|
477
602
|
return new Response(error.message, {
|
|
478
|
-
status: 400,
|
|
479
603
|
headers: {
|
|
480
|
-
"Content-Type": "text/event-stream",
|
|
481
604
|
"Cache-Control": "no-cache",
|
|
482
605
|
Connection: "keep-alive",
|
|
606
|
+
"Content-Type": "text/event-stream",
|
|
483
607
|
...corsHeaders(request, corsOptions)
|
|
484
|
-
}
|
|
608
|
+
},
|
|
609
|
+
status: 400
|
|
485
610
|
});
|
|
486
611
|
}
|
|
487
612
|
return new Response("Accepted", {
|
|
488
|
-
status: 202,
|
|
489
613
|
headers: {
|
|
490
|
-
"Content-Type": "text/event-stream",
|
|
491
614
|
"Cache-Control": "no-cache",
|
|
492
615
|
Connection: "keep-alive",
|
|
616
|
+
"Content-Type": "text/event-stream",
|
|
493
617
|
...corsHeaders(request, corsOptions)
|
|
494
|
-
}
|
|
618
|
+
},
|
|
619
|
+
status: 202
|
|
495
620
|
});
|
|
496
621
|
}
|
|
497
622
|
return new Response("Not Found", { status: 404 });
|
|
@@ -521,7 +646,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
521
646
|
);
|
|
522
647
|
return new Response("Invalid binding", { status: 500 });
|
|
523
648
|
}
|
|
524
|
-
if (bindingValue
|
|
649
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
525
650
|
return new Response("Invalid binding", { status: 500 });
|
|
526
651
|
}
|
|
527
652
|
const namespace = bindingValue;
|
|
@@ -529,24 +654,24 @@ data: ${JSON.stringify(result.data)}
|
|
|
529
654
|
const acceptHeader = request.headers.get("accept");
|
|
530
655
|
if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
|
|
531
656
|
const body2 = JSON.stringify({
|
|
532
|
-
jsonrpc: "2.0",
|
|
533
657
|
error: {
|
|
534
658
|
code: -32e3,
|
|
535
659
|
message: "Not Acceptable: Client must accept both application/json and text/event-stream"
|
|
536
660
|
},
|
|
537
|
-
id: null
|
|
661
|
+
id: null,
|
|
662
|
+
jsonrpc: "2.0"
|
|
538
663
|
});
|
|
539
664
|
return new Response(body2, { status: 406 });
|
|
540
665
|
}
|
|
541
666
|
const ct = request.headers.get("content-type");
|
|
542
667
|
if (!ct || !ct.includes("application/json")) {
|
|
543
668
|
const body2 = JSON.stringify({
|
|
544
|
-
jsonrpc: "2.0",
|
|
545
669
|
error: {
|
|
546
670
|
code: -32e3,
|
|
547
671
|
message: "Unsupported Media Type: Content-Type must be application/json"
|
|
548
672
|
},
|
|
549
|
-
id: null
|
|
673
|
+
id: null,
|
|
674
|
+
jsonrpc: "2.0"
|
|
550
675
|
});
|
|
551
676
|
return new Response(body2, { status: 415 });
|
|
552
677
|
}
|
|
@@ -556,12 +681,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
556
681
|
);
|
|
557
682
|
if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) {
|
|
558
683
|
const body2 = JSON.stringify({
|
|
559
|
-
jsonrpc: "2.0",
|
|
560
684
|
error: {
|
|
561
685
|
code: -32e3,
|
|
562
686
|
message: `Request body too large. Maximum size is ${MAXIMUM_MESSAGE_SIZE_BYTES} bytes`
|
|
563
687
|
},
|
|
564
|
-
id: null
|
|
688
|
+
id: null,
|
|
689
|
+
jsonrpc: "2.0"
|
|
565
690
|
});
|
|
566
691
|
return new Response(body2, { status: 413 });
|
|
567
692
|
}
|
|
@@ -569,14 +694,14 @@ data: ${JSON.stringify(result.data)}
|
|
|
569
694
|
let rawMessage;
|
|
570
695
|
try {
|
|
571
696
|
rawMessage = await request.json();
|
|
572
|
-
} catch (
|
|
697
|
+
} catch (_error) {
|
|
573
698
|
const body2 = JSON.stringify({
|
|
574
|
-
jsonrpc: "2.0",
|
|
575
699
|
error: {
|
|
576
700
|
code: -32700,
|
|
577
701
|
message: "Parse error: Invalid JSON"
|
|
578
702
|
},
|
|
579
|
-
id: null
|
|
703
|
+
id: null,
|
|
704
|
+
jsonrpc: "2.0"
|
|
580
705
|
});
|
|
581
706
|
return new Response(body2, { status: 400 });
|
|
582
707
|
}
|
|
@@ -590,12 +715,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
590
715
|
for (const msg of arrayMessage) {
|
|
591
716
|
if (!JSONRPCMessageSchema.safeParse(msg).success) {
|
|
592
717
|
const body2 = JSON.stringify({
|
|
593
|
-
jsonrpc: "2.0",
|
|
594
718
|
error: {
|
|
595
719
|
code: -32700,
|
|
596
720
|
message: "Parse error: Invalid JSON-RPC message"
|
|
597
721
|
},
|
|
598
|
-
id: null
|
|
722
|
+
id: null,
|
|
723
|
+
jsonrpc: "2.0"
|
|
599
724
|
});
|
|
600
725
|
return new Response(body2, { status: 400 });
|
|
601
726
|
}
|
|
@@ -606,34 +731,34 @@ data: ${JSON.stringify(result.data)}
|
|
|
606
731
|
);
|
|
607
732
|
if (isInitializationRequest && sessionId) {
|
|
608
733
|
const body2 = JSON.stringify({
|
|
609
|
-
jsonrpc: "2.0",
|
|
610
734
|
error: {
|
|
611
735
|
code: -32600,
|
|
612
736
|
message: "Invalid Request: Initialization requests must not include a sessionId"
|
|
613
737
|
},
|
|
614
|
-
id: null
|
|
738
|
+
id: null,
|
|
739
|
+
jsonrpc: "2.0"
|
|
615
740
|
});
|
|
616
741
|
return new Response(body2, { status: 400 });
|
|
617
742
|
}
|
|
618
743
|
if (isInitializationRequest && messages.length > 1) {
|
|
619
744
|
const body2 = JSON.stringify({
|
|
620
|
-
jsonrpc: "2.0",
|
|
621
745
|
error: {
|
|
622
746
|
code: -32600,
|
|
623
747
|
message: "Invalid Request: Only one initialization request is allowed"
|
|
624
748
|
},
|
|
625
|
-
id: null
|
|
749
|
+
id: null,
|
|
750
|
+
jsonrpc: "2.0"
|
|
626
751
|
});
|
|
627
752
|
return new Response(body2, { status: 400 });
|
|
628
753
|
}
|
|
629
754
|
if (!isInitializationRequest && !sessionId) {
|
|
630
755
|
const body2 = JSON.stringify({
|
|
631
|
-
jsonrpc: "2.0",
|
|
632
756
|
error: {
|
|
633
757
|
code: -32e3,
|
|
634
758
|
message: "Bad Request: Mcp-Session-Id header is required"
|
|
635
759
|
},
|
|
636
|
-
id: null
|
|
760
|
+
id: null,
|
|
761
|
+
jsonrpc: "2.0"
|
|
637
762
|
});
|
|
638
763
|
return new Response(body2, { status: 400 });
|
|
639
764
|
}
|
|
@@ -646,23 +771,30 @@ data: ${JSON.stringify(result.data)}
|
|
|
646
771
|
await doStub.setInitialized();
|
|
647
772
|
} else if (!isInitialized) {
|
|
648
773
|
const body2 = JSON.stringify({
|
|
649
|
-
jsonrpc: "2.0",
|
|
650
774
|
error: {
|
|
651
775
|
code: -32001,
|
|
652
776
|
message: "Session not found"
|
|
653
777
|
},
|
|
654
|
-
id: null
|
|
778
|
+
id: null,
|
|
779
|
+
jsonrpc: "2.0"
|
|
655
780
|
});
|
|
656
781
|
return new Response(body2, { status: 404 });
|
|
782
|
+
} else {
|
|
783
|
+
await doStub.updateProps(ctx.props);
|
|
657
784
|
}
|
|
658
785
|
const { readable, writable } = new TransformStream();
|
|
659
786
|
const writer = writable.getWriter();
|
|
660
787
|
const encoder = new TextEncoder();
|
|
661
788
|
const upgradeUrl = new URL(request.url);
|
|
662
789
|
upgradeUrl.pathname = "/streamable-http";
|
|
790
|
+
const existingHeaders = {};
|
|
791
|
+
request.headers.forEach((value, key) => {
|
|
792
|
+
existingHeaders[key] = value;
|
|
793
|
+
});
|
|
663
794
|
const response = await doStub.fetch(
|
|
664
795
|
new Request(upgradeUrl, {
|
|
665
796
|
headers: {
|
|
797
|
+
...existingHeaders,
|
|
666
798
|
Upgrade: "websocket",
|
|
667
799
|
// Required by PartyServer
|
|
668
800
|
"x-partykit-room": sessionId
|
|
@@ -674,12 +806,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
674
806
|
console.error("Failed to establish WebSocket connection");
|
|
675
807
|
await writer.close();
|
|
676
808
|
const body2 = JSON.stringify({
|
|
677
|
-
jsonrpc: "2.0",
|
|
678
809
|
error: {
|
|
679
810
|
code: -32001,
|
|
680
811
|
message: "Failed to establish WebSocket connection"
|
|
681
812
|
},
|
|
682
|
-
id: null
|
|
813
|
+
id: null,
|
|
814
|
+
jsonrpc: "2.0"
|
|
683
815
|
});
|
|
684
816
|
return new Response(body2, { status: 500 });
|
|
685
817
|
}
|
|
@@ -712,10 +844,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
712
844
|
onMessage(event).catch(console.error);
|
|
713
845
|
});
|
|
714
846
|
ws.addEventListener("error", (error) => {
|
|
715
|
-
async function onError(
|
|
847
|
+
async function onError(_error) {
|
|
716
848
|
try {
|
|
717
849
|
await writer.close();
|
|
718
|
-
} catch (
|
|
850
|
+
} catch (_e) {
|
|
719
851
|
}
|
|
720
852
|
}
|
|
721
853
|
onError(error).catch(console.error);
|
|
@@ -739,8 +871,8 @@ data: ${JSON.stringify(result.data)}
|
|
|
739
871
|
}
|
|
740
872
|
ws.close();
|
|
741
873
|
return new Response(null, {
|
|
742
|
-
|
|
743
|
-
|
|
874
|
+
headers: corsHeaders(request, corsOptions),
|
|
875
|
+
status: 202
|
|
744
876
|
});
|
|
745
877
|
}
|
|
746
878
|
for (const message of messages) {
|
|
@@ -751,9 +883,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
751
883
|
}
|
|
752
884
|
return new Response(readable, {
|
|
753
885
|
headers: {
|
|
754
|
-
"Content-Type": "text/event-stream",
|
|
755
886
|
"Cache-Control": "no-cache",
|
|
756
887
|
Connection: "keep-alive",
|
|
888
|
+
"Content-Type": "text/event-stream",
|
|
757
889
|
"mcp-session-id": sessionId,
|
|
758
890
|
...corsHeaders(request, corsOptions)
|
|
759
891
|
},
|
|
@@ -761,12 +893,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
761
893
|
});
|
|
762
894
|
}
|
|
763
895
|
const body = JSON.stringify({
|
|
764
|
-
jsonrpc: "2.0",
|
|
765
896
|
error: {
|
|
766
897
|
code: -32e3,
|
|
767
898
|
message: "Method not allowed"
|
|
768
899
|
},
|
|
769
|
-
id: null
|
|
900
|
+
id: null,
|
|
901
|
+
jsonrpc: "2.0"
|
|
770
902
|
});
|
|
771
903
|
return new Response(body, { status: 405 });
|
|
772
904
|
}
|
|
@@ -774,6 +906,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
774
906
|
}
|
|
775
907
|
};
|
|
776
908
|
export {
|
|
777
|
-
|
|
909
|
+
ElicitRequestSchema,
|
|
910
|
+
McpAgent,
|
|
911
|
+
SSEEdgeClientTransport,
|
|
912
|
+
StreamableHTTPEdgeClientTransport
|
|
778
913
|
};
|
|
779
914
|
//# sourceMappingURL=index.js.map
|