@frontmcp/plugin-cache 1.8.7 → 1.9.0

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.
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Error classes for the cache plugin.
3
+ *
4
+ * @module @frontmcp/plugin-cache
5
+ */
6
+ /**
7
+ * Raised when `CachePlugin` is configured with options it cannot honour, when the plugin is
8
+ * created (`CachePlugin.init()`), so the server never starts with a setting that silently does nothing.
9
+ */
10
+ export declare class CachePluginConfigurationError extends Error {
11
+ constructor(message: string);
12
+ }
13
+ //# sourceMappingURL=cache.errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.errors.d.ts","sourceRoot":"","sources":["../src/cache.errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;GAGG;AACH,qBAAa,6BAA8B,SAAQ,KAAK;gBAC1C,OAAO,EAAE,MAAM;CAI5B"}
@@ -1 +1 @@
1
- {"version":3,"file":"cache.plugin.d.ts","sourceRoot":"","sources":["../src/cache.plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAOb,KAAK,SAAS,EAEd,KAAK,YAAY,EAClB,MAAM,eAAe,CAAC;AAIvB,OAAO,EAAE,KAAK,kBAAkB,EAAsC,MAAM,eAAe,CAAC;AA8C5F,MAAM,CAAC,OAAO,OAAO,WAAY,SAAQ,aAAa,CAAC,kBAAkB,CAAC;IACxE,OAAgB,gBAAgB,GAAI,SAAS,kBAAkB,oBAuD7D;IAEF,MAAM,CAAC,cAAc,EAAE,kBAAkB,CAEvC;IACF,OAAO,EAAE,kBAAkB,CAAC;gBAEhB,OAAO,GAAE,kBAA+C;IAQpE;;OAEG;IACH,OAAO,CAAC,eAAe;IAkBvB;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAsBzB;;OAEG;IACH,OAAO,CAAC,MAAM;IAQR,aAAa,CAAC,OAAO,EAAE,SAAS,CAAC,iBAAiB,CAAC;IA0DnD,cAAc,CAAC,OAAO,EAAE,SAAS,CAAC,iBAAiB,CAAC;IAyB1D;;;;;;;;;OASG;IACH,OAAO,CAAC,qBAAqB;IAyB7B;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAQrB;;;OAGG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;CAGvC"}
1
+ {"version":3,"file":"cache.plugin.d.ts","sourceRoot":"","sources":["../src/cache.plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EAOb,KAAK,SAAS,EAEd,KAAK,YAAY,EAClB,MAAM,eAAe,CAAC;AAKvB,OAAO,EAAE,KAAK,kBAAkB,EAAsC,MAAM,eAAe,CAAC;AAkE5F,MAAM,CAAC,OAAO,OAAO,WAAY,SAAQ,aAAa,CAAC,kBAAkB,CAAC;IACxE,OAAgB,gBAAgB,GAAI,SAAS,kBAAkB,oBAuD7D;IAEF,MAAM,CAAC,cAAc,EAAE,kBAAkB,CAEvC;IACF,OAAO,EAAE,kBAAkB,CAAC;gBAEhB,OAAO,GAAE,kBAA+C;IASpE;;OAEG;IACH,OAAO,CAAC,eAAe;IAkBvB;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAsBzB;;OAEG;IACH,OAAO,CAAC,MAAM;IAQR,aAAa,CAAC,OAAO,EAAE,SAAS,CAAC,iBAAiB,CAAC;IA4DnD,cAAc,CAAC,OAAO,EAAE,SAAS,CAAC,iBAAiB,CAAC;IAgC1D;;;;;;;;;OASG;IACH,OAAO,CAAC,qBAAqB;IAyB7B;;;;;;OAMG;IACH,OAAO,CAAC,aAAa;IAQrB;;;OAGG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;CAGvC"}
package/cache.types.d.ts CHANGED
@@ -10,7 +10,9 @@ export interface CachePluginToolOptions {
10
10
  */
11
11
  ttl?: number;
12
12
  /**
13
- * If true, the cache value will be updated with the new value after the TTL.
13
+ * If true, every cache hit refreshes the entry's TTL (`ttl`, or the plugin's `defaultTTL`), so an
14
+ * entry that keeps being read stays cached. Without it an entry expires `ttl` seconds after it was
15
+ * written, however often it is read. `cache: true` does not slide.
14
16
  * Default is false.
15
17
  */
16
18
  slideWindow?: boolean;
@@ -35,8 +37,9 @@ export interface BaseCachePluginOptions {
35
37
  * HTTP header name that clients can send to bypass cache for a specific request.
36
38
  * When this header is present with a truthy value ('true' or '1'), cache read/write is skipped.
37
39
  *
38
- * Note: Headers must use the `x-frontmcp-*` prefix to be captured by the context storage.
39
- * Custom bypass headers should also follow this convention.
40
+ * Must start with `x-frontmcp-`: the plugin reads the header from the request context, which
41
+ * keeps only a request's `x-frontmcp-*` headers. Any other name throws
42
+ * `CachePluginConfigurationError` when the plugin is created.
40
43
  *
41
44
  * @default 'x-frontmcp-disable-cache'
42
45
  *
@@ -1 +1 @@
1
- {"version":3,"file":"cache.types.d.ts","sourceRoot":"","sources":["../src/cache.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,0BAA0B;QAClC,KAAK,CAAC,EAAE,sBAAsB,GAAG,IAAI,CAAC;KACvC;CACF;AAED,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;;;;;;OASG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IAExB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;;;;;;;;;;OAYG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,6BAA8B,SAAQ,sBAAsB;IAC3E,IAAI,EAAE,cAAc,CAAC;IACrB,MAAM,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,uBAAwB,SAAQ,sBAAsB;IACrE,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,EAAE,CAAC,EAAE,MAAM,CAAC;KACb,CAAC;CACH;AAED,MAAM,MAAM,wBAAwB,GAAG,sBAAsB,GAAG;IAC9D,IAAI,EAAE,QAAQ,CAAC;CAChB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,6BAA8B,SAAQ,sBAAsB;IAC3E,IAAI,EAAE,cAAc,CAAC;CACtB;AAED,MAAM,MAAM,iBAAiB,GAAG,6BAA6B,GAAG,uBAAuB,CAAC;AAExF,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG,iBAAiB,GAAG,6BAA6B,CAAC;AAE9G,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1E,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAE7E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
1
+ {"version":3,"file":"cache.types.d.ts","sourceRoot":"","sources":["../src/cache.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,0BAA0B;QAClC,KAAK,CAAC,EAAE,sBAAsB,GAAG,IAAI,CAAC;KACvC;CACF;AAED,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb;;;;;OAKG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;;;;;;;OASG;IACH,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IAExB;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB;;;;;;;;;;;;OAYG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,6BAA8B,SAAQ,sBAAsB;IAC3E,IAAI,EAAE,cAAc,CAAC;IACrB,MAAM,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,uBAAwB,SAAQ,sBAAsB;IACrE,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,EAAE,CAAC,EAAE,MAAM,CAAC;KACb,CAAC;CACH;AAED,MAAM,MAAM,wBAAwB,GAAG,sBAAsB,GAAG;IAC9D,IAAI,EAAE,QAAQ,CAAC;CAChB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,6BAA8B,SAAQ,sBAAsB;IAC3E,IAAI,EAAE,cAAc,CAAC;CACtB;AAED,MAAM,MAAM,iBAAiB,GAAG,6BAA6B,GAAG,uBAAuB,CAAC;AAExF,MAAM,MAAM,kBAAkB,GAAG,wBAAwB,GAAG,iBAAiB,GAAG,6BAA6B,CAAC;AAE9G,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE1E,QAAQ,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAE7E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAEtC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB"}
package/esm/index.mjs CHANGED
@@ -1,3 +1,6 @@
1
+ var __frontmcpModule = typeof process !== "undefined" && typeof process.getBuiltinModule === "function" ? process.getBuiltinModule("module") : undefined;
2
+ const require = __frontmcpModule ? __frontmcpModule.createRequire(import.meta.url || 'file:///') : undefined;
3
+ // __frontmcpCreateRequire
1
4
  var __create = Object.create;
2
5
  var __defProp = Object.defineProperty;
3
6
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -307,7 +310,6 @@ import { hkdf } from "@noble/hashes/hkdf.js";
307
310
  import { randomBytes as nobleRandomBytes } from "@noble/hashes/utils.js";
308
311
  import { gcm } from "@noble/ciphers/aes.js";
309
312
  import { analyzeForReDoS, REDOS_THRESHOLDS } from "@enclave-vm/ast";
310
- import { z as z3 } from "@frontmcp/lazy-zod";
311
313
  var __frontmcpModule = typeof process !== "undefined" && typeof process.getBuiltinModule === "function" ? process.getBuiltinModule("module") : void 0;
312
314
  var require2 = __frontmcpModule ? __frontmcpModule.createRequire(import.meta.url || "file:///") : void 0;
313
315
  var __defProp2 = Object.defineProperty;
@@ -645,8 +647,8 @@ var init_key_persistence = __esm({
645
647
  */
646
648
  async get(kid) {
647
649
  if (this.enableCache) {
648
- const cached4 = this.cache.get(kid);
649
- if (cached4) return cached4;
650
+ const cached = this.cache.get(kid);
651
+ if (cached) return cached;
650
652
  }
651
653
  const raw = await this.storage.get(kid);
652
654
  if (!raw) return null;
@@ -902,6 +904,61 @@ var init_errors = __esm({
902
904
  };
903
905
  }
904
906
  });
907
+ function globToRegex(pattern) {
908
+ if (pattern.length > MAX_PATTERN_LENGTH) {
909
+ throw new StoragePatternError(
910
+ pattern.substring(0, 50) + "...",
911
+ `Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH} characters`
912
+ );
913
+ }
914
+ const wildcardCount = (pattern.match(/[*?]/g) || []).length;
915
+ if (wildcardCount > MAX_WILDCARDS) {
916
+ throw new StoragePatternError(pattern, `Pattern has too many wildcards (max: ${MAX_WILDCARDS})`);
917
+ }
918
+ if (pattern === "" || pattern === "*") {
919
+ return /^.*$/;
920
+ }
921
+ let regexStr = "^";
922
+ let prevChar = "";
923
+ for (let i = 0; i < pattern.length; i++) {
924
+ const char = pattern[i];
925
+ switch (char) {
926
+ case "*":
927
+ if (prevChar !== "*") {
928
+ regexStr += ".*";
929
+ }
930
+ break;
931
+ case "?":
932
+ regexStr += ".";
933
+ break;
934
+ default:
935
+ regexStr += char.replace(REGEX_SPECIAL_CHARS, "\\$&");
936
+ }
937
+ prevChar = char;
938
+ }
939
+ regexStr += "$";
940
+ try {
941
+ return new RegExp(regexStr);
942
+ } catch {
943
+ throw new StoragePatternError(pattern, "Failed to compile pattern to regex");
944
+ }
945
+ }
946
+ function matchesPattern(key, pattern) {
947
+ const regex = globToRegex(pattern);
948
+ return regex.test(key);
949
+ }
950
+ var MAX_PATTERN_LENGTH;
951
+ var MAX_WILDCARDS;
952
+ var REGEX_SPECIAL_CHARS;
953
+ var init_pattern = __esm({
954
+ "libs/utils/src/storage/utils/pattern.ts"() {
955
+ "use strict";
956
+ init_errors();
957
+ MAX_PATTERN_LENGTH = 500;
958
+ MAX_WILDCARDS = 20;
959
+ REGEX_SPECIAL_CHARS = /[.+^${}()|[\]\\]/g;
960
+ }
961
+ });
905
962
  function validateTTL(ttlSeconds) {
906
963
  if (typeof ttlSeconds !== "number") {
907
964
  throw new StorageTTLError(ttlSeconds, `TTL must be a number, got ${typeof ttlSeconds}`);
@@ -1010,6 +1067,17 @@ var init_base = __esm({
1010
1067
  const matchedKeys = await this.keys(pattern);
1011
1068
  return matchedKeys.length;
1012
1069
  }
1070
+ /**
1071
+ * Default: not supported, since a read followed by a delete is not atomic.
1072
+ * Override in adapters whose backend can compare and delete in one step.
1073
+ */
1074
+ async deleteIfEquals(_key, _expectedValue) {
1075
+ throw new StorageNotSupportedError(
1076
+ "deleteIfEquals",
1077
+ this.backendName,
1078
+ "Use the memory, Redis, Upstash or Vercel KV adapter for an atomic compare-and-delete."
1079
+ );
1080
+ }
1013
1081
  // ============================================
1014
1082
  // Pub/Sub (default: not supported)
1015
1083
  // ============================================
@@ -1057,71 +1125,16 @@ var init_base = __esm({
1057
1125
  };
1058
1126
  }
1059
1127
  });
1060
- function globToRegex(pattern) {
1061
- if (pattern.length > MAX_PATTERN_LENGTH) {
1062
- throw new StoragePatternError(
1063
- pattern.substring(0, 50) + "...",
1064
- `Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH} characters`
1065
- );
1066
- }
1067
- const wildcardCount = (pattern.match(/[*?]/g) || []).length;
1068
- if (wildcardCount > MAX_WILDCARDS) {
1069
- throw new StoragePatternError(pattern, `Pattern has too many wildcards (max: ${MAX_WILDCARDS})`);
1070
- }
1071
- if (pattern === "" || pattern === "*") {
1072
- return /^.*$/;
1073
- }
1074
- let regexStr = "^";
1075
- let prevChar = "";
1076
- for (let i = 0; i < pattern.length; i++) {
1077
- const char = pattern[i];
1078
- switch (char) {
1079
- case "*":
1080
- if (prevChar !== "*") {
1081
- regexStr += ".*";
1082
- }
1083
- break;
1084
- case "?":
1085
- regexStr += ".";
1086
- break;
1087
- default:
1088
- regexStr += char.replace(REGEX_SPECIAL_CHARS, "\\$&");
1089
- }
1090
- prevChar = char;
1091
- }
1092
- regexStr += "$";
1093
- try {
1094
- return new RegExp(regexStr);
1095
- } catch {
1096
- throw new StoragePatternError(pattern, "Failed to compile pattern to regex");
1097
- }
1098
- }
1099
- function matchesPattern(key, pattern) {
1100
- const regex = globToRegex(pattern);
1101
- return regex.test(key);
1102
- }
1103
- var MAX_PATTERN_LENGTH;
1104
- var MAX_WILDCARDS;
1105
- var REGEX_SPECIAL_CHARS;
1106
- var init_pattern = __esm({
1107
- "libs/utils/src/storage/utils/pattern.ts"() {
1108
- "use strict";
1109
- init_errors();
1110
- MAX_PATTERN_LENGTH = 500;
1111
- MAX_WILDCARDS = 20;
1112
- REGEX_SPECIAL_CHARS = /[.+^${}()|[\]\\]/g;
1113
- }
1114
- });
1115
1128
  var DEFAULT_SWEEP_INTERVAL_SECONDS;
1116
1129
  var MAX_TIMEOUT_MS;
1117
1130
  var MemoryStorageAdapter;
1118
1131
  var init_memory = __esm({
1119
1132
  "libs/utils/src/storage/adapters/memory.ts"() {
1120
1133
  "use strict";
1121
- init_base();
1122
1134
  init_errors();
1123
1135
  init_pattern();
1124
1136
  init_ttl();
1137
+ init_base();
1125
1138
  DEFAULT_SWEEP_INTERVAL_SECONDS = 60;
1126
1139
  MAX_TIMEOUT_MS = 2147483647;
1127
1140
  MemoryStorageAdapter = class extends BaseStorageAdapter {
@@ -1213,6 +1226,12 @@ var init_memory = __esm({
1213
1226
  this.ensureConnected();
1214
1227
  return this.deleteEntry(key);
1215
1228
  }
1229
+ async deleteIfEquals(key, expectedValue) {
1230
+ this.ensureConnected();
1231
+ const entry = this.store.get(key);
1232
+ if (!entry || isExpired(entry.expiresAt) || entry.value !== expectedValue) return false;
1233
+ return this.deleteEntry(key);
1234
+ }
1216
1235
  async exists(key) {
1217
1236
  this.ensureConnected();
1218
1237
  const entry = this.store.get(key);
@@ -2584,6 +2603,13 @@ var init_utils = __esm({
2584
2603
  init_ttl();
2585
2604
  }
2586
2605
  });
2606
+ var COMPARE_AND_DELETE_SCRIPT;
2607
+ var init_compare_and_delete = __esm({
2608
+ "libs/utils/src/storage/utils/compare-and-delete.ts"() {
2609
+ "use strict";
2610
+ COMPARE_AND_DELETE_SCRIPT = `if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) end return 0`;
2611
+ }
2612
+ });
2587
2613
  var redis_exports = {};
2588
2614
  __export(redis_exports, {
2589
2615
  RedisStorageAdapter: () => RedisStorageAdapter,
@@ -2626,6 +2652,7 @@ var init_redis = __esm({
2626
2652
  init_errors();
2627
2653
  init_redis_error_listener();
2628
2654
  init_utils();
2655
+ init_compare_and_delete();
2629
2656
  init_base();
2630
2657
  RedisStorageAdapter = class extends BaseStorageAdapter {
2631
2658
  backendName = "redis";
@@ -2780,6 +2807,15 @@ var init_redis = __esm({
2780
2807
  const result = await this.getConnectedClient().del(this.prefixKey(key));
2781
2808
  return result > 0;
2782
2809
  }
2810
+ async deleteIfEquals(key, expectedValue) {
2811
+ const deleted = await this.getConnectedClient().eval(
2812
+ COMPARE_AND_DELETE_SCRIPT,
2813
+ 1,
2814
+ this.prefixKey(key),
2815
+ expectedValue
2816
+ );
2817
+ return deleted === 1;
2818
+ }
2783
2819
  async exists(key) {
2784
2820
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
2785
2821
  return result > 0;
@@ -2986,9 +3022,12 @@ var vercel_kv_exports = {};
2986
3022
  __export(vercel_kv_exports, {
2987
3023
  VercelKvStorageAdapter: () => VercelKvStorageAdapter
2988
3024
  });
2989
- function getVercelKv() {
3025
+ async function loadVercelKv() {
2990
3026
  try {
2991
- return __require2("@vercel/kv");
3027
+ const mod = await import("@vercel/kv");
3028
+ const resolved = typeof mod.createClient === "function" ? mod : mod.default;
3029
+ if (!resolved || typeof resolved.createClient !== "function") throw new Error("createClient missing");
3030
+ return resolved;
2992
3031
  } catch {
2993
3032
  throw new Error("@vercel/kv is required for Vercel KV storage adapter. Install it with: npm install @vercel/kv");
2994
3033
  }
@@ -2997,9 +3036,10 @@ var VercelKvStorageAdapter;
2997
3036
  var init_vercel_kv = __esm({
2998
3037
  "libs/utils/src/storage/adapters/vercel-kv.ts"() {
2999
3038
  "use strict";
3000
- init_base();
3001
3039
  init_errors();
3040
+ init_compare_and_delete();
3002
3041
  init_ttl();
3042
+ init_base();
3003
3043
  VercelKvStorageAdapter = class extends BaseStorageAdapter {
3004
3044
  backendName = "vercel-kv";
3005
3045
  client;
@@ -3024,17 +3064,13 @@ var init_vercel_kv = __esm({
3024
3064
  async connect() {
3025
3065
  if (this.connected) return;
3026
3066
  try {
3027
- const { createClient, kv } = getVercelKv();
3028
- if (this.options.url === process.env["KV_REST_API_URL"]) {
3029
- this.client = kv;
3030
- } else {
3031
- const url = this.options.url;
3032
- const token = this.options.token;
3033
- if (!url || !token) {
3034
- throw new StorageConfigError("vercel-kv", "URL and token are required");
3035
- }
3036
- this.client = createClient({ url, token });
3067
+ const { createClient } = await loadVercelKv();
3068
+ const url = this.options.url;
3069
+ const token = this.options.token;
3070
+ if (!url || !token) {
3071
+ throw new StorageConfigError("vercel-kv", "URL and token are required");
3037
3072
  }
3073
+ this.client = createClient({ url, token, cache: void 0 });
3038
3074
  await this.client.exists("__healthcheck__");
3039
3075
  this.connected = true;
3040
3076
  } catch (e) {
@@ -3096,6 +3132,14 @@ var init_vercel_kv = __esm({
3096
3132
  const result = await this.getConnectedClient().del(this.prefixKey(key));
3097
3133
  return result > 0;
3098
3134
  }
3135
+ async deleteIfEquals(key, expectedValue) {
3136
+ const deleted = await this.getConnectedClient().eval(
3137
+ COMPARE_AND_DELETE_SCRIPT,
3138
+ [this.prefixKey(key)],
3139
+ [expectedValue]
3140
+ );
3141
+ return deleted === 1;
3142
+ }
3099
3143
  async exists(key) {
3100
3144
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
3101
3145
  return result > 0;
@@ -3212,9 +3256,10 @@ var UpstashStorageAdapter;
3212
3256
  var init_upstash = __esm({
3213
3257
  "libs/utils/src/storage/adapters/upstash.ts"() {
3214
3258
  "use strict";
3215
- init_base();
3216
3259
  init_errors();
3260
+ init_compare_and_delete();
3217
3261
  init_ttl();
3262
+ init_base();
3218
3263
  PUBSUB_POLL_INTERVAL_MS = 100;
3219
3264
  UpstashStorageAdapter = class extends BaseStorageAdapter {
3220
3265
  backendName = "upstash";
@@ -3319,6 +3364,14 @@ var init_upstash = __esm({
3319
3364
  const result = await this.getConnectedClient().del(this.prefixKey(key));
3320
3365
  return result > 0;
3321
3366
  }
3367
+ async deleteIfEquals(key, expectedValue) {
3368
+ const deleted = await this.getConnectedClient().eval(
3369
+ COMPARE_AND_DELETE_SCRIPT,
3370
+ [this.prefixKey(key)],
3371
+ [expectedValue]
3372
+ );
3373
+ return deleted === 1;
3374
+ }
3322
3375
  async exists(key) {
3323
3376
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
3324
3377
  return result > 0;
@@ -3507,7 +3560,9 @@ var init_cloudflare_kv = __esm({
3507
3560
  constructor(options) {
3508
3561
  super();
3509
3562
  if (!options?.namespace) {
3510
- throw new Error("CloudflareKvStorageAdapter: a bound KV `namespace` is required (resolve it from the Worker `env`).");
3563
+ throw new Error(
3564
+ "CloudflareKvStorageAdapter: a bound KV `namespace` is required (resolve it from the Worker `env`)."
3565
+ );
3511
3566
  }
3512
3567
  this.kv = options.namespace;
3513
3568
  this.keyPrefix = options.keyPrefix ?? "";
@@ -3601,6 +3656,13 @@ var init_cloudflare_kv = __esm({
3601
3656
  "Cloudflare KV has no atomic increment (a get+put is racy under eventual consistency). Use a Durable Object for atomic counters."
3602
3657
  );
3603
3658
  }
3659
+ async deleteIfEquals(_key, _expectedValue) {
3660
+ throw new StorageNotSupportedError(
3661
+ "deleteIfEquals",
3662
+ this.backendName,
3663
+ "Cloudflare KV has no compare-and-set; use a Durable Object for an atomic compare-and-delete."
3664
+ );
3665
+ }
3604
3666
  // ── Pub/Sub (unsupported) ────────────────────────────────────────────────
3605
3667
  getPubSubSuggestion() {
3606
3668
  return "Cloudflare KV does not support pub/sub. Use a Durable Object (or Queues) for fan-out.";
@@ -3714,16 +3776,7 @@ function compileBlocks(entries, parseNetwork) {
3714
3776
  }
3715
3777
  init_fs2();
3716
3778
  init_crypto();
3717
- var entryAvailabilitySchema = z3.object({
3718
- os: z3.array(z3.string().min(1)).optional(),
3719
- platform: z3.array(z3.string().min(1)).optional(),
3720
- runtime: z3.array(z3.string().min(1)).optional(),
3721
- deployment: z3.array(z3.string().min(1)).optional(),
3722
- provider: z3.array(z3.string().min(1)).optional(),
3723
- target: z3.array(z3.string().min(1)).optional(),
3724
- surface: z3.array(z3.enum(["mcp", "cli", "http-trigger", "job", "agent"])).optional(),
3725
- env: z3.array(z3.string().min(1)).optional()
3726
- }).strict();
3779
+ init_errors();
3727
3780
  init_errors();
3728
3781
  init_memory();
3729
3782
  init_redis_error_listener();
@@ -3743,6 +3796,14 @@ init_ttl();
3743
3796
  init_crypto();
3744
3797
  init_runtime();
3745
3798
 
3799
+ // plugins/plugin-cache/src/cache.errors.ts
3800
+ var CachePluginConfigurationError = class extends Error {
3801
+ constructor(message) {
3802
+ super(message);
3803
+ this.name = "CachePluginConfigurationError";
3804
+ }
3805
+ };
3806
+
3746
3807
  // plugins/plugin-cache/src/cache.symbol.ts
3747
3808
  var CacheStoreToken = /* @__PURE__ */ Symbol("plugin:cache:store");
3748
3809
 
@@ -3974,6 +4035,15 @@ CacheVercelKvProvider = __decorateClass([
3974
4035
 
3975
4036
  // plugins/plugin-cache/src/cache.plugin.ts
3976
4037
  var DEFAULT_BYPASS_HEADER = "x-frontmcp-disable-cache";
4038
+ var BYPASS_HEADER_PREFIX = "x-frontmcp-";
4039
+ function assertReadableBypassHeader(bypassHeader) {
4040
+ if (bypassHeader === void 0) return;
4041
+ const name = bypassHeader.toLowerCase();
4042
+ if (name.startsWith(BYPASS_HEADER_PREFIX) && name.length > BYPASS_HEADER_PREFIX.length) return;
4043
+ throw new CachePluginConfigurationError(
4044
+ `CachePlugin bypassHeader "${bypassHeader}" is never seen by the plugin: only request headers that start with "${BYPASS_HEADER_PREFIX}" reach it. Rename it, e.g. "${BYPASS_HEADER_PREFIX}no-cache".`
4045
+ );
4046
+ }
3977
4047
  function matchesToolPattern(toolName, patterns) {
3978
4048
  if (!patterns || patterns.length === 0) return false;
3979
4049
  return patterns.some((pattern) => {
@@ -3989,6 +4059,7 @@ var CachePlugin = class extends DynamicPlugin {
3989
4059
  options;
3990
4060
  constructor(options = CachePlugin.defaultOptions) {
3991
4061
  super();
4062
+ assertReadableBypassHeader(options.bypassHeader);
3992
4063
  this.options = {
3993
4064
  defaultTTL: 60 * 60 * 24,
3994
4065
  ...options
@@ -4046,15 +4117,13 @@ var CachePlugin = class extends DynamicPlugin {
4046
4117
  const hash = this.buildCacheKey(tool.fullName, toolContext);
4047
4118
  const cached = await cacheStore.getValue(hash);
4048
4119
  if (cached !== void 0 && cached !== null) {
4049
- const cacheConfig = typeof cache === "object" ? cache : void 0;
4050
- if (cache === true || cacheConfig?.ttl && cacheConfig?.slideWindow) {
4051
- const ttl = this.getTtl(cache);
4052
- await cacheStore.setValue(hash, cached, ttl);
4053
- }
4054
- if (!tool.safeParseOutput(cached).success) {
4120
+ if (isErrorResult(cached) || !tool.safeParseOutput(cached).success) {
4055
4121
  await cacheStore.delete(hash);
4056
4122
  return;
4057
4123
  }
4124
+ if (typeof cache === "object" && cache.slideWindow) {
4125
+ await cacheStore.setValue(hash, cached, this.getTtl(cache));
4126
+ }
4058
4127
  flowCtx.state.set("resultMeta", { ...flowCtx.state.resultMeta, cache: "hit" });
4059
4128
  flowCtx.state.set("rawOutput", cached);
4060
4129
  toolContext.respond(cached);
@@ -4071,6 +4140,9 @@ var CachePlugin = class extends DynamicPlugin {
4071
4140
  if (!shouldCache || typeof toolContext.input === "undefined") {
4072
4141
  return;
4073
4142
  }
4143
+ if (isErrorResult(toolContext.output)) {
4144
+ return;
4145
+ }
4074
4146
  const cacheStore = this.get(CacheStoreToken);
4075
4147
  const ttl = this.getTtl(cache);
4076
4148
  const hash = this.buildCacheKey(tool.fullName, toolContext);
@@ -4195,6 +4267,11 @@ CachePlugin = __decorateClass([
4195
4267
  ]
4196
4268
  })
4197
4269
  ], CachePlugin);
4270
+ function isErrorResult(output) {
4271
+ if (typeof output !== "object" || output === null) return false;
4272
+ const result = output;
4273
+ return result.isError === true && Array.isArray(result.content);
4274
+ }
4198
4275
  function stableValue(value) {
4199
4276
  if (Array.isArray(value)) {
4200
4277
  return value.map(stableValue);
@@ -4210,5 +4287,6 @@ function stableValue(value) {
4210
4287
  }
4211
4288
  export {
4212
4289
  CachePlugin,
4290
+ CachePluginConfigurationError,
4213
4291
  CachePlugin as default
4214
4292
  };
package/esm/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontmcp/plugin-cache",
3
- "version": "1.8.7",
3
+ "version": "1.9.0",
4
4
  "description": "Cache plugin for FrontMCP - Redis, Vercel KV, and in-memory caching with automatic tool result caching",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "license": "Apache-2.0",
@@ -48,8 +48,8 @@
48
48
  }
49
49
  },
50
50
  "dependencies": {
51
- "@frontmcp/sdk": "1.8.7",
52
- "@frontmcp/utils": "1.8.7",
51
+ "@frontmcp/sdk": "1.9.0",
52
+ "@frontmcp/utils": "1.9.0",
53
53
  "ioredis": "^5.8.0"
54
54
  },
55
55
  "peerDependencies": {
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default, default as CachePlugin } from './cache.plugin';
2
2
  export * from './cache.types';
3
+ export { CachePluginConfigurationError } from './cache.errors';
3
4
  //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACjE,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,gBAAgB,CAAC;AACjE,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC"}
package/index.js CHANGED
@@ -288,6 +288,7 @@ var require_node = __commonJS({
288
288
  var index_exports = {};
289
289
  __export(index_exports, {
290
290
  CachePlugin: () => CachePlugin,
291
+ CachePluginConfigurationError: () => CachePluginConfigurationError,
291
292
  default: () => CachePlugin
292
293
  });
293
294
  module.exports = __toCommonJS(index_exports);
@@ -307,7 +308,6 @@ var import_utils = require("@noble/hashes/utils.js");
307
308
  var import_aes = require("@noble/ciphers/aes.js");
308
309
  var import_crypto_provider = __toESM(require_node(), 1);
309
310
  var import_ast = require("@enclave-vm/ast");
310
- var import_lazy_zod3 = require("@frontmcp/lazy-zod");
311
311
  var import_meta = {};
312
312
  var __frontmcpModule = typeof process !== "undefined" && typeof process.getBuiltinModule === "function" ? process.getBuiltinModule("module") : void 0;
313
313
  var require2 = __frontmcpModule ? __frontmcpModule.createRequire(import_meta.url || "file:///") : void 0;
@@ -646,8 +646,8 @@ var init_key_persistence = __esm({
646
646
  */
647
647
  async get(kid) {
648
648
  if (this.enableCache) {
649
- const cached4 = this.cache.get(kid);
650
- if (cached4) return cached4;
649
+ const cached = this.cache.get(kid);
650
+ if (cached) return cached;
651
651
  }
652
652
  const raw = await this.storage.get(kid);
653
653
  if (!raw) return null;
@@ -903,6 +903,61 @@ var init_errors = __esm({
903
903
  };
904
904
  }
905
905
  });
906
+ function globToRegex(pattern) {
907
+ if (pattern.length > MAX_PATTERN_LENGTH) {
908
+ throw new StoragePatternError(
909
+ pattern.substring(0, 50) + "...",
910
+ `Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH} characters`
911
+ );
912
+ }
913
+ const wildcardCount = (pattern.match(/[*?]/g) || []).length;
914
+ if (wildcardCount > MAX_WILDCARDS) {
915
+ throw new StoragePatternError(pattern, `Pattern has too many wildcards (max: ${MAX_WILDCARDS})`);
916
+ }
917
+ if (pattern === "" || pattern === "*") {
918
+ return /^.*$/;
919
+ }
920
+ let regexStr = "^";
921
+ let prevChar = "";
922
+ for (let i = 0; i < pattern.length; i++) {
923
+ const char = pattern[i];
924
+ switch (char) {
925
+ case "*":
926
+ if (prevChar !== "*") {
927
+ regexStr += ".*";
928
+ }
929
+ break;
930
+ case "?":
931
+ regexStr += ".";
932
+ break;
933
+ default:
934
+ regexStr += char.replace(REGEX_SPECIAL_CHARS, "\\$&");
935
+ }
936
+ prevChar = char;
937
+ }
938
+ regexStr += "$";
939
+ try {
940
+ return new RegExp(regexStr);
941
+ } catch {
942
+ throw new StoragePatternError(pattern, "Failed to compile pattern to regex");
943
+ }
944
+ }
945
+ function matchesPattern(key, pattern) {
946
+ const regex = globToRegex(pattern);
947
+ return regex.test(key);
948
+ }
949
+ var MAX_PATTERN_LENGTH;
950
+ var MAX_WILDCARDS;
951
+ var REGEX_SPECIAL_CHARS;
952
+ var init_pattern = __esm({
953
+ "libs/utils/src/storage/utils/pattern.ts"() {
954
+ "use strict";
955
+ init_errors();
956
+ MAX_PATTERN_LENGTH = 500;
957
+ MAX_WILDCARDS = 20;
958
+ REGEX_SPECIAL_CHARS = /[.+^${}()|[\]\\]/g;
959
+ }
960
+ });
906
961
  function validateTTL(ttlSeconds) {
907
962
  if (typeof ttlSeconds !== "number") {
908
963
  throw new StorageTTLError(ttlSeconds, `TTL must be a number, got ${typeof ttlSeconds}`);
@@ -1011,6 +1066,17 @@ var init_base = __esm({
1011
1066
  const matchedKeys = await this.keys(pattern);
1012
1067
  return matchedKeys.length;
1013
1068
  }
1069
+ /**
1070
+ * Default: not supported, since a read followed by a delete is not atomic.
1071
+ * Override in adapters whose backend can compare and delete in one step.
1072
+ */
1073
+ async deleteIfEquals(_key, _expectedValue) {
1074
+ throw new StorageNotSupportedError(
1075
+ "deleteIfEquals",
1076
+ this.backendName,
1077
+ "Use the memory, Redis, Upstash or Vercel KV adapter for an atomic compare-and-delete."
1078
+ );
1079
+ }
1014
1080
  // ============================================
1015
1081
  // Pub/Sub (default: not supported)
1016
1082
  // ============================================
@@ -1058,71 +1124,16 @@ var init_base = __esm({
1058
1124
  };
1059
1125
  }
1060
1126
  });
1061
- function globToRegex(pattern) {
1062
- if (pattern.length > MAX_PATTERN_LENGTH) {
1063
- throw new StoragePatternError(
1064
- pattern.substring(0, 50) + "...",
1065
- `Pattern exceeds maximum length of ${MAX_PATTERN_LENGTH} characters`
1066
- );
1067
- }
1068
- const wildcardCount = (pattern.match(/[*?]/g) || []).length;
1069
- if (wildcardCount > MAX_WILDCARDS) {
1070
- throw new StoragePatternError(pattern, `Pattern has too many wildcards (max: ${MAX_WILDCARDS})`);
1071
- }
1072
- if (pattern === "" || pattern === "*") {
1073
- return /^.*$/;
1074
- }
1075
- let regexStr = "^";
1076
- let prevChar = "";
1077
- for (let i = 0; i < pattern.length; i++) {
1078
- const char = pattern[i];
1079
- switch (char) {
1080
- case "*":
1081
- if (prevChar !== "*") {
1082
- regexStr += ".*";
1083
- }
1084
- break;
1085
- case "?":
1086
- regexStr += ".";
1087
- break;
1088
- default:
1089
- regexStr += char.replace(REGEX_SPECIAL_CHARS, "\\$&");
1090
- }
1091
- prevChar = char;
1092
- }
1093
- regexStr += "$";
1094
- try {
1095
- return new RegExp(regexStr);
1096
- } catch {
1097
- throw new StoragePatternError(pattern, "Failed to compile pattern to regex");
1098
- }
1099
- }
1100
- function matchesPattern(key, pattern) {
1101
- const regex = globToRegex(pattern);
1102
- return regex.test(key);
1103
- }
1104
- var MAX_PATTERN_LENGTH;
1105
- var MAX_WILDCARDS;
1106
- var REGEX_SPECIAL_CHARS;
1107
- var init_pattern = __esm({
1108
- "libs/utils/src/storage/utils/pattern.ts"() {
1109
- "use strict";
1110
- init_errors();
1111
- MAX_PATTERN_LENGTH = 500;
1112
- MAX_WILDCARDS = 20;
1113
- REGEX_SPECIAL_CHARS = /[.+^${}()|[\]\\]/g;
1114
- }
1115
- });
1116
1127
  var DEFAULT_SWEEP_INTERVAL_SECONDS;
1117
1128
  var MAX_TIMEOUT_MS;
1118
1129
  var MemoryStorageAdapter;
1119
1130
  var init_memory = __esm({
1120
1131
  "libs/utils/src/storage/adapters/memory.ts"() {
1121
1132
  "use strict";
1122
- init_base();
1123
1133
  init_errors();
1124
1134
  init_pattern();
1125
1135
  init_ttl();
1136
+ init_base();
1126
1137
  DEFAULT_SWEEP_INTERVAL_SECONDS = 60;
1127
1138
  MAX_TIMEOUT_MS = 2147483647;
1128
1139
  MemoryStorageAdapter = class extends BaseStorageAdapter {
@@ -1214,6 +1225,12 @@ var init_memory = __esm({
1214
1225
  this.ensureConnected();
1215
1226
  return this.deleteEntry(key);
1216
1227
  }
1228
+ async deleteIfEquals(key, expectedValue) {
1229
+ this.ensureConnected();
1230
+ const entry = this.store.get(key);
1231
+ if (!entry || isExpired(entry.expiresAt) || entry.value !== expectedValue) return false;
1232
+ return this.deleteEntry(key);
1233
+ }
1217
1234
  async exists(key) {
1218
1235
  this.ensureConnected();
1219
1236
  const entry = this.store.get(key);
@@ -2585,6 +2602,13 @@ var init_utils = __esm({
2585
2602
  init_ttl();
2586
2603
  }
2587
2604
  });
2605
+ var COMPARE_AND_DELETE_SCRIPT;
2606
+ var init_compare_and_delete = __esm({
2607
+ "libs/utils/src/storage/utils/compare-and-delete.ts"() {
2608
+ "use strict";
2609
+ COMPARE_AND_DELETE_SCRIPT = `if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) end return 0`;
2610
+ }
2611
+ });
2588
2612
  var redis_exports = {};
2589
2613
  __export2(redis_exports, {
2590
2614
  RedisStorageAdapter: () => RedisStorageAdapter,
@@ -2627,6 +2651,7 @@ var init_redis = __esm({
2627
2651
  init_errors();
2628
2652
  init_redis_error_listener();
2629
2653
  init_utils();
2654
+ init_compare_and_delete();
2630
2655
  init_base();
2631
2656
  RedisStorageAdapter = class extends BaseStorageAdapter {
2632
2657
  backendName = "redis";
@@ -2781,6 +2806,15 @@ var init_redis = __esm({
2781
2806
  const result = await this.getConnectedClient().del(this.prefixKey(key));
2782
2807
  return result > 0;
2783
2808
  }
2809
+ async deleteIfEquals(key, expectedValue) {
2810
+ const deleted = await this.getConnectedClient().eval(
2811
+ COMPARE_AND_DELETE_SCRIPT,
2812
+ 1,
2813
+ this.prefixKey(key),
2814
+ expectedValue
2815
+ );
2816
+ return deleted === 1;
2817
+ }
2784
2818
  async exists(key) {
2785
2819
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
2786
2820
  return result > 0;
@@ -2987,9 +3021,12 @@ var vercel_kv_exports = {};
2987
3021
  __export2(vercel_kv_exports, {
2988
3022
  VercelKvStorageAdapter: () => VercelKvStorageAdapter
2989
3023
  });
2990
- function getVercelKv() {
3024
+ async function loadVercelKv() {
2991
3025
  try {
2992
- return __require("@vercel/kv");
3026
+ const mod = await import("@vercel/kv");
3027
+ const resolved = typeof mod.createClient === "function" ? mod : mod.default;
3028
+ if (!resolved || typeof resolved.createClient !== "function") throw new Error("createClient missing");
3029
+ return resolved;
2993
3030
  } catch {
2994
3031
  throw new Error("@vercel/kv is required for Vercel KV storage adapter. Install it with: npm install @vercel/kv");
2995
3032
  }
@@ -2998,9 +3035,10 @@ var VercelKvStorageAdapter;
2998
3035
  var init_vercel_kv = __esm({
2999
3036
  "libs/utils/src/storage/adapters/vercel-kv.ts"() {
3000
3037
  "use strict";
3001
- init_base();
3002
3038
  init_errors();
3039
+ init_compare_and_delete();
3003
3040
  init_ttl();
3041
+ init_base();
3004
3042
  VercelKvStorageAdapter = class extends BaseStorageAdapter {
3005
3043
  backendName = "vercel-kv";
3006
3044
  client;
@@ -3025,17 +3063,13 @@ var init_vercel_kv = __esm({
3025
3063
  async connect() {
3026
3064
  if (this.connected) return;
3027
3065
  try {
3028
- const { createClient, kv } = getVercelKv();
3029
- if (this.options.url === process.env["KV_REST_API_URL"]) {
3030
- this.client = kv;
3031
- } else {
3032
- const url = this.options.url;
3033
- const token = this.options.token;
3034
- if (!url || !token) {
3035
- throw new StorageConfigError("vercel-kv", "URL and token are required");
3036
- }
3037
- this.client = createClient({ url, token });
3066
+ const { createClient } = await loadVercelKv();
3067
+ const url = this.options.url;
3068
+ const token = this.options.token;
3069
+ if (!url || !token) {
3070
+ throw new StorageConfigError("vercel-kv", "URL and token are required");
3038
3071
  }
3072
+ this.client = createClient({ url, token, cache: void 0 });
3039
3073
  await this.client.exists("__healthcheck__");
3040
3074
  this.connected = true;
3041
3075
  } catch (e) {
@@ -3097,6 +3131,14 @@ var init_vercel_kv = __esm({
3097
3131
  const result = await this.getConnectedClient().del(this.prefixKey(key));
3098
3132
  return result > 0;
3099
3133
  }
3134
+ async deleteIfEquals(key, expectedValue) {
3135
+ const deleted = await this.getConnectedClient().eval(
3136
+ COMPARE_AND_DELETE_SCRIPT,
3137
+ [this.prefixKey(key)],
3138
+ [expectedValue]
3139
+ );
3140
+ return deleted === 1;
3141
+ }
3100
3142
  async exists(key) {
3101
3143
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
3102
3144
  return result > 0;
@@ -3213,9 +3255,10 @@ var UpstashStorageAdapter;
3213
3255
  var init_upstash = __esm({
3214
3256
  "libs/utils/src/storage/adapters/upstash.ts"() {
3215
3257
  "use strict";
3216
- init_base();
3217
3258
  init_errors();
3259
+ init_compare_and_delete();
3218
3260
  init_ttl();
3261
+ init_base();
3219
3262
  PUBSUB_POLL_INTERVAL_MS = 100;
3220
3263
  UpstashStorageAdapter = class extends BaseStorageAdapter {
3221
3264
  backendName = "upstash";
@@ -3320,6 +3363,14 @@ var init_upstash = __esm({
3320
3363
  const result = await this.getConnectedClient().del(this.prefixKey(key));
3321
3364
  return result > 0;
3322
3365
  }
3366
+ async deleteIfEquals(key, expectedValue) {
3367
+ const deleted = await this.getConnectedClient().eval(
3368
+ COMPARE_AND_DELETE_SCRIPT,
3369
+ [this.prefixKey(key)],
3370
+ [expectedValue]
3371
+ );
3372
+ return deleted === 1;
3373
+ }
3323
3374
  async exists(key) {
3324
3375
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
3325
3376
  return result > 0;
@@ -3508,7 +3559,9 @@ var init_cloudflare_kv = __esm({
3508
3559
  constructor(options) {
3509
3560
  super();
3510
3561
  if (!options?.namespace) {
3511
- throw new Error("CloudflareKvStorageAdapter: a bound KV `namespace` is required (resolve it from the Worker `env`).");
3562
+ throw new Error(
3563
+ "CloudflareKvStorageAdapter: a bound KV `namespace` is required (resolve it from the Worker `env`)."
3564
+ );
3512
3565
  }
3513
3566
  this.kv = options.namespace;
3514
3567
  this.keyPrefix = options.keyPrefix ?? "";
@@ -3602,6 +3655,13 @@ var init_cloudflare_kv = __esm({
3602
3655
  "Cloudflare KV has no atomic increment (a get+put is racy under eventual consistency). Use a Durable Object for atomic counters."
3603
3656
  );
3604
3657
  }
3658
+ async deleteIfEquals(_key, _expectedValue) {
3659
+ throw new StorageNotSupportedError(
3660
+ "deleteIfEquals",
3661
+ this.backendName,
3662
+ "Cloudflare KV has no compare-and-set; use a Durable Object for an atomic compare-and-delete."
3663
+ );
3664
+ }
3605
3665
  // ── Pub/Sub (unsupported) ────────────────────────────────────────────────
3606
3666
  getPubSubSuggestion() {
3607
3667
  return "Cloudflare KV does not support pub/sub. Use a Durable Object (or Queues) for fan-out.";
@@ -3715,16 +3775,7 @@ function compileBlocks(entries, parseNetwork) {
3715
3775
  }
3716
3776
  init_fs2();
3717
3777
  init_crypto();
3718
- var entryAvailabilitySchema = import_lazy_zod3.z.object({
3719
- os: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
3720
- platform: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
3721
- runtime: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
3722
- deployment: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
3723
- provider: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
3724
- target: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional(),
3725
- surface: import_lazy_zod3.z.array(import_lazy_zod3.z.enum(["mcp", "cli", "http-trigger", "job", "agent"])).optional(),
3726
- env: import_lazy_zod3.z.array(import_lazy_zod3.z.string().min(1)).optional()
3727
- }).strict();
3778
+ init_errors();
3728
3779
  init_errors();
3729
3780
  init_memory();
3730
3781
  init_redis_error_listener();
@@ -3744,6 +3795,14 @@ init_ttl();
3744
3795
  init_crypto();
3745
3796
  init_runtime();
3746
3797
 
3798
+ // plugins/plugin-cache/src/cache.errors.ts
3799
+ var CachePluginConfigurationError = class extends Error {
3800
+ constructor(message) {
3801
+ super(message);
3802
+ this.name = "CachePluginConfigurationError";
3803
+ }
3804
+ };
3805
+
3747
3806
  // plugins/plugin-cache/src/cache.symbol.ts
3748
3807
  var CacheStoreToken = /* @__PURE__ */ Symbol("plugin:cache:store");
3749
3808
 
@@ -3975,6 +4034,15 @@ CacheVercelKvProvider = __decorateClass([
3975
4034
 
3976
4035
  // plugins/plugin-cache/src/cache.plugin.ts
3977
4036
  var DEFAULT_BYPASS_HEADER = "x-frontmcp-disable-cache";
4037
+ var BYPASS_HEADER_PREFIX = "x-frontmcp-";
4038
+ function assertReadableBypassHeader(bypassHeader) {
4039
+ if (bypassHeader === void 0) return;
4040
+ const name = bypassHeader.toLowerCase();
4041
+ if (name.startsWith(BYPASS_HEADER_PREFIX) && name.length > BYPASS_HEADER_PREFIX.length) return;
4042
+ throw new CachePluginConfigurationError(
4043
+ `CachePlugin bypassHeader "${bypassHeader}" is never seen by the plugin: only request headers that start with "${BYPASS_HEADER_PREFIX}" reach it. Rename it, e.g. "${BYPASS_HEADER_PREFIX}no-cache".`
4044
+ );
4045
+ }
3978
4046
  function matchesToolPattern(toolName, patterns) {
3979
4047
  if (!patterns || patterns.length === 0) return false;
3980
4048
  return patterns.some((pattern) => {
@@ -3990,6 +4058,7 @@ var CachePlugin = class extends import_sdk4.DynamicPlugin {
3990
4058
  options;
3991
4059
  constructor(options = CachePlugin.defaultOptions) {
3992
4060
  super();
4061
+ assertReadableBypassHeader(options.bypassHeader);
3993
4062
  this.options = {
3994
4063
  defaultTTL: 60 * 60 * 24,
3995
4064
  ...options
@@ -4047,15 +4116,13 @@ var CachePlugin = class extends import_sdk4.DynamicPlugin {
4047
4116
  const hash = this.buildCacheKey(tool.fullName, toolContext);
4048
4117
  const cached = await cacheStore.getValue(hash);
4049
4118
  if (cached !== void 0 && cached !== null) {
4050
- const cacheConfig = typeof cache === "object" ? cache : void 0;
4051
- if (cache === true || cacheConfig?.ttl && cacheConfig?.slideWindow) {
4052
- const ttl = this.getTtl(cache);
4053
- await cacheStore.setValue(hash, cached, ttl);
4054
- }
4055
- if (!tool.safeParseOutput(cached).success) {
4119
+ if (isErrorResult(cached) || !tool.safeParseOutput(cached).success) {
4056
4120
  await cacheStore.delete(hash);
4057
4121
  return;
4058
4122
  }
4123
+ if (typeof cache === "object" && cache.slideWindow) {
4124
+ await cacheStore.setValue(hash, cached, this.getTtl(cache));
4125
+ }
4059
4126
  flowCtx.state.set("resultMeta", { ...flowCtx.state.resultMeta, cache: "hit" });
4060
4127
  flowCtx.state.set("rawOutput", cached);
4061
4128
  toolContext.respond(cached);
@@ -4072,6 +4139,9 @@ var CachePlugin = class extends import_sdk4.DynamicPlugin {
4072
4139
  if (!shouldCache || typeof toolContext.input === "undefined") {
4073
4140
  return;
4074
4141
  }
4142
+ if (isErrorResult(toolContext.output)) {
4143
+ return;
4144
+ }
4075
4145
  const cacheStore = this.get(CacheStoreToken);
4076
4146
  const ttl = this.getTtl(cache);
4077
4147
  const hash = this.buildCacheKey(tool.fullName, toolContext);
@@ -4196,6 +4266,11 @@ CachePlugin = __decorateClass([
4196
4266
  ]
4197
4267
  })
4198
4268
  ], CachePlugin);
4269
+ function isErrorResult(output) {
4270
+ if (typeof output !== "object" || output === null) return false;
4271
+ const result = output;
4272
+ return result.isError === true && Array.isArray(result.content);
4273
+ }
4199
4274
  function stableValue(value) {
4200
4275
  if (Array.isArray(value)) {
4201
4276
  return value.map(stableValue);
@@ -4211,5 +4286,6 @@ function stableValue(value) {
4211
4286
  }
4212
4287
  // Annotate the CommonJS export names for ESM import in node:
4213
4288
  0 && (module.exports = {
4214
- CachePlugin
4289
+ CachePlugin,
4290
+ CachePluginConfigurationError
4215
4291
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontmcp/plugin-cache",
3
- "version": "1.8.7",
3
+ "version": "1.9.0",
4
4
  "description": "Cache plugin for FrontMCP - Redis, Vercel KV, and in-memory caching with automatic tool result caching",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "license": "Apache-2.0",
@@ -48,8 +48,8 @@
48
48
  }
49
49
  },
50
50
  "dependencies": {
51
- "@frontmcp/sdk": "1.8.7",
52
- "@frontmcp/utils": "1.8.7",
51
+ "@frontmcp/sdk": "1.9.0",
52
+ "@frontmcp/utils": "1.9.0",
53
53
  "ioredis": "^5.8.0"
54
54
  },
55
55
  "peerDependencies": {