Skip to content

Commit 1d09f2f

Browse files
bokelleyclaude
andcommitted
fix: use TYPE_CHECKING for optional MCP imports
Use TYPE_CHECKING pattern to avoid "Cannot assign to a type" error while maintaining proper type hints for optional MCP dependency. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ca7fd7b commit 1d09f2f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/adcp/protocols/mcp.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
import logging
77
import time
88
from contextlib import AsyncExitStack
9-
from typing import Any
9+
from typing import TYPE_CHECKING, Any
1010
from urllib.parse import urlparse
1111

1212
logger = logging.getLogger(__name__)
1313

14-
try:
14+
if TYPE_CHECKING:
1515
from mcp import ClientSession
16+
17+
try:
18+
from mcp import ClientSession as _ClientSession
1619
from mcp.client.sse import sse_client
1720
from mcp.client.streamable_http import streamablehttp_client
1821

1922
MCP_AVAILABLE = True
2023
except ImportError:
21-
ClientSession = None # type: ignore[assignment]
2224
MCP_AVAILABLE = False
2325

2426
from adcp.exceptions import ADCPConnectionError, ADCPTimeoutError
@@ -38,7 +40,7 @@ def __init__(self, *args: Any, **kwargs: Any):
3840
self._session: Any = None
3941
self._exit_stack: Any = None
4042

41-
async def _get_session(self) -> Any:
43+
async def _get_session(self) -> ClientSession: # type: ignore[name-defined]
4244
"""
4345
Get or create MCP client session with URL fallback handling.
4446
@@ -94,7 +96,7 @@ async def _get_session(self) -> Any:
9496
)
9597

9698
self._session = await self._exit_stack.enter_async_context(
97-
ClientSession(read, write)
99+
_ClientSession(read, write)
98100
)
99101

100102
# Initialize the session

0 commit comments

Comments
 (0)