fix(mcp): bound each OAuth metadata fetch instead of the whole startup discovery chain

The previous commit wrapped startup discovery in one MCP_METADATA_TIMEOUT
deadline. A slow but healthy chain (a few seconds per step) then gave up
before it finished, where it used to resolve. Each metadata GET now gets
its own MCP_METADATA_TIMEOUT bound instead, so a URL that keeps sending
bytes no longer hangs startup and a slow chain still resolves.
This commit is contained in:
mateo-berri 2026-10-20 13:28:48 -07:00
parent 488f90e7bf
commit 3370486faf
2 changed files with 29 additions and 45 deletions

View file

@ -2080,34 +2080,6 @@ class MCPServerManager:
def _registered_server(self, server: MCPServer) -> MCPServer:
return self.registry.get(server.server_id) or self.config_mcp_servers.get(server.server_id) or server
async def _discover_oauth_metadata_within_deadline(
self,
server_url: str | None,
issuer_anchor: str | None,
*,
allow_origin_fallback: bool,
warn_when_no_metadata: bool,
) -> MCPOAuthMetadata | None:
discovery: Final = (
self._fetch_issuer_anchored_oauth_metadata(issuer_anchor, server_url)
if issuer_anchor is not None
else self._descovery_metadata(
server_url=server_url or "",
allow_origin_fallback=allow_origin_fallback,
warn_when_no_metadata=warn_when_no_metadata,
)
)
try:
return await asyncio.wait_for(discovery, timeout=MCP_METADATA_TIMEOUT)
except asyncio.TimeoutError:
verbose_logger.warning(
"MCP OAuth discovery for %s timed out after %ss; "
"OAuth endpoints stay unresolved until a rebuild succeeds",
server_url,
MCP_METADATA_TIMEOUT,
)
return None
async def _discover_oauth_metadata_for_server(self, server: MCPServer) -> MCPOAuthMetadata | None:
manual_issuer: Final = _blank_to_none(server.issuer)
manual_authorization_url: Final = _blank_to_none(server.authorization_url)
@ -2655,10 +2627,11 @@ class MCPServerManager:
discovery_deferred = should_discover and not self._oauth_discovery_on_startup
if not should_discover or discovery_deferred:
mcp_oauth_metadata = None
elif use_issuer_anchor and manual_issuer is not None:
mcp_oauth_metadata = await self._fetch_issuer_anchored_oauth_metadata(manual_issuer, server_url)
else:
mcp_oauth_metadata = await self._discover_oauth_metadata_within_deadline(
server_url,
manual_issuer if use_issuer_anchor else None,
mcp_oauth_metadata = await self._descovery_metadata(
server_url=server_url,
allow_origin_fallback=is_discovery_auth_type,
warn_when_no_metadata=warn_on_empty_discovery,
)
@ -3147,16 +3120,16 @@ class MCPServerManager:
(is_discovery_auth_type and not has_all_upstream_oauth_fields) or obo_needs_discovery
)
discovery_deferred: Final = needs_discovery and not self._oauth_discovery_on_startup
mcp_oauth_metadata: Final = (
None
if not needs_discovery or discovery_deferred
else await self._discover_oauth_metadata_within_deadline(
server_url,
manual_issuer if use_issuer_anchor else None,
if not needs_discovery or discovery_deferred:
mcp_oauth_metadata: MCPOAuthMetadata | None = None
elif use_issuer_anchor and manual_issuer is not None:
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,
allow_origin_fallback=is_discovery_auth_type,
warn_when_no_metadata=warn_on_empty_discovery,
)
)
if use_issuer_anchor:
return mcp_oauth_metadata, discovery_deferred
gated_metadata: Final = (
@ -5138,8 +5111,8 @@ class MCPServerManager:
# internal MCP servers. Do not run them through user URL
# validation, but also do not follow redirects because the
# redirect target would not inherit the same-authority guarantee.
return await client.get(url, follow_redirects=False)
return await async_safe_get(client, url)
return await asyncio.wait_for(client.get(url, follow_redirects=False), timeout=MCP_METADATA_TIMEOUT)
return await asyncio.wait_for(async_safe_get(client, url), timeout=MCP_METADATA_TIMEOUT)
async def _descovery_metadata(
self,

View file

@ -11,7 +11,7 @@ import anyio
from collections.abc import AsyncIterator, Awaitable, Callable, Iterator
from datetime import datetime
from pathlib import Path
from types import SimpleNamespace
from types import MappingProxyType, SimpleNamespace
from typing import Any, Dict, Final, Literal, Optional, TypeVar, cast
from unittest.mock import AsyncMock, MagicMock, patch
@ -4642,17 +4642,28 @@ class TestMCPServerManager:
@pytest.mark.asyncio
@pytest.mark.parametrize("source", ["config", "database"])
async def test_startup_discovery_gives_up_on_metadata_that_never_finishes(
async def test_startup_discovery_moves_past_metadata_that_never_finishes(
self, source: str, monkeypatch: pytest.MonkeyPatch
):
body: Final = _OpenEventStream()
documents: Final = MappingProxyType(
{
"/.well-known/oauth-protected-resource": {"authorization_servers": ["https://stuck.example.com"]},
"/.well-known/oauth-authorization-server": {
"issuer": "https://stuck.example.com",
"authorization_endpoint": "https://stuck.example.com/authorize",
"token_endpoint": "https://stuck.example.com/token",
},
}
)
def respond(request: httpx.Request) -> httpx.Response:
if request.url.path == "/mcp":
return httpx.Response(200)
if request.url.path == "/.well-known/oauth-protected-resource/mcp":
return httpx.Response(200, headers={"Content-Type": "application/json"}, stream=body)
return httpx.Response(404)
document: Final = documents.get(request.url.path)
return httpx.Response(404) if document is None else httpx.Response(200, json=document)
monkeypatch.setenv("LITELLM_MCP_OAUTH_DISCOVERY_ON_STARTUP", "1")
manager: Final = MCPServerManager()
@ -4698,8 +4709,8 @@ class TestMCPServerManager:
)
assert body.read_started.is_set()
assert server.authorization_url is None
assert server.token_url is None
assert server.authorization_url == "https://stuck.example.com/authorize"
assert server.token_url == "https://stuck.example.com/token"
@pytest.mark.asyncio
async def test_fetch_single_authorization_server_metadata_supports_azure_issuer_path(