fix: Ensure .env loaded before config init during mcp dev
startup
#30
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem Description
When attempting to start the service using
mcp dev mcp_clickhouse/mcp_server.py
, the followingValueError
occurs:This error appears even if the project's root
.env
file is correctly configured.Root Cause
The issue stems from the fact that the
mcp_env.py
file directly instantiates theClickHouseConfig
class at the module's top level (near the end of the file). Whenmcp_server.py
executesfrom mcp_clickhouse.mcp_env import ...
, it triggers the execution of code withinmcp_env.py
, including the immediate instantiation ofClickHouseConfig
.During its initialization (
__init__
),ClickHouseConfig
attempts to read the ClickHouse-related environment variables. However, at this point, theload_dotenv()
function inmcp_server.py
(responsible for loading variables from the.env
file) might not have executed yet. This results in missing environment variables, thus raising theValueError
.Solution
To resolve this issue and adopt a more robust pattern, this change introduces a Lazy Loading mechanism for the
ClickHouseConfig
instantiation:Modify
mcp_env.py
:config = ClickHouseConfig()
at the module level._CONFIG_INSTANCE
initialized toNone
.get_config()
function (using the standardglobal
keyword to modify the module-level_CONFIG_INSTANCE
). This function creates theClickHouseConfig
instance upon its first call, stores it in_CONFIG_INSTANCE
, and returns the cached instance on subsequent calls.Modify
mcp_server.py
:load_dotenv()
call happens after allimport
statements but before the configuration is potentially needed.mcp_clickhouse.mcp_env
to import theget_config
function instead of the (now removed)config
instance.create_clickhouse_client
function), call theget_config()
function to retrieve the configuration instance.Benefits
.env
are loaded before the configuration object attempts to access them during themcp dev
startup flow.import
block cleaner and follows standard Python practices regarding module initialization.How to Test
.env
file is correctly configured according toREADME.md
.mcp dev mcp_clickhouse/mcp_server.py
.ValueError
.