agents 0.0.0-33ff003 → 0.0.0-352d62c
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 +131 -25
- package/dist/ai-chat-agent.d.ts +10 -5
- package/dist/ai-chat-agent.js +75 -42
- package/dist/ai-chat-agent.js.map +1 -1
- package/dist/ai-react.d.ts +14 -9
- package/dist/ai-react.js +31 -31
- package/dist/ai-react.js.map +1 -1
- package/dist/ai-types.d.ts +27 -9
- package/dist/ai-types.js +6 -0
- package/dist/chunk-EEKLJYON.js +17 -0
- package/dist/chunk-EEKLJYON.js.map +1 -0
- package/dist/{chunk-Y67CHZBI.js → chunk-EM3J4KV7.js} +168 -34
- package/dist/chunk-EM3J4KV7.js.map +1 -0
- package/dist/chunk-ID62XSAS.js +1290 -0
- package/dist/chunk-ID62XSAS.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-QSGN3REV.js → chunk-QEVM4BVL.js} +10 -17
- package/dist/chunk-QEVM4BVL.js.map +1 -0
- package/dist/client-DgyzBU_8.d.ts +4601 -0
- package/dist/client.d.ts +8 -2
- package/dist/client.js +2 -2
- package/dist/index.d.ts +192 -19
- package/dist/index.js +11 -5
- package/dist/mcp/client.d.ts +9 -781
- package/dist/mcp/client.js +1 -2
- package/dist/mcp/do-oauth-client-provider.js +1 -2
- package/dist/mcp/index.d.ts +38 -10
- package/dist/mcp/index.js +238 -62
- 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/react.d.ts +81 -11
- package/dist/react.js +20 -10
- package/dist/react.js.map +1 -1
- package/dist/schedule.d.ts +6 -6
- package/dist/schedule.js +4 -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 +79 -71
- package/src/index.ts +865 -187
- package/dist/chunk-BZXOAZUX.js.map +0 -1
- package/dist/chunk-NOUFNU2O.js +0 -12
- package/dist/chunk-NPGUKHFR.js +0 -773
- package/dist/chunk-NPGUKHFR.js.map +0 -1
- package/dist/chunk-QSGN3REV.js.map +0 -1
- package/dist/chunk-Y67CHZBI.js.map +0 -1
- /package/dist/{chunk-NOUFNU2O.js.map → observability/index.js.map} +0 -0
package/dist/mcp/index.js
CHANGED
|
@@ -1,32 +1,41 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
} from "../chunk-ID62XSAS.js";
|
|
4
|
+
import {
|
|
5
|
+
SSEEdgeClientTransport,
|
|
6
|
+
StreamableHTTPEdgeClientTransport
|
|
7
|
+
} from "../chunk-EM3J4KV7.js";
|
|
8
|
+
import "../chunk-PVQZBKN7.js";
|
|
9
|
+
import "../chunk-QEVM4BVL.js";
|
|
10
|
+
import "../chunk-EEKLJYON.js";
|
|
8
11
|
|
|
9
12
|
// src/mcp/index.ts
|
|
10
13
|
import { DurableObject } from "cloudflare:workers";
|
|
11
14
|
import {
|
|
12
15
|
InitializeRequestSchema,
|
|
16
|
+
JSONRPCMessageSchema,
|
|
13
17
|
isJSONRPCError,
|
|
14
18
|
isJSONRPCNotification,
|
|
15
19
|
isJSONRPCRequest,
|
|
16
|
-
isJSONRPCResponse
|
|
17
|
-
|
|
20
|
+
isJSONRPCResponse
|
|
21
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
22
|
+
import {
|
|
23
|
+
ElicitRequestSchema
|
|
18
24
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
19
25
|
var MAXIMUM_MESSAGE_SIZE_BYTES = 4 * 1024 * 1024;
|
|
20
|
-
function corsHeaders(
|
|
26
|
+
function corsHeaders(_request, corsOptions = {}) {
|
|
21
27
|
const origin = "*";
|
|
22
28
|
return {
|
|
23
|
-
"Access-Control-Allow-
|
|
29
|
+
"Access-Control-Allow-Headers": corsOptions.headers || "Content-Type, mcp-session-id, mcp-protocol-version",
|
|
24
30
|
"Access-Control-Allow-Methods": corsOptions.methods || "GET, POST, OPTIONS",
|
|
25
|
-
"Access-Control-Allow-
|
|
26
|
-
"Access-Control-
|
|
27
|
-
"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()
|
|
28
34
|
};
|
|
29
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
|
+
}
|
|
30
39
|
function handleCORS(request, corsOptions) {
|
|
31
40
|
if (request.method === "OPTIONS") {
|
|
32
41
|
return new Response(null, { headers: corsHeaders(request, corsOptions) });
|
|
@@ -139,6 +148,46 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
139
148
|
setState(state) {
|
|
140
149
|
return this._agent.setState(state);
|
|
141
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
|
|
142
191
|
onStateUpdate(state, source) {
|
|
143
192
|
}
|
|
144
193
|
async onStart() {
|
|
@@ -175,15 +224,32 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
175
224
|
await server.connect(this._transport);
|
|
176
225
|
}
|
|
177
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
|
+
}
|
|
178
240
|
async _init(props) {
|
|
179
|
-
await this.
|
|
241
|
+
await this.updateProps(props);
|
|
180
242
|
if (!this.ctx.storage.get("transportType")) {
|
|
181
243
|
await this.ctx.storage.put("transportType", "unset");
|
|
182
244
|
}
|
|
183
|
-
this.props = props;
|
|
184
245
|
if (!this.initRun) {
|
|
185
246
|
this.initRun = true;
|
|
186
|
-
|
|
247
|
+
try {
|
|
248
|
+
await this.init();
|
|
249
|
+
} catch (error) {
|
|
250
|
+
const errorResponse = this.onError(error);
|
|
251
|
+
throw new Error(`Initialization failed: ${errorResponse.message}`);
|
|
252
|
+
}
|
|
187
253
|
}
|
|
188
254
|
}
|
|
189
255
|
async setInitialized() {
|
|
@@ -192,6 +258,10 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
192
258
|
async isInitialized() {
|
|
193
259
|
return await this.ctx.storage.get("initialized") === true;
|
|
194
260
|
}
|
|
261
|
+
async updateProps(props) {
|
|
262
|
+
await this.ctx.storage.put("props", props ?? {});
|
|
263
|
+
this.props = props;
|
|
264
|
+
}
|
|
195
265
|
async _initialize() {
|
|
196
266
|
await this.ctx.blockConcurrencyWhile(async () => {
|
|
197
267
|
this._status = "starting";
|
|
@@ -278,14 +348,78 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
278
348
|
this._transport?.onerror?.(error);
|
|
279
349
|
return;
|
|
280
350
|
}
|
|
351
|
+
if (await this._handleElicitationResponse(message)) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
281
354
|
if (isJSONRPCRequest(message)) {
|
|
282
355
|
this._requestIdToConnectionId.set(message.id.toString(), connection.id);
|
|
283
356
|
}
|
|
284
357
|
this._transport?.onmessage?.(message);
|
|
285
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
|
+
}
|
|
286
420
|
// All messages received over SSE after the initial connection has been established
|
|
287
421
|
// will be passed here
|
|
288
|
-
async onSSEMcpMessage(
|
|
422
|
+
async onSSEMcpMessage(_sessionId, messageBody) {
|
|
289
423
|
if (this._status !== "started") {
|
|
290
424
|
await this._initialize();
|
|
291
425
|
}
|
|
@@ -293,14 +427,16 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
293
427
|
return new Error("Internal Server Error: Expected SSE protocol");
|
|
294
428
|
}
|
|
295
429
|
try {
|
|
296
|
-
const message = await request.json();
|
|
297
430
|
let parsedMessage;
|
|
298
431
|
try {
|
|
299
|
-
parsedMessage = JSONRPCMessageSchema.parse(
|
|
432
|
+
parsedMessage = JSONRPCMessageSchema.parse(messageBody);
|
|
300
433
|
} catch (error) {
|
|
301
434
|
this._transport?.onerror?.(error);
|
|
302
435
|
throw error;
|
|
303
436
|
}
|
|
437
|
+
if (await this._handleElicitationResponse(parsedMessage)) {
|
|
438
|
+
return null;
|
|
439
|
+
}
|
|
304
440
|
this._transport?.onmessage?.(parsedMessage);
|
|
305
441
|
return null;
|
|
306
442
|
} catch (error) {
|
|
@@ -357,7 +493,7 @@ var McpAgent = class _McpAgent extends DurableObject {
|
|
|
357
493
|
);
|
|
358
494
|
return new Response("Invalid binding", { status: 500 });
|
|
359
495
|
}
|
|
360
|
-
if (bindingValue
|
|
496
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
361
497
|
return new Response("Invalid binding", { status: 500 });
|
|
362
498
|
}
|
|
363
499
|
const namespace = bindingValue;
|
|
@@ -377,12 +513,26 @@ data: ${relativeUrlWithSession}
|
|
|
377
513
|
writer.write(encoder.encode(endpointMessage));
|
|
378
514
|
const id = namespace.idFromName(`sse:${sessionId}`);
|
|
379
515
|
const doStub = namespace.get(id);
|
|
380
|
-
|
|
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
|
+
}
|
|
381
526
|
const upgradeUrl = new URL(request.url);
|
|
382
527
|
upgradeUrl.pathname = "/sse";
|
|
528
|
+
const existingHeaders = {};
|
|
529
|
+
request.headers.forEach((value, key) => {
|
|
530
|
+
existingHeaders[key] = value;
|
|
531
|
+
});
|
|
383
532
|
const response = await doStub.fetch(
|
|
384
533
|
new Request(upgradeUrl, {
|
|
385
534
|
headers: {
|
|
535
|
+
...existingHeaders,
|
|
386
536
|
Upgrade: "websocket",
|
|
387
537
|
// Required by PartyServer
|
|
388
538
|
"x-partykit-room": sessionId
|
|
@@ -418,10 +568,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
418
568
|
onMessage(event).catch(console.error);
|
|
419
569
|
});
|
|
420
570
|
ws.addEventListener("error", (error) => {
|
|
421
|
-
async function onError(
|
|
571
|
+
async function onError(_error) {
|
|
422
572
|
try {
|
|
423
573
|
await writer.close();
|
|
424
|
-
} catch (
|
|
574
|
+
} catch (_e) {
|
|
425
575
|
}
|
|
426
576
|
}
|
|
427
577
|
onError(error).catch(console.error);
|
|
@@ -438,9 +588,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
438
588
|
});
|
|
439
589
|
return new Response(readable, {
|
|
440
590
|
headers: {
|
|
441
|
-
"Content-Type": "text/event-stream",
|
|
442
591
|
"Cache-Control": "no-cache",
|
|
443
592
|
Connection: "keep-alive",
|
|
593
|
+
"Content-Type": "text/event-stream",
|
|
444
594
|
...corsHeaders(request, corsOptions)
|
|
445
595
|
}
|
|
446
596
|
});
|
|
@@ -473,26 +623,28 @@ data: ${JSON.stringify(result.data)}
|
|
|
473
623
|
}
|
|
474
624
|
const id = namespace.idFromName(`sse:${sessionId}`);
|
|
475
625
|
const doStub = namespace.get(id);
|
|
476
|
-
const
|
|
626
|
+
const messageBody = await request.json();
|
|
627
|
+
await doStub.updateProps(ctx.props);
|
|
628
|
+
const error = await doStub.onSSEMcpMessage(sessionId, messageBody);
|
|
477
629
|
if (error) {
|
|
478
630
|
return new Response(error.message, {
|
|
479
|
-
status: 400,
|
|
480
631
|
headers: {
|
|
481
|
-
"Content-Type": "text/event-stream",
|
|
482
632
|
"Cache-Control": "no-cache",
|
|
483
633
|
Connection: "keep-alive",
|
|
634
|
+
"Content-Type": "text/event-stream",
|
|
484
635
|
...corsHeaders(request, corsOptions)
|
|
485
|
-
}
|
|
636
|
+
},
|
|
637
|
+
status: 400
|
|
486
638
|
});
|
|
487
639
|
}
|
|
488
640
|
return new Response("Accepted", {
|
|
489
|
-
status: 202,
|
|
490
641
|
headers: {
|
|
491
|
-
"Content-Type": "text/event-stream",
|
|
492
642
|
"Cache-Control": "no-cache",
|
|
493
643
|
Connection: "keep-alive",
|
|
644
|
+
"Content-Type": "text/event-stream",
|
|
494
645
|
...corsHeaders(request, corsOptions)
|
|
495
|
-
}
|
|
646
|
+
},
|
|
647
|
+
status: 202
|
|
496
648
|
});
|
|
497
649
|
}
|
|
498
650
|
return new Response("Not Found", { status: 404 });
|
|
@@ -522,7 +674,7 @@ data: ${JSON.stringify(result.data)}
|
|
|
522
674
|
);
|
|
523
675
|
return new Response("Invalid binding", { status: 500 });
|
|
524
676
|
}
|
|
525
|
-
if (bindingValue
|
|
677
|
+
if (!isDurableObjectNamespace(bindingValue)) {
|
|
526
678
|
return new Response("Invalid binding", { status: 500 });
|
|
527
679
|
}
|
|
528
680
|
const namespace = bindingValue;
|
|
@@ -530,24 +682,24 @@ data: ${JSON.stringify(result.data)}
|
|
|
530
682
|
const acceptHeader = request.headers.get("accept");
|
|
531
683
|
if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
|
|
532
684
|
const body2 = JSON.stringify({
|
|
533
|
-
jsonrpc: "2.0",
|
|
534
685
|
error: {
|
|
535
686
|
code: -32e3,
|
|
536
687
|
message: "Not Acceptable: Client must accept both application/json and text/event-stream"
|
|
537
688
|
},
|
|
538
|
-
id: null
|
|
689
|
+
id: null,
|
|
690
|
+
jsonrpc: "2.0"
|
|
539
691
|
});
|
|
540
692
|
return new Response(body2, { status: 406 });
|
|
541
693
|
}
|
|
542
694
|
const ct = request.headers.get("content-type");
|
|
543
695
|
if (!ct || !ct.includes("application/json")) {
|
|
544
696
|
const body2 = JSON.stringify({
|
|
545
|
-
jsonrpc: "2.0",
|
|
546
697
|
error: {
|
|
547
698
|
code: -32e3,
|
|
548
699
|
message: "Unsupported Media Type: Content-Type must be application/json"
|
|
549
700
|
},
|
|
550
|
-
id: null
|
|
701
|
+
id: null,
|
|
702
|
+
jsonrpc: "2.0"
|
|
551
703
|
});
|
|
552
704
|
return new Response(body2, { status: 415 });
|
|
553
705
|
}
|
|
@@ -557,12 +709,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
557
709
|
);
|
|
558
710
|
if (contentLength > MAXIMUM_MESSAGE_SIZE_BYTES) {
|
|
559
711
|
const body2 = JSON.stringify({
|
|
560
|
-
jsonrpc: "2.0",
|
|
561
712
|
error: {
|
|
562
713
|
code: -32e3,
|
|
563
714
|
message: `Request body too large. Maximum size is ${MAXIMUM_MESSAGE_SIZE_BYTES} bytes`
|
|
564
715
|
},
|
|
565
|
-
id: null
|
|
716
|
+
id: null,
|
|
717
|
+
jsonrpc: "2.0"
|
|
566
718
|
});
|
|
567
719
|
return new Response(body2, { status: 413 });
|
|
568
720
|
}
|
|
@@ -570,14 +722,14 @@ data: ${JSON.stringify(result.data)}
|
|
|
570
722
|
let rawMessage;
|
|
571
723
|
try {
|
|
572
724
|
rawMessage = await request.json();
|
|
573
|
-
} catch (
|
|
725
|
+
} catch (_error) {
|
|
574
726
|
const body2 = JSON.stringify({
|
|
575
|
-
jsonrpc: "2.0",
|
|
576
727
|
error: {
|
|
577
728
|
code: -32700,
|
|
578
729
|
message: "Parse error: Invalid JSON"
|
|
579
730
|
},
|
|
580
|
-
id: null
|
|
731
|
+
id: null,
|
|
732
|
+
jsonrpc: "2.0"
|
|
581
733
|
});
|
|
582
734
|
return new Response(body2, { status: 400 });
|
|
583
735
|
}
|
|
@@ -591,12 +743,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
591
743
|
for (const msg of arrayMessage) {
|
|
592
744
|
if (!JSONRPCMessageSchema.safeParse(msg).success) {
|
|
593
745
|
const body2 = JSON.stringify({
|
|
594
|
-
jsonrpc: "2.0",
|
|
595
746
|
error: {
|
|
596
747
|
code: -32700,
|
|
597
748
|
message: "Parse error: Invalid JSON-RPC message"
|
|
598
749
|
},
|
|
599
|
-
id: null
|
|
750
|
+
id: null,
|
|
751
|
+
jsonrpc: "2.0"
|
|
600
752
|
});
|
|
601
753
|
return new Response(body2, { status: 400 });
|
|
602
754
|
}
|
|
@@ -607,34 +759,34 @@ data: ${JSON.stringify(result.data)}
|
|
|
607
759
|
);
|
|
608
760
|
if (isInitializationRequest && sessionId) {
|
|
609
761
|
const body2 = JSON.stringify({
|
|
610
|
-
jsonrpc: "2.0",
|
|
611
762
|
error: {
|
|
612
763
|
code: -32600,
|
|
613
764
|
message: "Invalid Request: Initialization requests must not include a sessionId"
|
|
614
765
|
},
|
|
615
|
-
id: null
|
|
766
|
+
id: null,
|
|
767
|
+
jsonrpc: "2.0"
|
|
616
768
|
});
|
|
617
769
|
return new Response(body2, { status: 400 });
|
|
618
770
|
}
|
|
619
771
|
if (isInitializationRequest && messages.length > 1) {
|
|
620
772
|
const body2 = JSON.stringify({
|
|
621
|
-
jsonrpc: "2.0",
|
|
622
773
|
error: {
|
|
623
774
|
code: -32600,
|
|
624
775
|
message: "Invalid Request: Only one initialization request is allowed"
|
|
625
776
|
},
|
|
626
|
-
id: null
|
|
777
|
+
id: null,
|
|
778
|
+
jsonrpc: "2.0"
|
|
627
779
|
});
|
|
628
780
|
return new Response(body2, { status: 400 });
|
|
629
781
|
}
|
|
630
782
|
if (!isInitializationRequest && !sessionId) {
|
|
631
783
|
const body2 = JSON.stringify({
|
|
632
|
-
jsonrpc: "2.0",
|
|
633
784
|
error: {
|
|
634
785
|
code: -32e3,
|
|
635
786
|
message: "Bad Request: Mcp-Session-Id header is required"
|
|
636
787
|
},
|
|
637
|
-
id: null
|
|
788
|
+
id: null,
|
|
789
|
+
jsonrpc: "2.0"
|
|
638
790
|
});
|
|
639
791
|
return new Response(body2, { status: 400 });
|
|
640
792
|
}
|
|
@@ -643,27 +795,48 @@ data: ${JSON.stringify(result.data)}
|
|
|
643
795
|
const doStub = namespace.get(id);
|
|
644
796
|
const isInitialized = await doStub.isInitialized();
|
|
645
797
|
if (isInitializationRequest) {
|
|
646
|
-
|
|
647
|
-
|
|
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
|
+
}
|
|
648
814
|
} else if (!isInitialized) {
|
|
649
815
|
const body2 = JSON.stringify({
|
|
650
|
-
jsonrpc: "2.0",
|
|
651
816
|
error: {
|
|
652
817
|
code: -32001,
|
|
653
818
|
message: "Session not found"
|
|
654
819
|
},
|
|
655
|
-
id: null
|
|
820
|
+
id: null,
|
|
821
|
+
jsonrpc: "2.0"
|
|
656
822
|
});
|
|
657
823
|
return new Response(body2, { status: 404 });
|
|
824
|
+
} else {
|
|
825
|
+
await doStub.updateProps(ctx.props);
|
|
658
826
|
}
|
|
659
827
|
const { readable, writable } = new TransformStream();
|
|
660
828
|
const writer = writable.getWriter();
|
|
661
829
|
const encoder = new TextEncoder();
|
|
662
830
|
const upgradeUrl = new URL(request.url);
|
|
663
831
|
upgradeUrl.pathname = "/streamable-http";
|
|
832
|
+
const existingHeaders = {};
|
|
833
|
+
request.headers.forEach((value, key) => {
|
|
834
|
+
existingHeaders[key] = value;
|
|
835
|
+
});
|
|
664
836
|
const response = await doStub.fetch(
|
|
665
837
|
new Request(upgradeUrl, {
|
|
666
838
|
headers: {
|
|
839
|
+
...existingHeaders,
|
|
667
840
|
Upgrade: "websocket",
|
|
668
841
|
// Required by PartyServer
|
|
669
842
|
"x-partykit-room": sessionId
|
|
@@ -675,12 +848,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
675
848
|
console.error("Failed to establish WebSocket connection");
|
|
676
849
|
await writer.close();
|
|
677
850
|
const body2 = JSON.stringify({
|
|
678
|
-
jsonrpc: "2.0",
|
|
679
851
|
error: {
|
|
680
852
|
code: -32001,
|
|
681
853
|
message: "Failed to establish WebSocket connection"
|
|
682
854
|
},
|
|
683
|
-
id: null
|
|
855
|
+
id: null,
|
|
856
|
+
jsonrpc: "2.0"
|
|
684
857
|
});
|
|
685
858
|
return new Response(body2, { status: 500 });
|
|
686
859
|
}
|
|
@@ -713,10 +886,10 @@ data: ${JSON.stringify(result.data)}
|
|
|
713
886
|
onMessage(event).catch(console.error);
|
|
714
887
|
});
|
|
715
888
|
ws.addEventListener("error", (error) => {
|
|
716
|
-
async function onError(
|
|
889
|
+
async function onError(_error) {
|
|
717
890
|
try {
|
|
718
891
|
await writer.close();
|
|
719
|
-
} catch (
|
|
892
|
+
} catch (_e) {
|
|
720
893
|
}
|
|
721
894
|
}
|
|
722
895
|
onError(error).catch(console.error);
|
|
@@ -740,8 +913,8 @@ data: ${JSON.stringify(result.data)}
|
|
|
740
913
|
}
|
|
741
914
|
ws.close();
|
|
742
915
|
return new Response(null, {
|
|
743
|
-
|
|
744
|
-
|
|
916
|
+
headers: corsHeaders(request, corsOptions),
|
|
917
|
+
status: 202
|
|
745
918
|
});
|
|
746
919
|
}
|
|
747
920
|
for (const message of messages) {
|
|
@@ -752,9 +925,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
752
925
|
}
|
|
753
926
|
return new Response(readable, {
|
|
754
927
|
headers: {
|
|
755
|
-
"Content-Type": "text/event-stream",
|
|
756
928
|
"Cache-Control": "no-cache",
|
|
757
929
|
Connection: "keep-alive",
|
|
930
|
+
"Content-Type": "text/event-stream",
|
|
758
931
|
"mcp-session-id": sessionId,
|
|
759
932
|
...corsHeaders(request, corsOptions)
|
|
760
933
|
},
|
|
@@ -762,12 +935,12 @@ data: ${JSON.stringify(result.data)}
|
|
|
762
935
|
});
|
|
763
936
|
}
|
|
764
937
|
const body = JSON.stringify({
|
|
765
|
-
jsonrpc: "2.0",
|
|
766
938
|
error: {
|
|
767
939
|
code: -32e3,
|
|
768
940
|
message: "Method not allowed"
|
|
769
941
|
},
|
|
770
|
-
id: null
|
|
942
|
+
id: null,
|
|
943
|
+
jsonrpc: "2.0"
|
|
771
944
|
});
|
|
772
945
|
return new Response(body, { status: 405 });
|
|
773
946
|
}
|
|
@@ -775,6 +948,9 @@ data: ${JSON.stringify(result.data)}
|
|
|
775
948
|
}
|
|
776
949
|
};
|
|
777
950
|
export {
|
|
778
|
-
|
|
951
|
+
ElicitRequestSchema,
|
|
952
|
+
McpAgent,
|
|
953
|
+
SSEEdgeClientTransport,
|
|
954
|
+
StreamableHTTPEdgeClientTransport
|
|
779
955
|
};
|
|
780
956
|
//# sourceMappingURL=index.js.map
|