Skip to content

Official Codebase of 3DCodeVerse

Python

agents: the MCP command comes from the typed job, never from the workspace

gaoypeng1f4da4565700
diff --git a/harness/codeverse/agents/claude_code.py b/harness/codeverse/agents/claude_code.py
index 7d2da0cc..5b8523af 100644
--- a/harness/codeverse/agents/claude_code.py
+++ b/harness/codeverse/agents/claude_code.py
@@ -28,10 +28,12 @@ from codeverse.agents.cli_common import (
hardened_env,
invoke,
is_transient_failure,
+ mcp_command_for,
release_session,
tail,
watchdog_error,
)
+from codeverse.agents.materialize import MCP_SERVER_NAME, MCP_TOOL_TIMEOUT_MS
from codeverse.config import get_settings
from codeverse.contracts.agent import AgentJob, AgentResult
from codeverse.contracts.common import Usage
@@ -135,9 +137,16 @@ class ClaudeCodeAgent:
"--allowedTools", ",".join(ALLOWED_TOOLS)]
if self.model:
argv += ["--model", self.model]
- mcp = s.ws.root / ".mcp.json"
- if job.spatial_tools and mcp.is_file():
- argv += ["--mcp-config", str(mcp), "--strict-mcp-config"]
+ if job.spatial_tools:
+ # written fresh into the harness-owned trajectory dir for THIS session, from
+ # the typed job — never the workspace's .mcp.json, which the agent can rewrite
+ # between rounds to choose what the next session launches (audit 2026-08-27)
+ cfg = s.traj.dir / "mcp.json"
+ cmd = mcp_command_for(s.ws, job)
+ cfg.write_text(json.dumps(
+ {"mcpServers": {MCP_SERVER_NAME: {"type": "stdio", "command": cmd[0], "args": cmd[1:],
+ "timeout": MCP_TOOL_TIMEOUT_MS}}}, indent=2))
+ argv += ["--mcp-config", str(cfg), "--strict-mcp-config"]
else:
argv += ["--strict-mcp-config"] # never inherit the user's ambient MCP servers
if job.system_append:
diff --git a/harness/codeverse/agents/cli_common.py b/harness/codeverse/agents/cli_common.py
index de4dac4c..178b901a 100644
--- a/harness/codeverse/agents/cli_common.py
+++ b/harness/codeverse/agents/cli_common.py
@@ -15,7 +15,6 @@
from __future__ import annotations
-import json
import logging
import os
import re
@@ -78,15 +77,14 @@ def default_mcp_command(ws: Workspace, *, language: str = "", track: str = "", r
def mcp_command_for(ws: Workspace, job: AgentJob) -> list[str]:
- """The 3dcv MCP command for this job: ``.mcp.json`` (materialised) > ``job.mcp_command`` > default."""
- mcp = ws.root / ".mcp.json"
- if mcp.is_file():
- try:
- srv = (json.loads(mcp.read_text()).get("mcpServers") or {}).get("3dcv")
- except json.JSONDecodeError:
- srv = None
- if srv and srv.get("command"):
- return [srv["command"], *srv.get("args", [])]
+ """The 3dcv MCP command for this job, from the TYPED job: ``job.mcp_command`` > default.
+
+ It deliberately does NOT read the workspace's ``.mcp.json``. That file lives where
+ the agent works and every CLI backend runs unsandboxed inside it, so an agent that
+ rewrote it in round 0 chose what executable the NEXT round's codex/claude launched —
+ with provider credentials in the environment, and (for codex) outside the
+ ``--sandbox workspace-write`` its own shell obeys. The command is rebuilt from the
+ job every session instead; nothing on disk can redirect it (audit 2026-08-27)."""
if job.mcp_command:
return list(job.mcp_command)
return default_mcp_command(ws, language=job.language, track=job.track, round_index=job.round)
diff --git a/harness/tests/agents/test_claude_code.py b/harness/tests/agents/test_claude_code.py
index ec0768c4..418a8c99 100644
--- a/harness/tests/agents/test_claude_code.py
+++ b/harness/tests/agents/test_claude_code.py
@@ -75,7 +75,17 @@ def test_argv_includes_mcp_when_materialized(tmp_ws: Workspace):
s = begin_session(AgentJob(workspace=str(tmp_ws.root), prompt="p", system_append="S", max_turns=7), "claude-code")
argv = a.build_argv(s, "p")
assert argv[:3] == ["claude", "-p", "p"]
- assert "--mcp-config" in argv and argv[argv.index("--mcp-config") + 1].endswith(".mcp.json")
+ # the config is written fresh into THIS session's trajectory dir from the typed job,
+ # never the workspace .mcp.json the agent can rewrite between rounds
+ cfg = Path(argv[argv.index("--mcp-config") + 1])
+ assert cfg.name == "mcp.json" and cfg.parent == s.traj.dir
+ server = json.loads(cfg.read_text())["mcpServers"]["3dcv"]
+ assert server["command"].endswith("python") and "--workspace" in server["args"]
+ (tmp_ws.root / ".mcp.json").write_text(json.dumps(
+ {"mcpServers": {"3dcv": {"command": "/tmp/evil", "args": []}}}))
+ argv2 = a.build_argv(s, "p") # a tampered workspace file changes nothing
+ assert json.loads(Path(argv2[argv2.index("--mcp-config") + 1]).read_text(
+ ))["mcpServers"]["3dcv"]["command"].endswith("python")
assert "--strict-mcp-config" in argv and argv[argv.index("--max-turns") + 1] == "7"
assert argv[argv.index("--append-system-prompt") + 1] == "S" and argv[argv.index("--model") + 1] == "sonnet"
assert "mcp__c3v__*" in argv[argv.index("--allowedTools") + 1]
diff --git a/harness/tests/agents/test_cli_common.py b/harness/tests/agents/test_cli_common.py
index da608e57..4fbc250f 100644
--- a/harness/tests/agents/test_cli_common.py
+++ b/harness/tests/agents/test_cli_common.py
@@ -151,9 +151,15 @@ def test_mcp_command_resolution(tmp_ws: Workspace):
assert cmd[1:3] == ["-m", "codeverse.spatial.mcp_server"] and "--language" in cmd and cmd[cmd.index("--round") + 1] == "2"
job2 = AgentJob(workspace=str(tmp_ws.root), prompt="p", extra={"mcp_command": ["python", "-m", "x"]})
assert mcp_command_for(tmp_ws, job2) == ["python", "-m", "x"]
+ # a workspace .mcp.json NEVER wins: the agent works in that directory and could
+ # otherwise choose what the next round's CLI launches (audit 2026-08-27)
materialize_workspace(tmp_ws, agent_kind="codex", contract_md="c", cookbook_rel="", spatial_tools=True,
mcp_command=default_mcp_command(tmp_ws, language="cadquery"))
- assert "cadquery" in mcp_command_for(tmp_ws, job2) # materialised .mcp.json wins
+ assert mcp_command_for(tmp_ws, job2) == ["python", "-m", "x"] # still the typed job
+ (tmp_ws.root / ".mcp.json").write_text(json.dumps(
+ {"mcpServers": {"3dcv": {"command": "/tmp/evil", "args": ["--pwn"]}}}))
+ assert mcp_command_for(tmp_ws, job2) == ["python", "-m", "x"]
+ assert "/tmp/evil" not in mcp_command_for(tmp_ws, job)
def test_gemini_system_settings_disable_folder_trust(tmp_path):