Skip to content

Commit c320146

Browse files
committed
Fix Redis connection timing by deferring tool imports
- Move tool imports into _import_tools() function called after config reload - This ensures RedisConnectionManager singleton is never instantiated before config is set - Much cleaner solution than resetting singleton - prevents the problem instead of fixing it - Tools are imported after reload_redis_config() so they use the correct Redis configuration - Maintains backward compatibility with main() function
1 parent 0839247 commit c320146

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/common/connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ 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)
3536
redis_class: Type[Union[Redis, RedisCluster]] = redis.Redis
3637
connection_params = {
3738
"host": REDIS_CFG["host"],

src/main.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22
import os
33
import click
44

5-
from src.common.connection import RedisConnectionManager
65
from src.common.server import mcp
7-
import src.tools.server_management
8-
import src.tools.misc
9-
import src.tools.redis_query_engine
10-
import src.tools.hash
11-
import src.tools.list
12-
import src.tools.string
13-
import src.tools.json
14-
import src.tools.sorted_set
15-
import src.tools.set
16-
import src.tools.stream
17-
import src.tools.pub_sub
186
from src.common.config import MCP_TRANSPORT, parse_redis_uri, set_redis_env_from_config, reload_redis_config
197

208

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
22+
23+
2124
class RedisMCPServer:
2225
def __init__(self):
2326
print("Starting the Redis MCP Server", file=sys.stderr)
@@ -87,6 +90,9 @@ def cli(url, host, port, db, username, password,
8790
# Reload Redis configuration to pick up the new environment variables
8891
reload_redis_config()
8992

93+
# Import tools after configuration is set up (ensures Redis connection uses new config)
94+
_import_tools()
95+
9096
# Set MCP transport settings
9197
os.environ['MCP_TRANSPORT'] = mcp_transport
9298
os.environ['MCP_HOST'] = mcp_host
@@ -99,6 +105,8 @@ def cli(url, host, port, db, username, password,
99105

100106
def main():
101107
"""Legacy main function for backward compatibility."""
108+
# Import tools (uses default environment variables)
109+
_import_tools()
102110
server = RedisMCPServer()
103111
server.run()
104112

0 commit comments

Comments
 (0)