redis-mcp-server

wfqdreamcity/redis-mcp-server

3.2

If you are the rightful owner of redis-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

This is an MCP server designed for interacting with Redis databases, offering a comprehensive suite of Redis operation tools and resources.

Tools
  1. redis_get

    Retrieve the value of a key.

  2. redis_set

    Set a key-value pair with optional expiration.

  3. redis_del

    Delete one or more keys.

  4. redis_exists

    Check if a key exists.

  5. redis_keys

    Get a list of keys matching a pattern.

  6. redis_hget

    Get the value of a hash field.

  7. redis_hset

    Set the value of a hash field.

  8. redis_hgetall

    Get all fields and values of a hash.

  9. redis_lpush

    Push elements to the left of a list.

  10. redis_rpush

    Push elements to the right of a list.

  11. redis_lrange

    Get elements within a range from a list.

  12. redis_info

    Retrieve information about the Redis server.

Redis MCP Server

这是一个用于与Redis数据库交互的MCP(Model Context Protocol)服务器。它提供了丰富的Redis操作工具和资源。

功能特性

工具 (Tools)

  • redis_get - 获取键的值
  • redis_set - 设置键值对(支持过期时间)
  • redis_del - 删除一个或多个键
  • redis_exists - 检查键是否存在
  • redis_keys - 获取匹配模式的键列表
  • redis_hget - 获取哈希字段值
  • redis_hset - 设置哈希字段值
  • redis_hgetall - 获取哈希的所有字段和值
  • redis_lpush - 向列表左侧推入元素
  • redis_rpush - 向列表右侧推入元素
  • redis_lrange - 获取列表范围内的元素
  • redis_info - 获取Redis服务器信息

资源 (Resources)

  • redis://info - Redis服务器基本信息
  • redis://stats - Redis服务器统计信息

环境变量配置

基本连接配置

  • REDIS_HOST - Redis主机地址(默认: localhost)
  • REDIS_PORT - Redis端口(默认: 6379)
  • REDIS_PASSWORD - Redis密码(可选)
  • REDIS_DB - Redis数据库编号(默认: 0)

SSL/TLS配置

  • REDIS_TLS - 启用TLS连接(设置为 'true' 启用,默认: false)
  • REDIS_TLS_CERT - 客户端证书文件路径(可选)
  • REDIS_TLS_KEY - 客户端私钥文件路径(可选)
  • REDIS_TLS_CA - CA证书文件路径(可选)
  • REDIS_TLS_REJECT_UNAUTHORIZED - 是否验证服务器证书(默认: true,设置为 'false' 禁用验证)

安装和使用

  1. 确保Redis服务器正在运行:

    sudo systemctl start redis-server
    redis-cli ping  # 应该返回 PONG
    
  2. 构建项目:

    npm install
    npm run build
    
  3. 配置已自动添加到MCP设置文件中,服务器将自动启动。

使用示例

基本Redis操作

一旦MCP服务器运行,您可以使用以下命令与Redis交互:

  • 设置键值对:redis_set 工具,参数 {"key": "mykey", "value": "myvalue"}
  • 获取键值:redis_get 工具,参数 {"key": "mykey"}
  • 获取所有键:redis_keys 工具,参数 {"pattern": "*"}
  • 设置哈希:redis_hset 工具,参数 {"key": "myhash", "field": "field1", "value": "value1"}
  • 获取哈希:redis_hgetall 工具,参数 {"key": "myhash"}

SSL/TLS连接示例

要连接到启用了SSL/TLS的Redis服务器,请在MCP配置中设置相应的环境变量:

{
  "mcpServers": {
    "redis-server": {
      "command": "node",
      "args": ["/home/zx/Cline/MCP/redis-server/build/index.js"],
      "env": {
        "REDIS_HOST": "your-redis-host.com",
        "REDIS_PORT": "6380",
        "REDIS_TLS": "true",
        "REDIS_TLS_CA": "/path/to/ca-cert.pem",
        "REDIS_TLS_CERT": "/path/to/client-cert.pem",
        "REDIS_TLS_KEY": "/path/to/client-key.pem",
        "REDIS_PASSWORD": "your-password"
      }
    }
  }
}

对于自签名证书或测试环境,可以禁用证书验证:

{
  "env": {
    "REDIS_TLS": "true",
    "REDIS_TLS_REJECT_UNAUTHORIZED": "false"
  }
}

项目结构

redis-server/
├── package.json          # 项目配置
├── tsconfig.json         # TypeScript配置
├── src/
│   └── index.ts          # 主服务器实现
├── build/
│   └── index.js          # 编译后的JavaScript文件
└── README.md             # 本文档

开发

  • 开发模式:npm run watch
  • 构建:npm run build
  • 调试:npm run inspector

注意事项

  • 确保Redis服务器在使用前已启动
  • 默认连接到本地Redis实例(localhost:6379)
  • 支持通过环境变量自定义连接参数
  • 所有操作都有适当的错误处理