Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Merge branch 'litellm_internal_staging' of https://github.com/BerriAI/litellm into litellm_fix_mcp_test_connection_oauth_bearer

mateo-berric4ca4268a2c0
diff --cc litellm/proxy/_experimental/mcp_server/rest_endpoints.py
index e7ba7168ba0,37474f85fe7..bf0767b4069
--- a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py
+++ b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py
@@@ -1143,45 -1149,11 +1151,50 @@@ if MCP_AVAILABLE
scopes: Final[list[str] | None] = scopes_raw if isinstance(scopes_raw, list) else None
return client_id, client_secret, scopes
+ _STAGED_AUTH_VALUE_AUTH_TYPES: Final = frozenset(
+ (MCPAuth.api_key, MCPAuth.bearer_token, MCPAuth.basic, MCPAuth.authorization)
+ )
+
+ @dataclass(frozen=True, slots=True)
+ class _StagedServerTest:
+ request: NewMCPServerRequest
+ mcp_auth_header: str | None
+ oauth2_headers: dict[str, str] | None
+
+ def _stage_server_test(new_mcp_server_request: NewMCPServerRequest, headers: Headers) -> _StagedServerTest:
+ """
+ Resolve the credentials a not-yet-saved server config carries for a preview call.
+
+ Both preview endpoints (``/test/connection`` and ``/test/tools/list``) must hand the
+ temporary client the same credentials, or a server that the saved connection reaches
+ fine fails one of them.
+ """
+ from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import (
+ MCPRequestHandler,
+ )
+
+ request: Final = _inherit_credentials_from_existing_server(new_mcp_server_request)
+ mcp_auth_header: Final = (
+ request.credentials.get("auth_value")
+ if request.auth_type in _STAGED_AUTH_VALUE_AUTH_TYPES and isinstance(request.credentials, dict)
+ else None
+ )
+ # Authorization doubles as the admission fallback (LITELLM_API_KEY_HEADER_NAME_SECONDARY):
+ # when the primary x-litellm-api-key header is absent, the Authorization value is the
+ # caller's LiteLLM key, not an upstream token, and must never be forwarded upstream.
+ oauth2_headers: Final = (
+ MCPRequestHandler._get_oauth2_headers_from_headers(headers)
+ if request.auth_type in _UPSTREAM_OAUTH_DISCOVERY_AUTH_TYPES
+ and headers.get(MCPRequestHandler.LITELLM_API_KEY_HEADER_NAME_PRIMARY)
+ else None
+ )
+ return _StagedServerTest(request=request, mcp_auth_header=mcp_auth_header, oauth2_headers=oauth2_headers)
+
+ async def _list_tools_within(client: MCPClient, deadline: float) -> list[MCPTool] | None:
+ with anyio.move_on_after(deadline):
+ return await client.list_tools(raise_on_error=True)
+ return None
+
async def _execute_with_mcp_client(
request: NewMCPServerRequest,
operation: Callable[..., Awaitable[Mapping[str, object]]],