Redis模型上下文协议
允许用户使用模型上下文协议(MCP)工具执行 Redis 数据库操作,通过设置、获取和扫描哈希字段等命令实现高效的数据管理。
MCP 服务配置
复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用
{
"mcpServers": {
"redis": {
"args": [
"redis-mcp",
"--redis-host",
"localhost",
"--redis-port",
"6379"
],
"command": "npx",
"disabled": false
}
}
}
该服务需要配置环境变量:redis-host、redis-port
服务介绍
Redis MCP 服务器
一个提供访问 Redis 数据库操作的模型上下文协议 (MCP) 服务器。
项目结构
src/
├── interfaces/
│ └── types.ts # Shared TypeScript interfaces and types
├── tools/
│ ├── base_tool.ts # Abstract base class for Redis tools
│ ├── tool_registry.ts # Registry managing all available Redis tools
│ ├── hmset_tool.ts # HMSET Redis operation
│ ├── hget_tool.ts # HGET Redis operation
│ ├── hgetall_tool.ts # HGETALL Redis operation
│ ├── scan_tool.ts # SCAN Redis operation
│ ├── set_tool.ts # SET Redis operation
│ ├── get_tool.ts # GET Redis operation
│ ├── del_tool.ts # DEL Redis operation
│ ├── zadd_tool.ts # ZADD Redis operation
│ ├── zrange_tool.ts # ZRANGE Redis operation
│ ├── zrangebyscore_tool.ts # ZRANGEBYSCORE Redis operation
│ └── zrem_tool.ts # ZREM Redis operation
└── redis_server.ts # Main server implementation
可用工具
| 工具 | 类型 | 描述 | 输入模式 |
|---|---|---|---|
| hmset | 哈希命令 | 设置多个哈希字段到多个值 | key: 字符串 (哈希键)fields: 对象 (要设置的字段-值对) |
| hget | 哈希命令 | 获取哈希字段的值 | key: 字符串 (哈希键)field: 字符串 (要获取的字段) |
| hgetall | 哈希命令 | 获取哈希中的所有字段和值 | key: 字符串 (哈希键) |
| scan | 键命令 | 扫描与模式匹配的 Redis 键 | pattern: 字符串 (匹配模式,例如 "user:*")count: 数字, 可选 (返回的键的数量) |
| set | 字符串命令 | 设置字符串值,可选 NX 和 PX 选项 | key: 字符串 (要设置的键)value: 字符串 (要设置的值)nx: 布尔值, 可选 (仅在不存在时设置)px: 数字, 可选 (以毫秒为单位的过期时间) |
| get | 字符串命令 | 获取字符串值 | key: 字符串 (要获取的键) |
| del | 键命令 | 删除一个键 | key: 字符串 (要删除的键) |
| zadd | 有序集合命令 | 向有序集合添加一个或多个成员 | key: 字符串 (有序集合键)members: 对象数组,每个对象包含 score: 数字 和 value: 字符串 |
| zrange | 有序集合命令 | 按索引从有序集合中返回一系列成员 | key: 字符串 (有序集合键)start: 数字 (起始索引)stop: 数字 (结束索引)withScores: 布尔值, 可选 (输出中包含分数) |
| zrangebyscore | 有序集合命令 | 返回分数介于最小值和最大值之间的有序集合成员 | key: 字符串 (有序集合键)min: 数字 (最小分数)max: 数字 (最大分数)withScores: 布尔值, 可选 (输出中包含分数) |
| zrem | 有序集合命令 | 从有序集合中移除一个或多个成员 | key: 字符串 (有序集合键)members: 字符串数组 (要移除的成员) |
| sadd | 集合命令 | 向集合中添加一个或多个成员 | key: 字符串 (集合键)members: 字符串数组 (要添加到集合中的成员) |
| smembers | 集合命令 | 获取集合中的所有成员 | key: 字符串 (集合键) |
使用方法
在您的 MCP 客户端(如 Claude Desktop, Cline)中配置:
{
"mcpServers": {
"redis": {
"command": "npx",
"args": ["redis-mcp", "--redis-host", "localhost", "--redis-port", "6379"],
"disabled": false
}
}
}
命令行参数
--redis-host: Redis 服务器主机 (默认: localhost)--redis-port: Redis 服务器端口 (默认: 6379)
通过 Smithery 安装
要通过 Smithery 自动安装适用于 Claude Desktop 的 Redis 服务器:
npx -y @smithery/cli install redis-mcp --client claude
开发
要添加一个新的 Redis 工具:
- 在
src/tools/目录下创建一个新的工具类,继承自RedisTool - 在
src/interfaces/types.ts中定义该工具的接口 - 在
src/tools/tool_registry.ts中注册该工具
示例工具实现:
export class MyTool extends RedisTool {
name = 'mytool';
description = 'Description of what the tool does';
inputSchema = {
type: 'object',
properties: {
// Define input parameters
},
required: ['requiredParam']
};
validateArgs(args: unknown): args is MyToolArgs {
// Implement argument validation
}
async execute(args: unknown, client: RedisClientType): Promise<ToolResponse> {
// Implement tool logic
}
}