fix(mcp): decouple AnyUrl import from mcp dependency

AnyUrl was imported inside the same try block as mcp.client.auth, so
when the mcp package was not installed, AnyUrl was undefined and
_build_client_metadata raised NameError at runtime.

Moved the AnyUrl import to its own try/except block so it's available
whenever pydantic is installed (which is a core dependency), regardless
of whether the mcp SDK is present.

Also added pytest.importorskip('mcp') to the three
test_build_client_metadata tests that exercise _build_client_metadata,
since that function depends on OAuthClientMetadata from the mcp package.
This commit is contained in:
vominh1919 2026-04-26 19:52:03 +07:00 committed by Teknium
parent 0d563621fb
commit 135b4c8b35
2 changed files with 10 additions and 3 deletions

View file

@ -53,7 +53,7 @@ logger = logging.getLogger(__name__)
# Lazy imports -- MCP SDK with OAuth support is optional
# ---------------------------------------------------------------------------
_OAUTH_AVAILABLE = False
_OAUTH_AVAILABLE=False
try:
from mcp.client.auth import OAuthClientProvider
from mcp.shared.auth import (
@ -61,12 +61,16 @@ try:
OAuthClientMetadata,
OAuthToken,
)
from pydantic import AnyUrl
_OAUTH_AVAILABLE = True
_OAUTH_AVAILABLE=True
except ImportError:
logger.debug("MCP OAuth types not available -- OAuth MCP auth disabled")
try:
from pydantic import AnyUrl
except ImportError:
AnyUrl = None # type: ignore[assignment, misc]
# ---------------------------------------------------------------------------
# Exceptions