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]

fix(mcp): name invalid_token challenges by the caller's requested spelling

mateo-berri764048750ecb
diff --git a/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py b/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py
index 8bd0d4efae8..281a555dc5c 100644
--- a/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py
+++ b/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py
@@ -1,5 +1,6 @@
import re
from collections.abc import Sequence
+from dataclasses import dataclass
from datetime import datetime, timezone
from types import MappingProxyType
from typing import TYPE_CHECKING, Final, cast
@@ -300,6 +301,16 @@ def _admission_failure_fallback(
raise exc
+@dataclass(frozen=True, slots=True)
+class DcrBridgeTarget:
+ """The single DCR-bridge server a request targets, paired with the exact name the caller
+ used to reach it (alias or server_name, whichever they typed), which is the spelling an
+ ``invalid_token`` challenge must echo back."""
+
+ requested_name: str
+ server: MCPServer
+
+
class MCPRequestHandler:
"""
Class to handle MCP request processing, including:
@@ -462,7 +473,8 @@ class MCPRequestHandler:
validated_user_api_key_auth,
mcp_server_auth_headers,
) = await MCPRequestHandler._admit_dcr_bridge_authorization(
- server=bridge_delegate_target,
+ server=bridge_delegate_target.server,
+ requested_name=bridge_delegate_target.requested_name,
authorization_value=oauth2_headers["Authorization"],
litellm_api_key=litellm_api_key,
mcp_server_auth_headers=mcp_server_auth_headers,
@@ -730,10 +742,10 @@ class MCPRequestHandler:
@staticmethod
def _single_dcr_bridge_delegate_target(
path: str, mcp_servers: list[str] | None, client_ip: str | None
- ) -> MCPServer | None:
+ ) -> DcrBridgeTarget | None:
"""The one DCR-bridge ``oauth_delegate`` server this request targets, or ``None``.
- Returns the server only when EXACTLY ONE target resolves and it is both
+ Returns the target only when EXACTLY ONE name resolves and its server is both
``is_oauth_delegate`` and ``is_dcr_bridge``. Fails closed (``None``) on a
multi-target request, an unresolved target, or a non-matching server, so the
envelope admission arm never fires for an aggregate scope or a server that did not
@@ -756,11 +768,12 @@ class MCPRequestHandler:
# neither cannot receive the forwarded token, so fail closed rather than admit-and-drop.
if not (server.server_name or server.alias):
return None
- return server
+ return DcrBridgeTarget(requested_name=target_names[0], server=server)
@staticmethod
async def _admit_dcr_bridge_delegate(
server: MCPServer,
+ requested_name: str,
authorization_value: str,
mcp_server_auth_headers: dict[str, dict[str, str]] | None,
request: Request,
@@ -808,13 +821,16 @@ class MCPRequestHandler:
new_headers: Final = {**(mcp_server_auth_headers or {}), **injected}
return admitted, new_headers
case BridgeEnvelopeInvalid() | NotBridgeEnvelope():
- raise MCPRequestHandler._dcr_bridge_invalid_token_challenge(server=server, request=request)
+ raise MCPRequestHandler._dcr_bridge_invalid_token_challenge(
+ requested_name=requested_name, request=request
+ )
case _:
assert_never(result)
@staticmethod
async def _admit_dcr_bridge_authorization(
server: MCPServer,
+ requested_name: str,
authorization_value: str,
litellm_api_key: str,
mcp_server_auth_headers: dict[str, dict[str, str]] | None, # mutable-ok: existing MCP sink shape
@@ -824,6 +840,7 @@ class MCPRequestHandler:
if is_bridge_envelope_shaped(authorization_value):
return await MCPRequestHandler._admit_dcr_bridge_delegate(
server=server,
+ requested_name=requested_name,
authorization_value=authorization_value,
mcp_server_auth_headers=mcp_server_auth_headers,
request=request,
@@ -834,17 +851,18 @@ class MCPRequestHandler:
except (HTTPException, ProxyException) as exc:
if not _is_litellm_auth_admission_error(exc):
raise
- raise MCPRequestHandler._dcr_bridge_invalid_token_challenge(server=server, request=request) from exc
+ raise MCPRequestHandler._dcr_bridge_invalid_token_challenge(
+ requested_name=requested_name, request=request
+ ) from exc
return admitted, mcp_server_auth_headers
@staticmethod
- def _dcr_bridge_invalid_token_challenge(server: MCPServer, request: Request) -> HTTPException:
- resource_name: Final = server.alias or server.server_name
- if resource_name is None:
- raise HTTPException(
- status_code=500,
- detail="Server misconfigured: MCP server has no routable name",
- )
+ def _dcr_bridge_invalid_token_challenge(requested_name: str, request: Request) -> HTTPException:
+ """The RFC 6750 ``invalid_token`` challenge for a failed bridge admission.
+
+ Named by the exact spelling the caller requested, matching the per-server well-known
+ document and the other challenge emitters, so ``resource_metadata`` always points at the
+ resource the client actually asked for even when alias and server_name differ."""
return HTTPException(
status_code=401,
detail="Invalid or expired credential",
@@ -852,7 +870,7 @@ class MCPRequestHandler:
{
"www-authenticate": get_passthrough_www_authenticate(
scope=request.scope,
- server_name=resource_name,
+ server_name=requested_name,
invalid_token=True,
)
}
diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py b/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py
index cb3133502f7..99e3e8d7413 100644
--- a/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py
+++ b/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py
@@ -5891,6 +5891,7 @@ class TestMCPDcrBridgeDelegateAdmission:
):
_auth, new_headers = await MCPRequestHandler._admit_dcr_bridge_delegate(
server=self._bridge_delegate_server(server_name="bridge_name", alias="bridge_alias"),
+ requested_name="bridge_name",
authorization_value=f"Bearer {envelope}",
mcp_server_auth_headers=attacker_forwarded,
request=self._mcp_request(),
@@ -5927,6 +5928,7 @@ class TestMCPDcrBridgeDelegateAdmission:
):
_auth, new_headers = await MCPRequestHandler._admit_dcr_bridge_delegate(
server=server,
+ requested_name="bridge_delegate_server",
authorization_value=f"Bearer {envelope}",
mcp_server_auth_headers=None,
request=self._mcp_request(),
@@ -6076,6 +6078,47 @@ class TestMCPDcrBridgeDelegateAdmission:
)
}
+ @pytest.mark.parametrize("requested_name", ["bridge_name", "bridge_alias"])
+ async def test_invalid_envelope_challenge_names_the_requested_spelling(self, requested_name):
+ """A server reachable under both its server_name and a distinct alias must challenge with
+ metadata for the exact spelling the caller used, matching the per-server well-known
+ document, so the client rediscovers against the resource it actually asked for."""
+ foreign = self._mint_bridge_envelope(master_key="a-different-master-key-entirely")
+ scope = {
+ "type": "http",
+ "method": "POST",
+ "path": f"/mcp/{requested_name}",
+ "headers": [
+ (b"host", b"testserver"),
+ (b"authorization", f"Bearer {foreign}".encode("latin-1")),
+ ],
+ }
+
+ with (
+ patch( # test-quality-ok: prove standard admission is never consulted for an envelope bearer
+ "litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp.user_api_key_auth",
+ new_callable=AsyncMock,
+ ) as mock_auth,
+ patch( # test-quality-ok: isolate the MCP registry, same seam as the sibling challenge tests
+ "litellm.proxy._experimental.mcp_server.mcp_server_manager.global_mcp_server_manager"
+ ) as mock_mgr,
+ patch("litellm.proxy.proxy_server.master_key", self._MASTER_KEY), # test-quality-ok: envelope keys derive from the proxy master_key module global
+ ):
+ mock_mgr.get_mcp_server_by_name.return_value = self._bridge_delegate_server(
+ server_name="bridge_name", alias="bridge_alias"
+ )
+ with pytest.raises(HTTPException) as exc_info:
+ await MCPRequestHandler.process_mcp_request(scope)
+
+ assert exc_info.value.status_code == 401
+ mock_auth.assert_not_called()
+ assert exc_info.value.headers == {
+ "www-authenticate": (
+ 'Bearer error="invalid_token", '
+ f'resource_metadata="http://testserver/.well-known/oauth-protected-resource/mcp/{requested_name}"'
+ )
+ }
+
async def test_non_envelope_bearer_on_bridge_server_returns_named_challenge(self):
"""A raw provider bearer cannot authorize a bridge route and triggers reauthorization."""
scope = {
@@ -6301,6 +6344,7 @@ class TestMCPDcrBridgeDelegateAdmission:
):
auth_result, new_headers = await MCPRequestHandler._admit_dcr_bridge_delegate(
server=self._bridge_delegate_server(),
+ requested_name="bridge_delegate_server",
authorization_value=f"Bearer {envelope}",
mcp_server_auth_headers=existing,
request=self._mcp_request(),
@@ -6325,6 +6369,7 @@ class TestMCPDcrBridgeDelegateAdmission:
with pytest.raises(HTTPException) as exc_info:
await MCPRequestHandler._admit_dcr_bridge_delegate(
server=self._bridge_delegate_server(),
+ requested_name="bridge_delegate_server",
authorization_value=f"Bearer {envelope}",
mcp_server_auth_headers=None,
request=self._mcp_request(),
@@ -6343,6 +6388,7 @@ class TestMCPDcrBridgeDelegateAdmission:
with pytest.raises(HTTPException) as exc_info:
await MCPRequestHandler._admit_dcr_bridge_delegate(
server=self._bridge_delegate_server(),
+ requested_name="bridge_delegate_server",
authorization_value=f"Bearer {envelope}",
mcp_server_auth_headers=None,
request=self._mcp_request(),