@frontmcp/plugin-cache

中文文档
1.12.2发布于 2026/11/15Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 7841

Cache plugin for FrontMCP: answers repeated tool calls from memory, Redis or Vercel KV instead of running the tool again

原始使用文档
文档目录10 节
  1. Install
  2. Usage
  3. cache on @Tool
  4. Who gets which entry
  5. Options
  6. Stores
  7. Docs
  8. Related packages
  9. Contributing
  10. License

@frontmcp/plugin-cache

A FrontMCP plugin that stores what a tool returns and answers the next call with the same arguments from the store, without running the tool again. Use it for tools that are slow or costly and whose answer doesn't change every call. Tools opt in one by one, with cache in @Tool, or by name, with toolPatterns. Entries live in memory, in Redis, or in Vercel KV, and each entry belongs to the caller who made it unless you say otherwise.

npm License

Install

npm install @frontmcp/plugin-cache @frontmcp/sdk

Requires Node.js 24 or later. Keep @frontmcp/sdk on the same version as this package. The Redis client (ioredis) is included. @vercel/kv is an optional peer dependency: install it to use Vercel KV through type: 'global-store'.

Usage

Register the plugin on the app whose tools it should cache, and mark those tools with cache:

import { CachePlugin } from '@frontmcp/plugin-cache';
import { App, Tool, ToolContext, z } from '@frontmcp/sdk';

@Tool({
  name: 'get_exchange_rate',
  description: 'The current exchange rate between two currencies',
  inputSchema: { from: z.string().length(3), to: z.string().length(3) },
  cache: { ttl: 300 }, // answer repeated calls from the cache for 5 minutes
})
export class GetExchangeRate extends ToolContext {
  async execute({ from, to }: { from: string; to: string }) {
    return { from, to, rate: await lookUpRate(from, to) };
  }
}

@App({
  id: 'rates',
  name: 'Rates',
  tools: [GetExchangeRate],
  plugins: [CachePlugin.init({ type: 'memory', keyByIdentity: false })], // the same rate for every caller
})
export class RatesApp {}

A cached answer skips execute(), and its result carries _meta.cache: 'hit'. CachePlugin.init() with no argument, and plugins: [CachePlugin], mean type: 'memory' with the defaults. On @FrontMcp({ plugins }), the plugin caches every app's tools.

cache on @Tool

A tool is cached when it has cache, or its name matches toolPatterns. Nothing else is cached.

ValueAn entry livesReading the entry
cache: truedefaultTTLLeaves its expiry as it was
cache: { ttl: 60 }60 secondsLeaves its expiry as it was
cache: { ttl: 60, slideWindow: true }60 secondsGives it a full 60 seconds again
cache: { ttl: 0 }, or a negative TTLNot storedThe tool isn't cached, even when toolPatterns matches it

The key is a SHA-256 hash of the caller (see below), the tool (<app id>:<tool name>), and the validated arguments with object keys sorted. A result the tool returns with isError: true isn't stored.

Who gets which entry

With keyByIdentity: true, the default, each caller gets their own entries: a signed-in user is keyed by their token's subject, so one user never gets another user's cached answer.

  • A caller with no identity skips the cache. A caller without a token subject, a client id or a session the server verified, such as an MCP 2026-07-28 client without authentication, runs the tool on every call, and the plugin neither reads nor stores an entry for it. An anonymous id (anon:…) counts as no identity. There's no error or warning.
  • For tools whose answer is the same for everyone (exchange rates, a public catalog), set keyByIdentity: false. Every caller then shares one entry per tool and arguments, so never use it for tools whose answer depends on the caller's account, tenant or permissions.

Options

OptionDefaultDescription
type'memory'Where entries are kept: 'memory', 'redis', 'redis-client' or 'global-store'. Required when you pass options. Any other value makes init() throw a CachePluginConfigurationError.
defaultTTL86400 (a day)Seconds an entry lives when the tool's cache gives no ttl, and for tools matched by toolPatterns.
keyByIdentitytrueKeep separate entries per caller. See Who gets which entry.
toolPatterns[]Cache these tools too, without cache in their metadata. A tool name or <app id>:<tool name>, where * matches anything: 'catalog:*', 'get_*'.
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.
configWith type: 'redis': { host, port, password?, db? }.
clientWith type: 'redis-client': an ioredis client you created.

Stores

'memory' keeps entries in the server's process: they're lost on restart and aren't shared between instances. For Redis, let the plugin connect:

CachePlugin.init({
  type: 'redis',
  defaultTTL: 300,
  config: { host: 'localhost', port: 6379, password: process.env.REDIS_PASSWORD, db: 0 },
});

Or pass a client you already have:

import { Redis } from 'ioredis';

const redis = new Redis({ host: 'localhost', port: 6379 });

CachePlugin.init({ type: 'redis-client', client: redis, defaultTTL: 300 });

'global-store' uses the store configured on @FrontMcp({ redis }): Redis, or Vercel KV with redis: { provider: 'vercel-kv' } (which reads KV_REST_API_URL and KV_REST_API_TOKEN when you don't pass url and token). Without redis, the server doesn't start.

@App({ id: 'catalog', name: 'Catalog', plugins: [CachePlugin.init({ type: 'global-store' })] })
class CatalogApp {}

@FrontMcp({
  info: { name: 'shop', version: '1.0.0' },
  apps: [CatalogApp],
  redis: { host: 'localhost', port: 6379 },
})
export default class Server {}

A browser build (create() in a web page, WebMCP) has no TCP socket for Redis: there 'redis' and 'redis-client' throw BrowserStoreUnavailableError from init(), and 'global-store' on a Redis redis throws it when the server starts, before anything connects. The message names type: 'memory'. Vercel KV, over HTTPS, is allowed.

Docs

Contributing

Source: plugins/plugin-cache. Issues and pull requests are welcome; see CONTRIBUTING.md.

License

Apache-2.0. See LICENSE.

版本与发布记录

@frontmcp/plugin-cache 共发布 112 个版本,首次发布于 2026/01/09;过去 12 个月发布了 112 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.12.2latest2026/11/151 天前
1.12.12026/11/132 天前
1.12.02026/11/114 天前
1.12.0-rc.1rc2026/11/114 天前
1.11.32026/11/106 天前
1.11.22026/11/096 天前

其他发布通道:beta → 1.1.2-beta.1,rc → 1.12.0-rc.1。

关于 @frontmcp/plugin-cache 的常见问题

@frontmcp/plugin-cache 是什么?

Cache plugin for FrontMCP: answers repeated tool calls from memory, Redis or Vercel KV instead of running the tool again

如何安装 @frontmcp/plugin-cache?

在项目目录运行 npm install @frontmcp/plugin-cache,或使用 pnpm add / yarn add / bun add。

@frontmcp/plugin-cache 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@frontmcp/plugin-cache 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@frontmcp/plugin-cache 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@frontmcp/plugin-cache 可以商用吗?

@frontmcp/plugin-cache 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@frontmcp/plugin-cache 的下载量有多少?

根据 npm 官方下载统计,@frontmcp/plugin-cache 在 2026/10/16 – 2026/11/14 这 30 天内被下载 7,841 次,日均约 261 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@frontmcp/plugin-cache 还在维护吗?

最新版本 1.12.2 发布于 2026/11/15(1 天前);至今共发布 112 个版本,其中过去 12 个月发布了 112 个;首次发布于 2026/01/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。