Skip to content

The agent that grows with you

refactor(mcp): lifecycle loop reads its inputs once and stops re-deriving the recycle predicate

kshitijk4poor77779fe6427b
diff --git a/tools/mcp_tool_server_run.py b/tools/mcp_tool_server_run.py
index 97d4a9c9666..253c48b56bf 100644
--- a/tools/mcp_tool_server_run.py
+++ b/tools/mcp_tool_server_run.py
@@ -59,21 +59,21 @@ class MCPServerRunMixin:
"""Serve until a lifecycle event: ``"shutdown"`` (exits run), ``"reconnect"`` (session torn
down, transport re-entered; event cleared first) or ``"recycle"`` (stdio idle/lifetime
limit; restarts lazily on next call). Shutdown wins a tie. Remote transports run a
- keepalive (``ping``, list_tools fallback) every ``keepalive_interval`` (which must stay
- below the server's session TTL); stdio does so only when explicitly configured. A
- keepalive failure triggers a reconnect.
-
- Periodically sends a lightweight keepalive (``ping``, with a ``list_tools`` fallback for servers
- that don't implement the optional ping utility — see :meth:`_keepalive_probe`) to prevent
- TCP/session state from going stale during idle periods (#17003).
+ keepalive (``ping``, with a ``list_tools`` fallback for servers lacking the optional ping
+ utility — see :meth:`_keepalive_probe`) every ``keepalive_interval`` (which must stay
+ below the server's session TTL) so idle TCP/session state never goes stale (#17003);
+ stdio does so only when explicitly configured. A keepalive failure triggers a reconnect.
"""
+ is_http = self._is_http()
+ configured = self._config.get("keepalive_interval")
keepalive_interval = None
- if self._is_http() or "keepalive_interval" in self._config:
+ if is_http or configured is not None:
keepalive_interval = max(
_core._MIN_KEEPALIVE_INTERVAL,
- float(self._config.get("keepalive_interval", _core._DEFAULT_KEEPALIVE_INTERVAL)))
+ float(_core._DEFAULT_KEEPALIVE_INTERVAL if configured is None else configured))
shutdown_task, reconnect_task = self._event_waiters()
rpc_idle_task = None
+ waiters = [shutdown_task, reconnect_task]
try:
while True:
if self._recycle_if_due():
@@ -87,16 +87,13 @@ class MCPServerRunMixin:
if recycle_deadline is not None:
recycle_timeout = max(0.0, recycle_deadline - time.monotonic())
timeout = recycle_timeout if timeout is None else min(timeout, recycle_timeout)
- elif (not self._is_http() and self._rpc_lock.locked()
- and (self._idle_timeout_seconds is not None
- or self._max_lifetime_seconds is not None)):
+ elif not is_http and self._rpc_lock.locked():
# Recycle deadlines are intentionally hidden while an RPC is active. Without
# a default stdio keepalive timeout, lock release must wake this loop so the
- # now-visible deadline is evaluated instead of waiting forever.
+ # now-visible deadline is evaluated instead of waiting forever. (For a stdio
+ # server with no limits the wake is a harmless extra iteration.)
rpc_idle_task = rpc_idle_task or asyncio.ensure_future(self._wait_for_rpc_idle())
- waiters = {shutdown_task, reconnect_task}
- if rpc_idle_task is not None:
- waiters.add(rpc_idle_task)
+ waiters = [t for t in (shutdown_task, reconnect_task, rpc_idle_task) if t is not None]
done, _pending = await asyncio.wait(
waiters, timeout=timeout, return_when=asyncio.FIRST_COMPLETED)
if shutdown_task in done or reconnect_task in done:
@@ -133,9 +130,6 @@ class MCPServerRunMixin:
# Clear the rapid-drop budget (#62212).
self._mark_session_proven()
finally:
- waiters = [shutdown_task, reconnect_task]
- if rpc_idle_task is not None:
- waiters.append(rpc_idle_task)
await self._cancel_waiters(*waiters)
if self._shutdown_event.is_set():
self._fail_inflight_calls("shutdown")