Skip to content

The agent that grows with you

fix(mcp): an unset ${VAR} override also clears the inherited baseline value

Teknium5fc490e2b341
diff --git a/tests/tools/test_mcp_stdio_env_unset_ref.py b/tests/tools/test_mcp_stdio_env_unset_ref.py
index 1fcd09eea8f..b577f401219 100644
--- a/tests/tools/test_mcp_stdio_env_unset_ref.py
+++ b/tests/tools/test_mcp_stdio_env_unset_ref.py
@@ -9,11 +9,13 @@ def test_unset_whole_placeholder_env_entry_is_dropped():
from tools.mcp_tool_config import _build_safe_env, _interpolate_env_vars
user_env = {"OPT_DIR": "${OPT_DIR}", "OPT_ENV": "${env:OPT_ENV}", "SET_DIR": "${SET_DIR}",
- "PARTIAL": "${OPT_DIR}/bin", "PLAIN": "x"}
- with patch.dict("os.environ", {"PATH": "/usr/bin", "SET_DIR": "/opt/set"}, clear=True):
+ "PARTIAL": "${OPT_DIR}/bin", "PLAIN": "x", "XDG_CONFIG_HOME": "${OPT_CONFIG_HOME}"}
+ parent = {"PATH": "/usr/bin", "SET_DIR": "/opt/set", "XDG_CONFIG_HOME": "/parent/config"}
+ with patch.dict("os.environ", parent, clear=True):
result = _build_safe_env(_interpolate_env_vars(user_env))
assert "OPT_DIR" not in result and "OPT_ENV" not in result
+ assert "XDG_CONFIG_HOME" not in result # unset override does not fall back to the parent's value
assert result["SET_DIR"] == "/opt/set"
assert result["PARTIAL"] == "${OPT_DIR}/bin"
assert result["PLAIN"] == "x"
diff --git a/tools/mcp_tool_config.py b/tools/mcp_tool_config.py
index 6cffbf59c08..070d67bb99f 100644
--- a/tools/mcp_tool_config.py
+++ b/tools/mcp_tool_config.py
@@ -207,10 +207,14 @@ def _build_safe_env(user_env: Optional[dict]) -> dict:
if key in os.environ:
env[key] = os.environ[key]
if user_env:
- # A value that is still a whole ``${VAR}`` here was unset (or empty) at render time: drop
- # the key so a plugin can forward an optional var without the child seeing the literal
- # placeholder (servers that validate it, e.g. as an absolute path, refuse to start).
- env.update({key: value for key, value in user_env.items() if not _is_unresolved_ref(value)})
+ # A value that is still a whole ``${VAR}`` here was unset (or empty) at render time: the
+ # key is left unset (baseline copy included) so a plugin can forward an optional var
+ # without the child seeing the literal placeholder (servers that validate it refuse to start).
+ for key, value in user_env.items():
+ if _is_unresolved_ref(value):
+ env.pop(key, None)
+ else:
+ env[key] = value
from agent.delegation_context import delegated_child_subprocess_env
return delegated_child_subprocess_env(env)