使用说明
Redis MCP 服务器
一个小型的模型上下文协议(MCP)服务器,提供基本的Redis数据库工具。使用TypeScript和Zod构建,易于扩展。
快速入门
npm install
npm run build && npm start
服务器通过标准输入输出(stdio)运行,并连接到Redis localhost:6379。
发展
npm run dev
检查员
使用MCP Inspector进行交互式测试:
npm run build
npm run inspector
这会在检查器(inspector)下启动服务器,以便您可以列出工具并调用它们。
工具
- setKey: 在 Redis 中设置一个键值对
- getKey:通过键从Redis中获取值
- delKey:从 Redis 中删除一个键
- listKeys:列出 Redis 中的所有键
列出工具请求(MCP):
{
"method": "tools/list",
"params": {}
}
呼叫示例:
{
"method": "tools/call",
"params": {
"name": "setKey",
"arguments": { "key": "user:123", "value": "John Doe" }
}
}
响应示例:
{
"content": [{ "type": "text", "text": "Key \"user:123\" set with value \"John Doe\"" }]
}
验证与模式(或架构)
- 使用 Zod 直接进行输入验证
src/index.ts。 inputSchema由……返回tools/list是一个MCP客户端可以使用的JSON Schema对象。
键值输入模式(Zod):
const keyValueInput = z.object({ key: z.string(), value: z.string() });
JSON Schema(针对MCP):
const inputKeyValueJsonSchema = {
type: 'object',
properties: {
key: { type: 'string', description: 'Key' },
value: { type: 'string', description: 'Value' },
},
required: ['key', 'value'],
} as const;
项目结构
src/
index.ts # server setup, tool definitions, validation, and handlers
dist/
index.js # build output
扩展(添加新工具)
所有工具均在(某处)定义 src/index.ts 在……之下 tools 地图。要添加一个:
- 添加或重用一个Zod模式及其对应的JSON模式。
- 实施一个
execute功能。 - 在(系统/平台)中注册该工具
tools带有地图的description,inputSchema,validate,execute,和format。
脚本
来自 package.json:
build将 TypeScript 编译为dist/start跑node dist/index.jsdev:tsc && node dist/index.jsinspector:npx @modelcontextprotocol/inspector node dist/index.js
故障排除
- “找不到模块 …/x.js”:请确保ESM路径包含
.js扩展(tsconfig 使用 NodeNext)。 - “密钥未找到”:
getKey当键不存在时抛出异常。 - “无效的参数…”:Zod 验证失败;请确保为字符串
key和value。
许可证
麻省理工学院(MIT)