The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(review): MCP owner rebuild keeps the launch profile's env-only credentials (#119092 review)
teknium101d85137bd45
diff --git a/tests/tools/test_mcp_connect_secret_scope.py b/tests/tools/test_mcp_connect_secret_scope.pyindex 7518705afb8..3c8f125ec2f 100644--- a/tests/tools/test_mcp_connect_secret_scope.py+++ b/tests/tools/test_mcp_connect_secret_scope.py@@ -178,6 +178,35 @@ def test_connect_renders_remote_headers_under_the_owners_fresh_scope(tmp_path, sset_multiplex_active(False)+def test_launch_profile_env_only_credential_survives_the_owner_rebuild(spawn_env, monkeypatch):+ """Red on the salvage: the owner rebuild used ``build_profile_secret_scope`` (files only) for the+ LAUNCH profile too, so a credential that lives only in the launch env (systemd ``Environment=``,+ ``op run``, Compose) — present in the ``launch_secret_scope`` mapping the caller bound — vanished,+ the header stayed the literal ``${VAR}`` and the fail-closed check parked a server that worked."""+ import os+ from pathlib import Path+ from tools import mcp_tool_config as _config+ from tui_gateway import launch_profile_policy+ launch_home = Path(os.environ["HERMES_HOME"]) # conftest's per-test process home+ (launch_home / ".env").write_text("", encoding="utf-8")+ monkeypatch.setenv(TOKEN_NAME, "tok-from-systemd")+ monkeypatch.setattr(launch_profile_policy, "_snapshot", None)+ launch_profile_policy.activate_multi_profile_hosting() # freeze the launch env, fail closed, pin the home++ async def _run():+ with launch_profile_policy.launch_profile_runtime_scope(launch_home):+ await discovery._connect_server("demo", dict(REMOTE, headers=dict(REMOTE["headers"])))+ with discovery._owner_secret_scope(): # a later rebuild (reconnect / reconcile) under the same door+ return _config._interpolate_env_vars(dict(REMOTE["headers"]))++ try:+ rebuilt = asyncio.run(_run())+ finally:+ set_multiplex_active(False)+ assert spawn_env["config"]["headers"]["Authorization"] == "Bearer tok-from-systemd"+ assert rebuilt["Authorization"] == "Bearer tok-from-systemd"++def test_reconnect_rerenders_remote_headers_under_the_owners_fresh_scope(tmp_path, monkeypatch):"""Red on base (#119092): the run task re-read config.yaml on every rebuild but rendered it underits copied connect-time scope snapshot, so a parked server retried the literal ``${VAR}`` headerdiff --git a/tools/mcp_tool_discovery.py b/tools/mcp_tool_discovery.pyindex a8108f87ac8..d113a8f6e62 100644--- a/tools/mcp_tool_discovery.py+++ b/tools/mcp_tool_discovery.py@@ -91,14 +91,36 @@ async def _install_owner_secret_scope():and the server parks with zero tools (#113746). ``${VAR}`` refs are interpolated earlier, atconfig load, under :func:`_owner_secret_scope`."""- from agent.secret_scope import build_profile_secret_scope, set_secret_scope+ from agent.secret_scope import set_secret_scopehome = _owner_scope_home()if home is None:return None- from hermes_cli.env_loader import hydrate_profile_secret_sources# Off-loop: an external source runs a helper subprocess (once per home, then cached).- await asyncio.to_thread(hydrate_profile_secret_sources, home)- return set_secret_scope(build_profile_secret_scope(home), profile_home=str(home))+ return set_secret_scope(await asyncio.to_thread(_owner_secret_mapping, home), profile_home=str(home))+++_HYDRATE_RETRY_INTERVAL_SEC = 30.0+_hydrate_retry_after: Dict[str, float] = {}+++def _owner_secret_mapping(home: Path) -> Dict[str, str]:+ """The owner's FRESH secret mapping. The launch profile's is ``launch_secret_scope`` (its files+ over the frozen launch env): a credential injected only by systemd ``Environment=`` /+ ``op run`` / Compose has no file to rebuild from, so a files-only rebuild dropped it, left the+ header literal ``${VAR}`` and parked a server that worked. A served profile gets its own ``.env``+ + external sources only. A source that did not fully hydrate is retried at most once per+ interval per home: every retry is a helper subprocess, and connect/reconnect loops are tight."""+ from agent.secret_scope import _is_process_home, build_profile_secret_scope+ from hermes_cli import env_loader+ key = str(home.resolve())+ if time.monotonic() >= _hydrate_retry_after.get(key, 0.0):+ env_loader.hydrate_profile_secret_sources(home)+ if key not in env_loader._APPLIED_HOMES:+ _hydrate_retry_after[key] = time.monotonic() + _HYDRATE_RETRY_INTERVAL_SEC+ if _is_process_home(home):+ from tui_gateway.launch_profile_policy import launch_secret_scope+ return launch_secret_scope(home)+ return build_profile_secret_scope(home)@contextmanager@@ -109,14 +131,12 @@ def _owner_secret_scope():the resulting ``UnscopedSecretError`` into ``{}``, so an unscoped discover / reconcile / status /probe for a routed profile saw ZERO servers — stdio siblings included — before the connect-sitebinding could ever run (#113746). Same owner rule; scope key ``None`` binds nothing."""- from agent.secret_scope import build_profile_secret_scope, reset_secret_scope, set_secret_scope+ from agent.secret_scope import reset_secret_scope, set_secret_scopehome = _owner_scope_home()if home is None:yieldreturn- from hermes_cli.env_loader import hydrate_profile_secret_sources- hydrate_profile_secret_sources(home)- token = set_secret_scope(build_profile_secret_scope(home), profile_home=str(home))+ token = set_secret_scope(_owner_secret_mapping(home), profile_home=str(home))try:yieldfinally:diff --git a/website/docs/developer-guide/multiplexing-gateway.md b/website/docs/developer-guide/multiplexing-gateway.mdindex a2967827dac..efea24c3a59 100644--- a/website/docs/developer-guide/multiplexing-gateway.md+++ b/website/docs/developer-guide/multiplexing-gateway.md@@ -293,6 +293,17 @@ HERMES_HOME override. Asset writes are atomic, type- and size-capped.- Fail-closed: unscoped `get_secret()` under multiplexing raises; a routedevent targeting an unserved profile is dropped; an unscoped `/p/` requestenters the default profile's scope (`#61276`) rather than an undefined one.+- Fail-closed, per profile: a remote MCP server whose `url` / `headers` still+ carry a literal `${VAR}` after rendering under its owner profile's scope does+ not connect (`MCP server 'x': ${VAR} in url/headers is not set in this+ profile's .env or secret source`); the reference is re-rendered under the+ owner's fresh scope at every connect and reconnect, so it heals once that+ profile's `.env` or secret source supplies the value. The launch profile's+ mapping still includes its frozen launch env (systemd `Environment=` /+ `op run` credentials resolve); a secondary resolves from its own files only.+- Scoped platform gates: A2A (`A2A_PORT`) and Buzz enablement are read through+ the profile's own scope / `platforms.<name>` section, so a launch-profile+ env var no longer enables an inbound listener in every secondary.- Fallback: an external `cron.provider` does not support multiplexing andfalls back to the built-in ticker with a warning.diff --git a/website/docs/reference/mcp-config-reference.md b/website/docs/reference/mcp-config-reference.mdindex cdb53264cf5..4f8dd8d5a91 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.+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.### Context variables