agents 0.2.16 → 0.2.18

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/mcp/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import { t as MessageType } from "../ai-types-B3aQaFv3.js";
2
2
  import "../client-BfiZ3HQd.js";
3
- import { i as SSEEdgeClientTransport, r as StreamableHTTPEdgeClientTransport } from "../client-CIvp_OWw.js";
3
+ import { a as SSEEdgeClientTransport, i as StreamableHTTPEdgeClientTransport } from "../client-9Ld2_lnt.js";
4
4
  import "../do-oauth-client-provider-CswoD5Lu.js";
5
- import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-CTtjSFyX.js";
5
+ import { c as getCurrentAgent, s as getAgentByName, t as Agent } from "../src-Dz0H9hSU.js";
6
6
  import { AsyncLocalStorage } from "node:async_hooks";
7
7
  import { ElicitRequestSchema, InitializeRequestSchema, JSONRPCMessageSchema, isInitializeRequest, isJSONRPCError, isJSONRPCNotification, isJSONRPCRequest, isJSONRPCResponse } from "@modelcontextprotocol/sdk/types.js";
8
8
 
@@ -627,6 +627,9 @@ var StreamableHTTPServerTransport = class {
627
627
 
628
628
  //#endregion
629
629
  //#region src/mcp/worker-transport.ts
630
+ const SUPPORTED_PROTOCOL_VERSIONS = ["2025-03-26", "2025-06-18"];
631
+ const DEFAULT_PROTOCOL_VERSION = "2025-03-26";
632
+ const MCP_PROTOCOL_VERSION_HEADER = "MCP-Protocol-Version";
630
633
  var WorkerTransport = class {
631
634
  constructor(options) {
632
635
  this.started = false;
@@ -639,11 +642,80 @@ var WorkerTransport = class {
639
642
  this.sessionIdGenerator = options?.sessionIdGenerator;
640
643
  this.enableJsonResponse = options?.enableJsonResponse ?? false;
641
644
  this.onsessioninitialized = options?.onsessioninitialized;
645
+ this.corsOptions = options?.corsOptions;
642
646
  }
643
647
  async start() {
644
648
  if (this.started) throw new Error("Transport already started");
645
649
  this.started = true;
646
650
  }
651
+ validateProtocolVersion(request) {
652
+ const versionHeader = request.headers.get(MCP_PROTOCOL_VERSION_HEADER);
653
+ if (!versionHeader) {
654
+ if (!this.protocolVersion || this.protocolVersion === DEFAULT_PROTOCOL_VERSION) return;
655
+ return new Response(JSON.stringify({
656
+ jsonrpc: "2.0",
657
+ error: {
658
+ code: -32e3,
659
+ message: `Bad Request: ${MCP_PROTOCOL_VERSION_HEADER} header is required`
660
+ },
661
+ id: null
662
+ }), {
663
+ status: 400,
664
+ headers: {
665
+ "Content-Type": "application/json",
666
+ ...this.getHeaders()
667
+ }
668
+ });
669
+ }
670
+ if (!SUPPORTED_PROTOCOL_VERSIONS.includes(versionHeader)) return new Response(JSON.stringify({
671
+ jsonrpc: "2.0",
672
+ error: {
673
+ code: -32e3,
674
+ message: `Bad Request: Unsupported ${MCP_PROTOCOL_VERSION_HEADER}: ${versionHeader}. Supported versions: ${SUPPORTED_PROTOCOL_VERSIONS.join(", ")}`
675
+ },
676
+ id: null
677
+ }), {
678
+ status: 400,
679
+ headers: {
680
+ "Content-Type": "application/json",
681
+ ...this.getHeaders()
682
+ }
683
+ });
684
+ if (this.protocolVersion && versionHeader !== this.protocolVersion) return new Response(JSON.stringify({
685
+ jsonrpc: "2.0",
686
+ error: {
687
+ code: -32e3,
688
+ message: `Bad Request: ${MCP_PROTOCOL_VERSION_HEADER} mismatch. Expected: ${this.protocolVersion}, Got: ${versionHeader}`
689
+ },
690
+ id: null
691
+ }), {
692
+ status: 400,
693
+ headers: {
694
+ "Content-Type": "application/json",
695
+ ...this.getHeaders()
696
+ }
697
+ });
698
+ }
699
+ getHeaders({ forPreflight } = {}) {
700
+ const options = {
701
+ origin: "*",
702
+ headers: "Content-Type, Accept, Authorization, mcp-session-id, MCP-Protocol-Version",
703
+ methods: "GET, POST, DELETE, OPTIONS",
704
+ exposeHeaders: "mcp-session-id",
705
+ maxAge: 86400,
706
+ ...this.corsOptions
707
+ };
708
+ if (forPreflight) return {
709
+ "Access-Control-Allow-Origin": options.origin,
710
+ "Access-Control-Allow-Headers": options.headers,
711
+ "Access-Control-Allow-Methods": options.methods,
712
+ "Access-Control-Max-Age": options.maxAge.toString()
713
+ };
714
+ return {
715
+ "Access-Control-Allow-Origin": options.origin,
716
+ "Access-Control-Expose-Headers": options.exposeHeaders
717
+ };
718
+ }
647
719
  async handleRequest(request, parsedBody) {
648
720
  switch (request.method) {
649
721
  case "OPTIONS": return this.handleOptionsRequest(request);
@@ -663,10 +735,15 @@ var WorkerTransport = class {
663
735
  id: null
664
736
  }), {
665
737
  status: 406,
666
- headers: { "Content-Type": "application/json" }
738
+ headers: {
739
+ "Content-Type": "application/json",
740
+ ...this.getHeaders()
741
+ }
667
742
  });
668
- const sessionValid = this.validateSession(request);
669
- if (sessionValid !== true) return sessionValid;
743
+ const sessionError = this.validateSession(request);
744
+ if (sessionError) return sessionError;
745
+ const versionError = this.validateProtocolVersion(request);
746
+ if (versionError) return versionError;
670
747
  const streamId = this.standaloneSseStreamId;
671
748
  if (this.streamMapping.get(streamId) !== void 0) return new Response(JSON.stringify({
672
749
  jsonrpc: "2.0",
@@ -677,7 +754,10 @@ var WorkerTransport = class {
677
754
  id: null
678
755
  }), {
679
756
  status: 409,
680
- headers: { "Content-Type": "application/json" }
757
+ headers: {
758
+ "Content-Type": "application/json",
759
+ ...this.getHeaders()
760
+ }
681
761
  });
682
762
  const { readable, writable } = new TransformStream();
683
763
  const writer = writable.getWriter();
@@ -686,8 +766,7 @@ var WorkerTransport = class {
686
766
  "Content-Type": "text/event-stream",
687
767
  "Cache-Control": "no-cache",
688
768
  Connection: "keep-alive",
689
- "Access-Control-Allow-Origin": "*",
690
- "Access-Control-Expose-Headers": "mcp-session-id"
769
+ ...this.getHeaders()
691
770
  });
692
771
  if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
693
772
  const keepAlive = setInterval(() => {
@@ -719,7 +798,10 @@ var WorkerTransport = class {
719
798
  id: null
720
799
  }), {
721
800
  status: 406,
722
- headers: { "Content-Type": "application/json" }
801
+ headers: {
802
+ "Content-Type": "application/json",
803
+ ...this.getHeaders()
804
+ }
723
805
  });
724
806
  if (!request.headers.get("Content-Type")?.includes("application/json")) return new Response(JSON.stringify({
725
807
  jsonrpc: "2.0",
@@ -730,7 +812,10 @@ var WorkerTransport = class {
730
812
  id: null
731
813
  }), {
732
814
  status: 415,
733
- headers: { "Content-Type": "application/json" }
815
+ headers: {
816
+ "Content-Type": "application/json",
817
+ ...this.getHeaders()
818
+ }
734
819
  });
735
820
  let rawMessage = parsedBody;
736
821
  if (rawMessage === void 0) try {
@@ -745,7 +830,10 @@ var WorkerTransport = class {
745
830
  id: null
746
831
  }), {
747
832
  status: 400,
748
- headers: { "Content-Type": "application/json" }
833
+ headers: {
834
+ "Content-Type": "application/json",
835
+ ...this.getHeaders()
836
+ }
749
837
  });
750
838
  }
751
839
  let messages;
@@ -762,7 +850,10 @@ var WorkerTransport = class {
762
850
  id: null
763
851
  }), {
764
852
  status: 400,
765
- headers: { "Content-Type": "application/json" }
853
+ headers: {
854
+ "Content-Type": "application/json",
855
+ ...this.getHeaders()
856
+ }
766
857
  });
