@ frontmcp/ 插件缓存
一个FrontMCP插件,它存储一个工具返回的内容并用商店的相同参数回答下一个呼叫,
不再运行工具。 使用它的工具 慢或昂贵的 答案不会改变每一个
电话 工具逐一选择,使用 cache 内 @Tool,或名称,与 toolPatterns。条目生活在记忆中,在
Redis,或者在Vercel KV中,每个条目属于制作它的人,除非你另有说法.
安装
npm install @frontmcp/plugin-cache @frontmcp/sdk
需要节点.js 24或后. 保留 @frontmcp/sdk 上。 红色
客户端( e)ioredis页:1. @vercel/kv 是可选的对等依赖: 安装它以通过 Vercel KV
type: 'global-store'。 。 。 .
使用量
在应用程序上注册插件,该插件应该缓存,并将这些工具标记为 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 {}
缓存的答案跳过 execute(),其结果包括: _meta.cache: 'hit'。 。 。 . CachePlugin.init() 没有
参数,以及 plugins: [CachePlugin]说 type: 'memory' 与默认。 打开 @FrontMcp({ plugins }),则
插件缓存每个应用程序的工具 .
cache 打开 @Tool
工具在有时被缓存 cache,或名称匹配 toolPatterns。没有其他的缓存.
| 数值 | 进入生命 | 读取条目 |
|---|---|---|
cache: true | defaultTTL | 离开它的过去 |
cache: { ttl: 60 } | 60秒钟 | 离开它的过去 |
cache: { ttl: 60, slideWindow: true } | 60秒钟 | 再给它60秒 |
cache: { ttl: 0 },或负 TTL | 未存储 | 工具没有缓存,即使 toolPatterns 和它比对 |
关键是呼叫者的SHA-256散列(见下文),工具(<app id>:<tool name>),以及经过验证的论点
用对象键排序。 工具返回的结果 isError: true 没有存储.
谁得到哪个条目
与 keyByIdentity: true,默认情况下,每个呼叫者都会得到自己的条目: 登录的用户按下他们的代号
主体,所以一个用户永远不会得到另一个用户的缓存答案.
- 无身份的呼叫者跳过缓存. 没有符号主题、客户端 ID 或会话的呼叫器服务器
已校验, 例如没有认证的 MCP 2026-07-28 客户端, 运行每个调用上的工具, 以及插件
既不读也不存储条目。 匿名身份证明( A)
anon:…)算作无身份. 没有错误或 警告 警告他们. - 对每个人都一样的答案 (汇率,公共目录)
keyByIdentity: false。每个呼叫者然后共享每个工具和参数的一个条目,因此从不将其用于其 答案取决于打电话者的账户、租户或许可.
选项
| 选项 | 默认 | 说明 |
|---|---|---|
type | 'memory' | 在保留条目时: 'memory', (中文(简体) ), 'redis', (中文(简体) ), 'redis-client' 或者说 'global-store'选项。 任何其他价值 init() 丢出一个 CachePluginConfigurationError。 。 。 . |
defaultTTL | 86400 (a) 日数 | 秒一个条目在工具存在时存在 cache 给予不 ttl,用于匹配的工具 toolPatterns。 。 。 . |
keyByIdentity | true | 保持每个呼叫者的单独条目. 见 谁得到哪个条目。 。 。 . |
toolPatterns | [] | 缓存这些工具,没有 cache 在他们的元数据。 工具名称或 <app id>:<tool name>时 * 匹配任何东西 : 'catalog:*', (中文(简体) ), 'get_*'。 。 。 . |
bypassHeader | 'x-frontmcp-disable-cache' | 当该调用值值为时跳过缓存的请求头 true 或者说 1它必须从 x-frontmcp-,或 init() 丢出一个 CachePluginConfigurationError。 。 。 . |
config | 与 type: 'redis'数字 : { host, port, password?, db? }。 。 。 . | |
client | 与 type: 'redis-client'页:1 ioredis 您创建的客户端 . |
存储
'memory' 将条目保存在服务器的进程中 : 它们在重新启动时丢失, 并且没有在实例之间共享 。 对于
Redis, 让插件连接 :
CachePlugin.init({
type: 'redis',
defaultTTL: 300,
config: { host: 'localhost', port: 6379, password: process.env.REDIS_PASSWORD, db: 0 },
});
或者通过一个客户 你已经有了:
import { Redis } from 'ioredis';
const redis = new Redis({ host: 'localhost', port: 6379 });
CachePlugin.init({ type: 'redis-client', client: redis, defaultTTL: 300 });
'global-store' 使用配置在 @FrontMcp({ redis }): Redis, 或 Vercel KV 带
redis: { provider: 'vercel-kv' } (原文为: KV_REST_API_URL 财务报告和审定财务报表 KV_REST_API_TOKEN 当你不通过 url
财务报告和审定财务报表 token) (中文(简体) ). 无 redis,服务器不会启动.
@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)create() 在网页WebMCP中,Redis没有 TCP 套接字: 'redis' 财务报告和审定财务报表 'redis-client'
掷出 BrowserStoreUnavailableError 从 init(),以及 'global-store' 在雷迪丝上 redis 服务器时抛出
开始,在连接之前。 信件名称 type: 'memory'. Vercel KV, over HTTPS, 允许使用.
文档
相关软件包
@frontmcp/sdk: 此插件扩展的框架@frontmcp/plugin-remember数字 :this.remember值 您的工具存储和阅读自己@frontmcp/plugin-codecall: 取决于此插件@frontmcp/plugins: 重新导出此插件的元包 与另外三个
捐款
资料来源: plugins/plugin-cacheB. 议题和议题
欢迎拉请求;见 捐款额。 。 。 .
许可证
阿帕奇2.0 (英语). 见 自由主义。 。 。 .