From 9ced18b69518c09cb2ceaebf29267309cc0a5544 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Fri, 14 Nov 2025 18:56:14 -0800 Subject: [PATCH] perf: use reusable http client --- .../mcp_server/mcp_server_manager.py | 28 +++++++++---------- litellm/types/llms/custom_http.py | 1 + 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py index b42364b6db8..71f1fab13bc 100644 --- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py +++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py @@ -14,8 +14,8 @@ import re from typing import Any, Dict, List, Optional, Set, Tuple, Union, cast from urllib.parse import urlparse -from fastapi import HTTPException import httpx +from fastapi import HTTPException from httpx import HTTPStatusError from mcp.types import CallToolRequestParams as MCPCallToolRequestParams from mcp.types import CallToolResult @@ -24,6 +24,7 @@ from mcp.types import Tool as MCPTool from litellm._logging import verbose_logger from litellm.exceptions import BlockedPiiEntityError, GuardrailRaisedException from litellm.experimental_mcp_client.client import MCPClient +from litellm.llms.custom_httpx.http_handler import get_async_httpx_client from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import ( MCPRequestHandler, ) @@ -42,10 +43,9 @@ from litellm.proxy._types import ( MCPTransportType, UserAPIKeyAuth, ) -from litellm.proxy.common_utils.encrypt_decrypt_utils import ( - decrypt_value_helper, -) +from litellm.proxy.common_utils.encrypt_decrypt_utils import decrypt_value_helper from litellm.proxy.utils import ProxyLogging +from litellm.types.llms.custom_http import httpxSpecialProvider from litellm.types.mcp import MCPAuth, MCPStdioConfig from litellm.types.mcp_server.mcp_server_manager import ( MCPInfo, @@ -386,12 +386,12 @@ class MCPServerManager: ) # Update tool name to server name mapping (for both prefixed and base names) - self.tool_name_to_mcp_server_name_mapping[ - base_tool_name - ] = server_prefix - self.tool_name_to_mcp_server_name_mapping[ - prefixed_tool_name - ] = server_prefix + self.tool_name_to_mcp_server_name_mapping[base_tool_name] = ( + server_prefix + ) + self.tool_name_to_mcp_server_name_mapping[prefixed_tool_name] = ( + server_prefix + ) registered_count += 1 verbose_logger.debug( @@ -729,11 +729,9 @@ class MCPServerManager: """Discover OAuth metadata by following RFC 9728 (protected resource metadata discovery).""" try: - async with httpx.AsyncClient( - timeout=10.0, follow_redirects=False - ) as client: - response = await client.get(server_url) - response.raise_for_status() + client = get_async_httpx_client(llm_provider=httpxSpecialProvider.MCP) + response = await client.get(server_url) + response.raise_for_status() verbose_logger.warning( "MCP OAuth discovery unexpectedly succeeded for %s; server did not challenge", server_url, diff --git a/litellm/types/llms/custom_http.py b/litellm/types/llms/custom_http.py index 6b86f55e025..46956fedc8e 100644 --- a/litellm/types/llms/custom_http.py +++ b/litellm/types/llms/custom_http.py @@ -22,6 +22,7 @@ class httpxSpecialProvider(str, Enum): PromptFactory = "prompt_factory" SSO_HANDLER = "sso_handler" Search = "search" + MCP = "mcp" VerifyTypes = Union[str, bool, ssl.SSLContext]