Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: add explicit timeouts to httpx clients to prevent ReadTimeout errors (#8538)
github-actions[bot]5fbf8f93e28e
diff --git a/letta/llm_api/google_ai_client.py b/letta/llm_api/google_ai_client.pyindex 8a90e7cb9..b1281b62b 100644--- a/letta/llm_api/google_ai_client.py+++ b/letta/llm_api/google_ai_client.py@@ -82,7 +82,8 @@ async def google_ai_get_model_list_async(# Determine if we need to close the client at the endclose_client = Falseif client is None:- client = httpx.AsyncClient()+ # Use explicit timeout to prevent httpx.ReadTimeout errors+ client = httpx.AsyncClient(timeout=httpx.Timeout(30.0, connect=10.0))close_client = Truetry:@@ -129,7 +130,8 @@ async def google_ai_get_model_details_async(# Determine if we need to close the client at the endclose_client = Falseif client is None:- client = httpx.AsyncClient()+ # Use explicit timeout to prevent httpx.ReadTimeout errors+ client = httpx.AsyncClient(timeout=httpx.Timeout(30.0, connect=10.0))close_client = Truetry:diff --git a/letta/llm_api/openai.py b/letta/llm_api/openai.pyindex 8c8692b82..8dc2318d8 100644--- a/letta/llm_api/openai.py+++ b/letta/llm_api/openai.py@@ -72,7 +72,8 @@ async def openai_get_model_list_async(# Use provided client or create a new oneclose_client = Falseif client is None:- client = httpx.AsyncClient()+ # Use explicit timeout to prevent httpx.ReadTimeout errors+ client = httpx.AsyncClient(timeout=httpx.Timeout(30.0, connect=10.0))close_client = Truetry:diff --git a/letta/services/mcp/sse_client.py b/letta/services/mcp/sse_client.pyindex 0327fc269..ee8dfc17b 100644--- a/letta/services/mcp/sse_client.py+++ b/letta/services/mcp/sse_client.py@@ -7,6 +7,7 @@ from mcp.client.sse import sse_clientfrom letta.functions.mcp_client.types import SSEServerConfigfrom letta.log import get_loggerfrom letta.services.mcp.base_client import AsyncBaseMCPClient+from letta.settings import tool_settings# see: https://modelcontextprotocol.io/quickstart/userMCP_CONFIG_TOPLEVEL_KEY = "mcpServers"@@ -33,10 +34,12 @@ class AsyncSSEMCPClient(AsyncBaseMCPClient):headers[self.AGENT_ID_HEADER] = self.agent_id# Use OAuth provider if available, otherwise use regular headers+ # Pass timeout to prevent httpx.ReadTimeout errors on slow connections+ timeout = tool_settings.mcp_connect_to_server_timeoutif self.oauth_provider:- sse_cm = sse_client(url=server_config.server_url, headers=headers if headers else None, auth=self.oauth_provider)+ sse_cm = sse_client(url=server_config.server_url, headers=headers if headers else None, auth=self.oauth_provider, timeout=timeout)else:- sse_cm = sse_client(url=server_config.server_url, headers=headers if headers else None)+ sse_cm = sse_client(url=server_config.server_url, headers=headers if headers else None, timeout=timeout)sse_transport = await self.exit_stack.enter_async_context(sse_cm)self.stdio, self.write = sse_transportdiff --git a/letta/services/mcp/streamable_http_client.py b/letta/services/mcp/streamable_http_client.pyindex e2f256f5e..9d29b4a6b 100644--- a/letta/services/mcp/streamable_http_client.py+++ b/letta/services/mcp/streamable_http_client.py@@ -1,3 +1,4 @@+from datetime import timedeltafrom typing import Optionalfrom mcp import ClientSession@@ -7,6 +8,7 @@ from mcp.client.streamable_http import streamablehttp_clientfrom letta.functions.mcp_client.types import BaseServerConfig, StreamableHTTPServerConfigfrom letta.log import get_loggerfrom letta.services.mcp.base_client import AsyncBaseMCPClient+from letta.settings import tool_settingslogger = get_logger(__name__)@@ -38,16 +40,18 @@ class AsyncStreamableHTTPMCPClient(AsyncBaseMCPClient):headers[self.AGENT_ID_HEADER] = self.agent_id# Use OAuth provider if available, otherwise use regular headers+ # Pass timeout to prevent httpx.ReadTimeout errors on slow connections+ timeout = timedelta(seconds=tool_settings.mcp_connect_to_server_timeout)if self.oauth_provider:streamable_http_cm = streamablehttp_client(- server_config.server_url, headers=headers if headers else None, auth=self.oauth_provider+ server_config.server_url, headers=headers if headers else None, auth=self.oauth_provider, timeout=timeout)else:# Use streamablehttp_client context manager with headers if providedif headers:- streamable_http_cm = streamablehttp_client(server_config.server_url, headers=headers)+ streamable_http_cm = streamablehttp_client(server_config.server_url, headers=headers, timeout=timeout)else:- streamable_http_cm = streamablehttp_client(server_config.server_url)+ streamable_http_cm = streamablehttp_client(server_config.server_url, timeout=timeout)read_stream, write_stream, _ = await self.exit_stack.enter_async_context(streamable_http_cm)