-
Notifications
You must be signed in to change notification settings - Fork 5
feat: support MCP SDK 2.x alongside 1.x #33
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
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,88 @@ | ||
| """Compatibility across MCP SDK major versions. | ||
|
|
||
| SDK 2.0 changed three things this proxy depends on: | ||
|
|
||
| 1. the HTTP client library moved from ``httpx`` to ``httpx2``; | ||
| 2. ``streamable_http_client`` yields ``(read, write)`` where 1.x yielded | ||
| ``(read, write, get_session_id)``; | ||
| 3. ``SessionMessage.message`` is the JSON-RPC model itself, where 1.x wrapped | ||
| it in the ``JSONRPCMessage`` pydantic root model. | ||
|
|
||
| Only (1) and (3) need a shim here -- (2) is absorbed by a starred unpack at | ||
| the one call site in ``__main__``. | ||
|
|
||
| The HTTP library is read back off the SDK module rather than imported by a | ||
| guessed name. ``httpx`` and ``httpx2`` install side by side: httpx2 does not | ||
| replace httpx, and databricks-sdk and others still pull httpx in. So | ||
| ``try: import httpx2`` would hand an SDK 1.x transport a client built from a | ||
| library that SDK never imported -- and because the proxy owns the client it | ||
| passes to ``streamable_http_client``, that mismatch surfaces as a type error | ||
| deep inside the SDK's request path rather than at import time. Asking the SDK | ||
| which module it bound is the only answer that cannot drift. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from types import ModuleType | ||
| from typing import TYPE_CHECKING, Any, Protocol | ||
|
|
||
| from mcp.client import streamable_http as _sdk_streamable_http | ||
|
|
||
| __all__ = ["MessageReceiveStream", "MessageSendStream", "httpx", "jsonrpc_payload"] | ||
|
|
||
| #: Module names to look for on the SDK transport, newest SDK first. | ||
| _HTTPX_MODULE_NAMES = ("httpx2", "httpx") | ||
|
|
||
|
|
||
| def _resolve_httpx() -> ModuleType: | ||
| """Return the HTTP client module the installed MCP SDK builds clients from.""" | ||
| for name in _HTTPX_MODULE_NAMES: | ||
| module = getattr(_sdk_streamable_http, name, None) | ||
| if isinstance(module, ModuleType): | ||
| return module | ||
| raise RuntimeError( | ||
| "uc-mcp-proxy: cannot tell which HTTP client library this MCP SDK uses. " | ||
| f"Expected mcp.client.streamable_http to import one of {_HTTPX_MODULE_NAMES}." | ||
| ) | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| # httpx2 mirrors the httpx API for every name the proxy touches, so the | ||
| # httpx stubs describe both. Only the runtime object has to match the SDK. | ||
| import httpx | ||
| else: | ||
| httpx = _resolve_httpx() | ||
|
|
||
|
|
||
| def jsonrpc_payload(message: Any) -> Any: | ||
| """Return the JSON-RPC model carried by a ``SessionMessage``. | ||
|
|
||
| SDK 1.x wraps it in the ``JSONRPCMessage`` root model; 2.0 stores the | ||
| ``JSONRPCRequest``/``JSONRPCNotification``/... directly. Both are reached | ||
| through the attribute that exists, so neither version is special-cased. | ||
| """ | ||
| payload = message.message | ||
| return getattr(payload, "root", payload) | ||
|
|
||
|
|
||
| class MessageReceiveStream(Protocol): | ||
| """The read half of a transport, as the bridge actually uses it. | ||
|
|
||
| Structural on purpose. SDK 1.x hands out anyio ``MemoryObjectReceiveStream`` | ||
| objects and 2.0 hands out its own context-carrying wrappers; naming either | ||
| concrete class here would type-check against one SDK and fail on the other. | ||
| """ | ||
|
|
||
| def __aiter__(self) -> Any: ... | ||
|
|
||
| async def __anext__(self) -> Any: ... | ||
|
|
||
| async def aclose(self) -> None: ... | ||
|
|
||
|
|
||
| class MessageSendStream(Protocol): | ||
| """The write half of a transport, as the bridge actually uses it.""" | ||
|
|
||
| async def send(self, item: Any, /) -> None: ... | ||
|
|
||
| async def aclose(self) -> None: ... |
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,33 @@ | ||
| """Version-agnostic helpers for building SDK objects in tests. | ||
|
|
||
| SDK 2.0 turned ``JSONRPCMessage`` from a pydantic root model into a plain | ||
| union alias, so ``JSONRPCMessage(...)`` is no longer callable and the | ||
| ``SessionMessage.message`` it produced no longer has ``.root``. Tests build | ||
| messages by validating a raw wire payload through the SDK's own schema, which | ||
| accepts both shapes and keeps the JSON that actually goes on the wire as the | ||
| source of truth rather than a hand-built object graph. | ||
|
|
||
| ``httpx`` is re-exported from the proxy's compat module so tests construct | ||
| requests, responses and transports from the same library the installed SDK | ||
| uses -- otherwise ``isinstance`` checks inside the proxy would compare objects | ||
| from two different HTTP libraries. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from mcp.shared.message import SessionMessage | ||
| from mcp.types import JSONRPCMessage | ||
| from pydantic import TypeAdapter | ||
|
|
||
| from uc_mcp_proxy._compat import httpx, jsonrpc_payload | ||
|
|
||
| __all__ = ["httpx", "jsonrpc_payload", "session_message"] | ||
|
|
||
| _JSONRPC_ADAPTER: TypeAdapter[Any] = TypeAdapter(JSONRPCMessage) | ||
|
|
||
|
|
||
| def session_message(payload: dict[str, Any]) -> SessionMessage: | ||
| """Build a ``SessionMessage`` from a raw JSON-RPC ``payload`` dict.""" | ||
| return SessionMessage(_JSONRPC_ADAPTER.validate_python(payload)) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blocking: this removes the only major cap. The shim is tested for 1.x/2.x, but a future 3.x would now resolve automatically. Please use
mcp>=1.24,<3: 1.24 is the actual lower API requirement, and<3limits installs to the majors this PR supports.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in commit 4b1adf9. The MCP dependency is now bounded to >=1.24,<3, matching the actual API floor and the two supported SDK majors. Please re-review.