Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(tools): name the allowed roots when a path is refused

Haozhe Wu539b9ab69cdc
diff --git a/README.md b/README.md
index 1e548b17d..c7cab5434 100644
--- a/README.md
+++ b/README.md
@@ -1052,6 +1052,19 @@ Each fire runs the `prompt` through a fresh agent session (optional backtest par
Vibe-Trading exposes 54 MCP tools for any MCP-compatible client. Runs as a stdio subprocess — no server setup needed. Core research tools work with zero API keys for HK/US/crypto; trading connector tools use the selected connector profile, and `run_swarm` needs an LLM key.
+**Environment variables:** the client spawns the server itself, so a shell `export` never reaches it — set them in the client's `env` block. Generated backtest code is sandboxed to the allowed run roots, so writing results into a workspace of your own needs `VIBE_TRADING_ALLOWED_RUN_ROOTS`:
+
+```json
+{
+ "mcpServers": {
+ "vibe-trading": {
+ "command": "vibe-trading-mcp",
+ "env": { "VIBE_TRADING_ALLOWED_RUN_ROOTS": "C:\\Users\\me\\research" }
+ }
+ }
+}
+```
+
<details>
<summary><b>Claude Desktop</b></summary>
diff --git a/README_ar.md b/README_ar.md
index 4707f4693..97009657e 100644
--- a/README_ar.md
+++ b/README_ar.md
@@ -991,6 +991,19 @@ curl -X DELETE http://localhost:8899/scheduled-runs/<job_id>
يعرض Vibe-Trading 54 أداة MCP لأي عميل متوافق مع MCP. يعمل كعملية stdio فرعية، دون إعداد خادم. أدوات البحث الأساسية تعمل دون أي مفاتيح API لأسواق HK/US/crypto؛ وأدوات connector للتداول تستخدم profile الموصل المختار، ويحتاج `run_swarm` وحده إلى مفتاح LLM.
+**متغيرات البيئة:** العميل هو من يشغّل الخادم بنفسه، لذا لا يصل إليه `export` من الـ shell أبداً —— اضبطها في كتلة `env` الخاصة بالعميل. كود الاختبار الخلفي المولَّد محصور ضمن جذور التشغيل المسموح بها، لذا تحتاج إلى `VIBE_TRADING_ALLOWED_RUN_ROOTS` لكتابة النتائج في دليل عمل خاص بك:
+
+```json
+{
+ "mcpServers": {
+ "vibe-trading": {
+ "command": "vibe-trading-mcp",
+ "env": { "VIBE_TRADING_ALLOWED_RUN_ROOTS": "C:\\Users\\me\\research" }
+ }
+ }
+}
+```
+
<details>
<summary><b>Claude Desktop</b></summary>
diff --git a/README_ja.md b/README_ja.md
index cf61c1510..169b4a095 100644
--- a/README_ja.md
+++ b/README_ja.md
@@ -988,6 +988,19 @@ curl -X DELETE http://localhost:8899/scheduled-runs/<job_id>
Vibe-Trading は MCP-compatible client 向けに 54 MCP tools を公開します。stdio subprocess として動作し、server setup は不要です。Core research tools は HK/US/crypto で API key なしに動作し、trading connector tools は選択中の connector profile を使います。LLM key が必要なのは `run_swarm` のみです。
+**環境変数:** server は client 自身が spawn するため、shell の `export` は届きません —— client の `env` block に設定してください。生成された backtest code は allowed run roots 内に制限されるので、結果を自分の作業 directory に書き出すには `VIBE_TRADING_ALLOWED_RUN_ROOTS` が必要です:
+
+```json
+{
+ "mcpServers": {
+ "vibe-trading": {
+ "command": "vibe-trading-mcp",
+ "env": { "VIBE_TRADING_ALLOWED_RUN_ROOTS": "C:\\Users\\me\\research" }
+ }
+ }
+}
+```
+
<details>
<summary><b>Claude Desktop</b></summary>
diff --git a/README_ko.md b/README_ko.md
index 75d9f45ba..b50160dd5 100644
--- a/README_ko.md
+++ b/README_ko.md
@@ -991,6 +991,19 @@ curl -X DELETE http://localhost:8899/scheduled-runs/<job_id>
Vibe-Trading은 모든 MCP-compatible client를 위해 54개 MCP tools를 제공합니다. stdio subprocess로 실행되므로 server setup이 필요 없습니다. 핵심 research tools는 HK/US/crypto에서 API key 없이 작동하고, trading connector tools는 선택된 connector profile을 사용하며, `run_swarm`만 LLM key가 필요합니다.
+**환경 변수:** server는 client가 직접 spawn하므로 shell의 `export`는 전달되지 않습니다 —— client의 `env` block에 설정하세요. 생성된 backtest code는 allowed run roots 안으로 제한되므로, 결과를 자신의 작업 directory에 쓰려면 `VIBE_TRADING_ALLOWED_RUN_ROOTS`가 필요합니다:
+
+```json
+{
+ "mcpServers": {
+ "vibe-trading": {
+ "command": "vibe-trading-mcp",
+ "env": { "VIBE_TRADING_ALLOWED_RUN_ROOTS": "C:\\Users\\me\\research" }
+ }
+ }
+}
+```
+
<details>
<summary><b>Claude Desktop</b></summary>
diff --git a/README_zh.md b/README_zh.md
index 85bcc92a8..110519055 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -1015,6 +1015,19 @@ curl -X DELETE http://localhost:8899/scheduled-runs/<job_id>
Vibe-Trading 为任何 MCP-compatible client 暴露 54 个 MCP tools。它作为 stdio subprocess 运行,无需 server setup。核心 research tools 对港股/美股/加密零 API key 可用;trading connector tools 使用当前选择的 connector profile;只有 `run_swarm` 需要 LLM key。
+**环境变量:** server 由 client 自己 spawn,因此在 shell 里 `export` 永远传不进去 —— 请写在 client 的 `env` 块里。生成的回测代码被限制在 allowed run roots 内,所以要把结果写进你自己的工作目录,需要 `VIBE_TRADING_ALLOWED_RUN_ROOTS`:
+
+```json
+{
+ "mcpServers": {
+ "vibe-trading": {
+ "command": "vibe-trading-mcp",
+ "env": { "VIBE_TRADING_ALLOWED_RUN_ROOTS": "C:\\Users\\me\\research" }
+ }
+ }
+}
+```
+
<details>
<summary><b>Claude Desktop</b></summary>
diff --git a/agent/src/tools/path_utils.py b/agent/src/tools/path_utils.py
index 01843242d..1c09fb877 100644
--- a/agent/src/tools/path_utils.py
+++ b/agent/src/tools/path_utils.py
@@ -25,6 +25,17 @@ from src.config.accessor import get_env_config
_ALLOWED_FILE_ROOTS_ENV = "VIBE_TRADING_ALLOWED_FILE_ROOTS"
_ALLOWED_RUN_ROOTS_ENV = "VIBE_TRADING_ALLOWED_RUN_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."
+)
+
+
+def _describe_roots(roots: list[Path]) -> str:
+ """Render allowed roots as an indented list for a rejection message."""
+ return "Allowed roots:\n" + "\n".join(f" - {root}" for root in roots)
+
def _rejects_unc(p: str) -> None:
"""Raise ValueError if `p` starts with a UNC share prefix."""
@@ -286,13 +297,15 @@ def _safe_import_path(p: str, *, purpose: str) -> Path:
_rejects_unc(p)
resolved = _import_candidate(p)
- for root in allowed_file_roots():
+ roots = allowed_file_roots()
+ for root in roots:
if resolved.is_relative_to(root):
return resolved
raise ValueError(
- f"Path {p!r} is outside allowed {purpose} roots. "
- f"Set {_ALLOWED_FILE_ROOTS_ENV} to add an import directory."
+ f"Path {p!r} is outside allowed {purpose} roots.\n"
+ f"{_describe_roots(roots)}\n"
+ f"Set {_ALLOWED_FILE_ROOTS_ENV} to add an import directory. {_ENV_SCOPE_HINT}"
)
@@ -344,13 +357,15 @@ def safe_run_dir(p: str) -> Path:
_rejects_unc(p)
resolved = Path(p).expanduser().resolve()
- for root in _allowed_run_roots():
+ roots = _allowed_run_roots()
+ for root in roots:
if resolved.is_relative_to(root):
return resolved
raise ValueError(
- f"run_dir {p!r} is outside allowed run roots. "
- f"Set {_ALLOWED_RUN_ROOTS_ENV} to add a run directory."
+ f"run_dir {p!r} is outside allowed run roots.\n"
+ f"{_describe_roots(roots)}\n"
+ f"Set {_ALLOWED_RUN_ROOTS_ENV} to add a run directory. {_ENV_SCOPE_HINT}"
)
@@ -376,9 +391,13 @@ def safe_run_id(run_id: str) -> Path:
):
raise ValueError(f"run_id {run_id!r} must be a bare run directory name")
- for root in _allowed_run_roots():
+ roots = _allowed_run_roots()
+ for root in roots:
resolved = (root / candidate.name).resolve()
if resolved.is_relative_to(root) and resolved.is_dir():
return resolved
- raise ValueError(f"run_id {run_id!r} was not found under allowed run roots")
+ raise ValueError(
+ f"run_id {run_id!r} was not found under allowed run roots.\n"
+ f"{_describe_roots(roots)}"
+ )
diff --git a/agent/tests/test_path_safety.py b/agent/tests/test_path_safety.py
index 2e5c75bdc..0db5775c8 100644
--- a/agent/tests/test_path_safety.py
+++ b/agent/tests/test_path_safety.py
@@ -148,6 +148,27 @@ class TestSafeRunDir:
assert result == agent_runs.resolve()
+ def test_rejection_lists_allowed_roots_and_mcp_scope(self, tmp_path: Path, monkeypatch):
+ """A rejection must say what IS allowed, not only which env var exists.
+
+ Reported in #963: an MCP client user could only guess at the boundary,
+ and setting the variable in a shell silently does nothing because the
+ client spawns the server itself.
+ """
+ allowed = tmp_path / "allowed"
+ allowed.mkdir()
+ monkeypatch.setenv("VIBE_TRADING_ALLOWED_RUN_ROOTS", str(allowed))
+ rejected = tmp_path / "elsewhere"
+ rejected.mkdir()
+
+ with pytest.raises(ValueError) as excinfo:
+ safe_run_dir(str(rejected))
+
+ message = str(excinfo.value)
+ assert "outside allowed run roots" in message
+ assert f" - {allowed.resolve()}" in message
+ assert "MCP client" in message
+
class TestSafeRunId:
def test_configured_run_id_accepted(self, tmp_path: Path, monkeypatch):