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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(mcp): scope authorization server issuer for named MCP servers (#37204)
yucheng-berri7984ae463693
diff --git a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.pyindex 86e97b55a8e..8911aafa33e 100644--- a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py+++ b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py@@ -2397,6 +2397,7 @@ def _build_oauth_authorization_server_response(request_base_url: Final = get_request_base_url(request)client_ip: Final = IPAddressUtils.get_mcp_client_ip(request)+ explicitly_named: Final = mcp_server_name is not None# When no server name provided, try to resolve the single OAuth2 serverif mcp_server_name is None:@@ -2415,8 +2416,10 @@ def _build_oauth_authorization_server_response(_raise_unless_oauth2_discovery_server(mcp_server, mcp_server_name, "not an OAuth authorization server")+ issuer: Final = f"{request_base_url}/{mcp_server_name}" if explicitly_named else request_base_url+return {- "issuer": request_base_url, # point to your proxy+ "issuer": issuer,"authorization_endpoint": authorization_endpoint,"token_endpoint": token_endpoint,"response_types_supported": ["code"],diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_discoverable_endpoints.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_discoverable_endpoints.pyindex 9bc84b43fc5..514e34d6241 100644--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_discoverable_endpoints.py+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_discoverable_endpoints.py@@ -3161,6 +3161,7 @@ def _create_oauth2_server(client_id="test_client_id",client_secret="test_client_secret",available_on_public_internet=True,+ delegate_auth_to_upstream: bool = False,):"""Helper to create a mock OAuth2 MCPServer."""from litellm.proxy._types import MCPTransport@@ -3180,6 +3181,7 @@ def _create_oauth2_server(token_url="https://provider.com/oauth/token",scopes=["read", "write"],available_on_public_internet=available_on_public_internet,+ delegate_auth_to_upstream=delegate_auth_to_upstream,)@@ -8169,6 +8171,69 @@ async def test_bare_origin_discovery_resolves_single_server_not_aggregate():global_mcp_server_manager.registry.clear()+async def test_named_discovery_issuer_matches_protected_resource_authorization_servers():+ """RFC 8414 requires the issuer to equal the authorization server identifier the client+ resolved the metadata from, which is the protected-resource authorization_servers entry."""+ from fastapi import Request++ from litellm.proxy._experimental.mcp_server.discoverable_endpoints import (+ _build_oauth_authorization_server_response,+ _build_oauth_protected_resource_response,+ )+ from litellm.proxy._experimental.mcp_server.mcp_server_manager import (+ global_mcp_server_manager,+ )++ global_mcp_server_manager.registry.clear()+ server = _create_oauth2_server(delegate_auth_to_upstream=True)+ global_mcp_server_manager.registry[server.server_id] = server++ mock_request = MagicMock(spec=Request)+ mock_request.base_url = "https://llm.example.com/"+ mock_request.headers = {}++ try:+ resource_response = await _build_oauth_protected_resource_response(+ request=mock_request, mcp_server_name="test_oauth", use_standard_pattern=True+ )+ authorization_response = _build_oauth_authorization_server_response(+ request=mock_request, mcp_server_name="test_oauth"+ )+ assert resource_response["authorization_servers"] == ["https://llm.example.com/test_oauth"]+ assert authorization_response["issuer"] == resource_response["authorization_servers"][0]+ finally:+ global_mcp_server_manager.registry.clear()+++async def test_openid_configuration_issuer_stays_bare_origin_for_single_oauth2_server():+ """The OIDC discovery document is served from the bare origin, so its issuer must stay the+ bare origin even when root discovery resolves the one configured OAuth2 server."""+ from fastapi import Request++ from litellm.proxy._experimental.mcp_server.discoverable_endpoints import (+ openid_configuration,+ )+ from litellm.proxy._experimental.mcp_server.mcp_server_manager import (+ global_mcp_server_manager,+ )++ global_mcp_server_manager.registry.clear()+ server = _create_oauth2_server()+ global_mcp_server_manager.registry[server.server_id] = server++ mock_request = MagicMock(spec=Request)+ mock_request.base_url = "https://llm.example.com/"+ mock_request.headers = {}++ try:+ response = await openid_configuration(mock_request)+ assert response["issuer"] == "https://llm.example.com"+ finally:+ global_mcp_server_manager.registry.clear()++def test_gateway_dcr_flow_routing_engages_only_for_llm_dcrc_clients(monkeypatch):"""The aggregate DCR arms engage for llm_dcrc_ client_ids (register always mints one,authorize/token route into the aggregate flow); a non-gateway client_id keeps the