@agentwares/agentguard 0.1.9 → 0.1.10

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/dist/audit-5LUDXZLG.js +18 -0
  3. package/dist/chunk-2QDPPTPY.js +179 -0
  4. package/dist/chunk-2QDPPTPY.js.map +1 -0
  5. package/dist/{chunk-SUXQTT3X.js → chunk-4WTFMFRY.js} +18 -16
  6. package/dist/chunk-4WTFMFRY.js.map +1 -0
  7. package/dist/{chunk-VQWQ5TOM.js → chunk-DEVXG3IB.js} +4 -4
  8. package/dist/chunk-DEVXG3IB.js.map +1 -0
  9. package/dist/chunk-F73VDY3W.js +202 -0
  10. package/dist/chunk-F73VDY3W.js.map +1 -0
  11. package/dist/{chunk-X4M7YWQA.js → chunk-IEJK7N3B.js} +32 -27
  12. package/dist/chunk-IEJK7N3B.js.map +1 -0
  13. package/dist/{chunk-ZSLWPKXI.js → chunk-IS3DAHZI.js} +562 -8
  14. package/dist/chunk-IS3DAHZI.js.map +1 -0
  15. package/dist/{chunk-KRKZGAJ3.js → chunk-JSELR4SG.js} +66 -55
  16. package/dist/chunk-JSELR4SG.js.map +1 -0
  17. package/dist/{chunk-TW5KJREH.js → chunk-OM5L7A32.js} +20 -11
  18. package/dist/chunk-OM5L7A32.js.map +1 -0
  19. package/dist/{chunk-HPYZ3JDX.js → chunk-SWZQMPDG.js} +3 -3
  20. package/dist/{chunk-HPYZ3JDX.js.map → chunk-SWZQMPDG.js.map} +1 -1
  21. package/dist/{chunk-D66U3PVB.js → chunk-TJXZBOHL.js} +6 -6
  22. package/dist/chunk-TJXZBOHL.js.map +1 -0
  23. package/dist/{chunk-BKXXTWIS.js → chunk-XOBFANL4.js} +5 -5
  24. package/dist/chunk-XOBFANL4.js.map +1 -0
  25. package/dist/chunk-XPNBYSIN.js +23 -0
  26. package/dist/chunk-XPNBYSIN.js.map +1 -0
  27. package/dist/{chunk-6S7LYVOC.js → chunk-YBVD6HTT.js} +27 -24
  28. package/dist/chunk-YBVD6HTT.js.map +1 -0
  29. package/dist/{chunk-6TUQD666.js → chunk-ZG4LGUJE.js} +100 -81
  30. package/dist/chunk-ZG4LGUJE.js.map +1 -0
  31. package/dist/cli.js +6 -4
  32. package/dist/fixtures/crm-server.js +2 -2
  33. package/dist/fixtures/demo-agent.js +1 -1
  34. package/dist/index.d.ts +5 -5
  35. package/dist/index.js +23 -21
  36. package/dist/index.js.map +1 -1
  37. package/dist/init-MKPQU2JG.js +17 -0
  38. package/dist/{proxy-3Z5YEZ4R.js → proxy-6GP63TLD.js} +21 -29
  39. package/dist/proxy-6GP63TLD.js.map +1 -0
  40. package/dist/tools-XS7PMTLT.js +15 -0
  41. package/llms.txt +8 -6
  42. package/package.json +10 -4
  43. package/server.json +9 -9
  44. package/dist/audit-2YCMHMCP.js +0 -17
  45. package/dist/chunk-3O6KWT3E.js +0 -345
  46. package/dist/chunk-3O6KWT3E.js.map +0 -1
  47. package/dist/chunk-6S7LYVOC.js.map +0 -1
  48. package/dist/chunk-6TUQD666.js.map +0 -1
  49. package/dist/chunk-BKXXTWIS.js.map +0 -1
  50. package/dist/chunk-D66U3PVB.js.map +0 -1
  51. package/dist/chunk-KRKZGAJ3.js.map +0 -1
  52. package/dist/chunk-SUXQTT3X.js.map +0 -1
  53. package/dist/chunk-TW5KJREH.js.map +0 -1
  54. package/dist/chunk-VQWQ5TOM.js.map +0 -1
  55. package/dist/chunk-X4M7YWQA.js.map +0 -1
  56. package/dist/chunk-ZSLWPKXI.js.map +0 -1
  57. package/dist/init-CWROGV5I.js +0 -15
  58. package/dist/proxy-3Z5YEZ4R.js.map +0 -1
  59. package/dist/tools-3LLDG3XQ.js +0 -15
  60. /package/dist/{audit-2YCMHMCP.js.map → audit-5LUDXZLG.js.map} +0 -0
  61. /package/dist/{init-CWROGV5I.js.map → init-MKPQU2JG.js.map} +0 -0
  62. /package/dist/{tools-3LLDG3XQ.js.map → tools-XS7PMTLT.js.map} +0 -0
