@wenit/redis-mcp-server
v1.0.3
Published
Redis MCP Server - 通过 MCP 协议操作 Redis 数据库
Maintainers
Readme
Redis MCP Server
简体中文 | English
让 AI 安全高效地操作 Redis。通过 MCP 协议提供18 个精心设计的工具,在仅消耗 ~1,600 tokens(GPT-4 上下文的 1.2%)的情况下,覆盖 90% 的 Redis 使用场景。
为什么选择这个工具?
| 痛点 | 传统方案 | Redis MCP Server |
|------|----------|------------------|
| 上下文占用 | 60+ 工具,~5,000+ tokens | 18 工具,~1,600 tokens ✅ |
| 生产安全 | KEYS 命令阻塞服务器 | SCAN 实现,永不阻塞 ✅ |
| 灵活性 | 新增命令需等待更新 | execute 万能工具,即查即用 ✅ |
| 高可用 | 仅支持单机 | Cluster/Sentinel/SSL 全支持 ✅ |
一分钟上手
npm install && npm run build# .env
REDIS_HOST=localhost
REDIS_PORT=6379npm start核心工具(18个)
数据类型操作(12个)
| 类型 | 工具 | 用途 |
|------|------|------|
| 字符串 | get / set / del | 缓存、Session |
| 哈希 | hgetall / hmset | 用户信息、配置存储 |
| 列表 | lrange / lpush / lpop | 消息队列、任务队列 |
| 集合 | smembers / sadd | 标签、去重 |
| 有序集合 | zrange / zadd | 排行榜、延时队列 |
管理与扩展(6个)
| 工具 | 亮点 |
|------|------|
| keys | SCAN 实现,大数据量不阻塞 |
| expire / ttl | 过期时间管理 |
| execute | 万能工具,执行任意 Redis 命令 |
| batch_execute | Pipeline 批量,性能提升 10x+ |
| info / select | 服务管理 |
使用示例
基础操作
// 缓存(带过期时间)
set({ key: "session:123", value: "user_data", ex: 3600 })
// 用户信息存储
hmset({ key: "user:1", fields: { name: "张三", age: "25" } })
hgetall({ key: "user:1" })
// 任务队列
lpush({ key: "queue:tasks", values: ["task1", "task2"] })
lpop({ key: "queue:tasks" })
// 排行榜
zadd({ key: "rank:daily", members: [{ member: "user1", score: 100 }] })
zrange({ key: "rank:daily", start: 0, stop: 9, withscores: true })万能工具 - execute
当 18 个工具不够用时,使用 execute 执行任意命令:
// 原子递增
execute({ command: "hincrby", args: ["user:1", "views", 1] })
// 按分数范围查询
execute({ command: "zrangebyscore", args: ["rank", 100, 200] })
// 事务操作
execute({ command: "multi" })
execute({ command: "incr", args: ["counter"] })
execute({ command: "exec" })批量操作(Pipeline)
// 一次往返执行多个命令,性能提升 10x+
batch_execute({
commands: [
{ command: "get", args: ["key1"] },
{ command: "hgetall", args: ["hash1"] },
{ command: "zrange", args: ["rank", 0, 9] }
]
})安全地查找键
// 使用 SCAN 实现,大数据量不会阻塞服务器
keys({ pattern: "user:*" })生产环境配置
只读模式(安全)
REDIS_READONLY=true禁止写入操作,只允许查询,适合生产数据分析。
Cluster 集群
REDIS_CLUSTER=true
REDIS_CLUSTER_NODES=node1:6379,node2:6379,node3:6379Sentinel 高可用
REDIS_SENTINEL=true
REDIS_SENTINEL_MASTER_NAME=mymaster
REDIS_SENTINEL_NODES=s1:26379,s2:26379,s3:26379SSL/TLS 加密
REDIS_TLS_CA=/path/to/ca.pem
REDIS_TLS_CERT=/path/to/client-cert.pem
REDIS_TLS_KEY=/path/to/client-key.pem在 Claude 中使用
VS Code 配置
在 .vscode/settings.json 中添加:
{
"mcpServers": {
"redis": {
"command": "npx",
"args": ["-y", "@wenit/redis-mcp-server"],
"env": {
"REDIS_HOST": "localhost",
"REDIS_PORT": "6379",
"REDIS_PASSWORD": "",
"REDIS_DB": "0"
}
}
}
}本地开发配置
{
"mcpServers": {
"redis": {
"command": "node",
"args": ["/path/to/redis-mcp-server/dist/index.js"],
"env": {
"REDIS_HOST": "localhost",
"REDIS_PORT": "6379",
"REDIS_PASSWORD": ""
}
}
}
}故障排查
// 测试连接
ping({})
// 查看服务器信息
info({ section: "server" })
// 查看内存使用
info({ section: "memory" })
// 查看慢查询
execute({ command: "slowlog", args: ["get", 10] })许可证
MIT
