Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(mcp): read .env in the MCP server, after the shell-tool decision

alex3776bee0d934
diff --git a/agent/mcp_server.py b/agent/mcp_server.py
index d0268583b..1ebd25a82 100644
--- a/agent/mcp_server.py
+++ b/agent/mcp_server.py
@@ -3335,7 +3335,8 @@ def main():
action="store_true",
help="Register bash / background_run / cancel_background (OS process "
"control — RCE surface). OFF by default for every transport; equivalent "
- "to setting VIBE_TRADING_ENABLE_SHELL_TOOLS=1.",
+ "to setting VIBE_TRADING_ENABLE_SHELL_TOOLS=1 in the process environment "
+ "(.env is not consulted for this switch).",
)
args = parser.parse_args()
@@ -3349,6 +3350,11 @@ def main():
logging.getLogger(__name__).warning("Legacy state migration failed", exc_info=True)
_include_shell_tools = _resolve_include_shell_tools(args.enable_shell_tools)
+ # After the shell-tool decision: that switch must come from the launch
+ # flag or process env, never from a .env written for the web server.
+ from src.providers.llm import _ensure_dotenv
+
+ _ensure_dotenv()
_registry = None
_get_registry() # pre-warm: avoids deadlock when first tools/call lazy-inits inside FastMCP worker thread
diff --git a/agent/src/tools/path_utils.py b/agent/src/tools/path_utils.py
index 24c7003ef..3906b4296 100644
--- a/agent/src/tools/path_utils.py
+++ b/agent/src/tools/path_utils.py
@@ -27,8 +27,9 @@ _ALLOWED_WRITE_ROOTS_ENV = "VIBE_TRADING_ALLOWED_WRITE_ROOTS"
# MCP clients spawn the server themselves, so a shell export never reaches it.
_ENV_SCOPE_HINT = (
- "Under an MCP client, set it in that client's server env block — "
- "exporting it in a shell does not reach the spawned server."
+ "Put it in the runtime .env (read by both the web and MCP servers) or "
+ "in the MCP client's server env block — exporting it in a shell does not "
+ "reach a spawned MCP server."
)
# The env var that widens each resolve_safe_path purpose; rejections name it
diff --git a/agent/tests/test_mcp_server_dotenv.py b/agent/tests/test_mcp_server_dotenv.py
new file mode 100644
index 000000000..3ce710cb1
--- /dev/null
+++ b/agent/tests/test_mcp_server_dotenv.py
@@ -0,0 +1,30 @@
+"""mcp_server.main() reads .env, but never takes the shell-tool switch from it."""
+
+import os
+import sys
+
+import mcp_server
+from src.providers import llm
+
+
+def test_main_loads_dotenv_after_the_shell_tool_decision(tmp_path, monkeypatch):
+ env_file = tmp_path / ".env"
+ env_file.write_text(
+ "VIBE_TRADING_ENABLE_SHELL_TOOLS=1\n"
+ "MARKET_DATA_ORDER_HK_EQUITY=sentinel_from_dotenv\n",
+ encoding="utf-8",
+ )
+ monkeypatch.setattr(llm, "_ENV_CANDIDATES", (env_file,))
+ monkeypatch.setattr(llm, "_dotenv_loaded", False)
+ monkeypatch.delenv("VIBE_TRADING_ENABLE_SHELL_TOOLS", raising=False)
+ monkeypatch.delenv("MARKET_DATA_ORDER_HK_EQUITY", raising=False)
+ monkeypatch.setattr(sys, "argv", ["vibe-trading-mcp"])
+ monkeypatch.setattr(mcp_server, "_include_shell_tools", False)
+ monkeypatch.setattr(mcp_server, "_registry", None)
+ monkeypatch.setattr(mcp_server, "_get_registry", lambda: None)
+ monkeypatch.setattr(mcp_server.mcp, "run", lambda *a, **k: None)
+
+ mcp_server.main()
+
+ assert os.environ.get("MARKET_DATA_ORDER_HK_EQUITY") == "sentinel_from_dotenv"
+ assert mcp_server._include_shell_tools is False