767
858
  }
768
859
  const isInitializationRequest = messages.some(isInitializeRequest);
@@ -776,7 +867,10 @@ var WorkerTransport = class {
776
867
  id: null
777
868
  }), {
778
869
  status: 400,
779
- headers: { "Content-Type": "application/json" }
870
+ headers: {
871
+ "Content-Type": "application/json",
872
+ ...this.getHeaders()
873
+ }
780
874
  });
781
875
  if (messages.length > 1) return new Response(JSON.stringify({
782
876
  jsonrpc: "2.0",
@@ -787,21 +881,32 @@ var WorkerTransport = class {
787
881
  id: null
788
882
  }), {
789
883
  status: 400,
790
- headers: { "Content-Type": "application/json" }
884
+ headers: {
885
+ "Content-Type": "application/json",
886
+ ...this.getHeaders()
887
+ }
791
888
  });
889
+ const initRequest = messages.find(isInitializeRequest);
890
+ if (initRequest?.params) {
891
+ const version = initRequest.params.protocolVersion;
892
+ if (version && SUPPORTED_PROTOCOL_VERSIONS.includes(version)) this.protocolVersion = version;
893
+ else this.protocolVersion = DEFAULT_PROTOCOL_VERSION;
894
+ }
792
895
  this.sessionId = this.sessionIdGenerator?.();
793
896
  this.initialized = true;
794
897
  if (this.sessionId && this.onsessioninitialized) this.onsessioninitialized(this.sessionId);
795
898
  }
