fix(mcp): propagate HERMES_HOME override onto the MCP event loop (#44220)

* fix(mcp): propagate HERMES_HOME override onto the MCP event loop

Closes the known limit documented in #44007: tasks scheduled via
run_coroutine_threadsafe are created INSIDE the MCP loop thread, so they
copy that thread's context — a per-request profile scope (dashboard
?profile= endpoints, e.g. the MCP 'Test server' probe) silently vanished
for anything resolving get_hermes_home() inside the coroutine. Most
visible symptom: OAuth token-store paths (HERMES_HOME/mcp-tokens/)
resolved against the process home instead of the selected profile, so
testing an OAuth MCP cross-profile read the wrong tokens.

_run_on_mcp_loop now wraps scheduled coroutines with the caller's
context-local override (_wrap_with_home_override): set inside the task's
own context on the loop, reset on completion — task-local, so concurrent
calls carrying different scopes don't interfere, and the loop thread's
default context stays untouched. No-op (coroutine passes through
unwrapped) when no override is active, i.e. every non-dashboard caller.

web_server's probe comment updated from 'known limit' to 'covered'.

Tests: override propagation (direct + factory form), OAuth token-path
resolution on the loop, loop-context cleanliness after scoped calls,
no-op passthrough. 225 green across mcp_tool + unification suites.

* test(mcp): concurrent different-scope calls don't interfere
This commit is contained in:
Teknium
2026-06-11 04:37:01 -07:00
committed by GitHub
parent 3edd09a46f
commit 73dd584995
3 changed files with 188 additions and 4 deletions
+4 -3
View File
@@ -6577,9 +6577,10 @@ async def test_mcp_server(name: str, profile: Optional[str] = None):
# selected profile, matching the config the server was saved into.
# (asyncio.to_thread copies contextvars, but entering explicitly
# keeps the lock-protected SKILLS_DIR swap balanced per-thread.)
# Known limit: the dedicated MCP event-loop thread spawned by the
# probe doesn't inherit the contextvar, so OAuth token-store paths
# resolve against the process HERMES_HOME.
# The probe's dedicated MCP event-loop thread is covered too:
# _run_on_mcp_loop wraps scheduled coroutines with the caller's
# HERMES_HOME override (see mcp_tool._wrap_with_home_override), so
# OAuth token stores resolve against the selected profile as well.
with _profile_scope(profile):
return _probe_single_server(name, servers[name])