Skip to content

Commit dc02a5d

Browse files
committed
Remove environment variable logging and simplify configuration
- Remove set_redis_env_from_config function that was logging 'Setting REDIS_*' messages - Simplify configuration by using REDIS_CFG dictionary directly instead of environment variables - Remove MCP transport configuration options (MCP_TRANSPORT, MCP_HOST, MCP_PORT) - Update CLI to use stdio transport only - Refactor configuration management for better maintainability
1 parent c320146 commit dc02a5d

File tree

7 files changed

+46
-169
lines changed

7 files changed

+46
-169
lines changed

.env.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ REDIS_SSL_KEYFILE=/path/to/key.pem
99
REDIS_SSL_CERTFILE=/path/to/cert.pem
1010
REDIS_CERT_REQS=required
1111
REDIS_CA_CERTS=/path/to/ca_certs.pem
12-
REDIS_CLUSTER_MODE=False
13-
MCP_TRANSPORT=stdio
12+
REDIS_CLUSTER_MODE=False

.idea/AugmentWebviewStateStore.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,6 @@ uvx --from git+https://github.com/redis/mcp-redis.git@feature/uvx-cli-support re
118118
- `--ssl-keyfile` - Path to SSL key file
119119
- `--ssl-certfile` - Path to SSL certificate file
120120
- `--cluster-mode` - Enable Redis cluster mode
121-
- `--mcp-transport` - MCP transport method (stdio, streamable-http, sse)
122-
- `--mcp-host` - MCP server host (default: 127.0.0.1)
123-
- `--mcp-port` - MCP server port (default: 8000)
124121

125122
### Configuration via Environment Variables
126123

@@ -140,9 +137,7 @@ When running the server directly (`uv run src/main.py`) or for legacy compatibil
140137
| `REDIS_CERT_REQS` | Whether the client should verify the server's certificate | `"required"` |
141138
| `REDIS_CA_CERTS` | Path to the trusted CA certificates file | None |
142139
| `REDIS_CLUSTER_MODE` | Enable Redis Cluster mode | `False` |
143-
| `MCP_TRANSPORT` | Use the `stdio`, `streamable-http` or `sse` transport | `stdio` |
144-
| `MCP_HOST` | Server host when `streamable-http` or `sse` are set | `127.0.0.1` |
145-
| `MCP_PORT` | Server port when `streamable-http` or `sse` are set | `8000` |
140+
146141

147142