796
899
  if (!isInitializationRequest) {
797
- const sessionValid = this.validateSession(request);
798
- if (sessionValid !== true) return sessionValid;
900
+ const sessionError = this.validateSession(request);
901
+ if (sessionError) return sessionError;
902
+ const versionError = this.validateProtocolVersion(request);
903
+ if (versionError) return versionError;
799
904
  }
800
905
  if (!messages.some(isJSONRPCRequest)) {
801
906
  for (const message of messages) this.onmessage?.(message);
802
907
  return new Response(null, {
803
908
  status: 202,
804
- headers: { "Access-Control-Allow-Origin": "*" }
909
+ headers: { ...this.getHeaders() }
805
910
  });
806
911
  }
807
912
  const streamId = crypto.randomUUID();
@@ -822,8 +927,7 @@ var WorkerTransport = class {
822
927
  "Content-Type": "text/event-stream",
823
928
  "Cache-Control": "no-cache",
824
929
  Connection: "keep-alive",
825
- "Access-Control-Allow-Origin": "*",
826
- "Access-Control-Expose-Headers": "mcp-session-id"
930
+ ...this.getHeaders()
827
931
  });
828
932
  if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
829
933
  this.streamMapping.set(streamId, {
@@ -839,24 +943,20 @@ var WorkerTransport = class {
839
943
  return new Response(readable, { headers });
840
944
  }
841
945
  async handleDeleteRequest(request) {
842
- const sessionValid = this.validateSession(request);
843
- if (sessionValid !== true) return sessionValid;
946
+ const sessionError = this.validateSession(request);
947
+ if (sessionError) return sessionError;
948
+ const versionError = this.validateProtocolVersion(request);
949
+ if (versionError) return versionError;
844
950
  await this.close();
845
951
  return new Response(null, {
846
952
  status: 200,
847
- headers: { "Access-Control-Allow-Origin": "*" }
953
+ headers: { ...this.getHeaders() }
848
954
  });
849
955
  }
850
956
  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
957
  return new Response(null, {
858
- status: 204,
859
- headers
958
+ status: 200,
959
+ headers: { ...this.getHeaders({ forPreflight: true }) }
860
960
  });
861
961
  }
862
962
  handleUnsupportedRequest() {
@@ -876,7 +976,7 @@ var WorkerTransport = class {
876
976
  });
877
977
  }
