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:
+45
-1
@@ -90,7 +90,7 @@ import sys
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import Any, Coroutine, Dict, List, Optional
|
||||
from urllib.parse import urlparse
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -2460,6 +2460,37 @@ def _ensure_mcp_loop():
|
||||
_mcp_thread.start()
|
||||
|
||||
|
||||
def _wrap_with_home_override(coro: "Coroutine") -> "Coroutine":
|
||||
"""Carry the caller's context-local HERMES_HOME override into ``coro``.
|
||||
|
||||
Returns ``coro`` unchanged when no override is active. Otherwise wraps
|
||||
it so the override is set inside the coroutine's own (task-local)
|
||||
context on the MCP loop and reset when it completes — concurrent calls
|
||||
carrying different scopes don't interfere.
|
||||
"""
|
||||
try:
|
||||
from hermes_constants import (
|
||||
get_hermes_home_override,
|
||||
reset_hermes_home_override,
|
||||
set_hermes_home_override,
|
||||
)
|
||||
|
||||
home_override = get_hermes_home_override()
|
||||
except Exception:
|
||||
return coro
|
||||
if not home_override:
|
||||
return coro
|
||||
|
||||
async def _scoped():
|
||||
token = set_hermes_home_override(home_override)
|
||||
try:
|
||||
return await coro
|
||||
finally:
|
||||
reset_hermes_home_override(token)
|
||||
|
||||
return _scoped()
|
||||
|
||||
|
||||
def _run_on_mcp_loop(coro_or_factory, timeout: float = 30):
|
||||
"""Schedule a coroutine on the MCP event loop and block until done.
|
||||
|
||||
@@ -2482,6 +2513,19 @@ def _run_on_mcp_loop(coro_or_factory, timeout: float = 30):
|
||||
raise RuntimeError("MCP event loop is not running")
|
||||
|
||||
coro = coro_or_factory() if callable(coro_or_factory) else coro_or_factory
|
||||
|
||||
# Propagate the context-local HERMES_HOME override onto the MCP loop.
|
||||
# Tasks scheduled via run_coroutine_threadsafe are created INSIDE the
|
||||
# loop thread, so they copy the loop thread's context — not the
|
||||
# scheduling thread's. A per-request profile scope (the dashboard's
|
||||
# ?profile= endpoints, e.g. the MCP "Test server" probe) would silently
|
||||
# vanish here: OAuth token stores and any other get_hermes_home()
|
||||
# resolution inside the coroutine would read the process home instead
|
||||
# of the selected profile's. Re-establish the override inside the
|
||||
# task's own context (task-local — concurrent calls carrying different
|
||||
# scopes don't interfere). No-op when no override is active.
|
||||
coro = _wrap_with_home_override(coro)
|
||||
|
||||
future = safe_schedule_threadsafe(
|
||||
coro, loop,
|
||||
logger=logger,
|
||||
|
||||
Reference in New Issue
Block a user