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.
This commit is contained in:
teknium1
2026-09-13 18:36:07 -07:00
committed by Teknium
parent 28138f2524
commit 0388d03f22
3 changed files with 20 additions and 23 deletions
+2 -12
View File
@@ -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).
+16 -7
View File
@@ -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 ``<key> 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
+2 -4
View File
@@ -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