Redis交互服务器

MCP Redis服务器是一个动态连接和交互式工具集,用于管理和操作Redis数据库,支持所有主要Redis操作。

  • by efeabio
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 efeabio/mcp-redis-server

使用说明

MCP Redis服务器

用于与 Redis 交互的 MCP(模型上下文协议)服务器,具有动态和交互式连接系统。

特征

  • 动态连接无需预先配置连接
  • 工具交互需要时请求连接数据
  • 国家统计局: 执行操作前检查连接
  • 全面支持 Redis所有主要 Redis 操作

Como Usar

1. 启动服务器

./start-mcp.sh

2. 设置连接

重要的在使用任何 Redis 工具之前,您必须使用:

# Tool: redis_configure_connection
# Parâmetros:
# - host: Host do Redis (ex: localhost, 127.0.0.1)
# - port: Porta do Redis (padrão: 6379)
# - password: Senha do Redis (deixe vazio se não houver)
# - database: Database inicial (0-15, padrão: 0)

# Exemplo:
await redis_configure_connection(
    host="localhost",
    port=6379,
    password="",
    database=0
)

3. 检查连接状态

# Verificar se está conectado
await redis_check_connection()

# Obter status detalhado
await redis_get_connection_status()

4. 使用 Redis 工具

设置连接后,您可以使用所有可用的工具:

  • 服务器信息: redis_get_server_info()
  • 记忆体统计: redis_get_memory_stats()
  • Keyspace 信息: redis_get_keyspace_info()
  • 一般统计: redis_get_stats()
  • 客户信息: redis_get_clients_info()
  • 设置: redis_get_config()
  • 密钥管理: redis_list_keys(), redis_get_key_type()等等。
  • 数据库管理: redis_select_database(), redis_create_key()等等。

推荐使用流程

  1. 启动 MCP 服务器
  2. 配置连接 com redis_configure_connection()
  3. 检查状态 com redis_check_connection()
  4. 用作工具 根据需要
  5. 检查连接 必要时定期

错误处理

所有工具在运行前都会自动检查是否存在活动连接。如果没有连接,您将收到一个明确的错误,表明您应该使用 redis_configure_connection() 首先。

完整使用范例

# 1. Configurar conexão
connection_result = await redis_configure_connection(
    host="localhost",
    port=6379,
    password="",
    database=0
)

# 2. Verificar status
status = await redis_check_connection()

# 3. Usar tools
if status["connected"]:
    # Obter informações do servidor
    server_info = await redis_get_server_info()
    
    # Listar chaves
    keys = await redis_list_keys(pattern="*", limit=10)
    
    # Obter estatísticas
    stats = await redis_get_stats()

开发

项目结构

src/
├── tools/
│   ├── connection.py      # Tools de conexão
│   ├── server_info.py     # Informações do servidor
│   ├── memory.py          # Estatísticas de memória
│   ├── keyspace.py        # Informações do keyspace
│   ├── stats.py           # Estatísticas gerais
│   ├── clients.py         # Informações dos clientes
│   ├── config.py          # Configurações
│   ├── keys.py            # Gerenciamento de chaves
│   └── database.py        # Gerenciamento de databases
├── utils/
│   ├── redis_connector.py # Conector Redis principal
│   └── logger.py          # Sistema de logging
└── core/
    └── exceptions.py      # Exceções personalizadas

测试

# Executar testes unitários
pytest tests/unit/

# Com cobertura
pytest --cov=src tests/unit/

要求

  • Python 3.8+
  • Redis服务器
  • 依赖列在 requirements.txt

安装

# Criar ambiente virtual
python -m venv venv
source venv/bin/activate  # Linux/Mac
# ou
venv\Scripts\activate     # Windows

# Instalar dependências
pip install -r requirements.txt

# Para desenvolvimento
pip install -r requirements-dev.txt

编辑器设置

克劳德代码

  1. 添加 MCP 服务器 :
# Na pasta do projeto, adicionar o script de inicialização
claude mcp add /caminho/completo/para/o/projeto/redis/start-mcp.sh
  1. 检查是否已添加 :
claude mcp list
  1. 使用服务器 :
    • O克劳德代码检测器自动为MCP服务
    • Redis工具将在聊天中可用
    • 直接使用工具,如 redis_configure_connection()

光标IDE

  1. 配置器MCP无光标:
    • 打开光标设置( Cmd/Ctrl + ,)
    • 在设置中搜索“MCP”
    • 添加服务器配置 :
{
  "mcpServers": {
    "redis-mcp": {
      "command": "/caminho/completo/para/o/projeto/redis/start-mcp.sh"
    }
  }
}
  1. 使用服务器 :
    • 光标将自动检测到 MCP 服务器
    • 使用 @redis-mcp 在聊天中与工具进行交互
    • 工具将出现在可用工具列表中