@@ -1,4 +1,4 @@
1
- // ../../packages/mcp-kit/dist/chunk-7LRFDQNC.js
1
+ // ../../packages/mcp-kit/dist/chunk-VX7P4M7P.js
2
2
  import { completable } from "@modelcontextprotocol/sdk/server/completable.js";
3
3
  import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
4
4
  import {
@@ -112,9 +112,12 @@ var StubVerifier = class {
112
112
  }
113
113
  };
114
114
 
115
- // ../../packages/mcp-kit/dist/chunk-7LRFDQNC.js
115
+ // ../../packages/mcp-kit/dist/chunk-VX7P4M7P.js
116
116
  import { z as z2 } from "zod";
117
117
  import { z as z3 } from "zod";
118
+ import {
119
+ SUPPORTED_PROTOCOL_VERSIONS
120
+ } from "@modelcontextprotocol/sdk/types.js";
118
121
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
119
122
  import {
120
123
  CallToolRequestSchema,
@@ -669,6 +672,379 @@ var examplePaidLookup = withPayment(
669
672
  }),
670
673
  { priceUsd: 0.05, description: "One record lookup" }
671
674
  );
675
+ var MODERN_PROTOCOL_VERSION = "2026-07-28";
676
+ var MODERN_PROTOCOL_VERSIONS = [MODERN_PROTOCOL_VERSION];
677
+ var LEGACY_PROTOCOL_VERSIONS = [...SUPPORTED_PROTOCOL_VERSIONS];
678
+ var SERVED_PROTOCOL_VERSIONS = [
679
+ ...MODERN_PROTOCOL_VERSIONS,
680
+ ...LEGACY_PROTOCOL_VERSIONS
681
+ ];
682
+ var MODERN_META = {
683
+ protocolVersion: "io.modelcontextprotocol/protocolVersion",
684
+ clientInfo: "io.modelcontextprotocol/clientInfo",
685
+ clientCapabilities: "io.modelcontextprotocol/clientCapabilities",
686
+ logLevel: "io.modelcontextprotocol/logLevel",
687
+ serverInfo: "io.modelcontextprotocol/serverInfo"
688
+ };
689
+ var MODERN_ERROR_CODES = {
690
+ HeaderMismatch: -32020,
691
+ MissingRequiredClientCapability: -32021,
692
+ UnsupportedProtocolVersion: -32022
693
+ };
694
+ var INVALID_REQUEST = -32600;
695
+ var METHOD_NOT_FOUND = -32601;
696
+ var INVALID_PARAMS = -32602;
697
+ var INTERNAL_ERROR = -32603;
698
+ var NOT_MODERN = /* @__PURE__ */ new Set([
699
+ "initialize",
700
+ "ping",
701
+ "logging/setLevel",
702
+ "resources/subscribe",
703
+ "resources/unsubscribe",
704
+ "subscriptions/listen"
705
+ ]);
706
+ var CACHEABLE = /* @__PURE__ */ new Set([
707
+ "server/discover",
708
+ "tools/list",
709
+ "prompts/list",
710
+ "resources/list",
711
+ "resources/read",
712
+ "resources/templates/list"
713
+ ]);
714
+ var LOG_LEVELS = [
715
+ "debug",
716
+ "info",
717
+ "notice",
718
+ "warning",
719
+ "error",
720
+ "critical",
721
+ "alert",
722
+ "emergency"
723
+ ];
724
+ var STATIC_TTL_MS = 3e5;
725
+ var STATIC_LISTING = /* @__PURE__ */ Symbol.for("@agentwares/mcp-kit/static-listing");
726
+ function markStaticListing(server) {
727
+ server[STATIC_LISTING] = true;
728
+ }
729
+ function hasStaticListing(server) {
730
+ return server[STATIC_LISTING] === true;
731
+ }
732
+ function isObject(value) {
733
+ return typeof value === "object" && value !== null && !Array.isArray(value);
734
+ }
735
+ function requestId(value) {
736
+ return typeof value === "string" || typeof value === "number" ? value : null;
737
+ }
738
+ function modernClaim(message) {
739
+ if (!isObject(message) || !isObject(message["params"])) return void 0;
740
+ const meta = message["params"]["_meta"];
741
+ return isObject(meta) ? meta[MODERN_META.protocolVersion] : void 0;
742
+ }
743
+ function unsupportedVersionError(requested) {
744
+ return {
745
+ code: MODERN_ERROR_CODES.UnsupportedProtocolVersion,
746
+ message: `Unsupported protocol version: ${requested}. Supported: ${SERVED_PROTOCOL_VERSIONS.join(", ")}`,
747
+ data: { supported: [...SERVED_PROTOCOL_VERSIONS], requested }
748
+ };
749
+ }
750
+ function routeModern(message) {
751
+ if (!isObject(message) || typeof message["method"] !== "string") return { kind: "legacy" };
752
+ const claim = modernClaim(message);
753
+ if (claim === void 0) return { kind: "legacy" };
754
+ const method = message["method"];
755
+ const hasId = "id" in message;
756
+ const id = requestId(message["id"]);
757
+ if (typeof claim !== "string") {
758
+ return {
759
+ kind: "reject",
760
+ id,
761
+ status: 400,
762
+ error: { code: INVALID_PARAMS, message: `${MODERN_META.protocolVersion} must be a string` }
763
+ };
764
+ }
765
+ if (LEGACY_PROTOCOL_VERSIONS.includes(claim)) return { kind: "legacy" };
766
+ if (!MODERN_PROTOCOL_VERSIONS.includes(claim)) {
767
+ return { kind: "reject", id, status: 400, error: unsupportedVersionError(claim) };
768
+ }
769
+ if (!hasId) return { kind: "notification" };
770
+ if (id === null) {
771
+ return {
772
+ kind: "reject",
773
+ id: null,
774
+ status: 400,
775
+ error: { code: INVALID_REQUEST, message: "a request id must be a string or a number" }
776
+ };
777
+ }
778
+ const meta = message["params"]["_meta"];
779
+ if (!isObject(meta[MODERN_META.clientCapabilities])) {
780
+ return {
781
+ kind: "reject",
782
+ id,
783
+ status: 400,
784
+ error: {
785
+ code: INVALID_PARAMS,
786
+ message: `Missing required _meta field ${MODERN_META.clientCapabilities} (an object; {} when the client declares none)`
787
+ }
788
+ };
789
+ }
790
+ if (method === "server/discover") return { kind: "discover", id, method, version: claim };
791
+ if (NOT_MODERN.has(method)) {
792
+ return {
793
+ kind: "reject",
794
+ id,
795
+ status: 404,
796
+ error: {
797
+ code: METHOD_NOT_FOUND,
798
+ message: method === "initialize" ? `Method not found: initialize is not part of ${claim}; call server/discover, or send initialize without the ${MODERN_META.protocolVersion} _meta field to open a 2025-era session` : `Method not found: ${method} is not served at ${claim}`
799
+ }
800
+ };
801
+ }
802
+ const level = meta[MODERN_META.logLevel];
803
+ return {
804
+ kind: "dispatch",
805
+ id,
806
+ method,
807
+ version: claim,
808
+ ...typeof level === "string" ? { logLevel: level } : {}
809
+ };
810
+ }
811
+ function nameSource(method, params) {
812
+ if (!isObject(params)) return void 0;
813
+ const field = method === "tools/call" || method === "prompts/get" ? "name" : method === "resources/read" ? "uri" : void 0;
814
+ const value = field ? params[field] : void 0;
815
+ return typeof value === "string" ? value : void 0;
816
+ }
817
+ function decodeHeaderValue(value) {
818
+ const match = /^=\?base64\?(.*)\?=$/.exec(value);
819
+ if (!match) return value;
820
+ try {
821
+ const bytes = Uint8Array.from(atob(match[1] ?? ""), (c) => c.charCodeAt(0));
822
+ return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
823
+ } catch {
824
+ return void 0;
825
+ }
826
+ }
827
+ function headerMismatch(id, message) {
828
+ return {
829
+ kind: "reject",
830
+ id,
831
+ status: 400,
832
+ error: { code: MODERN_ERROR_CODES.HeaderMismatch, message: `Header mismatch: ${message}` }
833
+ };
834
+ }
835
+ function checkModernHeaders(headers, message, version) {
836
+ const { id, method } = message;
837
+ const protocolHeader = headers.get("mcp-protocol-version")?.trim();
838
+ if (protocolHeader === void 0) {
839
+ return headerMismatch(id, "the required MCP-Protocol-Version header is missing");
840
+ }
841
+ if (protocolHeader !== version) {
842
+ return headerMismatch(
843
+ id,
844
+ `MCP-Protocol-Version header value '${protocolHeader}' does not match body value '${version}'`
845
+ );
846
+ }
847
+ const methodHeader = headers.get("mcp-method")?.trim();
848
+ if (methodHeader === void 0) {
849
+ return headerMismatch(id, "the required Mcp-Method header is missing");
850
+ }
851
+ if (methodHeader !== method) {
852
+ return headerMismatch(
853
+ id,
854
+ `Mcp-Method header value '${methodHeader}' does not match body value '${method}'`
855
+ );
856
+ }
857
+ const bodyName = nameSource(method, message.params);
858
+ if (bodyName === void 0) return void 0;
859
+ const nameHeader = headers.get("mcp-name")?.trim();
860
+ if (nameHeader === void 0) {
861
+ return headerMismatch(id, `the required Mcp-Name header is missing (body value '${bodyName}')`);
862
+ }
863
+ const decoded = decodeHeaderValue(nameHeader);
864
+ if (decoded === void 0) {
865
+ return headerMismatch(id, "the Mcp-Name header carries an invalid =?base64?\u2026?= value");
866
+ }
867
+ if (decoded !== bodyName) {
868
+ return headerMismatch(
869
+ id,
870
+ `Mcp-Name header value '${decoded}' does not match body value '${bodyName}'`
871
+ );
872
+ }
873
+ return void 0;
874
+ }
875
+ function checkLegacyHeader(headers, message) {
876
+ if (!isObject(message) || message["method"] === "initialize") return void 0;
877
+ const header = headers.get("mcp-protocol-version")?.trim();
878
+ if (!header || LEGACY_PROTOCOL_VERSIONS.includes(header)) return void 0;
879
+ const id = requestId(message["id"]);
880
+ if (MODERN_PROTOCOL_VERSIONS.includes(header)) {
881
+ return {
882
+ kind: "reject",
883
+ id,
884
+ status: 400,
885
+ error: {
886
+ code: INVALID_PARAMS,
887
+ message: `Missing required _meta field ${MODERN_META.protocolVersion}: MCP-Protocol-Version says ${header}, which carries its version in each request's params._meta`
888
+ }
889
+ };
890
+ }
891
+ return { kind: "reject", id, status: 400, error: unsupportedVersionError(header) };
892
+ }
893
+ function internals(server) {
894
+ return server.server;
895
+ }
896
+ function serverInfoOf(server) {
897
+ const info = internals(server)._serverInfo;
898
+ return info ? { ...info } : { name: "mcp-server", version: "0.0.0" };
899
+ }
900
+ function modernCapabilities(capabilities) {
901
+ const out = {};
902
+ for (const [key, value] of Object.entries(capabilities)) {
903
+ if (key === "tasks" || value === void 0) continue;
904
+ if (isObject(value) && (key === "tools" || key === "prompts" || key === "resources")) {
905
+ const { listChanged: _listChanged, subscribe: _subscribe, ...rest } = value;
906
+ out[key] = rest;
907
+ } else {
908
+ out[key] = value;
909
+ }
910
+ }
911
+ return out;
912
+ }
913
+ function cacheHints(server, method) {
914
+ const isStatic = hasStaticListing(server) && (method === "server/discover" || method === "tools/list");
915
+ return isStatic ? { ttlMs: STATIC_TTL_MS, cacheScope: "public" } : { ttlMs: 0, cacheScope: "private" };
916
+ }
917
+ function discoverResult(server) {
918
+ const instructions = internals(server)._instructions;
919
+ return {
920
+ resultType: "complete",
921
+ supportedVersions: [...SERVED_PROTOCOL_VERSIONS],
922
+ capabilities: modernCapabilities(internals(server).getCapabilities()),
923
+ ...instructions ? { instructions } : {},
924
+ ...cacheHints(server, "server/discover"),
925
+ _meta: { [MODERN_META.serverInfo]: serverInfoOf(server) }
926
+ };
927
+ }
928
+ function decorateResult(server, method, result) {
929
+ const meta = isObject(result["_meta"]) ? { ...result["_meta"] } : {};
930
+ if (meta[MODERN_META.serverInfo] === void 0) {
931
+ meta[MODERN_META.serverInfo] = serverInfoOf(server);
932
+ }
933
+ const out = { resultType: "complete", ...result, _meta: meta };
934
+ if (CACHEABLE.has(method)) {
935
+ const hints = cacheHints(server, method);
936
+ if (out["ttlMs"] === void 0) out["ttlMs"] = hints.ttlMs;
937
+ if (out["cacheScope"] === void 0) out["cacheScope"] = hints.cacheScope;
938
+ }
939
+ return out;
940
+ }
941
+ function logAllowed(requested, level) {
942
+ if (requested === void 0) return false;
943
+ const floor = LOG_LEVELS.indexOf(requested);
944
+ const at = LOG_LEVELS.indexOf(String(level));
945
+ return floor !== -1 && at !== -1 && at >= floor;
946
+ }
947
+ function errorMessage(id, error) {
948
+ return { jsonrpc: "2.0", id, error };
949
+ }
950
+ function messageId(message) {
951
+ const id = message.id;
952
+ return typeof id === "string" || typeof id === "number" ? id : void 0;
953
+ }
954
+ function isResponse(message) {
955
+ return !("method" in message) && ("result" in message || "error" in message);
956
+ }
957
+ var DualEraTransport = class {
958
+ constructor(inner, server) {
959
+ this.inner = inner;
960
+ this.server = server;
961
+ }
962
+ inner;
963
+ server;
964
+ onclose;
965
+ onerror;
966
+ onmessage;
967
+ modern = /* @__PURE__ */ new Map();
968
+ get sessionId() {
969
+ return this.inner.sessionId;
970
+ }
971
+ setProtocolVersion(version) {
972
+ this.inner.setProtocolVersion?.(version);
973
+ }
974
+ async start() {
975
+ this.inner.onmessage = (message, extra) => this.receive(message, extra);
976
+ this.inner.onclose = () => this.onclose?.();
977
+ this.inner.onerror = (error) => this.onerror?.(error);
978
+ await this.inner.start();
979
+ }
980
+ async close() {
981
+ await this.inner.close();
982
+ }
983
+ write(message) {
984
+ this.inner.send(message).catch((error) => {
985
+ this.onerror?.(error instanceof Error ? error : new Error(String(error)));
986
+ });
987
+ }
988
+ receive(message, extra) {
989
+ const route = routeModern(message);
990
+ switch (route.kind) {
991
+ case "legacy":
992
+ case "notification":
993
+ this.onmessage?.(message, extra);
994
+ return;
995
+ case "reject":
996
+ if ("id" in message) this.write(errorMessage(route.id, route.error));
997
+ return;
998
+ case "discover":
999
+ this.write({
1000
+ jsonrpc: "2.0",
1001
+ id: route.id,
1002
+ result: discoverResult(this.server)
1003
+ });
1004
+ return;
1005
+ case "dispatch":
1006
+ this.modern.set(route.id, {
1007
+ method: route.method,
1008
+ ...route.logLevel !== void 0 ? { logLevel: route.logLevel } : {}
1009
+ });
1010
+ this.onmessage?.(message, extra);
1011
+ return;
1012
+ }
1013
+ }
1014
+ async send(message, options) {
1015
+ const id = messageId(message);
1016
+ if (id !== void 0 && isResponse(message)) {
1017
+ const request = this.modern.get(id);
1018
+ if (request) {
1019
+ this.modern.delete(id);
1020
+ const result = message.result;
1021
+ if (isObject(result)) {
1022
+ message = {
1023
+ ...message,
1024
+ result: decorateResult(this.server, request.method, result)
1025
+ };
1026
+ }
1027
+ }
1028
+ return this.inner.send(message, options);
1029
+ }
1030
+ const related = options?.relatedRequestId !== void 0 ? this.modern.get(options.relatedRequestId) : void 0;
1031
+ if (related && "method" in message) {
1032
+ if (id !== void 0) {
1033
+ const refusal = errorMessage(id, {
1034
+ code: INTERNAL_ERROR,
1035
+ message: `${message.method} cannot reach a ${MODERN_PROTOCOL_VERSION} client from this server (multi round-trip requests are not served); call this tool from a 2025-era session (initialize) instead`
1036
+ });
1037
+ queueMicrotask(() => this.onmessage?.(refusal));
1038
+ return;
1039
+ }
1040
+ if (message.method === "notifications/message") {
1041
+ const level = message.params?.level;
1042
+ if (!logAllowed(related.logLevel, level)) return;
1043
+ }
1044
+ }
1045
+ return this.inner.send(message, options);
1046
+ }
1047
+ };
672
1048
  function isZodObject(schema) {
673
1049
  return schema instanceof z4.ZodObject;
674
1050
  }