148143
There are several ways to set environment variables:
@@ -170,71 +165,6 @@ OR,
170165
```
171166
This method is useful for temporary overrides or quick testing.
172167

173-
## Transports
174-
175-
This MCP server can be configured to handle requests locally, running as a process and communicating with the MCP client via `stdin` and `stdout`.
176-
This is the default configuration, `stdio`. The `streamable-http` and `sse` (deprecated) transports are also configurable, which make the server available over the network.
177-
Configure the `MCP_TRANSPORT` variable accordingly.
178-
179-
> Authentication has not yet been implemented, and [attackers could use DNS rebinding](https://modelcontextprotocol.io/docs/concepts/transports#security-considerations) to access the server.
180-
181-
### Streamable HTTP
182-
183-
```commandline
184-
export MCP_TRANSPORT="streamable-http"
185-
```
186-
187-
Then start the server.
188-
189-
```commandline
190-
uv run src/main.py
191-
```
192-
193-
Configure in GitHub Copilot
194-
195-
```commandline
196-
"mcp": {
197-
"servers": {
198-
"redis-mcp": {
199-
"type": "http",
200-
"url": "http://127.0.0.1:8000/mcp/"
201-
},
202-
}
203-
},
204-
```
205-
206-
### SSE (deprecated)
207-
208-
```commandline
209-
export MCP_TRANSPORT="sse"
210-
```
211-
212-
Then start the server.
213-
214-
```commandline
215-
uv run src/main.py
216-
```
217-
218-
Test the server:
219-
220-
```commandline
221-
curl -i http://127.0.0.1:8000/sse
222-
HTTP/1.1 200 OK
223-
```
224-
225-
Integrate with your favorite tool or client. The VS Code configuration for GitHub Copilot is:
226-
227-
```commandline
228-
"mcp": {
229-
"servers": {
230-
"redis-mcp": {
231-
"type": "sse",
232-
"url": "http://127.0.0.1:8000/sse"
233-
},
234-
}
235-
},
236-
```
237-
238168

239169
## Integration with OpenAI Agents SDK
240170

src/common/config.py

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,23 @@
1+
import sys
2+
13
from dotenv import load_dotenv
24
import os
35
import urllib.parse
46

57
load_dotenv()
68

7-
MCP_TRANSPORT = os.getenv('MCP_TRANSPORT', 'stdio')
8-
MCP_HOST = os.getenv('MCP_HOST', '127.0.0.1')
9-
MCP_PORT = os.getenv('MCP_PORT', 8000)
10-
11-
def _load_redis_config():
12-
"""Load Redis configuration from environment variables."""
13-
return {"host": os.getenv('REDIS_HOST', '127.0.0.1'),
14-
"port": int(os.getenv('REDIS_PORT',6379)),
15-
"username": os.getenv('REDIS_USERNAME', None),
16-
"password": os.getenv('REDIS_PWD',''),
17-
"ssl": os.getenv('REDIS_SSL', False) in ('true', '1', 't'),
18-
"ssl_ca_path": os.getenv('REDIS_SSL_CA_PATH', None),
19-
"ssl_keyfile": os.getenv('REDIS_SSL_KEYFILE', None),
20-
"ssl_certfile": os.getenv('REDIS_SSL_CERTFILE', None),
21-
"ssl_cert_reqs": os.getenv('REDIS_SSL_CERT_REQS', 'required'),
22-
"ssl_ca_certs": os.getenv('REDIS_SSL_CA_CERTS', None),
23-
"cluster_mode": os.getenv('REDIS_CLUSTER_MODE', False) in ('true', '1', 't'),
24-
"db": int(os.getenv('REDIS_DB', 0))}
25-
26-
REDIS_CFG = _load_redis_config()
27-
28-
29-
def reload_redis_config():
30-
"""Reload Redis configuration from environment variables."""
31-
global REDIS_CFG
32-
REDIS_CFG = _load_redis_config()
33-
9+
REDIS_CFG = {"host": os.getenv('REDIS_HOST', '127.0.0.1'),
10+
"port": int(os.getenv('REDIS_PORT',6379)),
11+
"username": os.getenv('REDIS_USERNAME', None),
12+
"password": os.getenv('REDIS_PWD',''),
13+
"ssl": os.getenv('REDIS_SSL', False) in ('true', '1', 't'),
14+
"ssl_ca_path": os.getenv('REDIS_SSL_CA_PATH', None),
15+
"ssl_keyfile": os.getenv('REDIS_SSL_KEYFILE', None),
16+
"ssl_certfile": os.getenv('REDIS_SSL_CERTFILE', None),
17+
"ssl_cert_reqs": os.getenv('REDIS_SSL_CERT_REQS', 'required'),
18+
"ssl_ca_certs": os.getenv('REDIS_SSL_CA_CERTS', None),
19+
"cluster_mode": os.getenv('REDIS_CLUSTER_MODE', False) in ('true', '1', 't'),
20+
"db": int(os.getenv('REDIS_DB', 0))}
3421

3522
def parse_redis_uri(uri: str) -> dict:
3623
"""Parse a Redis URI and return connection parameters."""
@@ -81,7 +68,12 @@ def parse_redis_uri(uri: str) -> dict:
8168
if 'ssl_certfile' in query_params:
8269
config['ssl_certfile'] = query_params['ssl_certfile'][0]
8370

84-
# Handle other parameters
71+
# Handle other parameters. According to https://www.iana.org/assignments/uri-schemes/prov/redis,
72+
# The database number to use for the Redis SELECT command comes from
73+
# either the "db-number" portion of the URI (described in the previous
74+
# section) or the value from the key-value pair from the "query" URI
75+
# field with the key "db". If neither of these are present, the
76+
# default database number is 0.
8577
if 'db' in query_params:
8678
try:
8779
config['db'] = int(query_params['db'][0])
@@ -91,27 +83,8 @@ def parse_redis_uri(uri: str) -> dict:
9183
return config
9284

9385

94-
def set_redis_env_from_config(config: dict):
95-
"""Set environment variables from Redis configuration."""
96-
env_mapping = {
97-
'host': 'REDIS_HOST',
98-
'port': 'REDIS_PORT',
99-
'db': 'REDIS_DB',
100-
'username': 'REDIS_USERNAME',
101-
'password': 'REDIS_PWD',
102-
'ssl': 'REDIS_SSL',
103-
'ssl_ca_path': 'REDIS_SSL_CA_PATH',
104-
'ssl_keyfile': 'REDIS_SSL_KEYFILE',
105-
'ssl_certfile': 'REDIS_SSL_CERTFILE',
106-
'ssl_cert_reqs': 'REDIS_SSL_CERT_REQS',
107-
'ssl_ca_certs': 'REDIS_SSL_CA_CERTS',
108-
'cluster_mode': 'REDIS_CLUSTER_MODE'
109-
}
110-
111-
for key, env_var in env_mapping.items():
112-
if key in config:
113-
value = config[key]
114-
if isinstance(value, bool):
115-
value = 'true' if value else 'false'
116-
os.environ[env_var] = str(value)
117-
print(f"Setting {env_var} to {value}")
86+
def set_redis_config_from_cli(config: dict):
87+
for key, value in config.items():
88+
if isinstance(value, bool):
89+
value = 'true' if value else 'false'
90+
REDIS_CFG[key] = str(value)

src/common/connection.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def get_connection(cls, decode_responses=True) -> Redis:
3232
"max_connections_per_node": 10
3333
}
3434
else:
35-
print("Redis config:", REDIS_CFG, file=sys.stderr)
3635
redis_class: Type[Union[Redis, RedisCluster]] = redis.Redis
3736
connection_params = {
3837
"host": REDIS_CFG["host"],

src/common/server.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from mcp.server.fastmcp import FastMCP
22

3-
from src.common.config import MCP_PORT, MCP_HOST
4-
53
# Initialize FastMCP server
64
mcp = FastMCP(
75
"Redis MCP Server",
8-
host=MCP_HOST,
9-
port=MCP_PORT,
106
dependencies=["redis", "dotenv", "numpy"]
117
)
128

src/main.py

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
import sys
2-
import os
32
import click
4-
3+
from src.common.connection import RedisConnectionManager
54
from src.common.server import mcp
6-
from src.common.config import MCP_TRANSPORT, parse_redis_uri, set_redis_env_from_config, reload_redis_config
7-
8-
9-
def _import_tools():
10-
"""Import all tool modules after configuration is set up."""
11-
import src.tools.server_management
12-
import src.tools.misc
13-
import src.tools.redis_query_engine
14-
import src.tools.hash
15-
import src.tools.list
16-
import src.tools.string
17-
import src.tools.json
18-
import src.tools.sorted_set
19-
import src.tools.set
20-
import src.tools.stream
21-
import src.tools.pub_sub
5+
from src.common.config import parse_redis_uri, set_redis_config_from_cli
6+
import src.tools.server_management
7+
import src.tools.misc
8+
import src.tools.redis_query_engine
9+
import src.tools.hash
10+
import src.tools.list
11+
import src.tools.string
12+
import src.tools.json
13+
import src.tools.sorted_set
14+
import src.tools.set
15+
import src.tools.stream
16+
import src.tools.pub_sub
2217

2318

2419
class RedisMCPServer:
2520
def __init__(self):
2621
print("Starting the Redis MCP Server", file=sys.stderr)
2722

2823
def run(self):
29-
mcp.run(transport=MCP_TRANSPORT)
24+
mcp.run()
3025

3126

3227
@click.command()
@@ -43,20 +38,16 @@ def run(self):
4338
@click.option('--ssl-cert-reqs', default='required', help='SSL certificate requirements')
4439
@click.option('--ssl-ca-certs', help='Path to CA certificates file')
4540
@click.option('--cluster-mode', is_flag=True, help='Enable Redis cluster mode')
46-
@click.option('--mcp-transport', default='stdio', type=click.Choice(['stdio', 'streamable-http', 'sse']), help='MCP transport method')
47-
@click.option('--mcp-host', default='127.0.0.1', help='MCP server host (for http/sse transport)')
48-
@click.option('--mcp-port', default=8000, type=int, help='MCP server port (for http/sse transport)')
4941
def cli(url, host, port, db, username, password,
5042
ssl, ssl_ca_path, ssl_keyfile, ssl_certfile,
51-
ssl_cert_reqs, ssl_ca_certs, cluster_mode,
52-
mcp_transport, mcp_host, mcp_port):
43+
ssl_cert_reqs, ssl_ca_certs, cluster_mode):
5344
"""Redis MCP Server - Model Context Protocol server for Redis."""
5445

5546
# Handle Redis URI if provided
5647
if url:
5748
try:
5849
uri_config = parse_redis_uri(url)
59-
set_redis_env_from_config(uri_config)
50+
set_redis_config_from_cli(uri_config)
6051
except ValueError as e:
6152
click.echo(f"Error parsing Redis URI: {e}", err=True)
6253
sys.exit(1)
@@ -85,18 +76,9 @@ def cli(url, host, port, db, username, password,
8576
if ssl_ca_certs:
8677
config['ssl_ca_certs'] = ssl_ca_certs
8778

88-
set_redis_env_from_config(config)
89-
90-
# Reload Redis configuration to pick up the new environment variables
91-
reload_redis_config()
92-
93-
# Import tools after configuration is set up (ensures Redis connection uses new config)
94-
_import_tools()
79+
set_redis_config_from_cli(config)
9580

96-
# Set MCP transport settings
97-
os.environ['MCP_TRANSPORT'] = mcp_transport
98-
os.environ['MCP_HOST'] = mcp_host
99-
os.environ['MCP_PORT'] = str(mcp_port)
81+
# RedisConnectionManager.get_connection().ping()
10082

10183
# Start the server
10284
server = RedisMCPServer()
@@ -105,8 +87,6 @@ def cli(url, host, port, db, username, password,
10587

10688
def main():
10789
"""Legacy main function for backward compatibility."""
108-
# Import tools (uses default environment variables)
109-
_import_tools()
11090
server = RedisMCPServer()
11191
server.run()
11292

0 commit comments

Comments
 (0)