| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """Compatibility shim: prefer `httpx2`, fall back to `httpx` with a deprecation warning. | ||
|
|
||
| Mirrors the pattern from | ||
| [Kludex/starlette@508023b](https://github.com/Kludex/starlette/commit/508023b488b649d97c091eb60da1d8ef3636ee06) | ||
| and [pydantic/pydantic-ai#5664](https://github.com/pydantic/pydantic-ai/pull/5664). | ||
|
|
||
| `mcp` declares `httpx` (not `httpx2`) as a dependency, so unless the user installs `httpx2` | ||
| explicitly the fallback path is exercised. The MCP v2 cut will drop the fallback and bump the | ||
| dependency to `httpx2`. | ||
|
|
||
| The warning is emitted at module-import time and fires at most once per process via Python's | ||
| module cache. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import warnings | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from mcp.shared.exceptions import MCPDeprecationWarning | ||
|
|
||
| __all__ = ["httpx"] | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| import httpx as httpx | ||
| else: | ||
| try: | ||
| import httpx2 as httpx | ||
| except ImportError: | ||
| import httpx | ||
|
|
||
| warnings.warn( | ||
| "Using `httpx` with `mcp` is deprecated; install `httpx2` instead.", | ||
| MCPDeprecationWarning, | ||
| stacklevel=2, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| """Tests for the `httpx` → `httpx2` migration shim in `mcp.shared._httpx`. | ||
|
|
||
| `mcp` prefers `httpx2` and falls back to `httpx` with an `MCPDeprecationWarning` emitted at | ||
| the shim's import time. The lockfile pins `httpx` (not `httpx2`), so the canonical state of | ||
| the shim is the fallback path. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import importlib | ||
| import warnings | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| import mcp.shared._httpx | ||
| from mcp.shared.exceptions import MCPDeprecationWarning | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def _restore_shim_state(): | ||
| """Reload the shim after each test so a simulated `httpx2` doesn't leak into later tests.""" | ||
| yield | ||
| importlib.reload(mcp.shared._httpx) | ||
|
|
||
|
|
||
| def test_fallback_emits_warning() -> None: | ||
| with mock.patch.dict("sys.modules", {"httpx2": None}): | ||
| with pytest.warns(MCPDeprecationWarning, match=r"install `httpx2` instead"): | ||
| importlib.reload(mcp.shared._httpx) | ||
|
|
||
|
|
||
| def test_httpx2_present_is_silent() -> None: | ||
| import httpx | ||
|
|
||
| with mock.patch.dict("sys.modules", {"httpx2": httpx}): | ||
| with warnings.catch_warnings(): | ||
| warnings.simplefilter("error", MCPDeprecationWarning) | ||
| importlib.reload(mcp.shared._httpx) | ||
| assert mcp.shared._httpx.httpx is httpx |
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityCheck how we test integrations in logfire. It's a bit more cleaner than this.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.