The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(mcp): _running_loop helper for the loop-up check
Tekniuma0f15d1f2a6d
diff --git a/tools/mcp_tool.py b/tools/mcp_tool.pyindex fb35a5e9ba5..197f855b483 100644--- a/tools/mcp_tool.py+++ b/tools/mcp_tool.py@@ -127,6 +127,7 @@ from tools.mcp_tool_transport import MCPServerTransportMixinfrom tools.mcp_tool_server_run import MCPServerRunMixinfrom tools.mcp_tool_health import MCPServerHealthMixinfrom tools.mcp_tool_loop import ( # noqa: F401 -- re-exported for callers and test patches+ _running_loop,_LockCookie, _acquire_lock_on_fh, _try_acquire_mcp_discovery_lock,_mcp_loop_exception_handler, _wrap_with_home_override,_wrap_with_dashboard_oauth_flow, _run_on_mcp_loop, _signal_reconnect,diff --git a/tools/mcp_tool_discovery.py b/tools/mcp_tool_discovery.pyindex c5a4db10b63..38aa0be6d13 100644--- a/tools/mcp_tool_discovery.py+++ b/tools/mcp_tool_discovery.py@@ -81,9 +81,8 @@ def _request_lazy_reconnect(server_name: str, server: _core.MCPServerTask) -> boif not server._is_recycled_stdio():return False- with _core._lock:- loop = _core._mcp_loop- if loop is None or not loop.is_running():+ loop = _core._running_loop()+ if loop is None:return Falsedef _wake() -> None:diff --git a/tools/mcp_tool_loop.py b/tools/mcp_tool_loop.pyindex eb7b68113ec..87f90579c20 100644--- a/tools/mcp_tool_loop.py+++ b/tools/mcp_tool_loop.py@@ -13,7 +13,7 @@ import loggingimport osimport threadingimport time-from typing import Any, Coroutine+from typing import Any, Coroutine, Optionalfrom tools.mcp_tool_common import _corelogger = logging.getLogger("tools.mcp_tool")@@ -168,6 +168,13 @@ def _wrap_with_dashboard_oauth_flow(coro):return _scoped()+def _running_loop() -> Optional[asyncio.AbstractEventLoop]:+ """The MCP loop when it is up, else None (read under ``_lock``)."""+ with _core._lock:+ loop = _core._mcp_loop+ return loop if loop is not None and loop.is_running() else None++def _run_on_mcp_loop(coro_or_factory, timeout: float = 30):"""Schedule a coroutine on the MCP loop and block until done.@@ -178,9 +185,8 @@ def _run_on_mcp_loop(coro_or_factory, timeout: float = 30):from tools.interrupt import is_interruptedfrom agent.async_utils import safe_schedule_threadsafe- with _core._lock:- loop = _core._mcp_loop- if loop is None or not loop.is_running():+ loop = _running_loop()+ if loop is None:if asyncio.iscoroutine(coro_or_factory):coro_or_factory.close()raise RuntimeError("MCP event loop is not running")@@ -240,11 +246,7 @@ def _signal_reconnect(server: Any) -> bool:if event is None:return Falseloop = _core._mcp_loop- if (- isinstance(event, asyncio.Event)- and loop is not None- and loop.is_running()- ):+ if isinstance(event, asyncio.Event) and loop is not None and loop.is_running():loop.call_soon_threadsafe(event.set)else:event.set()@@ -307,7 +309,6 @@ def _signal_reconnect_and_wait(loop = _core._mcp_loopif loop is None or not loop.is_running():return False-old_session = getattr(srv, "session", None)def _request_reconnect() -> None: