From 4297c02b121180e239a61c483ac8477cc557d4ef Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 1 Jun 2026 12:44:16 -0700 Subject: [PATCH] refac --- backend/open_webui/env.py | 9 +++++++++ backend/open_webui/utils/mcp/client.py | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/open_webui/env.py b/backend/open_webui/env.py index 8f7180e606..01b1e9dde3 100644 --- a/backend/open_webui/env.py +++ b/backend/open_webui/env.py @@ -545,6 +545,15 @@ else: except Exception: AIOHTTP_CLIENT_TIMEOUT_TOOL_SERVER = AIOHTTP_CLIENT_TIMEOUT +# Timeout (in seconds) for the MCP session.initialize() handshake. +# The handshake performs a list-tools round-trip and can take tens of +# seconds on cold-start servers or servers exposing many tools. +MCP_INITIALIZE_TIMEOUT = os.getenv('MCP_INITIALIZE_TIMEOUT', '10') +try: + MCP_INITIALIZE_TIMEOUT = int(MCP_INITIALIZE_TIMEOUT) +except (ValueError, TypeError): + MCP_INITIALIZE_TIMEOUT = 10 + #################################### # AIOHTTP Connection Pool diff --git a/backend/open_webui/utils/mcp/client.py b/backend/open_webui/utils/mcp/client.py index a4691750db..d6f5265bd3 100644 --- a/backend/open_webui/utils/mcp/client.py +++ b/backend/open_webui/utils/mcp/client.py @@ -11,7 +11,7 @@ from mcp import ClientSession from mcp.client.auth import OAuthClientProvider, TokenStorage from mcp.client.streamable_http import streamablehttp_client from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken -from open_webui.env import AIOHTTP_CLIENT_SESSION_TOOL_SERVER_SSL, AIOHTTP_CLIENT_TIMEOUT_TOOL_SERVER +from open_webui.env import AIOHTTP_CLIENT_SESSION_TOOL_SERVER_SSL, AIOHTTP_CLIENT_TIMEOUT_TOOL_SERVER, MCP_INITIALIZE_TIMEOUT def _build_httpx_client(headers=None, timeout=None, auth=None, verify=True): @@ -69,7 +69,7 @@ class MCPClient: self._session_context = ClientSession(read_stream, write_stream) # pylint: disable=W0201 self.session = await exit_stack.enter_async_context(self._session_context) - with anyio.fail_after(10): + with anyio.fail_after(MCP_INITIALIZE_TIMEOUT): await self.session.initialize() self.exit_stack = exit_stack.pop_all() except Exception as e: