Content
# Redis MCP Server
A Redis database operation service based on the MCP (Model Context Protocol) protocol, allowing AI assistants to operate Redis through the standard MCP protocol.
## Features
- **Low Resource Usage**: Compiled with Go, memory usage < 20MB, millisecond-level startup
- **stdio Transmission**: In-process communication, no need to expose ports, zero network overhead
- **Security and Controllability**: Supports read-only mode, safe for production environments
- **Single-File Deployment**: Compiled into a single binary file, no runtime dependencies required
- **20 Built-in Tools**: Covers common Redis operations, complex commands can be called through the `execute` tool
- **Pipeline Batching**: `batch_execute` supports batch execution of up to 50 commands
## Tool List
| Category | Tool | Description |
|------|------|------|
| String | `get` | Get key value |
| | `set` | Set key value (supports EX/NX) |
| | `del` | Delete key |
| Hash | `hgetall` | Get entire hash |
| | `hmset` | Batch set hash fields |
| List | `lrange` | Get range elements |
| | `lpush` | Push to left |
| | `lpop` | Pop from left |
| Set | `smembers` | Get all members |
| | `sadd` | Add member |
| Sorted Set | `zrange` | Get by rank (supports WITHSCORES) |
| | `zadd` | Add member with score |
| Key Operation | `keys` | SCAN way to find (non-blocking) |
| | `expire` | Set expiration time |
| | `ttl` | Get remaining survival time |
| General | `execute` | Execute arbitrary Redis command |
| | `batch_execute` | Pipeline batch execution |
| Server | `info` | Server information |
| | `select` | Switch database (0-15) |
| | `ping` | Test connection |
## Quick Start
### Environment Requirements
- Go 1.22+
- Redis 6.0+
### Installation
```bash
# Clone repository
git clone <repo-url>
cd redis-mcp
# Compile
go build -ldflags "-X main.buildVersion=1.0.0 -X 'main.buildTime=$(date +%F)'" -o redis-mcp .
# Copy default configuration
cp .env.example .env
```
### Configuration
Edit `.env` file:
```env
REDIS_HOST=localhost # Redis address
REDIS_PORT=6379 # Redis port
REDIS_PASSWORD= # Redis password (optional)
REDIS_DB=0 # Database index
REDIS_READONLY=false # Read-only mode (recommended true for production)
```
### Configure to AI Client
The program communicates with the AI client through stdio, directly declaring it as a subprocess in the client's MCP configuration:
```json
{
"mcpServers": {
"redis": {
"command": "D:\\path\\to\\redis-mcp.exe",
"args": [],
"env": {
"REDIS_HOST": "localhost",
"REDIS_PORT": "6379",
"REDIS_PASSWORD": "",
"REDIS_DB": "0",
"REDIS_READONLY": "false"
}
}
}
}
```
> **Tip**: You can also skip `env` in the client configuration and place the `.env` file in the same directory as `redis-mcp.exe`, which will be automatically loaded when the program starts.
## Debugging
```bash
# Development mode: go vet check + compile
go vet ./... && go build -o redis-mcp .
# View dependencies
go mod tidy
```
## Environment Variable Reference
| Variable | Default Value | Description |
|------|--------|------|
| `REDIS_HOST` | `localhost` | Redis server address |
| `REDIS_PORT` | `6379` | Redis port |
| `REDIS_PASSWORD` | (empty) | Authentication password |
| `REDIS_DB` | `0` | Database number |
| `REDIS_READONLY` | `false` | Set to `true` to allow only read operations |
## Security Recommendations
1. **Production Environment**: Set `REDIS_READONLY=true` to prevent AI from executing write operations by mistake
2. **Authentication**: Set `REDIS_PASSWORD` обязательно
3. **Environment Variables**: Declare passwords in the client's configuration `env` for better security than `.env` files
## Project Structure
```
redis-mcp/
├── main.go # Entry file
├── go.mod / go.sum # Go module
├── .env.example # Environment variable template
├── redis-mcp-service.xml # WinSW service configuration file (only required for streamable-http mode)
├── internal/
│ ├── config/config.go # Configuration loading
│ ├── redis/
│ │ ├── client.go # Redis connection management
│ │ └── executor.go # Command executor
│ └── tools/register.go # MCP tool registration (20 tools)
```
## Future Plans
- [ ] Redis Cluster mode support
- [ ] Redis Sentinel mode support
- [ ] TLS/SSL connection
- [ ] Connection pool optimization
- [ ] streamable-http mode (switchable as needed)
## License
MIT