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): keep scopes resource-driven under a pinned issuer

Tin Chi Lob3af125078bc
diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
index 42c9921ecf7..70180d5d469 100644
--- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
+++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
@@ -1165,7 +1165,7 @@ class MCPServerManager:
if not should_discover:
mcp_oauth_metadata = None
elif manual_issuer is not None and is_discovery_auth_type:
- mcp_oauth_metadata = await self._fetch_issuer_anchored_oauth_metadata(manual_issuer)
+ mcp_oauth_metadata = await self._fetch_issuer_anchored_oauth_metadata(manual_issuer, server_url)
else:
mcp_oauth_metadata = await self._descovery_metadata(
server_url=server_url,
@@ -1608,7 +1608,10 @@ class MCPServerManager:
if not needs_discovery:
mcp_oauth_metadata = None
elif manual_issuer is not None and is_discovery_auth_type:
- mcp_oauth_metadata = await self._fetch_issuer_anchored_oauth_metadata(manual_issuer)
+ mcp_oauth_metadata = await self._fetch_issuer_anchored_oauth_metadata(
+ manual_issuer,
+ server_url, # type: ignore[arg-type]
+ )
else:
mcp_oauth_metadata = await self._descovery_metadata(
server_url=server_url, # type: ignore[arg-type]
@@ -3372,25 +3375,34 @@ class MCPServerManager:
return metadata
return None
- async def _fetch_issuer_anchored_oauth_metadata(self, issuer: str) -> Optional[MCPOAuthMetadata]:
- """RFC 8414 issuer-anchored discovery.
+ async def _fetch_issuer_anchored_oauth_metadata(self, issuer: str, server_url: str) -> Optional[MCPOAuthMetadata]:
+ """RFC 8414 issuer-anchored discovery for the OAuth endpoints, with resource-driven scopes.
Fetch authorization-server metadata from the admin-configured issuer's own origin and adopt
- it only when the document self-attests that same issuer (RFC 8414 §3.3). Because the trust
- anchor is the pinned issuer rather than anything the MCP resource server advertises, the
- resulting token_endpoint/registration_endpoint/scopes are authoritative for that issuer and
- cannot be substituted by a compromised resource. Fails closed (returns None) on a §3.3
- mismatch or a fetch failure. The issuer is passed as its own ``server_url`` so the fetch is
- treated as same-authority and is not subject to the resource-scoped SSRF shortcut.
+ its ``token_endpoint``/``registration_endpoint`` only when the document self-attests that same
+ issuer (RFC 8414 §3.3). Because the trust anchor is the pinned issuer rather than anything the
+ MCP resource advertises, the endpoints are authoritative for that issuer and cannot be
+ substituted by a compromised resource. Fails closed (returns None) on a §3.3 mismatch or a
+ fetch failure. The issuer is passed as its own ``server_url`` so the endpoint fetch is treated
+ as same-authority and is not subject to the resource-scoped SSRF shortcut.
+
+ Scopes are NOT taken from the issuer document. Per the MCP authorization spec Scope Selection
+ Strategy and RFC 9728, the scopes a client requests are resource-driven (the WWW-Authenticate
+ challenge or the protected-resource ``scopes_supported``), so the resource's advertised scopes
+ are fetched separately and used; the resource can influence only the requested scope, which
+ the authorization server and user consent bound (RFC 6749 §3.3), never the token endpoint.
"""
metadata = await self._fetch_single_authorization_server_metadata(issuer, issuer, require_issuer=issuer)
if metadata is None:
verbose_logger.warning(
"MCP OAuth issuer-anchored discovery for issuer %s yielded no metadata whose issuer "
- "matched (RFC 8414 §3.3); OAuth endpoints/scopes stay unresolved until a rebuild succeeds",
+ "matched (RFC 8414 §3.3); OAuth endpoints stay unresolved until a rebuild succeeds",
issuer,
)
- return metadata
+ return None
+ resource_metadata = await self._descovery_metadata(server_url, allow_origin_fallback=False)
+ resource_scopes = resource_metadata.scopes if resource_metadata else None
+ return metadata.model_copy(update={"scopes": resource_scopes})
async def _fetch_single_authorization_server_metadata(
self, issuer_url: str, server_url: str, require_issuer: Optional[str] = None
diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py
index 3dcd05e5936..75f77c1ec6d 100644
--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py
+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py
@@ -1231,12 +1231,12 @@ class TestMCPServerManager:
assert built.scopes == ["read"]
@pytest.mark.asyncio
- async def test_build_from_table_issuer_anchor_ignores_resource_rooted_discovery(self):
- """When an admin configures an issuer, discovery is anchored on that issuer's own metadata
- (RFC 8414), not on the resource-rooted RFC 9728 chain a compromised MCP resource controls.
- The resource-rooted _descovery_metadata must not run at all, and the authoritative token_url,
- registration_url, and scopes come from the issuer document. This closes the mix-up where a
- compromised resource echoes the pinned authorize endpoint to smuggle its own token_url."""
+ async def test_build_from_table_uses_issuer_anchored_endpoints_when_issuer_configured(self):
+ """When an admin configures an issuer, the build takes its endpoints from the issuer-anchored
+ fetch (RFC 8414 §3.3) rather than the resource-rooted corroboration path. The build path does
+ not call _descovery_metadata directly; the issuer-anchored helper is responsible for combining
+ issuer endpoints with resource-driven scopes internally, and is invoked with the server url so
+ it can fetch those scopes."""
manager = MCPServerManager()
row = LiteLLM_MCPServerTable(
server_id="issuer-anchored-1",
@@ -1250,7 +1250,7 @@ class TestMCPServerManager:
updated_at=datetime.now(),
)
- authoritative = MCPOAuthMetadata(
+ resolved = MCPOAuthMetadata(
authorization_url="https://idp.example.com/authorize",
token_url="https://idp.example.com/token",
registration_url="https://idp.example.com/register",
@@ -1258,12 +1258,12 @@ class TestMCPServerManager:
)
resource_rooted = AsyncMock(return_value=MCPOAuthMetadata(token_url="https://attacker.example.com/steal"))
with (
- patch.object(manager, "_fetch_issuer_anchored_oauth_metadata", new=AsyncMock(return_value=authoritative)) as anchored,
+ patch.object(manager, "_fetch_issuer_anchored_oauth_metadata", new=AsyncMock(return_value=resolved)) as anchored,
patch.object(manager, "_descovery_metadata", new=resource_rooted),
):
built = await manager.build_mcp_server_from_table(row, credentials_are_encrypted=False)
- anchored.assert_awaited_once_with("https://idp.example.com")
+ anchored.assert_awaited_once_with("https://idp.example.com", "https://up.example.com/mcp")
resource_rooted.assert_not_awaited()
assert built.issuer == "https://idp.example.com"
assert built.authorization_url == "https://idp.example.com/authorize"
@@ -1271,6 +1271,41 @@ class TestMCPServerManager:
assert built.registration_url == "https://idp.example.com/register"
assert built.scopes == ["read", "write"]
+ @pytest.mark.asyncio
+ async def test_fetch_issuer_anchored_metadata_takes_endpoints_from_issuer_scopes_from_resource(self):
+ """The issuer-anchored helper adopts token_endpoint/registration_endpoint from the pinned
+ issuer's own §3.3-validated document, but the scopes are resource-driven: it fetches the
+ resource's advertised scopes and uses those, not the issuer document's scopes_supported. This
+ keeps endpoint trust anchored on the issuer while scope selection stays resource-driven per the
+ MCP Scope Selection Strategy."""
+ manager = MCPServerManager()
+
+ issuer_document = MCPOAuthMetadata(
+ authorization_url="https://idp.example.com/authorize",
+ token_url="https://idp.example.com/token",
+ registration_url="https://idp.example.com/register",
+ scopes=["as.everything"],
+ )
+ resource_document = MCPOAuthMetadata(scopes=["resource.read"])
+ with (
+ patch.object(
+ manager, "_fetch_single_authorization_server_metadata", new=AsyncMock(return_value=issuer_document)
+ ) as issuer_fetch,
+ patch.object(manager, "_descovery_metadata", new=AsyncMock(return_value=resource_document)) as resource_fetch,
+ ):
+ result = await manager._fetch_issuer_anchored_oauth_metadata(
+ "https://idp.example.com", "https://up.example.com/mcp"
+ )
+
+ issuer_fetch.assert_awaited_once_with(
+ "https://idp.example.com", "https://idp.example.com", require_issuer="https://idp.example.com"
+ )
+ resource_fetch.assert_awaited_once()
+ assert result is not None
+ assert result.token_url == "https://idp.example.com/token"
+ assert result.registration_url == "https://idp.example.com/register"
+ assert result.scopes == ["resource.read"]
+
@pytest.mark.asyncio
async def test_build_from_table_issuer_anchor_fails_closed_without_falling_back_to_resource(self):
"""A configured issuer whose metadata does not validate (RFC 8414 §3.3 mismatch or fetch