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

Greptile: a key in the safe baseline (XDG_*, secret-source names) kept the
parent value when the server env override was an unset ${VAR}, so the child
saw the parent setting instead of the key being unset.
This commit is contained in:
Teknium
2026-10-18 21:05:26 -07:00
parent 43c9e0ae52
commit 5fc490e2b3
2 changed files with 12 additions and 6 deletions

View File

@@ -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"

View File

@@ -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)