Skip to content

The agent that grows with you

fix(mcp): route non-secret catalog env vars to config.yaml instead of .env

Halldrixd19003450ca2
diff --git a/hermes_cli/mcp_catalog.py b/hermes_cli/mcp_catalog.py
index 3645fa3c21f..8eec5cb3a90 100644
--- a/hermes_cli/mcp_catalog.py
+++ b/hermes_cli/mcp_catalog.py
@@ -456,23 +456,45 @@ def _expand_install_dir(value: str, install_dir: Optional[Path]) -> str:
def _prompt_env_vars(specs: List[EnvVarSpec]) -> Dict[str, str]:
- """Prompt for each env spec; secrets and non-secrets alike go to ~/.hermes/.env."""
+ """Prompt for each env spec.
+
+ Secrets persist to ~/.hermes/.env. Non-secrets are only collected and
+ returned — the caller inlines them into the server config (config.yaml),
+ since .env is secrets-only.
+ """
collected: Dict[str, str] = {}
for spec in specs:
- existing = get_env_value(spec.name)
+ existing = get_env_value(spec.name) if spec.secret else None
if existing:
_say(f" ✓ {spec.name} already set in .env")
collected[spec.name] = existing
continue
value = _prompt_input(spec.prompt, default=spec.default or None, password=spec.secret)
if value:
- save_env_value(spec.name, value)
+ if spec.secret:
+ save_env_value(spec.name, value)
collected[spec.name] = value
elif spec.required:
raise CatalogError(f"{spec.name} is required but no value was provided")
return collected
+def _inline_non_secret_value(obj: Any, name: str, value: str) -> Any:
+ """Recursively replace literal ``${name}`` refs with the collected value.
+
+ Only non-secret env vars are inlined this way: secret refs stay as
+ ``${VAR}`` so config.yaml never carries credentials (resolved from .env
+ at load time).
+ """
+ if isinstance(obj, str):
+ return obj.replace("${" + name + "}", value)
+ if isinstance(obj, dict):
+ return {k: _inline_non_secret_value(v, name, value) for k, v in obj.items()}
+ if isinstance(obj, list):
+ return [_inline_non_secret_value(v, name, value) for v in obj]
+ return obj
+
+
def _build_server_config(entry: CatalogEntry, install_dir: Optional[Path]) -> dict:
"""Translate a manifest into the ``mcp_servers.<name>`` block format used by hermes_cli/mcp_config.py."""
cfg: dict = {}
@@ -698,10 +720,11 @@ def install_entry(entry: CatalogEntry, *, enable: bool = True) -> None:
install_dir = _do_git_install(entry) if entry.install is not None else None
+ env_values: Dict[str, str] = {}
if entry.auth.env:
print()
_say(" Configure credentials:", Colors.CYAN)
- _prompt_env_vars(entry.auth.env)
+ env_values = _prompt_env_vars(entry.auth.env)
if entry.auth.type == "oauth" and entry.auth.provider:
# Provider-mediated OAuth relies on the existing `hermes auth <provider>` flow; surface
# guidance rather than auto-running it to keep install decoupled from provider-auth lifecycle.
@@ -722,6 +745,11 @@ def install_entry(entry: CatalogEntry, *, enable: bool = True) -> None:
prior_exclude = _read_prior_tool_list(entry.name, "exclude")
server_cfg = _build_server_config(entry, install_dir)
+ # Inline non-secret env values into config.yaml; secrets keep their ${VAR}
+ # refs so the raw file never carries credentials (resolved from .env at load).
+ for spec in entry.auth.env:
+ if not spec.secret and spec.name in env_values:
+ server_cfg = _inline_non_secret_value(server_cfg, spec.name, env_values[spec.name])
server_cfg["enabled"] = enable
from hermes_cli.mcp_config import _save_mcp_server
diff --git a/hermes_cli/web_routers/mcp.py b/hermes_cli/web_routers/mcp.py
index a34babb6f71..9bdaa8bdbbc 100644
--- a/hermes_cli/web_routers/mcp.py
+++ b/hermes_cli/web_routers/mcp.py
@@ -473,9 +473,12 @@ async def install_mcp_catalog_entry(body: MCPCatalogInstall, profile: Optional[s
if body.env:
def _write_env():
with _profile_scope(effective_profile):
- for k, v in body.env.items():
- if v:
- save_env_value(k, v)
+ # Only declared secrets go to .env; non-secret values ride in the
+ # server config (inlined by install_entry) so .env stays secrets-only.
+ for spec in entry.auth.env or []:
+ value = (body.env or {}).get(spec.name)
+ if spec.secret and value:
+ save_env_value(spec.name, value)
await asyncio.to_thread(_write_env)
diff --git a/tests/hermes_cli/test_mcp_catalog.py b/tests/hermes_cli/test_mcp_catalog.py
index ffbdad9fa2e..5f8e3c8ca05 100644
--- a/tests/hermes_cli/test_mcp_catalog.py
+++ b/tests/hermes_cli/test_mcp_catalog.py
@@ -622,6 +622,11 @@ class TestInstall:
assert get_env_value("DEMO_CLIENT_SECRET") == "val-for-secret"
raw = get_config_path().read_text(encoding="utf-8")
assert "${DEMO_CLIENT_SECRET}" in raw and "val-for-secret" not in raw
+ # Non-secret client id is inlined into config.yaml (not .env, not a ref):
+ # .env stays secrets-only.
+ assert get_env_value("DEMO_CLIENT_ID") is None
+ assert "${DEMO_CLIENT_ID}" not in raw
+ assert "val-for-id" in raw
# A ``${VAR}`` the manifest never declares would reach the token endpoint as a literal
# placeholder (invalid_client): rejected at parse time, like the api_key header contract.
diff --git a/tests/hermes_cli/test_mcp_catalog_env_boundary.py b/tests/hermes_cli/test_mcp_catalog_env_boundary.py
index a6670c5cb5b..73d1698a273 100644
--- a/tests/hermes_cli/test_mcp_catalog_env_boundary.py
+++ b/tests/hermes_cli/test_mcp_catalog_env_boundary.py
@@ -168,6 +168,54 @@ def test_catalog_accepts_declared_credential(
).read_text(encoding="utf-8")
+def test_catalog_non_secret_env_never_lands_in_env_file(
+ client: TestClient,
+ catalog_env: Path,
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """Non-secret declared env vars (e.g. a base URL) are not written to .env:
+ install_entry inlines them into the server config instead."""
+ import hermes_cli.mcp_catalog as mcp_catalog
+
+ catalog_root = Path(os.environ["HERMES_OPTIONAL_MCPS"])
+ manifest_path = catalog_root / "demo" / "manifest.yaml"
+ manifest = yaml.safe_load(manifest_path.read_text(encoding="utf-8"))
+ manifest["auth"]["env"].append(
+ {
+ "name": "DEMO_BASE_URL",
+ "prompt": "Demo base URL",
+ "secret": False,
+ }
+ )
+ manifest_path.write_text(yaml.safe_dump(manifest), encoding="utf-8")
+
+ installs: list[str] = []
+ monkeypatch.setattr(
+ mcp_catalog,
+ "install_entry",
+ lambda entry, enable=True: installs.append(entry.name),
+ )
+
+ response = client.post(
+ "/api/mcp/catalog/install",
+ headers=HEADERS,
+ json={
+ "name": "demo",
+ "env": {
+ "DEMO_API_KEY": "valid-demo-value",
+ "DEMO_BASE_URL": "https://demo.example.test",
+ },
+ },
+ )
+
+ assert response.status_code == 200
+ assert installs == ["demo"]
+ env_text = (catalog_env / ".env").read_text(encoding="utf-8")
+ assert "DEMO_API_KEY=valid-demo-value" in env_text
+ assert "DEMO_BASE_URL" not in env_text
+ assert "https://demo.example.test" not in env_text
+
+
@pytest.mark.parametrize(
"protected_key",
[