-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Deprecate httpx in favor of httpx2
#2693
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe8afee
Deprecate `httpx` in favor of `httpx2`
dsfaccini faa5298
Apply ruff-format
dsfaccini 786ee07
Correct framing: `httpx2` is on PyPI; CI exercises the fallback becau…
dsfaccini 3de2aa1
Simplify shim per @Kludex: emit warning at module import, drop the la…
dsfaccini 240b701
Move MCPDeprecationWarning to mcp.shared.exceptions per @Kludex review
dsfaccini 0ccc72d
Mirror Starlette docstring; rewrite shim tests in logfire's reload+pa…
dsfaccini 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
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,38 @@ | ||
| """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. `MCPDeprecationWarning` lives in `mcp.shared._warnings` so pytest's | ||
| `filterwarnings` parser can resolve the category symbol without importing this shim. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import warnings | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from mcp.shared._warnings import MCPDeprecationWarning | ||
|
|
||
| __all__ = ["MCPDeprecationWarning", "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, | ||
| ) |
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,19 @@ | ||
| """Warning categories emitted by the `mcp` package.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| __all__ = ["MCPDeprecationWarning"] | ||
|
|
||
|
|
||
| class MCPDeprecationWarning(UserWarning): | ||
| """Deprecation warning emitted by the `mcp` package. | ||
|
|
||
| Subclasses `UserWarning` (not `DeprecationWarning`) so it is visible by default — | ||
| `DeprecationWarning` is silenced at the Python level for non-`__main__` callers. | ||
|
|
||
| Defined in its own module so that pytest's `filterwarnings` parser can resolve the | ||
| category symbol without importing any side-effecting module — e.g. | ||
| `mcp.shared._httpx` emits a `MCPDeprecationWarning` at import time when only `httpx` | ||
| is installed, and resolving the symbol through that module would fire the warning | ||
| before pytest finishes registering the filter. | ||
| """ |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check how we test integrations in logfire. It's a bit more cleaner than this. |
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,61 @@ | ||
| """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. Today the lockfile pins `httpx` (not `httpx2`), so importing the shim | ||
| exercises the fallback. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import importlib | ||
| import sys | ||
| import warnings | ||
|
|
||
| import pytest | ||
|
|
||
| from mcp.shared._warnings import MCPDeprecationWarning | ||
|
|
||
|
|
||
| def _force_reimport_shim() -> None: | ||
| """Drop the cached shim module so the next import re-runs its top-level code.""" | ||
| sys.modules.pop("mcp.shared._httpx", None) | ||
|
|
||
|
|
||
| def test_fallback_emits_warning_at_import(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """With only `httpx` installed, importing the shim emits `MCPDeprecationWarning`.""" | ||
| monkeypatch.delitem(sys.modules, "httpx2", raising=False) | ||
| _force_reimport_shim() | ||
|
|
||
| from collections.abc import Mapping, Sequence | ||
|
|
||
| real_import = __import__ | ||
|
|
||
| def fake_import( | ||
| name: str, | ||
| globals: Mapping[str, object] | None = None, | ||
| locals: Mapping[str, object] | None = None, | ||
| fromlist: Sequence[str] = (), | ||
| level: int = 0, | ||
| ) -> object: | ||
| if name == "httpx2": | ||
| raise ImportError("simulated: httpx2 not installed") | ||
| return real_import(name, globals, locals, fromlist, level) | ||
|
|
||
| monkeypatch.setattr("builtins.__import__", fake_import) | ||
| with pytest.warns(MCPDeprecationWarning, match=r"install `httpx2` instead"): | ||
| importlib.import_module("mcp.shared._httpx") | ||
|
|
||
|
|
||
| def test_httpx2_present_is_silent(monkeypatch: pytest.MonkeyPatch) -> None: | ||
| """When `httpx2` is importable, the shim selects it and emits no warning.""" | ||
| import httpx as real_httpx | ||
|
|
||
| monkeypatch.setitem(sys.modules, "httpx2", real_httpx) | ||
| _force_reimport_shim() | ||
|
|
||
| with warnings.catch_warnings(record=True) as caught: | ||
| warnings.simplefilter("always", MCPDeprecationWarning) | ||
| reloaded = importlib.import_module("mcp.shared._httpx") | ||
|
|
||
| assert reloaded.httpx is real_httpx | ||
| assert [w for w in caught if issubclass(w.category, MCPDeprecationWarning)] == [] |
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.
Don't we have an _exceptions.py module? If we do, we should put this there.
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.
yeahp that turned out to be the case