The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): leave an unset ${VAR} env entry out of a stdio server's environment
Teknium43c9e0ae5297
diff --git a/tests/tools/test_mcp_stdio_env_unset_ref.py b/tests/tools/test_mcp_stdio_env_unset_ref.pynew file mode 100644index 00000000000..1fcd09eea8f--- /dev/null+++ b/tests/tools/test_mcp_stdio_env_unset_ref.py@@ -0,0 +1,19 @@+"""Stdio MCP launch env: an optional ``"VAR": "${VAR}"`` forward never reaches the child literally."""++from unittest.mock import patch+++def test_unset_whole_placeholder_env_entry_is_dropped():+ """An optional ``"VAR": "${VAR}"`` forward never reaches the child as the literal+ placeholder: unset drops the key, set passes the value, partial refs keep today's text."""+ 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):+ result = _build_safe_env(_interpolate_env_vars(user_env))++ assert "OPT_DIR" not in result and "OPT_ENV" not in result+ 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.pyindex 8eec864cc15..6cffbf59c08 100644--- a/tools/mcp_tool_config.py+++ b/tools/mcp_tool_config.py@@ -181,6 +181,12 @@ _CONTEXT_VAR_RESOLVERS = {"workspaceFolderBasename": _workspace_basename, "pathSeparator": lambda: os.sep, "/": lambda: os.sep}+def _is_unresolved_ref(value: Any) -> bool:+ """True when *value* is entirely one env ``${VAR}`` / ``${env:VAR}`` placeholder (not a context var)."""+ match = _ENV_VAR_PATTERN.fullmatch(value) if isinstance(value, str) else None+ return match is not None and match.group(1).strip() not in _CONTEXT_VAR_RESOLVERS++def _build_safe_env(user_env: Optional[dict]) -> dict:"""Filtered env for stdio subprocesses so API keys/tokens don't leak: the safe baselinekeys, ``XDG_*``, vars injected by an external secret source (users configured that backend@@ -201,7 +207,10 @@ def _build_safe_env(user_env: Optional[dict]) -> dict:if key in os.environ:env[key] = os.environ[key]if user_env:- env.update(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)})from agent.delegation_context import delegated_child_subprocess_envreturn delegated_child_subprocess_env(env)diff --git a/website/docs/developer-guide/plugins/index.md b/website/docs/developer-guide/plugins/index.mdindex b5c2cd8ab78..3de41889644 100644--- a/website/docs/developer-guide/plugins/index.md+++ b/website/docs/developer-guide/plugins/index.md@@ -103,6 +103,11 @@ skipped at its own boundary when valid sibling components can still load.profile-scoped writable directory managed by Hermes.Values declared in portable MCP `env` are visible package data, not a secretstorage mechanism. Do not place credentials in `mcp.json`.+To pass a user's variable through to a stdio server (Hermes otherwise hands MCP+servers a filtered environment), declare it as `"NAME": "${NAME}"`. The value+comes from the user's shell or profile `.env`; when the variable is unset, Hermes+leaves `NAME` out of the server's environment rather than passing the literal+`${NAME}`, so optional variables are safe to forward.The current portable subset supports stdio and Streamable HTTP MCP entries.Portable `streamable-http` entries are routed through Hermes' existing nativediff --git a/website/docs/reference/mcp-config-reference.md b/website/docs/reference/mcp-config-reference.mdindex f2dbceddd08..3613cc9fb54 100644--- a/website/docs/reference/mcp-config-reference.md+++ b/website/docs/reference/mcp-config-reference.md@@ -83,7 +83,7 @@ mcp_servers:GITHUB_PERSONAL_ACCESS_TOKEN: "${env:GITHUB_TOKEN}" # same as "${GITHUB_TOKEN}"```-Values resolve from the active profile's secret scope (falling back to the process environment), so put the secret in `~/.hermes/.env`. An unset variable keeps its literal placeholder — except in a remote server's `url` or `headers`: sending a literal `${VAR}` there is a guaranteed 401 that reads as a bad credential, so the connect fails closed instead, with the variable named in the server's status (`MCP server 'name': ${VAR} in url/headers is not set in this profile's .env or secret source`). Fix the profile's `.env` (or its secret source); the next connect attempt renders the reference afresh.+Values resolve from the active profile's secret scope (falling back to the process environment), so put the secret in `~/.hermes/.env`. An unset (or empty) variable keeps its literal placeholder, with two exceptions. A stdio server's `env` entry whose whole value is one reference (`GHIDRA_INSTALL_DIR: "${GHIDRA_INSTALL_DIR}"`) is left out of the server's environment instead, so a config or plugin can forward an optional variable and the server sees it as unset rather than as the text `${GHIDRA_INSTALL_DIR}`; a value that only contains a reference (`"${OPT_DIR}/bin"`) is still passed with the placeholder. And in a remote server's `url` or `headers`: sending a literal `${VAR}` there is a guaranteed 401 that reads as a bad credential, so the connect fails closed instead, with the variable named in the server's status (`MCP server 'name': ${VAR} in url/headers is not set in this profile's .env or secret source`). Fix the profile's `.env` (or its secret source); the next connect attempt renders the reference afresh.### Context variables