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
π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
- Install required dependencies:
uv venv
source .venv/bin/activate
uv sync
(If not already installed, see https://docs.astral.sh/uv/)
-
Configure your Redis connection by editing the
.env.example
file and adding the appropriate variables. -
Run the server:
python src/server.py
Server Capabilities
Resources
redis://status
- Current connection statusredis://info
- Redis server informationredis://keys/{pattern}
- List keys matching a pattern
Tools
Basic operations:
get_value
- Get a valueset_value
- Set a value with optional expirydelete_key
- Delete a keyincrement
- Increment a numeric value
List operations:
list_push
- Push values to a listlist_range
- Get range of list values
Hash operations:
hash_set
- Set hash fieldshash_get
- Get hash fields
Set operations:
set_add
- Add to a setset_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.