From 0388d03f225719fc0df3a239c39bbc103fbf1888 Mon Sep 17 00:00:00 2001 From: teknium1 <127238744+teknium1@users.noreply.github.com> Date: Sun, 13 Sep 2026 18:36:07 -0700 Subject: [PATCH] fix(tui_gateway): profile-scoped RPCs bind the requested profile's secret + terminal scope _profile_scoped_rpc (mcp.servers.*, insights.get, cron.manage, skills.manage, mcp.catalog, plugins.manage) bound only HERMES_HOME for params.profile. config.yaml's ${VAR} expansion (config._env_ref_lookup) and the MCP probe's header/env interpolation resolve through get_secret, which with no scope installed reads plain os.environ, i.e. the launch profile's values. The Desktop MCP setup Test-connection (mcp.servers.test) for a secondary thus sent the default profile's token (or the literal placeholder) and reported green against the wrong credential - the JSON-RPC twin of the dashboard REST gap fixed in #110271 (#109901). Bind the same home + secret + terminal composition a turn binds (_session_profile_runtime_scope), hydrating the profile's external secret sources first. os.environ is never mutated. Drops the now-unused mcp_rpc_helpers.reset_profile. --- tui_gateway/mcp_rpc_helpers.py | 14 ++------------ tui_gateway/methods_tools.py | 23 ++++++++++++++++------- tui_gateway/server.py | 6 ++---- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/tui_gateway/mcp_rpc_helpers.py b/tui_gateway/mcp_rpc_helpers.py index 3b38c6a03c..013a5133bc 100644 --- a/tui_gateway/mcp_rpc_helpers.py +++ b/tui_gateway/mcp_rpc_helpers.py @@ -1,24 +1,14 @@ """Shared helpers for the per-profile MCP lifecycle RPCs (mcp.servers.*). -Published onto ``tui_gateway.server`` as ``_mcp_reset_profile`` / -``_mcp_summarize_server`` so the rebound handler bodies in methods_tools resolve them. +Published onto ``tui_gateway.server`` as ``_mcp_summarize_server`` so the rebound handler +bodies in methods_tools resolve it. """ from __future__ import annotations -import contextlib from typing import Any, Dict -def reset_profile(token) -> None: - if token is None: - return - with contextlib.suppress(Exception): - from hermes_constants import reset_hermes_home_override - - reset_hermes_home_override(token) - - def summarize_server(name: str, cfg: dict) -> Dict[str, Any]: """Serialize one server's config for a UI (no secret values). diff --git a/tui_gateway/methods_tools.py b/tui_gateway/methods_tools.py index 70161a5a77..9a9df6b4a3 100644 --- a/tui_gateway/methods_tools.py +++ b/tui_gateway/methods_tools.py @@ -5,6 +5,7 @@ bodies reference server globals bare (``_ok``, ``_err``, ``_sessions``, ...). Helper names must not collide with server.py's own (``_cmd_`` / ``_toolset_`` / ``_mcp_`` prefixes). """ +import contextlib import sys from pathlib import Path @@ -20,12 +21,20 @@ def _profile_scoped_rpc( fail_code: int, *, required=(), catch_resolve: bool = True, prefix: str = "", scoped: bool = True, live_session: bool = False, ): - """Wrap a handler body with the optional ``profile`` HERMES_HOME scope. Order: ``required`` + """Wrap a handler body with the optional ``profile`` runtime scope. Order: ``required`` params (4063 `` required``) → ``live_session`` resolution via ``_sess`` (waits for the agent build; body gets ``session`` as 3rd arg) → profile (4064 when its dir is missing) → body; body exceptions become ``fail_code`` (``prefix`` + message). ``catch_resolve`` also maps resolve-time exceptions to ``fail_code``; mcp.servers.* let them propagate to dispatch(). - ``scoped=False`` ignores ``profile``. The override is always reset afterwards.""" + ``scoped=False`` ignores ``profile``. + + The scope is the same home + secret + terminal composition a turn binds + (``_session_profile_runtime_scope``), not HERMES_HOME alone: these bodies read config.yaml, + whose ``${VAR}`` refs (``config._env_ref_lookup``) and the MCP probe's own header/env + interpolation resolve through ``get_secret`` — with only the home bound they read plain + ``os.environ``, i.e. the launch profile's values, so ``mcp.servers.test`` for a secondary + reported green against the default profile's token (or the literal placeholder). External + sources are hydrated first (the requested profile may never have been served in this process).""" def deco(body): def handler(rid, params: dict) -> dict: @@ -38,7 +47,7 @@ def _profile_scoped_rpc( if err: return err args = (rid, params, session) - token = None + scope = contextlib.nullcontext() if profile := _str_arg(params, "profile") if scoped else "": try: try: @@ -47,17 +56,17 @@ def _profile_scoped_rpc( profile_dir = None if not profile_dir or not profile_dir.is_dir(): return _err(rid, 4064, f"profile '{profile}' not found") - token = _tools_mod("hermes_constants").set_hermes_home_override(str(profile_dir)) + _tools_mod("hermes_cli.env_loader").hydrate_profile_secret_sources(profile_dir) + scope = _session_profile_runtime_scope({"profile_home": str(profile_dir)}) except Exception as e: if not catch_resolve: raise return _err(rid, fail_code, str(e)) try: - return body(*args) + with scope: + return body(*args) except Exception as e: return _err(rid, fail_code, f"{prefix}{e}") - finally: - _mcp_reset_profile(token) handler.__doc__ = body.__doc__ return handler return deco diff --git a/tui_gateway/server.py b/tui_gateway/server.py index bafa526aa2..40de67816c 100644 --- a/tui_gateway/server.py +++ b/tui_gateway/server.py @@ -3238,10 +3238,8 @@ def _resolve_name(name: str) -> str: _paste_counter = 0 -# mcp.servers.* handlers (methods_tools) resolve these BARE through this namespace. -from .mcp_rpc_helpers import ( # noqa: E402, F401 - reset_profile as _mcp_reset_profile, - summarize_server as _mcp_summarize_server) +# mcp.servers.* handlers (methods_tools) resolve this BARE through this namespace. +from .mcp_rpc_helpers import summarize_server as _mcp_summarize_server # noqa: E402, F401 # ── Split @method handler modules (see method_ctx.py): imported last so every global the handlers close