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:
@@ -440,6 +440,7 @@ class TestBuildOAuthAuthNonInteractive:
|
|||||||
|
|
||||||
def test_build_client_metadata_basic():
|
def test_build_client_metadata_basic():
|
||||||
"""_build_client_metadata returns metadata with expected defaults."""
|
"""_build_client_metadata returns metadata with expected defaults."""
|
||||||
|
pytest.importorskip("mcp")
|
||||||
from tools.mcp_oauth import _build_client_metadata, _configure_callback_port
|
from tools.mcp_oauth import _build_client_metadata, _configure_callback_port
|
||||||
|
|
||||||
cfg = {"client_name": "Test Client"}
|
cfg = {"client_name": "Test Client"}
|
||||||
@@ -453,6 +454,7 @@ def test_build_client_metadata_basic():
|
|||||||
|
|
||||||
def test_build_client_metadata_without_secret_is_public():
|
def test_build_client_metadata_without_secret_is_public():
|
||||||
"""Without client_secret, token endpoint auth is 'none' (public client)."""
|
"""Without client_secret, token endpoint auth is 'none' (public client)."""
|
||||||
|
pytest.importorskip("mcp")
|
||||||
from tools.mcp_oauth import _build_client_metadata, _configure_callback_port
|
from tools.mcp_oauth import _build_client_metadata, _configure_callback_port
|
||||||
|
|
||||||
cfg = {}
|
cfg = {}
|
||||||
@@ -463,6 +465,7 @@ def test_build_client_metadata_without_secret_is_public():
|
|||||||
|
|
||||||
def test_build_client_metadata_with_secret_is_confidential():
|
def test_build_client_metadata_with_secret_is_confidential():
|
||||||
"""With client_secret, token endpoint auth is 'client_secret_post'."""
|
"""With client_secret, token endpoint auth is 'client_secret_post'."""
|
||||||
|
pytest.importorskip("mcp")
|
||||||
from tools.mcp_oauth import _build_client_metadata, _configure_callback_port
|
from tools.mcp_oauth import _build_client_metadata, _configure_callback_port
|
||||||
|
|
||||||
cfg = {"client_secret": "shh"}
|
cfg = {"client_secret": "shh"}
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ logger = logging.getLogger(__name__)
|
|||||||
# Lazy imports -- MCP SDK with OAuth support is optional
|
# Lazy imports -- MCP SDK with OAuth support is optional
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
_OAUTH_AVAILABLE = False
|
_OAUTH_AVAILABLE=False
|
||||||
try:
|
try:
|
||||||
from mcp.client.auth import OAuthClientProvider
|
from mcp.client.auth import OAuthClientProvider
|
||||||
from mcp.shared.auth import (
|
from mcp.shared.auth import (
|
||||||
@@ -61,12 +61,16 @@ try:
|
|||||||
OAuthClientMetadata,
|
OAuthClientMetadata,
|
||||||
OAuthToken,
|
OAuthToken,
|
||||||
)
|
)
|
||||||
from pydantic import AnyUrl
|
|
||||||
|
|
||||||
_OAUTH_AVAILABLE = True
|
_OAUTH_AVAILABLE=True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
logger.debug("MCP OAuth types not available -- OAuth MCP auth disabled")
|
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
|
# Exceptions
|
||||||
|
|||||||
Reference in New Issue
Block a user