878
978
  validateSession(request) {
879
- if (this.sessionIdGenerator === void 0) return true;
979
+ if (this.sessionIdGenerator === void 0) return;
880
980
  if (!this.initialized) return new Response(JSON.stringify({
881
981
  jsonrpc: "2.0",
882
982
  error: {
@@ -886,7 +986,10 @@ var WorkerTransport = class {
886
986
  id: null
887
987
  }), {
888
988
  status: 400,
889
- headers: { "Content-Type": "application/json" }
989
+ headers: {
990
+ "Content-Type": "application/json",
991
+ ...this.getHeaders()
992
+ }
890
993
  });
891
994
  const sessionId = request.headers.get("mcp-session-id");
892
995
  if (!sessionId) return new Response(JSON.stringify({
@@ -898,7 +1001,10 @@ var WorkerTransport = class {
898
1001
  id: null
899
1002
  }), {
900
1003
  status: 400,
901
- headers: { "Content-Type": "application/json" }
1004
+ headers: {
1005
+ "Content-Type": "application/json",
1006
+ ...this.getHeaders()
1007
+ }
902
1008
  });
903
1009
  if (sessionId !== this.sessionId) return new Response(JSON.stringify({
904
1010
  jsonrpc: "2.0",
@@ -909,9 +1015,11 @@ var WorkerTransport = class {
909
1015
  id: null
910
1016
  }), {
911
1017
  status: 404,
912
- headers: { "Content-Type": "application/json" }
1018
+ headers: {
1019
+ "Content-Type": "application/json",
1020
+ ...this.getHeaders()
1021
+ }
913
1022
  });
914
- return true;
915
1023
  }
916
1024
  async close() {
917
1025
  for (const { cleanup } of this.streamMapping.values()) cleanup();
@@ -950,8 +1058,7 @@ var WorkerTransport = class {
950
1058
  const responses = relatedIds.map((id) => this.requestResponseMap.get(id));
951
1059
  const headers = new Headers({
952
1060
  "Content-Type": "application/json",
953
- "Access-Control-Allow-Origin": "*",
954
- "Access-Control-Expose-Headers": "mcp-session-id"
1061
+ ...this.getHeaders()
955
1062
  });
956
1063
  if (this.sessionId !== void 0) headers.set("mcp-session-id", this.sessionId);
957
1064
  const body = responses.length === 1 ? responses[0] : responses;
@@ -978,7 +1085,7 @@ function runWithAuthContext(context, fn) {
978
1085
 
979
1086
  //#endregion
980
1087
  //#region src/mcp/handler.ts
981
- function experimental_createMcpHandler(server, options = {}) {
1088
+ function createMcpHandler(server, options = {}) {
982
1089
  const route = options.route ?? "/mcp";
983
1090
  return async (request, _env, ctx) => {
984
1091
  const url = new URL(request.url);
@@ -991,10 +1098,8 @@ function experimental_createMcpHandler(server, options = {}) {
991
1098
  return await transport.handleRequest(request);
992
1099
  };
993
1100
  try {
994
- let response;
995
- if (authContext) response = await runWithAuthContext(authContext, handleRequest);
996
- else response = await handleRequest();
997
- return response;
1101
+ if (authContext) return await runWithAuthContext(authContext, handleRequest);
1102
+ else return await handleRequest();
998
1103
  } catch (error) {
999
1104
  console.error("MCP handler error:", error);
1000
1105
  return new Response(JSON.stringify({
@@ -1011,6 +1116,17 @@ function experimental_createMcpHandler(server, options = {}) {
1011
1116
  }
1012
1117
  };
1013
1118
  }
1119
+ let didWarnAboutExperimentalCreateMcpHandler = false;
1120
+ /**
1121
+ * @deprecated This has been renamed to createMcpHandler, and experimental_createMcpHandler will be removed in the next major version
1122
+ */
1123
+ function experimental_createMcpHandler(server, options = {}) {
1124
+ if (!didWarnAboutExperimentalCreateMcpHandler) {
1125
+ didWarnAboutExperimentalCreateMcpHandler = true;
1126
+ console.warn("experimental_createMcpHandler is deprecated, use createMcpHandler instead. experimental_createMcpHandler will be removed in the next major version.");
1127
+ }
1128
+ return createMcpHandler(server, options);
1129
+ }
1014
1130
 
1015
1131
  //#endregion
1016
1132
  //#region src/mcp/index.ts
@@ -1231,5 +1347,5 @@ var McpAgent = class McpAgent extends Agent {
1231
1347
  };
1232
1348
 
1233
1349
  //#endregion
1234
- export { ElicitRequestSchema, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, experimental_createMcpHandler, getMcpAuthContext };
1350
+ export { ElicitRequestSchema, McpAgent, SSEEdgeClientTransport, StreamableHTTPEdgeClientTransport, WorkerTransport, createMcpHandler, experimental_createMcpHandler, getMcpAuthContext };
1235
1351
  //# sourceMappingURL=index.js.map