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): let a keyless-user active key mint a bridge envelope

Tin Chi Lo362c78e30864
diff --git a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py
index ef42fef312d..df1615aff99 100644
--- a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py
+++ b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py
@@ -329,26 +329,37 @@ def _litellm_key_from_request(request: Request) -> Optional[str]:
return None
-def _active_key_user_id(key_obj: "UserAPIKeyAuth") -> Optional[str]:
- """The key's ``user_id``, or ``None`` if the key is blocked or expired.
-
- The OAuth token endpoint is unauthenticated, so the presented key is validated here before its
- identity is trusted to key a stored credential; a revoked or expired key must not be able to
- write or overwrite the per-user OAuth token. ``get_key_object`` resolves a row without these
- checks (the main ``user_api_key_auth`` pipeline enforces them downstream, which this endpoint
- bypasses), so they are applied here. Deleted keys are already rejected upstream, where
- ``get_key_object`` raises on a row that no longer exists.
+def _key_is_active(key_obj: "UserAPIKeyAuth") -> bool:
+ """``True`` when the presented key is neither blocked nor past its expiry.
+
+ The OAuth token endpoint is unauthenticated, so the presented key is validated here before it is
+ trusted; a revoked or expired key must not mint a bridge envelope or write a stored credential.
+ ``get_key_object`` resolves a row without these checks (the main ``user_api_key_auth`` pipeline
+ enforces them downstream, which this endpoint bypasses), so they are applied here. Deleted keys
+ are already rejected upstream, where ``get_key_object`` raises on a row that no longer exists.
+
+ This is an active-state gate only; it deliberately does not require a ``user_id``. A valid
+ team-scoped or service-account key has no ``user_id`` yet is a legitimate credential, so gating
+ on ``user_id`` presence would wrongly reject it. Callers that need the user (the per-user token
+ store) derive it separately via :func:`_active_key_user_id`.
"""
if key_obj.blocked is True:
- return None
+ return False
expires = key_obj.expires
if expires is not None:
expiry = expires if isinstance(expires, datetime) else datetime.fromisoformat(expires)
if expiry.tzinfo is None or expiry.tzinfo.utcoffset(expiry) is None:
expiry = expiry.replace(tzinfo=timezone.utc)
if expiry < datetime.now(timezone.utc):
- return None
- return key_obj.user_id
+ return False
+ return True
+
+
+def _active_key_user_id(key_obj: "UserAPIKeyAuth") -> Optional[str]:
+ """The active key's ``user_id``, or ``None`` when the key is blocked/expired or simply has no
+ ``user_id`` (a team-scoped or service-account key). Used only by the per-user token store, which
+ needs a user to key the stored credential; the bridge mint uses the key hash and does not."""
+ return key_obj.user_id if _key_is_active(key_obj) else None
async def _resolve_active_litellm_key(request: Request) -> Optional[Tuple[str, "UserAPIKeyAuth"]]:
@@ -361,10 +372,11 @@ async def _resolve_active_litellm_key(request: Request) -> Optional[Tuple[str, "
cross-replica Redis hit deserializes to a plain ``dict`` rather than a ``UserAPIKeyAuth``; the
previous code read only ``Authorization`` and did ``getattr(cached, "user_id")`` with no
``model_type`` rehydration and no DB fallback, so it silently returned ``None``. The resolved key
- is validated (``_active_key_user_id``) before it is trusted, so a blocked or expired key resolves
- to ``None``. The returned hash is the value ``get_key_object`` and the cache/DB layer key the
- record by. Callers derive the ``user_id`` (per-user token store) or seal the hash (dcr_bridge
- envelope) from the result.
+ is validated (``_key_is_active``) before it is trusted, so a blocked or expired key resolves to
+ ``None``, while a valid team-scoped or service-account key (no ``user_id``) still resolves so it
+ can mint a bridge envelope. The returned hash is the value ``get_key_object`` and the cache/DB
+ layer key the record by. Callers derive the ``user_id`` (per-user token store) or seal the hash
+ (dcr_bridge envelope) from the result.
"""
token = _litellm_key_from_request(request)
if not token:
@@ -393,7 +405,7 @@ async def _resolve_active_litellm_key(request: Request) -> Optional[Tuple[str, "
type(exc).__name__,
)
return None
- if _active_key_user_id(key_obj) is None:
+ if not _key_is_active(key_obj):
return None
return key_hash, key_obj
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.py
index 5826d1f28b6..2bf5e49ec79 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
@@ -4865,6 +4865,35 @@ async def test_extract_active_key_hash_returns_hash_for_active_key(proxy_globals
assert await _extract_active_key_hash_from_request(request) == hash_token(key)
+async def test_extract_active_key_hash_returns_hash_for_active_key_without_user_id(proxy_globals):
+ """A valid team-scoped or service-account key has no user_id but is a legitimate credential, so it
+ must still resolve to a hash and be able to mint a bridge envelope. Gating the resolver on user_id
+ presence wrongly rejected these keys with invalid_request; the active-state gate now checks only
+ blocked and expiry, and the key hash (not the user) is what the mint seals. The per-user token
+ store still gets no user for such a key, since there is none to key a stored credential by."""
+ from litellm.proxy._experimental.mcp_server.discoverable_endpoints import (
+ _extract_active_key_hash_from_request,
+ _extract_user_id_from_request,
+ )
+ from litellm.proxy._types import UserAPIKeyAuth, hash_token
+ from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache
+
+ key = "sk-team-scoped-key"
+ cache = UserApiKeyCache()
+ await cache.async_set_cache(
+ hash_token(key),
+ UserAPIKeyAuth(token=hash_token(key), user_id=None, team_id="team-x"),
+ model_type=UserAPIKeyAuth,
+ )
+ proxy_globals.user_api_key_cache = cache
+ proxy_globals.prisma_client = object()
+
+ request = _token_request({"x-litellm-api-key": f"Bearer {key}"})
+ assert await _extract_active_key_hash_from_request(request) == hash_token(key)
+ assert await _extract_user_id_from_request(request) is None
+
+
@pytest.mark.asyncio
async def test_extract_active_key_hash_rejects_blocked_key(proxy_globals):
"""A blocked key must not yield a hash, so no gateway-bound envelope is minted for a revoked key;