Skip to content

Commit 7f0805c

Browse files
authored
fix: Ensure .env loaded before config init during mcp dev startup (#30)
1 parent 1dc0c48 commit 7f0805c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

mcp_clickhouse/mcp_env.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,17 @@ def _validate_required_vars(self) -> None:
138138
)
139139

140140

141-
# Global instance for easy access
142-
config = ClickHouseConfig()
141+
# Global instance placeholder for the singleton pattern
142+
_CONFIG_INSTANCE = None
143+
144+
145+
def get_config():
146+
"""
147+
Gets the singleton instance of ClickHouseConfig.
148+
Instantiates it on the first call.
149+
"""
150+
global _CONFIG_INSTANCE
151+
if _CONFIG_INSTANCE is None:
152+
# Instantiate the config object here, ensuring load_dotenv() has likely run
153+
_CONFIG_INSTANCE = ClickHouseConfig()
154+
return _CONFIG_INSTANCE

mcp_clickhouse/mcp_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from dotenv import load_dotenv
99
from mcp.server.fastmcp import FastMCP
1010

11-
from mcp_clickhouse.mcp_env import config
11+
from mcp_clickhouse.mcp_env import get_config
1212

1313
MCP_SERVER_NAME = "mcp-clickhouse"
1414

@@ -161,7 +161,7 @@ def run_select_query(query: str):
161161

162162

163163
def create_clickhouse_client():
164-
client_config = config.get_client_config()
164+
client_config = get_config().get_client_config()
165165
logger.info(
166166
f"Creating ClickHouse client connection to {client_config['host']}:{client_config['port']} "
167167
f"as {client_config['username']} "

0 commit comments

Comments
 (0)