agents 0.2.14 → 0.2.16
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/dist/ai-chat-agent.d.ts +10 -10
- package/dist/ai-chat-agent.js +6 -6
- package/dist/{ai-chat-v5-migration-gdyLiTd8.js → ai-chat-v5-migration-DBHGW4Hv.js} +2 -2
- package/dist/{ai-chat-v5-migration-gdyLiTd8.js.map → ai-chat-v5-migration-DBHGW4Hv.js.map} +1 -1
- package/dist/ai-chat-v5-migration.js +1 -1
- package/dist/ai-react.d.ts +9 -9
- package/dist/ai-react.js +1 -1
- package/dist/{ai-types-UZlfLOYP.js → ai-types-B3aQaFv3.js} +2 -2
- package/dist/{ai-types-UZlfLOYP.js.map → ai-types-B3aQaFv3.js.map} +1 -1
- package/dist/{ai-types-BWW4umHY.d.ts → ai-types-D5YoPrBZ.d.ts} +2 -2
- package/dist/ai-types.d.ts +4 -4
- package/dist/ai-types.js +1 -1
- package/dist/{client-DjR-lC16.js → client-BfiZ3HQd.js} +3 -3
- package/dist/{client-DjR-lC16.js.map → client-BfiZ3HQd.js.map} +1 -1
- package/dist/{client-CrWcaPgn.d.ts → client-C1R7IU9g.d.ts} +20 -20
- package/dist/{client-CZBVDDoO.js → client-CIvp_OWw.js} +2 -2
- package/dist/{client-CZBVDDoO.js.map → client-CIvp_OWw.js.map} +1 -1
- package/dist/{client-CmMi85Sj.d.ts → client-CbWe9FBd.d.ts} +10 -10
- package/dist/client.d.ts +8 -8
- package/dist/client.js +2 -2
- package/dist/codemode/ai.js +5 -5
- package/dist/{do-oauth-client-provider-B2jr6UNq.js → do-oauth-client-provider-CswoD5Lu.js} +2 -2
- package/dist/{do-oauth-client-provider-B2jr6UNq.js.map → do-oauth-client-provider-CswoD5Lu.js.map} +1 -1
- package/dist/{do-oauth-client-provider-CCwGwnrA.d.ts → do-oauth-client-provider-DGc5pP0l.d.ts} +2 -2
- package/dist/{index-DpH9o0ao.d.ts → index-CaUf7Wsc.d.ts} +46 -46
- package/dist/{index-W4JUkafc.d.ts → index-DhJCaDWd.d.ts} +7 -3
- package/dist/index.d.ts +36 -36
- package/dist/index.js +5 -5
- package/dist/mcp/client.d.ts +3 -3
- package/dist/mcp/client.js +1 -1
- package/dist/mcp/do-oauth-client-provider.d.ts +1 -1
- package/dist/mcp/do-oauth-client-provider.js +1 -1
- package/dist/mcp/index.d.ts +61 -7
- package/dist/mcp/index.js +395 -7
- package/dist/mcp/index.js.map +1 -1
- package/dist/{mcp-BEwaCsxO.d.ts → mcp-Dw5vDrY8.d.ts} +2 -2
- package/dist/observability/index.d.ts +2 -2
- package/dist/observability/index.js +5 -5
- package/dist/{react-LfPKBVtU.d.ts → react-BWIluznB.d.ts} +28 -26
- package/dist/react.d.ts +9 -9
- package/dist/react.js +1 -1
- package/dist/{serializable-gtr9YMhp.d.ts → serializable-CymX8ovI.d.ts} +8 -3
- package/dist/serializable.d.ts +5 -5
- package/dist/{src-L3cHuAag.js → src-CTtjSFyX.js} +6 -6
- package/dist/{src-L3cHuAag.js.map → src-CTtjSFyX.js.map} +1 -1
- package/package.json +5 -6
- package/src/index.ts +0 -2167
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import "./ai-types-
|
|
2
|
-
import "./client-
|
|
3
|
-
import "./client-
|
|
4
|
-
import "./do-oauth-client-provider-
|
|
5
|
-
import {
|
|
1
|
+
import "./ai-types-B3aQaFv3.js";
|
|
2
|
+
import "./client-BfiZ3HQd.js";
|
|
3
|
+
import "./client-CIvp_OWw.js";
|
|
4
|
+
import "./do-oauth-client-provider-CswoD5Lu.js";
|
|
5
|
+
import { a as createCatchAllEmailResolver, c as getCurrentAgent, d as unstable_callable, i as createAddressBasedEmailResolver, l as routeAgentEmail, n as StreamingResponse, o as createHeaderBasedEmailResolver, r as callable, s as getAgentByName, t as Agent, u as routeAgentRequest } from "./src-CTtjSFyX.js";
|
|
6
6
|
|
|
7
7
|
export { Agent, StreamingResponse, callable, createAddressBasedEmailResolver, createCatchAllEmailResolver, createHeaderBasedEmailResolver, getAgentByName, getCurrentAgent, routeAgentEmail, routeAgentRequest, unstable_callable };
|
package/dist/mcp/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import "../mcp-
|
|
3
|
-
import "../do-oauth-client-provider-
|
|
1
|
+
import { i as getNamespacedData, n as MCPClientOAuthCallbackConfig, r as MCPClientOAuthResult, t as MCPClientManager } from "../client-C1R7IU9g.js";
|
|
2
|
+
import "../mcp-Dw5vDrY8.js";
|
|
3
|
+
import "../do-oauth-client-provider-DGc5pP0l.js";
|
|
4
4
|
export { MCPClientManager, MCPClientOAuthCallbackConfig, MCPClientOAuthResult, getNamespacedData };
|
package/dist/mcp/client.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as DurableObjectOAuthClientProvider, t as AgentsOAuthProvider } from "../do-oauth-client-provider-DGc5pP0l.js";
|
|
2
2
|
export { AgentsOAuthProvider, DurableObjectOAuthClientProvider };
|
package/dist/mcp/index.d.ts
CHANGED
|
@@ -1,13 +1,67 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import "../mcp-
|
|
3
|
-
import "../do-oauth-client-provider-
|
|
4
|
-
import "../index-
|
|
5
|
-
import "../ai-types-
|
|
6
|
-
import {
|
|
1
|
+
import { c as ServeOptions, d as SSEEdgeClientTransport, n as MCPClientOAuthCallbackConfig, o as BaseTransportType, r as MCPClientOAuthResult, s as MaybePromise, u as StreamableHTTPEdgeClientTransport } from "../client-C1R7IU9g.js";
|
|
2
|
+
import "../mcp-Dw5vDrY8.js";
|
|
3
|
+
import "../do-oauth-client-provider-DGc5pP0l.js";
|
|
4
|
+
import "../index-DhJCaDWd.js";
|
|
5
|
+
import "../ai-types-D5YoPrBZ.js";
|
|
6
|
+
import { c as ConnectionContext, s as Connection, t as Agent } from "../index-CaUf7Wsc.js";
|
|
7
7
|
import { ElicitRequest, ElicitRequestSchema, ElicitResult, ElicitResult as ElicitResult$1, JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js";
|
|
8
8
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
9
9
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
10
|
+
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
|
|
10
11
|
|
|
12
|
+
//#region src/mcp/worker-transport.d.ts
|
|
13
|
+
interface WorkerTransportOptions {
|
|
14
|
+
sessionIdGenerator?: () => string;
|
|
15
|
+
enableJsonResponse?: boolean;
|
|
16
|
+
onsessioninitialized?: (sessionId: string) => void;
|
|
17
|
+
}
|
|
18
|
+
declare class WorkerTransport implements Transport {
|
|
19
|
+
private started;
|
|
20
|
+
private initialized;
|
|
21
|
+
private sessionIdGenerator?;
|
|
22
|
+
private enableJsonResponse;
|
|
23
|
+
private onsessioninitialized?;
|
|
24
|
+
private standaloneSseStreamId;
|
|
25
|
+
private streamMapping;
|
|
26
|
+
private requestToStreamMapping;
|
|
27
|
+
private requestResponseMap;
|
|
28
|
+
sessionId?: string;
|
|
29
|
+
onclose?: () => void;
|
|
30
|
+
onerror?: (error: Error) => void;
|
|
31
|
+
onmessage?: (message: JSONRPCMessage) => void;
|
|
32
|
+
constructor(options?: WorkerTransportOptions);
|
|
33
|
+
start(): Promise<void>;
|
|
34
|
+
handleRequest(request: Request, parsedBody?: unknown): Promise<Response>;
|
|
35
|
+
private handleGetRequest;
|
|
36
|
+
private handlePostRequest;
|
|
37
|
+
private handleDeleteRequest;
|
|
38
|
+
private handleOptionsRequest;
|
|
39
|
+
private handleUnsupportedRequest;
|
|
40
|
+
private validateSession;
|
|
41
|
+
close(): Promise<void>;
|
|
42
|
+
send(message: JSONRPCMessage): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/mcp/handler.d.ts
|
|
46
|
+
interface CreateMcpHandlerOptions extends WorkerTransportOptions {
|
|
47
|
+
/**
|
|
48
|
+
* The route path that this MCP handler should respond to.
|
|
49
|
+
* If specified, the handler will only process requests that match this route.
|
|
50
|
+
* @default "/mcp"
|
|
51
|
+
*/
|
|
52
|
+
route?: string;
|
|
53
|
+
}
|
|
54
|
+
type OAuthExecutionContext = ExecutionContext & {
|
|
55
|
+
props?: Record<string, unknown>;
|
|
56
|
+
};
|
|
57
|
+
declare function experimental_createMcpHandler(server: McpServer | Server, options?: CreateMcpHandlerOptions): (request: Request, env: unknown, ctx: ExecutionContext) => Promise<Response>;
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/mcp/auth-context.d.ts
|
|
60
|
+
interface McpAuthContext {
|
|
61
|
+
props: Record<string, unknown>;
|
|
62
|
+
}
|
|
63
|
+
declare function getMcpAuthContext(): McpAuthContext | undefined;
|
|
64
|
+
//#endregion
|
|
11
65
|
//#region src/mcp/index.d.ts
|
|
12
66
|
declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Record<string, unknown> = Record<string, unknown>> extends Agent<Env, State, Props> {
|
|
13
67
|
private _transport?;
|
|
@@ -71,5 +125,5 @@ declare abstract class McpAgent<Env = unknown, State = unknown, Props extends Re
|
|
|
71
125
|
};
|
|
72
126
|
}
|
|
73
127
|
//#endregion
|
|
74
|
-
export { type ElicitRequest, ElicitRequestSchema, type ElicitResult, type MCPClientOAuthCallbackConfig, type MCPClientOAuthResult, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport };
|
|
128
|
+
export { type CreateMcpHandlerOptions, type ElicitRequest, ElicitRequestSchema, type ElicitResult, type MCPClientOAuthCallbackConfig, type MCPClientOAuthResult, McpAgent, type McpAuthContext, type OAuthExecutionContext, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, type WorkerTransportOptions, experimental_createMcpHandler, getMcpAuthContext };
|
|
75
129
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { MessageType } from "../ai-types-
|
|
2
|
-
import "../client-
|
|
3
|
-
import { SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport } from "../client-
|
|
4
|
-
import "../do-oauth-client-provider-
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { t as MessageType } from "../ai-types-B3aQaFv3.js";
|
|
2
|
+
import "../client-BfiZ3HQd.js";
|
|
3
|
+
import { i as SSEEdgeClientTransport, r as StreamableHTTPEdgeClientTransport } from "../client-CIvp_OWw.js";
|
|
4
|
+
import "../do-oauth-client-provider-CswoD5Lu.js";
|
|
5
|
+
import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-CTtjSFyX.js";
|
|
6
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
7
|
+
import { ElicitRequestSchema, InitializeRequestSchema, JSONRPCMessageSchema, isInitializeRequest, isJSONRPCError, isJSONRPCNotification, isJSONRPCRequest, isJSONRPCResponse } from "@modelcontextprotocol/sdk/types.js";
|
|
7
8
|
|
|
8
9
|
//#region src/mcp/utils.ts
|
|
9
10
|
/**
|
|
@@ -624,6 +625,393 @@ var StreamableHTTPServerTransport = class {
|
|
|
624
625
|
}
|
|
625
626
|
};
|
|
626
627
|
|
|
628
|
+
//#endregion
|
|
629
|
+
//#region src/mcp/worker-transport.ts
|
|
630
|
+
var WorkerTransport = class {
|
|
631
|
+
constructor(options) {
|
|
632
|
+
this.started = false;
|
|
633
|
+
this.initialized = false;
|
|
634
|
+
this.enableJsonResponse = false;
|
|
635
|
+
this.standaloneSseStreamId = "_GET_stream";
|
|
636
|
+
this.streamMapping = /* @__PURE__ */ new Map();
|
|
637
|
+
this.requestToStreamMapping = /* @__PURE__ */ new Map();
|
|
638
|
+
this.requestResponseMap = /* @__PURE__ */ new Map();
|
|
639
|
+
this.sessionIdGenerator = options?.sessionIdGenerator;
|
|
640
|
+
this.enableJsonResponse = options?.enableJsonResponse ?? false;
|
|
641
|
+
this.onsessioninitialized = options?.onsessioninitialized;
|
|
642
|
+
}
|
|
643
|
+
async start() {
|
|
644
|
+
if (this.started) throw new Error("Transport already started");
|
|
645
|
+
this.started = true;
|
|
646
|
+
}
|
|
647
|
+
async handleRequest(request, parsedBody) {
|
|
648
|
+
switch (request.method) {
|
|
649
|
+
case "OPTIONS": return this.handleOptionsRequest(request);
|
|
650
|
+
case "GET": return this.handleGetRequest(request);
|
|
651
|
+
case "POST": return this.handlePostRequest(request, parsedBody);
|
|
652
|
+
case "DELETE": return this.handleDeleteRequest(request);
|
|
653
|
+
default: return this.handleUnsupportedRequest();
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
async handleGetRequest(request) {
|
|
657
|
+
if (!request.headers.get("Accept")?.includes("text/event-stream")) return new Response(JSON.stringify({
|
|
658
|
+
jsonrpc: "2.0",
|
|
659
|
+
error: {
|
|
660
|
+
code: -32e3,
|
|
661
|
+
message: "Not Acceptable: Client must accept text/event-stream"
|
|
662
|
+
},
|
|
663
|
+
id: null
|
|
664
|
+
}), {
|
|
665
|
+
status: 406,
|
|
666
|
+
headers: { "Content-Type": "application/json" }
|
|
667
|
+
});
|
|
668
|
+
const sessionValid = this.validateSession(request);
|
|
669
|
+
if (sessionValid !== true) return sessionValid;
|
|
670
|
+
const streamId = this.standaloneSseStreamId;
|
|
671
|
+
if (this.streamMapping.get(streamId) !== void 0) return new Response(JSON.stringify({
|
|
672
|
+
jsonrpc: "2.0",
|
|
673
|
+
error: {
|
|
674
|
+
code: -32e3,
|
|
675
|
+
message: "Conflict: Only one SSE stream is allowed per session"
|
|
676
|
+
},
|
|
677
|
+
id: null
|
|
678
|
+
}), {
|
|
679
|
+
status: 409,
|
|
680
|
+
headers: { "Content-Type": "application/json" }
|
|
681
|
+
});
|
|
682
|
+
const { readable, writable } = new TransformStream();
|
|
683
|
+
const writer = writable.getWriter();
|
|
684
|
+
const encoder = new TextEncoder();
|
|
685
|
+
const headers = new Headers({
|
|
686
|
+
"Content-Type": "text/event-stream",
|
|
687
|
+
"Cache-Control": "no-cache",
|
|
688
|
+
Connection: "keep-alive",
|
|
689
|
+
"Access-Control-Allow-Origin": "*",
|
|
690
|
+
"Access-Control-Expose-Headers": "mcp-session-id"
|
|
691
|
+
});
|
|
692
|
+
if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
|
|
693
|
+
const keepAlive = setInterval(() => {
|
|
694
|
+
try {
|
|
695
|
+
writer.write(encoder.encode("event: ping\ndata: \n\n"));
|
|
696
|
+
} catch {
|
|
697
|
+
clearInterval(keepAlive);
|
|
698
|
+
}
|
|
699
|
+
}, 3e4);
|
|
700
|
+
this.streamMapping.set(streamId, {
|
|
701
|
+
writer,
|
|
702
|
+
encoder,
|
|
703
|
+
cleanup: () => {
|
|
704
|
+
clearInterval(keepAlive);
|
|
705
|
+
this.streamMapping.delete(streamId);
|
|
706
|
+
writer.close().catch(() => {});
|
|
707
|
+
}
|
|
708
|
+
});
|
|
709
|
+
return new Response(readable, { headers });
|
|
710
|
+
}
|
|
711
|
+
async handlePostRequest(request, parsedBody) {
|
|
712
|
+
const acceptHeader = request.headers.get("Accept");
|
|
713
|
+
if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) return new Response(JSON.stringify({
|
|
714
|
+
jsonrpc: "2.0",
|
|
715
|
+
error: {
|
|
716
|
+
code: -32e3,
|
|
717
|
+
message: "Not Acceptable: Client must accept both application/json and text/event-stream"
|
|
718
|
+
},
|
|
719
|
+
id: null
|
|
720
|
+
}), {
|
|
721
|
+
status: 406,
|
|
722
|
+
headers: { "Content-Type": "application/json" }
|
|
723
|
+
});
|
|
724
|
+
if (!request.headers.get("Content-Type")?.includes("application/json")) return new Response(JSON.stringify({
|
|
725
|
+
jsonrpc: "2.0",
|
|
726
|
+
error: {
|
|
727
|
+
code: -32e3,
|
|
728
|
+
message: "Unsupported Media Type: Content-Type must be application/json"
|
|
729
|
+
},
|
|
730
|
+
id: null
|
|
731
|
+
}), {
|
|
732
|
+
status: 415,
|
|
733
|
+
headers: { "Content-Type": "application/json" }
|
|
734
|
+
});
|
|
735
|
+
let rawMessage = parsedBody;
|
|
736
|
+
if (rawMessage === void 0) try {
|
|
737
|
+
rawMessage = await request.json();
|
|
738
|
+
} catch {
|
|
739
|
+
return new Response(JSON.stringify({
|
|
740
|
+
jsonrpc: "2.0",
|
|
741
|
+
error: {
|
|
742
|
+
code: -32700,
|
|
743
|
+
message: "Parse error: Invalid JSON"
|
|
744
|
+
},
|
|
745
|
+
id: null
|
|
746
|
+
}), {
|
|
747
|
+
status: 400,
|
|
748
|
+
headers: { "Content-Type": "application/json" }
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
let messages;
|
|
752
|
+
try {
|
|
753
|
+
if (Array.isArray(rawMessage)) messages = rawMessage.map((msg) => JSONRPCMessageSchema.parse(msg));
|
|
754
|
+
else messages = [JSONRPCMessageSchema.parse(rawMessage)];
|
|
755
|
+
} catch {
|
|
756
|
+
return new Response(JSON.stringify({
|
|
757
|
+
jsonrpc: "2.0",
|
|
758
|
+
error: {
|
|
759
|
+
code: -32700,
|
|
760
|
+
message: "Parse error: Invalid JSON-RPC message"
|
|
761
|
+
},
|
|
762
|
+
id: null
|
|
763
|
+
}), {
|
|
764
|
+
status: 400,
|
|
765
|
+
headers: { "Content-Type": "application/json" }
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
const isInitializationRequest = messages.some(isInitializeRequest);
|
|
769
|
+
if (isInitializationRequest) {
|
|
770
|
+
if (this.initialized && this.sessionId !== void 0) return new Response(JSON.stringify({
|
|
771
|
+
jsonrpc: "2.0",
|
|
772
|
+
error: {
|
|
773
|
+
code: -32600,
|
|
774
|
+
message: "Invalid Request: Server already initialized"
|
|
775
|
+
},
|
|
776
|
+
id: null
|
|
777
|
+
}), {
|
|
778
|
+
status: 400,
|
|
779
|
+
headers: { "Content-Type": "application/json" }
|
|
780
|
+
});
|
|
781
|
+
if (messages.length > 1) return new Response(JSON.stringify({
|
|
782
|
+
jsonrpc: "2.0",
|
|
783
|
+
error: {
|
|
784
|
+
code: -32600,
|
|
785
|
+
message: "Invalid Request: Only one initialization request is allowed"
|
|
786
|
+
},
|
|
787
|
+
id: null
|
|
788
|
+
}), {
|
|
789
|
+
status: 400,
|
|
790
|
+
headers: { "Content-Type": "application/json" }
|
|
791
|
+
});
|
|
792
|
+
this.sessionId = this.sessionIdGenerator?.();
|
|
793
|
+
this.initialized = true;
|
|
794
|
+
if (this.sessionId && this.onsessioninitialized) this.onsessioninitialized(this.sessionId);
|
|
795
|
+
}
|
|
796
|
+
if (!isInitializationRequest) {
|
|
797
|
+
const sessionValid = this.validateSession(request);
|
|
798
|
+
if (sessionValid !== true) return sessionValid;
|
|
799
|
+
}
|
|
800
|
+
if (!messages.some(isJSONRPCRequest)) {
|
|
801
|
+
for (const message of messages) this.onmessage?.(message);
|
|
802
|
+
return new Response(null, {
|
|
803
|
+
status: 202,
|
|
804
|
+
headers: { "Access-Control-Allow-Origin": "*" }
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
const streamId = crypto.randomUUID();
|
|
808
|
+
if (this.enableJsonResponse) return new Promise((resolve) => {
|
|
809
|
+
this.streamMapping.set(streamId, {
|
|
810
|
+
resolveJson: resolve,
|
|
811
|
+
cleanup: () => {
|
|
812
|
+
this.streamMapping.delete(streamId);
|
|
813
|
+
}
|
|
814
|
+
});
|
|
815
|
+
for (const message of messages) if (isJSONRPCRequest(message)) this.requestToStreamMapping.set(message.id, streamId);
|
|
816
|
+
for (const message of messages) this.onmessage?.(message);
|
|
817
|
+
});
|
|
818
|
+
const { readable, writable } = new TransformStream();
|
|
819
|
+
const writer = writable.getWriter();
|
|
820
|
+
const encoder = new TextEncoder();
|
|
821
|
+
const headers = new Headers({
|
|
822
|
+
"Content-Type": "text/event-stream",
|
|
823
|
+
"Cache-Control": "no-cache",
|
|
824
|
+
Connection: "keep-alive",
|
|
825
|
+
"Access-Control-Allow-Origin": "*",
|
|
826
|
+
"Access-Control-Expose-Headers": "mcp-session-id"
|
|
827
|
+
});
|
|
828
|
+
if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
|
|
829
|
+
this.streamMapping.set(streamId, {
|
|
830
|
+
writer,
|
|
831
|
+
encoder,
|
|
832
|
+
cleanup: () => {
|
|
833
|
+
this.streamMapping.delete(streamId);
|
|
834
|
+
writer.close().catch(() => {});
|
|
835
|
+
}
|
|
836
|
+
});
|
|
837
|
+
for (const message of messages) if (isJSONRPCRequest(message)) this.requestToStreamMapping.set(message.id, streamId);
|
|
838
|
+
for (const message of messages) this.onmessage?.(message);
|
|
839
|
+
return new Response(readable, { headers });
|
|
840
|
+
}
|
|
841
|
+
async handleDeleteRequest(request) {
|
|
842
|
+
const sessionValid = this.validateSession(request);
|
|
843
|
+
if (sessionValid !== true) return sessionValid;
|
|
844
|
+
await this.close();
|
|
845
|
+
return new Response(null, {
|
|
846
|
+
status: 200,
|
|
847
|
+
headers: { "Access-Control-Allow-Origin": "*" }
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
handleOptionsRequest(_request) {
|
|
851
|
+
const headers = new Headers({
|
|
852
|
+
"Access-Control-Allow-Origin": "*",
|
|
853
|
+
"Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
|
|
854
|
+
"Access-Control-Allow-Headers": "Content-Type, Accept, Authorization, mcp-session-id",
|
|
855
|
+
"Access-Control-Max-Age": "86400"
|
|
856
|
+
});
|
|
857
|
+
return new Response(null, {
|
|
858
|
+
status: 204,
|
|
859
|
+
headers
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
handleUnsupportedRequest() {
|
|
863
|
+
return new Response(JSON.stringify({
|
|
864
|
+
jsonrpc: "2.0",
|
|
865
|
+
error: {
|
|
866
|
+
code: -32e3,
|
|
867
|
+
message: "Method not allowed."
|
|
868
|
+
},
|
|
869
|
+
id: null
|
|
870
|
+
}), {
|
|
871
|
+
status: 405,
|
|
872
|
+
headers: {
|
|
873
|
+
Allow: "GET, POST, DELETE, OPTIONS",
|
|
874
|
+
"Content-Type": "application/json"
|
|
875
|
+
}
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
validateSession(request) {
|
|
879
|
+
if (this.sessionIdGenerator === void 0) return true;
|
|
880
|
+
if (!this.initialized) return new Response(JSON.stringify({
|
|
881
|
+
jsonrpc: "2.0",
|
|
882
|
+
error: {
|
|
883
|
+
code: -32e3,
|
|
884
|
+
message: "Bad Request: Server not initialized"
|
|
885
|
+
},
|
|
886
|
+
id: null
|
|
887
|
+
}), {
|
|
888
|
+
status: 400,
|
|
889
|
+
headers: { "Content-Type": "application/json" }
|
|
890
|
+
});
|
|
891
|
+
const sessionId = request.headers.get("mcp-session-id");
|
|
892
|
+
if (!sessionId) return new Response(JSON.stringify({
|
|
893
|
+
jsonrpc: "2.0",
|
|
894
|
+
error: {
|
|
895
|
+
code: -32e3,
|
|
896
|
+
message: "Bad Request: Mcp-Session-Id header is required"
|
|
897
|
+
},
|
|
898
|
+
id: null
|
|
899
|
+
}), {
|
|
900
|
+
status: 400,
|
|
901
|
+
headers: { "Content-Type": "application/json" }
|
|
902
|
+
});
|
|
903
|
+
if (sessionId !== this.sessionId) return new Response(JSON.stringify({
|
|
904
|
+
jsonrpc: "2.0",
|
|
905
|
+
error: {
|
|
906
|
+
code: -32001,
|
|
907
|
+
message: "Session not found"
|
|
908
|
+
},
|
|
909
|
+
id: null
|
|
910
|
+
}), {
|
|
911
|
+
status: 404,
|
|
912
|
+
headers: { "Content-Type": "application/json" }
|
|
913
|
+
});
|
|
914
|
+
return true;
|
|
915
|
+
}
|
|
916
|
+
async close() {
|
|
917
|
+
for (const { cleanup } of this.streamMapping.values()) cleanup();
|
|
918
|
+
this.streamMapping.clear();
|
|
919
|
+
this.requestResponseMap.clear();
|
|
920
|
+
this.onclose?.();
|
|
921
|
+
}
|
|
922
|
+
async send(message) {
|
|
923
|
+
let requestId;
|
|
924
|
+
if (isJSONRPCResponse(message) || isJSONRPCError(message)) requestId = message.id;
|
|
925
|
+
if (requestId === void 0) {
|
|
926
|
+
if (isJSONRPCResponse(message) || isJSONRPCError(message)) throw new Error("Cannot send a response on a standalone SSE stream unless resuming a previous client request");
|
|
927
|
+
const standaloneSse = this.streamMapping.get(this.standaloneSseStreamId);
|
|
928
|
+
if (standaloneSse === void 0) return;
|
|
929
|
+
if (standaloneSse.writer && standaloneSse.encoder) {
|
|
930
|
+
const data = `event: message\ndata: ${JSON.stringify(message)}\n\n`;
|
|
931
|
+
await standaloneSse.writer.write(standaloneSse.encoder.encode(data));
|
|
932
|
+
}
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
const streamId = this.requestToStreamMapping.get(requestId);
|
|
936
|
+
if (!streamId) throw new Error(`No connection established for request ID: ${String(requestId)}`);
|
|
937
|
+
const response = this.streamMapping.get(streamId);
|
|
938
|
+
if (!response) throw new Error(`No connection established for request ID: ${String(requestId)}`);
|
|
939
|
+
if (!this.enableJsonResponse) {
|
|
940
|
+
if (response.writer && response.encoder) {
|
|
941
|
+
const data = `event: message\ndata: ${JSON.stringify(message)}\n\n`;
|
|
942
|
+
await response.writer.write(response.encoder.encode(data));
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
|
|
946
|
+
this.requestResponseMap.set(requestId, message);
|
|
947
|
+
const relatedIds = Array.from(this.requestToStreamMapping.entries()).filter(([, sid]) => sid === streamId).map(([id]) => id);
|
|
948
|
+
if (relatedIds.every((id) => this.requestResponseMap.has(id))) {
|
|
949
|
+
if (this.enableJsonResponse && response.resolveJson) {
|
|
950
|
+
const responses = relatedIds.map((id) => this.requestResponseMap.get(id));
|
|
951
|
+
const headers = new Headers({
|
|
952
|
+
"Content-Type": "application/json",
|
|
953
|
+
"Access-Control-Allow-Origin": "*",
|
|
954
|
+
"Access-Control-Expose-Headers": "mcp-session-id"
|
|
955
|
+
});
|
|
956
|
+
if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
|
|
957
|
+
const body = responses.length === 1 ? responses[0] : responses;
|
|
958
|
+
response.resolveJson(new Response(JSON.stringify(body), { headers }));
|
|
959
|
+
} else response.cleanup();
|
|
960
|
+
for (const id of relatedIds) {
|
|
961
|
+
this.requestResponseMap.delete(id);
|
|
962
|
+
this.requestToStreamMapping.delete(id);
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
|
|
969
|
+
//#endregion
|
|
970
|
+
//#region src/mcp/auth-context.ts
|
|
971
|
+
const authContextStorage = new AsyncLocalStorage();
|
|
972
|
+
function getMcpAuthContext() {
|
|
973
|
+
return authContextStorage.getStore();
|
|
974
|
+
}
|
|
975
|
+
function runWithAuthContext(context, fn) {
|
|
976
|
+
return authContextStorage.run(context, fn);
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
//#endregion
|
|
980
|
+
//#region src/mcp/handler.ts
|
|
981
|
+
function experimental_createMcpHandler(server, options = {}) {
|
|
982
|
+
const route = options.route ?? "/mcp";
|
|
983
|
+
return async (request, _env, ctx) => {
|
|
984
|
+
const url = new URL(request.url);
|
|
985
|
+
if (route && url.pathname !== route) return new Response("Not Found", { status: 404 });
|
|
986
|
+
const oauthCtx = ctx;
|
|
987
|
+
const authContext = oauthCtx.props ? { props: oauthCtx.props } : void 0;
|
|
988
|
+
const transport = new WorkerTransport(options);
|
|
989
|
+
await server.connect(transport);
|
|
990
|
+
const handleRequest = async () => {
|
|
991
|
+
return await transport.handleRequest(request);
|
|
992
|
+
};
|
|
993
|
+
try {
|
|
994
|
+
let response;
|
|
995
|
+
if (authContext) response = await runWithAuthContext(authContext, handleRequest);
|
|
996
|
+
else response = await handleRequest();
|
|
997
|
+
return response;
|
|
998
|
+
} catch (error) {
|
|
999
|
+
console.error("MCP handler error:", error);
|
|
1000
|
+
return new Response(JSON.stringify({
|
|
1001
|
+
jsonrpc: "2.0",
|
|
1002
|
+
error: {
|
|
1003
|
+
code: -32603,
|
|
1004
|
+
message: error instanceof Error ? error.message : "Internal server error"
|
|
1005
|
+
},
|
|
1006
|
+
id: null
|
|
1007
|
+
}), {
|
|
1008
|
+
status: 500,
|
|
1009
|
+
headers: { "Content-Type": "application/json" }
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
}
|
|
1014
|
+
|
|
627
1015
|
//#endregion
|
|
628
1016
|
//#region src/mcp/index.ts
|
|
629
1017
|
var McpAgent = class McpAgent extends Agent {
|
|
@@ -843,5 +1231,5 @@ var McpAgent = class McpAgent extends Agent {
|
|
|
843
1231
|
};
|
|
844
1232
|
|
|
845
1233
|
//#endregion
|
|
846
|
-
export { ElicitRequestSchema, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport };
|
|
1234
|
+
export { ElicitRequestSchema, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, experimental_createMcpHandler, getMcpAuthContext };
|
|
847
1235
|
//# sourceMappingURL=index.js.map
|