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