From e04364eae067d2b52c06ac7fc4a29a4d75c82784 Mon Sep 17 00:00:00 2001 From: SonicBotMan Date: Sat, 18 Apr 2026 19:30:07 +0800 Subject: [PATCH] feat(mcp): make max_reconnect_retries configurable per server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow MCP servers to override the default reconnect retry limit (5) via config: mcp_servers: my-server: url: http://localhost:8764 max_reconnect_retries: 50 # for servers with long maintenance windows Previously _MAX_RECONNECT_RETRIES was a module-level constant (5), requiring code changes to adjust. This is problematic for servers behind load balancers with session expiry (e.g., wiki-brain behind reverse proxy) where short reconnect limits cause silent failures. The default remains 5 — fully backward compatible. --- tools/mcp_tool.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/mcp_tool.py b/tools/mcp_tool.py index fa8b945ca..99b934a83 100644 --- a/tools/mcp_tool.py +++ b/tools/mcp_tool.py @@ -1033,6 +1033,7 @@ class MCPServerTask: """ self._config = config self.tool_timeout = config.get("timeout", _DEFAULT_TOOL_TIMEOUT) + self.max_reconnect_retries = config.get("max_reconnect_retries", _MAX_RECONNECT_RETRIES) self._auth_type = (config.get("auth") or "").lower().strip() # Set up sampling handler if enabled and SDK types are available @@ -1106,18 +1107,18 @@ class MCPServerTask: return retries += 1 - if retries > _MAX_RECONNECT_RETRIES: + if retries > self.max_reconnect_retries: logger.warning( "MCP server '%s' failed after %d reconnection attempts, " "giving up: %s", - self.name, _MAX_RECONNECT_RETRIES, exc, + self.name, self.max_reconnect_retries, exc, ) return logger.warning( "MCP server '%s' connection lost (attempt %d/%d), " "reconnecting in %.0fs: %s", - self.name, retries, _MAX_RECONNECT_RETRIES, + self.name, retries, self.max_reconnect_retries, backoff, exc, ) await asyncio.sleep(backoff)