@@ -749,6 +1125,7 @@ function createMcpServer(opts) {
749
1125
  return tool.invoke(request.params.arguments ?? {}, contextFromExtra(extra));
750
1126
  });
751
1127
  }
1128
+ markStaticListing(server);
752
1129
  return server;
753
1130
  }
754
1131
  function toJsonSchema(schema, io = "input") {
@@ -774,7 +1151,7 @@ function listToolManifest(tools) {
774
1151
  var DEFAULT_CORS = {
775
1152
  origin: "*",
776
1153
  methods: "GET, POST, DELETE, OPTIONS",
777
- headers: "Content-Type, Accept, Authorization, Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID, PAYMENT, X-PAYMENT",
1154
+ headers: "Content-Type, Accept, Authorization, Mcp-Session-Id, Mcp-Protocol-Version, Mcp-Method, Mcp-Name, Last-Event-ID, PAYMENT, X-PAYMENT",
778
1155
  exposeHeaders: "Mcp-Session-Id, Mcp-Protocol-Version, WWW-Authenticate, X-Payment-Requirements, PAYMENT-REQUIRED",
779
1156
  maxAge: 86400
780
1157
  };
@@ -801,7 +1178,7 @@ function methodNotAllowed(method, allow, cors) {
801
1178
  {
802
1179
  code: "METHOD_NOT_ALLOWED",
803
1180
  cause: `${method} is not supported here without a session`,
804
- fix: "send JSON-RPC over POST with `Accept: application/json, text/event-stream` (start with `initialize`)",
1181
+ fix: `send JSON-RPC over POST with \`Accept: application/json, text/event-stream\`: at ${MODERN_PROTOCOL_VERSION}, any request with its version in params._meta (start with \`server/discover\`); on a 2025-era client, start with \`initialize\``,
805
1182
  retryable: false
806
1183
  },
807
1184
  405,
@@ -908,7 +1285,7 @@ function createStatefulHandler({ factory, cors, transportBase }, options) {
908
1285
  {
909
1286
  code: "NOT_FOUND",
910
1287
  cause: "unknown or expired Mcp-Session-Id",
911
- fix: "send `initialize` again without an Mcp-Session-Id header and use the id from the response",
1288
+ fix: `send \`initialize\` again without an Mcp-Session-Id header and use the id from the response, or drop the header and send the request at ${MODERN_PROTOCOL_VERSION} (version in params._meta), which needs no session`,
912
1289
  retryable: true
913
1290
  },
914
1291
  404,
@@ -951,6 +1328,173 @@ function createStatefulHandler({ factory, cors, transportBase }, options) {
951
1328
  }
952
1329
  };
953
1330
  }
1331
+ function rpcErrorResponse(route, cors) {
1332
+ return jsonResponse(errorMessage(route.id, route.error), route.status, cors);
1333
+ }
1334
+ function rebindingError(request, base) {
1335
+ if (!base.enableDnsRebindingProtection) return void 0;
1336
+ if (base.allowedHosts?.length) {
1337
+ const host = request.headers.get("host");
1338
+ if (!host || !base.allowedHosts.includes(host)) return `Invalid Host header: ${host}`;
1339
+ }
1340
+ if (base.allowedOrigins?.length) {
1341
+ const origin = request.headers.get("origin");
1342
+ if (origin && !base.allowedOrigins.includes(origin)) return `Invalid Origin header: ${origin}`;
1343
+ }
1344
+ return void 0;
1345
+ }
1346
+ async function peek(request) {
1347
+ let parsed;
1348
+ try {
1349
+ parsed = JSON.parse(await request.clone().text());
1350
+ } catch {
1351
+ return { kind: "legacy" };
1352
+ }
1353
+ if (Array.isArray(parsed)) {
1354
+ if (!parsed.some((element) => modernClaim(element) !== void 0)) return { kind: "legacy" };
1355
+ return {
1356
+ kind: "reject",
1357
+ route: {
1358
+ kind: "reject",
1359
+ id: null,
1360
+ status: 400,
1361
+ error: { code: -32600, message: "a JSON-RPC batch cannot carry 2026-07-28 requests" }
1362
+ }
1363
+ };
1364
+ }
1365
+ const route = routeModern(parsed);
1366
+ if (route.kind === "legacy") {
1367
+ const refused = checkLegacyHeader(request.headers, parsed);
1368
+ return refused?.kind === "reject" ? { kind: "reject", route: refused } : { kind: "legacy" };
1369
+ }
1370
+ return { kind: "modern", message: parsed, route };
1371
+ }
1372
+ var SinkTransport = class {
1373
+ constructor(out) {
1374
+ this.out = out;
1375
+ }
1376
+ out;
1377
+ onclose;
1378
+ onerror;
1379
+ onmessage;
1380
+ async start() {
1381
+ }
1382
+ async send(message) {
1383
+ this.out(message);
1384
+ }
1385
+ async close() {
1386
+ this.onclose?.();
1387
+ }
1388
+ };
1389
+ var encoder = new TextEncoder();
1390
+ function sseEvent(message) {
1391
+ return encoder.encode(`event: message
1392
+ data: ${JSON.stringify(message)}
1393
+
1394
+ `);
1395
+ }
1396
+ function statusFor(message) {
1397
+ return message.error?.code === -32601 ? 404 : 200;
1398
+ }
1399
+ async function serveModern(request, message, route, { factory, cors, transportBase }) {
1400
+ const rebinding = rebindingError(request, transportBase);
1401
+ if (rebinding) {
1402
+ return jsonResponse(errorMessage(null, { code: -32600, message: rebinding }), 403, cors);
1403
+ }
1404
+ if (route.kind === "reject") return rpcErrorResponse(route, cors);
1405
+ if (route.kind === "notification") return new Response(null, { status: 202, headers: cors });
1406
+ if (route.kind === "legacy") throw new Error("unreachable: legacy requests take the SDK path");
1407
+ const headerError = checkModernHeaders(
1408
+ request.headers,
1409
+ { method: route.method, id: route.id, params: message["params"] },
1410
+ route.version
1411
+ );
1412
+ if (headerError?.kind === "reject") return rpcErrorResponse(headerError, cors);
1413
+ const server = factory();
1414
+ const streams = !transportBase.enableJsonResponse && (request.headers.get("accept") ?? "").includes("text/event-stream");
1415
+ let closed = false;
1416
+ const cleanup = async () => {
1417
+ if (closed) return;
1418
+ closed = true;
1419
+ await server.close().catch(() => void 0);
1420
+ };
1421
+ let settle;
1422
+ const first = new Promise((resolve) => settle = resolve);
1423
+ let settled = false;
1424
+ let controller;
1425
+ let ended = false;
1426
+ const deliver = (out) => {
1427
+ const final = !("method" in out) && out.id === route.id;
1428
+ if (!settled) {
1429
+ if (final) {
1430
+ settled = true;
1431
+ settle({ kind: "json", message: out });
1432
+ return;
1433
+ }
1434
+ if (!streams) return;
1435
+ settled = true;
1436
+ const body = new ReadableStream({
1437
+ start: (c) => {
1438
+ controller = c;
1439
+ },
1440
+ cancel: () => cleanup()
1441
+ });
1442
+ controller?.enqueue(sseEvent(out));
1443
+ settle({ kind: "sse", body });
1444
+ return;
1445
+ }
1446
+ if (ended || !controller) return;
1447
+ controller.enqueue(sseEvent(out));
1448
+ if (final) {
1449
+ ended = true;
1450
+ controller.close();
1451
+ void cleanup();
1452
+ }
1453
+ };
1454
+ const sink = new SinkTransport(deliver);
1455
+ const era = new DualEraTransport(sink, server);
1456
+ request.signal?.addEventListener(
1457
+ "abort",
1458
+ () => {
1459
+ void cleanup();
1460
+ if (!settled) {
1461
+ settled = true;
1462
+ settle({
1463
+ kind: "json",
1464
+ message: errorMessage(route.id, { code: -32603, message: "request aborted" })
1465
+ });
1466
+ }
1467
+ },
1468
+ { once: true }
1469
+ );
1470
+ try {
1471
+ await server.connect(era);
1472
+ const headers = {};
1473
+ request.headers.forEach((value, key) => {
1474
+ headers[key] = value;
1475
+ });
1476
+ sink.onmessage?.(message, {
1477
+ requestInfo: { headers, url: new URL(request.url) }
1478
+ });
1479
+ const answer = await first;
1480
+ if (answer.kind === "json") {
1481
+ await cleanup();
1482
+ return jsonResponse(answer.message, statusFor(answer.message), cors);
1483
+ }
1484
+ return new Response(answer.body, {
1485
+ status: 200,
1486
+ headers: {
1487
+ "Content-Type": "text/event-stream",
1488
+ "Cache-Control": "no-cache",
1489
+ "X-Accel-Buffering": "no",
1490
+ ...cors
1491
+ }
1492
+ });
1493
+ } catch (err) {
1494
+ await cleanup();
1495
+ return errorResponse(err, cors);
1496
+ }
1497
+ }
954
1498
  function createHttpHandler(opts, transportOptions = {}) {
955
1499
  const { factory, transport: t } = splitOptions(opts, transportOptions);
956
1500
  const cors = corsHeaders(t.cors);
@@ -971,6 +1515,13 @@ function createHttpHandler(opts, transportOptions = {}) {
971
1515
  if (request.method === "OPTIONS") {
972
1516
  return new Response(null, { status: 204, headers: { Allow: allow, ...cors } });
973
1517
  }
1518
+ if (request.method === "POST") {
1519
+ const peeked = await peek(request);
1520
+ if (peeked.kind === "reject") return rpcErrorResponse(peeked.route, cors);
1521
+ if (peeked.kind === "modern") {
1522
+ return serveModern(request, peeked.message, peeked.route, context);
1523
+ }
1524
+ }
974
1525
  return handler(request);
975
1526
  };
976
1527
  }
@@ -1114,10 +1665,13 @@ async function readAllPages(fetchPage, opts = {}) {
1114
1665
 
1115
1666
  // ../../packages/mcp-kit/dist/index.js
1116
1667
  import { McpServer as McpServer2 } from "@modelcontextprotocol/sdk/server/mcp.js";
1117
- async function serveStdio(serverOrOpts) {
1668
+ async function serveStdio(serverOrOpts, streams = {}) {
1118
1669
  const server = serverOrOpts instanceof McpServer2 ? serverOrOpts : createMcpServer(serverOrOpts);
1119
1670
  const { StdioServerTransport } = await import("@modelcontextprotocol/sdk/server/stdio.js");
1120
- const transport = new StdioServerTransport();
1671
+ const transport = new DualEraTransport(
1672
+ new StdioServerTransport(streams.stdin, streams.stdout),
1673
+ server
1674
+ );
1121
1675
  await server.connect(transport);
1122
1676
  await new Promise((resolve) => {
1123
1677
  const previous = transport.onclose;
@@ -1141,4 +1695,4 @@ export {
1141
1695
  readAllPages,
1142
1696
  serveStdio
1143
1697
  };
1144
- //# sourceMappingURL=chunk-ZSLWPKXI.js.map
1698
+ //# sourceMappingURL=chunk-IS3DAHZI.js.map