MCP HubMCP Hub
prajwalnayak7

mcp-server-redis

by: prajwalnayak7

MCP server to interact with Redis Server, AWS Memory DB, etc for caching or other use-cases where in-memory and key-value based storage is appropriate

17created 02/02/2025
Visit
Redis
caching

πŸ“ŒOverview

Purpose: To provide a robust Redis server framework that facilitates easy integration and management of Redis functionalities for various use cases.

Overview: This framework simplifies interactions with Redis by offering organized resources and tools for basic and advanced operations. It allows users to establish a connection, manage keys, and perform data operations efficiently, enhancing Redis integration with ease of use and reliability.

Key Features:

  • Resource Management: Provides endpoints like redis://status for checking connection status, redis://info for server information, and redis://keys/{pattern} for listing keys, streamlining Redis resource access.

  • Comprehensive Toolset: Includes basic operations (get_value, set_value, delete_key), list operations (list_push, list_range), hash operations (hash_set, hash_get), set operations (set_add, set_members), and Pub/Sub functionality (publish_message), enabling diverse data manipulation capabilities.


Usage

The project structure is as follows:

mcp-server-redis/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py           # Main entry point
β”‚   β”œβ”€β”€ config.py         # Configuration
β”‚   β”œβ”€β”€ connection.py     # Redis connection management
β”‚   β”œβ”€β”€ resources/        # Resource implementations
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ status.py     # Connection status resources
β”‚   β”‚   └── keys.py       # Key-related resources
β”‚   └── tools/            # Tool implementations
β”‚       β”œβ”€β”€ __init__.py
β”‚       β”œβ”€β”€ basic.py      # Basic Redis operations
β”‚       β”œβ”€β”€ lists.py      # List operations
β”‚       β”œβ”€β”€ hashes.py     # Hash operations
β”‚       β”œβ”€β”€ sets.py       # Set operations
β”‚       └── pubsub.py     # Pub/Sub operations
β”œβ”€β”€ tests/                # Test directory
β”‚   └── __init__.py
β”œβ”€β”€ README.md
└── pyproject.toml

Setup

  1. Install required dependencies:
uv venv
source .venv/bin/activate
uv sync

(If not already installed, see https://docs.astral.sh/uv/)

  1. Configure your Redis connection by editing the .env.example file and adding the appropriate variables.

  2. Run the server:

python src/server.py

Server Capabilities

Resources

  • redis://status - Current connection status
  • redis://info - Redis server information
  • redis://keys/{pattern} - List keys matching a pattern

Tools

Basic operations:

  • get_value - Get a value
  • set_value - Set a value with optional expiry
  • delete_key - Delete a key
  • increment - Increment a numeric value

List operations:

  • list_push - Push values to a list
  • list_range - Get range of list values

Hash operations:

  • hash_set - Set hash fields
  • hash_get - Get hash fields

Set operations:

  • set_add - Add to a set
  • set_members - Get set members

Pub/Sub:

  • publish_message - Publish a message

Each tool includes error handling and returns meaningful responses. The server automatically reconnects to Redis if the connection is lost.

Testing and Installation

You can test the server with the MCP Inspector:

mcp dev src/server.py

Or install it in Claude Desktop:

mcp install src/server.py

Example Interaction

User: What's the current Redis connection status?
LLM: Let me check the Redis connection status for you.

[Reading resource redis://status]

The Redis server is currently connected and running on localhost:6379, database 0.

User: Can you store my name "Alice" in Redis?
LLM: I'll help you store your name in Redis using the set_value tool.

[Calling set_value with key="name" value="Alice"]
Successfully set key 'name'

I've stored your name "Alice" in Redis under the key "name". Would you like me to verify the value was stored correctly by retrieving it?

User: Yes please verify it
LLM: I'll retrieve the value using the get_value tool.

[Calling get_value with key="name"]
Alice

Yes, I can confirm that your name "Alice" was successfully stored in Redis and can be retrieved correctly. The value matches exactly what we stored.

This implementation provides a solid foundation for Redis integration through MCP. You can extend it by adding more Redis commands as needed for your specific use case.