@frontmcp/plugin-cache 1.9.6 → 1.10.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.
package/README.md CHANGED
@@ -1,110 +1,153 @@
1
1
  # @frontmcp/plugin-cache
2
2
 
3
- Cache plugin for FrontMCP - provides automatic tool result caching with support for Redis, Vercel KV, and in-memory storage.
3
+ A FrontMCP plugin that stores what a tool returns and answers the next call with the same arguments from the store,
4
+ without running the tool again. Use it for tools that are slow or costly and whose answer doesn't change every
5
+ call. Tools opt in one by one, with `cache` in `@Tool`, or by name, with `toolPatterns`. Entries live in memory, in
6
+ Redis, or in Vercel KV, and each entry belongs to the caller who made it unless you say otherwise.
4
7
 
5
- ## Installation
8
+ [![npm](https://img.shields.io/npm/v/@frontmcp/plugin-cache.svg)](https://www.npmjs.com/package/@frontmcp/plugin-cache) [![License](https://img.shields.io/npm/l/@frontmcp/plugin-cache.svg)](https://github.com/agentfront/frontmcp/blob/main/LICENSE)
9
+
10
+ ## Install
6
11
 
7
12
  ```bash
8
- npm install @frontmcp/plugin-cache
13
+ npm install @frontmcp/plugin-cache @frontmcp/sdk
9
14
  ```
10
15
 
16
+ Requires Node.js 24 or later. Keep `@frontmcp/sdk` on the same version as this package. The Redis
17
+ client (`ioredis`) is included. `@vercel/kv` is an optional peer dependency: install it to use Vercel KV through
18
+ `type: 'global-store'`.
19
+
11
20
  ## Usage
12
21
 
13
- ```typescript
22
+ Register the plugin on the app whose tools it should cache, and mark those tools with `cache`:
23
+
24
+ ```ts
14
25
  import { CachePlugin } from '@frontmcp/plugin-cache';
15
- import { App } from '@frontmcp/sdk';
26
+ import { App, Tool, ToolContext, z } from '@frontmcp/sdk';
27
+
28
+ @Tool({
29
+ name: 'get_exchange_rate',
30
+ description: 'The current exchange rate between two currencies',
31
+ inputSchema: { from: z.string().length(3), to: z.string().length(3) },
32
+ cache: { ttl: 300 }, // answer repeated calls from the cache for 5 minutes
33
+ })
34
+ export class GetExchangeRate extends ToolContext {
35
+ async execute({ from, to }: { from: string; to: string }) {
36
+ return { from, to, rate: await lookUpRate(from, to) };
37
+ }
38
+ }
16
39
 
17
40
  @App({
18
- plugins: [CachePlugin],
41
+ id: 'rates',
42
+ name: 'Rates',
43
+ tools: [GetExchangeRate],
44
+ plugins: [CachePlugin.init({ type: 'memory', keyByIdentity: false })], // the same rate for every caller
19
45
  })
20
- class MyApp {}
46
+ export class RatesApp {}
21
47
  ```
22
48
 
23
- ## Features
49
+ A cached answer skips `execute()`, and its result carries `_meta.cache: 'hit'`. `CachePlugin.init()` with no
50
+ argument, and `plugins: [CachePlugin]`, mean `type: 'memory'` with the defaults. On `@FrontMcp({ plugins })`, the
51
+ plugin caches every app's tools.
24
52
 
25
- - **Multiple storage backends**: Redis, Vercel KV, or in-memory
26
- - **Automatic TTL management**: Configure cache expiration
27
- - **Tool result caching**: Automatically cache tool execution results
28
- - **Configurable cache keys**: Customize how cache keys are generated
53
+ ## `cache` on `@Tool`
29
54
 
30
- ## Configuration
55
+ A tool is cached when it has `cache`, or its name matches `toolPatterns`. Nothing else is cached.
31
56
 
32
- ### In-Memory (Default)
57
+ | Value | An entry lives | Reading the entry |
58
+ | --------------------------------------- | -------------- | ---------------------------------------------------------- |
59
+ | `cache: true` | `defaultTTL` | Leaves its expiry as it was |
60
+ | `cache: { ttl: 60 }` | 60 seconds | Leaves its expiry as it was |
61
+ | `cache: { ttl: 60, slideWindow: true }` | 60 seconds | Gives it a full 60 seconds again |
62
+ | `cache: { ttl: 0 }`, or a negative TTL | Not stored | The tool isn't cached, even when `toolPatterns` matches it |
33
63
 
34
- ```typescript
35
- import { CachePlugin } from '@frontmcp/plugin-cache';
64
+ The key is a SHA-256 hash of the caller (see below), the tool (`<app id>:<tool name>`), and the validated arguments
65
+ with object keys sorted. A result the tool returns with `isError: true` isn't stored.
36
66
 
37
- @App({
38
- plugins: [
39
- CachePlugin.init({
40
- type: 'memory',
41
- defaultTTL: 300, // TTL in seconds (default: 1 day)
42
- }),
43
- ],
44
- })
45
- class MyApp {}
46
- ```
67
+ ## Who gets which entry
47
68
 
48
- ### Redis
69
+ With `keyByIdentity: true`, the default, each caller gets their own entries: a signed-in user is keyed by their token's
70
+ subject, so one user never gets another user's cached answer.
49
71
 
50
- ```typescript
51
- import { CachePlugin } from '@frontmcp/plugin-cache';
72
+ - **Without authentication, MCP 2026-07-28 clients never get a cached answer.** FrontMCP gives an anonymous caller a
73
+ new identity on every request, so the plugin stores an entry on every call and never reads one back. There's no
74
+ error or warning.
75
+ - **For tools whose answer is the same for everyone** (exchange rates, a public catalog), set
76
+ `keyByIdentity: false`. Every caller then shares one entry per tool and arguments, so never use it for tools whose
77
+ answer depends on the caller's account, tenant or permissions.
52
78
 
53
- @App({
54
- plugins: [
55
- CachePlugin.init({
56
- type: 'redis',
57
- defaultTTL: 300,
58
- config: {
59
- host: 'localhost',
60
- port: 6379,
61
- password: process.env.REDIS_PASSWORD, // optional
62
- db: 0, // optional
63
- },
64
- }),
65
- ],
66
- })
67
- class MyApp {}
79
+ ## Options
80
+
81
+ | Option | Default | Description |
82
+ | --------------- | ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
83
+ | `type` | `'memory'` | Where entries are kept: `'memory'`, `'redis'`, `'redis-client'` or `'global-store'`. Required when you pass options. |
84
+ | `defaultTTL` | `86400` (a day) | Seconds an entry lives when the tool's `cache` gives no `ttl`, and for tools matched by `toolPatterns`. |
85
+ | `keyByIdentity` | `true` | Keep separate entries per caller. See [Who gets which entry](#who-gets-which-entry). |
86
+ | `toolPatterns` | `[]` | Cache these tools too, without `cache` in their metadata. A tool name or `<app id>:<tool name>`, where `*` matches anything: `'catalog:*'`, `'get_*'`. |
87
+ | `bypassHeader` | `'x-frontmcp-disable-cache'` | A request header that skips the cache for that call when its value is `true` or `1`. It must start with `x-frontmcp-`, or `init()` throws a `CachePluginConfigurationError`. |
88
+ | `config` | | With `type: 'redis'`: `{ host, port, password?, db? }`. |
89
+ | `client` | | With `type: 'redis-client'`: an `ioredis` client you created. |
90
+
91
+ ## Stores
92
+
93
+ `'memory'` keeps entries in the server's process: they're lost on restart and aren't shared between instances. For
94
+ Redis, let the plugin connect:
95
+
96
+ ```ts
97
+ CachePlugin.init({
98
+ type: 'redis',
99
+ defaultTTL: 300,
100
+ config: { host: 'localhost', port: 6379, password: process.env.REDIS_PASSWORD, db: 0 },
101
+ });
68
102
  ```
69
103
 
70
- ### Redis Client (Reuse Existing)
104
+ Or pass a client you already have:
71
105
 
72
- ```typescript
106
+ ```ts
73
107
  import { Redis } from 'ioredis';
74
108
 
75
- import { CachePlugin } from '@frontmcp/plugin-cache';
76
-
77
109
  const redis = new Redis({ host: 'localhost', port: 6379 });
78
110
 
79
- @App({
80
- plugins: [
81
- CachePlugin.init({
82
- type: 'redis-client',
83
- client: redis,
84
- defaultTTL: 300,
85
- }),
86
- ],
87
- })
88
- class MyApp {}
111
+ CachePlugin.init({ type: 'redis-client', client: redis, defaultTTL: 300 });
89
112
  ```
90
113
 
91
- ### Global Store
92
-
93
- Use the store configuration from `@FrontMcp` decorator:
114
+ `'global-store'` uses the store configured on `@FrontMcp({ redis })`: Redis, or Vercel KV with
115
+ `redis: { provider: 'vercel-kv' }` (which reads `KV_REST_API_URL` and `KV_REST_API_TOKEN` when you don't pass `url`
116
+ and `token`). Without `redis`, the server doesn't start.
94
117
 
95
- ```typescript
96
- import { CachePlugin } from '@frontmcp/plugin-cache';
118
+ ```ts
119
+ @App({ id: 'catalog', name: 'Catalog', plugins: [CachePlugin.init({ type: 'global-store' })] })
120
+ class CatalogApp {}
97
121
 
98
- @App({
99
- plugins: [
100
- CachePlugin.init({
101
- type: 'global-store', // Uses redis/vercel-kv from FrontMcp config
102
- }),
103
- ],
122
+ @FrontMcp({
123
+ info: { name: 'shop', version: '1.0.0' },
124
+ apps: [CatalogApp],
125
+ redis: { host: 'localhost', port: 6379 },
104
126
  })
105
- class MyApp {}
127
+ export default class Server {}
106
128
  ```
107
129
 
130
+ ## Docs
131
+
132
+ - [Cache plugin reference](https://frontmcp.dev/reference/plugins/cache): every option, the key, hits, bypassing and
133
+ troubleshooting, with runnable examples
134
+ - [Caching Results](https://frontmcp.dev/learn/caching-results): a step-by-step lesson
135
+ - [Plugins and adapters](https://frontmcp.dev/reference/plugins): where to register a plugin
136
+
137
+ ## Related packages
138
+
139
+ - [`@frontmcp/sdk`](https://www.npmjs.com/package/@frontmcp/sdk): the framework this plugin extends
140
+ - [`@frontmcp/plugin-remember`](https://www.npmjs.com/package/@frontmcp/plugin-remember): `this.remember`, for values
141
+ your tools store and read themselves
142
+ - [`@frontmcp/plugin-codecall`](https://www.npmjs.com/package/@frontmcp/plugin-codecall): depends on this plugin
143
+ - [`@frontmcp/plugins`](https://www.npmjs.com/package/@frontmcp/plugins): a meta-package that re-exports this plugin
144
+ with three others
145
+
146
+ ## Contributing
147
+
148
+ Source: [`plugins/plugin-cache`](https://github.com/agentfront/frontmcp/tree/main/plugins/plugin-cache). Issues and
149
+ pull requests are welcome; see [CONTRIBUTING.md](https://github.com/agentfront/frontmcp/blob/main/CONTRIBUTING.md).
150
+
108
151
  ## License
109
152
 
110
- Apache-2.0
153
+ Apache-2.0. See [LICENSE](https://github.com/agentfront/frontmcp/blob/main/LICENSE).
package/esm/index.mjs CHANGED
@@ -1070,6 +1070,14 @@ var init_base = __esm({
1070
1070
  "Use the memory, Redis, Upstash or Vercel KV adapter for an atomic compare-and-delete."
1071
1071
  );
1072
1072
  }
1073
+ /** Default: not supported, for the same reason as {@link deleteIfEquals}. */
1074
+ async expireIfEquals(_key, _expectedValue, _ttlSeconds) {
1075
+ throw new StorageNotSupportedError(
1076
+ "expireIfEquals",
1077
+ this.backendName,
1078
+ "Use the memory, Redis, Upstash or Vercel KV adapter for an atomic compare-and-expire."
1079
+ );
1080
+ }
1073
1081
  // ============================================
1074
1082
  // Pub/Sub (default: not supported)
1075
1083
  // ============================================
@@ -1224,6 +1232,12 @@ var init_memory = __esm({
1224
1232
  if (!entry || isExpired(entry.expiresAt) || entry.value !== expectedValue) return false;
1225
1233
  return this.deleteEntry(key);
1226
1234
  }
1235
+ async expireIfEquals(key, expectedValue, ttlSeconds) {
1236
+ this.ensureConnected();
1237
+ const entry = this.store.get(key);
1238
+ if (!entry || isExpired(entry.expiresAt) || entry.value !== expectedValue) return false;
1239
+ return this.expire(key, ttlSeconds);
1240
+ }
1227
1241
  async exists(key) {
1228
1242
  this.ensureConnected();
1229
1243
  const entry = this.store.get(key);
@@ -2521,10 +2535,12 @@ var init_utils = __esm({
2521
2535
  }
2522
2536
  });
2523
2537
  var COMPARE_AND_DELETE_SCRIPT;
2538
+ var COMPARE_AND_EXPIRE_SCRIPT;
2524
2539
  var init_compare_and_delete = __esm({
2525
2540
  "libs/utils/src/storage/utils/compare-and-delete.ts"() {
2526
2541
  "use strict";
2527
2542
  COMPARE_AND_DELETE_SCRIPT = `if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) end return 0`;
2543
+ COMPARE_AND_EXPIRE_SCRIPT = `if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('EXPIRE', KEYS[1], ARGV[2]) end return 0`;
2528
2544
  }
2529
2545
  });
2530
2546
  var redis_exports = {};
@@ -2767,6 +2783,17 @@ var init_redis = __esm({
2767
2783
  );
2768
2784
  return deleted === 1;
2769
2785
  }
2786
+ async expireIfEquals(key, expectedValue, ttlSeconds) {
2787
+ validateTTL(ttlSeconds);
2788
+ const extended = await this.getConnectedClient().eval(
2789
+ COMPARE_AND_EXPIRE_SCRIPT,
2790
+ 1,
2791
+ this.prefixKey(key),
2792
+ expectedValue,
2793
+ String(ttlSeconds)
2794
+ );
2795
+ return extended === 1;
2796
+ }
2770
2797
  async exists(key) {
2771
2798
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
2772
2799
  return result > 0;
@@ -3098,6 +3125,15 @@ var init_vercel_kv = __esm({
3098
3125
  );
3099
3126
  return deleted === 1;
3100
3127
  }
3128
+ async expireIfEquals(key, expectedValue, ttlSeconds) {
3129
+ validateTTL(ttlSeconds);
3130
+ const extended = await this.getConnectedClient().eval(
3131
+ COMPARE_AND_EXPIRE_SCRIPT,
3132
+ [this.prefixKey(key)],
3133
+ [expectedValue, String(ttlSeconds)]
3134
+ );
3135
+ return extended === 1;
3136
+ }
3101
3137
  async exists(key) {
3102
3138
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
3103
3139
  return result > 0;
@@ -3332,6 +3368,15 @@ var init_upstash = __esm({
3332
3368
  );
3333
3369
  return deleted === 1;
3334
3370
  }
3371
+ async expireIfEquals(key, expectedValue, ttlSeconds) {
3372
+ validateTTL(ttlSeconds);
3373
+ const extended = await this.getConnectedClient().eval(
3374
+ COMPARE_AND_EXPIRE_SCRIPT,
3375
+ [this.prefixKey(key)],
3376
+ [expectedValue, String(ttlSeconds)]
3377
+ );
3378
+ return extended === 1;
3379
+ }
3335
3380
  async exists(key) {
3336
3381
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
3337
3382
  return result > 0;
package/esm/package.json CHANGED
@@ -1,19 +1,20 @@
1
1
  {
2
2
  "name": "@frontmcp/plugin-cache",
3
- "version": "1.9.6",
4
- "description": "Cache plugin for FrontMCP - Redis, Vercel KV, and in-memory caching with automatic tool result caching",
3
+ "version": "1.10.0",
4
+ "description": "Cache plugin for FrontMCP: answers repeated tool calls from memory, Redis or Vercel KV instead of running the tool again",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "license": "Apache-2.0",
7
7
  "engines": {
8
8
  "node": ">=24.0.0"
9
9
  },
10
10
  "keywords": [
11
+ "frontmcp",
11
12
  "mcp",
13
+ "model-context-protocol",
12
14
  "cache",
13
15
  "redis",
14
16
  "vercel-kv",
15
17
  "plugin",
16
- "frontmcp",
17
18
  "agentfront"
18
19
  ],
19
20
  "repository": {
@@ -48,8 +49,8 @@
48
49
  }
49
50
  },
50
51
  "dependencies": {
51
- "@frontmcp/sdk": "1.9.6",
52
- "@frontmcp/utils": "1.9.6",
52
+ "@frontmcp/sdk": "1.10.0",
53
+ "@frontmcp/utils": "1.10.0",
53
54
  "ioredis": "^5.8.0"
54
55
  },
55
56
  "peerDependencies": {
package/index.js CHANGED
@@ -1069,6 +1069,14 @@ var init_base = __esm({
1069
1069
  "Use the memory, Redis, Upstash or Vercel KV adapter for an atomic compare-and-delete."
1070
1070
  );
1071
1071
  }
1072
+ /** Default: not supported, for the same reason as {@link deleteIfEquals}. */
1073
+ async expireIfEquals(_key, _expectedValue, _ttlSeconds) {
1074
+ throw new StorageNotSupportedError(
1075
+ "expireIfEquals",
1076
+ this.backendName,
1077
+ "Use the memory, Redis, Upstash or Vercel KV adapter for an atomic compare-and-expire."
1078
+ );
1079
+ }
1072
1080
  // ============================================
1073
1081
  // Pub/Sub (default: not supported)
1074
1082
  // ============================================
@@ -1223,6 +1231,12 @@ var init_memory = __esm({
1223
1231
  if (!entry || isExpired(entry.expiresAt) || entry.value !== expectedValue) return false;
1224
1232
  return this.deleteEntry(key);
1225
1233
  }
1234
+ async expireIfEquals(key, expectedValue, ttlSeconds) {
1235
+ this.ensureConnected();
1236
+ const entry = this.store.get(key);
1237
+ if (!entry || isExpired(entry.expiresAt) || entry.value !== expectedValue) return false;
1238
+ return this.expire(key, ttlSeconds);
1239
+ }
1226
1240
  async exists(key) {
1227
1241
  this.ensureConnected();
1228
1242
  const entry = this.store.get(key);
@@ -2520,10 +2534,12 @@ var init_utils = __esm({
2520
2534
  }
2521
2535
  });
2522
2536
  var COMPARE_AND_DELETE_SCRIPT;
2537
+ var COMPARE_AND_EXPIRE_SCRIPT;
2523
2538
  var init_compare_and_delete = __esm({
2524
2539
  "libs/utils/src/storage/utils/compare-and-delete.ts"() {
2525
2540
  "use strict";
2526
2541
  COMPARE_AND_DELETE_SCRIPT = `if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) end return 0`;
2542
+ COMPARE_AND_EXPIRE_SCRIPT = `if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('EXPIRE', KEYS[1], ARGV[2]) end return 0`;
2527
2543
  }
2528
2544
  });
2529
2545
  var redis_exports = {};
@@ -2766,6 +2782,17 @@ var init_redis = __esm({
2766
2782
  );
2767
2783
  return deleted === 1;
2768
2784
  }
2785
+ async expireIfEquals(key, expectedValue, ttlSeconds) {
2786
+ validateTTL(ttlSeconds);
2787
+ const extended = await this.getConnectedClient().eval(
2788
+ COMPARE_AND_EXPIRE_SCRIPT,
2789
+ 1,
2790
+ this.prefixKey(key),
2791
+ expectedValue,
2792
+ String(ttlSeconds)
2793
+ );
2794
+ return extended === 1;
2795
+ }
2769
2796
  async exists(key) {
2770
2797
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
2771
2798
  return result > 0;
@@ -3097,6 +3124,15 @@ var init_vercel_kv = __esm({
3097
3124
  );
3098
3125
  return deleted === 1;
3099
3126
  }
3127
+ async expireIfEquals(key, expectedValue, ttlSeconds) {
3128
+ validateTTL(ttlSeconds);
3129
+ const extended = await this.getConnectedClient().eval(
3130
+ COMPARE_AND_EXPIRE_SCRIPT,
3131
+ [this.prefixKey(key)],
3132
+ [expectedValue, String(ttlSeconds)]
3133
+ );
3134
+ return extended === 1;
3135
+ }
3100
3136
  async exists(key) {
3101
3137
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
3102
3138
  return result > 0;
@@ -3331,6 +3367,15 @@ var init_upstash = __esm({
3331
3367
  );
3332
3368
  return deleted === 1;
3333
3369
  }
3370
+ async expireIfEquals(key, expectedValue, ttlSeconds) {
3371
+ validateTTL(ttlSeconds);
3372
+ const extended = await this.getConnectedClient().eval(
3373
+ COMPARE_AND_EXPIRE_SCRIPT,
3374
+ [this.prefixKey(key)],
3375
+ [expectedValue, String(ttlSeconds)]
3376
+ );
3377
+ return extended === 1;
3378
+ }
3334
3379
  async exists(key) {
3335
3380
  const result = await this.getConnectedClient().exists(this.prefixKey(key));
3336
3381
  return result > 0;
package/package.json CHANGED
@@ -1,19 +1,20 @@
1
1
  {
2
2
  "name": "@frontmcp/plugin-cache",
3
- "version": "1.9.6",
4
- "description": "Cache plugin for FrontMCP - Redis, Vercel KV, and in-memory caching with automatic tool result caching",
3
+ "version": "1.10.0",
4
+ "description": "Cache plugin for FrontMCP: answers repeated tool calls from memory, Redis or Vercel KV instead of running the tool again",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "license": "Apache-2.0",
7
7
  "engines": {
8
8
  "node": ">=24.0.0"
9
9
  },
10
10
  "keywords": [
11
+ "frontmcp",
11
12
  "mcp",
13
+ "model-context-protocol",
12
14
  "cache",
13
15
  "redis",
14
16
  "vercel-kv",
15
17
  "plugin",
16
- "frontmcp",
17
18
  "agentfront"
18
19
  ],
19
20
  "repository": {
@@ -48,8 +49,8 @@
48
49
  }
49
50
  },
50
51
  "dependencies": {
51
- "@frontmcp/sdk": "1.9.6",
52
- "@frontmcp/utils": "1.9.6",
52
+ "@frontmcp/sdk": "1.10.0",
53
+ "@frontmcp/utils": "1.10.0",
53
54
  "ioredis": "^5.8.0"
54
55
  },
55
56
  "peerDependencies": {