-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: mcp v2 compatible changes under a flag #3708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
poshinchen
merged 4 commits into
strands-agents:main
from
poshinchen:feat/mcp-v2-compat
Aug 28, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d817ea2
feat(mcp): add compatibility layer for the mcp 2.x line
poshinchen 6dbc9ca
fix(mcp): close the owned HTTPX client and type-check _compat on both…
poshinchen 13bca50
test(mcp): exercise the 2.x compat branches on the 1.x test line
poshinchen 53aa5a1
ci(python): verify the package against the mcp 2.x line
poshinchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """Compatibility layer over the `mcp` 1.x and 2.x lines. | ||
|
|
||
| The official `mcp` package renamed and relocated several public names in 2.0. | ||
| Import any version-dependent name from this module instead of from `mcp` | ||
| directly so the rest of the codebase stays version-agnostic. Branch on | ||
| `MCP_V2` only where behavior differs between the two lines; pure renames are | ||
| resolved here once. | ||
|
|
||
| mypy note: only one `mcp` line is installed at a time, so each try/except | ||
| branch below is an `attr-defined` error when checked against the other line. | ||
| The branch-level ignores cover whichever line mypy runs under, and the | ||
| per-module `warn_unused_ignores` override in pyproject.toml silences the | ||
| ignores the installed line doesn't need. | ||
| """ | ||
|
|
||
| from collections.abc import AsyncIterator | ||
| from contextlib import AbstractAsyncContextManager, asynccontextmanager | ||
| from typing import Any | ||
|
|
||
| import httpx | ||
| from mcp import ClientSession | ||
|
|
||
| __all__ = ["MCP_V2", "GetSessionIdCallback", "MCPError", "streamable_http_transport"] | ||
|
|
||
| # Feature-probed rather than version-parsed so pre-releases and backports | ||
| # resolve by capability: `ClientSession.discover` is the 2.x replacement for | ||
| # the removed initialize handshake. | ||
|
poshinchen marked this conversation as resolved.
|
||
| MCP_V2: bool = hasattr(ClientSession, "discover") | ||
|
|
||
| try: | ||
|
poshinchen marked this conversation as resolved.
|
||
| from mcp.shared.exceptions import MCPError # type: ignore[attr-defined] | ||
| except ImportError: | ||
| # mcp 1.x spells the class McpError | ||
| from mcp.shared.exceptions import McpError as MCPError # type: ignore[attr-defined, no-redef] | ||
|
|
||
| try: | ||
| from mcp.client.streamable_http import GetSessionIdCallback # type: ignore[attr-defined] | ||
| except ImportError: | ||
| # mcp 2.x removed protocol sessions, so its transports never yield a | ||
| # session-id callback; the alias survives only to type 1.x transports. | ||
| from collections.abc import Callable | ||
|
|
||
| GetSessionIdCallback = Callable[[], str | None] # type: ignore[misc, assignment] | ||
|
|
||
|
|
||
| def streamable_http_transport( | ||
| url: str, headers: dict[str, str] | None = None, auth: httpx.Auth | None = None | ||
| ) -> AbstractAsyncContextManager[Any]: | ||
| """Open a streamable HTTP client transport on either `mcp` major line. | ||
|
|
||
| `mcp` 2.x replaced `streamablehttp_client(url, headers=..., auth=...)` with | ||
| `streamable_http_client(url, http_client=...)`, which takes a | ||
| pre-configured HTTPX client instead of loose header and auth kwargs. This | ||
| adapter keeps the 1.x-style call shape for both lines. | ||
|
|
||
| The imports are resolved at call time because each name exists on only | ||
| one major line. | ||
|
|
||
| Args: | ||
| url: The MCP server endpoint URL. | ||
| headers: Optional HTTP headers to send with each request. | ||
| auth: Optional HTTPX authentication handler for each request. | ||
|
|
||
| Returns: | ||
| An async context manager yielding the transport's read/write streams. | ||
| """ | ||
| if MCP_V2: | ||
| from mcp.client.streamable_http import ( # type: ignore[attr-defined] | ||
| create_mcp_http_client, | ||
| streamable_http_client, | ||
| ) | ||
|
|
||
| # `streamable_http_client` closes an HTTPX client only when it created | ||
| # it (`client_provided` check in mcp 2.x), so a caller-provided client | ||
| # must be closed by the caller: enter both context managers together | ||
| # so the client's lifetime is bound to the transport's. | ||
| @asynccontextmanager | ||
| async def _owned_client_transport() -> AsyncIterator[Any]: | ||
| async with ( | ||
| create_mcp_http_client(headers=headers, auth=auth) as http_client, | ||
|
poshinchen marked this conversation as resolved.
|
||
| streamable_http_client(url=url, http_client=http_client) as transport_streams, | ||
| ): | ||
| yield transport_streams | ||
|
|
||
| return _owned_client_transport() | ||
|
|
||
| from mcp.client.streamable_http import streamablehttp_client # type: ignore[attr-defined] | ||
|
|
||
| return streamablehttp_client(url=url, headers=headers, auth=auth) # type: ignore[no-any-return] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.