The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): redirect header stripper builds httpx.AsyncClient at call time so proxy mounts stay observable
teknium17b6e2ac375b6
diff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.pyindex 94206e6fc83..db4dc5774bc 100644--- a/tests/tools/test_mcp_tool.py+++ b/tests/tools/test_mcp_tool.py@@ -3295,7 +3295,8 @@ class TestMCPDiscoveryCrossProcessLock:class TestRedirectHeaderStripper:"""Cross-origin redirect header boundary (portable Agent Plugins v1).- The stripper is an ``AsyncClient`` subclass overriding ``_build_redirect_request`` — the only+ The stripper is an ``AsyncClient`` factory overriding ``_build_redirect_request`` on each built+ client — the onlyseam that sees the actual redirect follow-up (``response.next_request`` is unset when responsehooks fire) without also touching non-redirect traffic (a request hook would strip the OAuthauth flow's token/registration calls to a different-origin authorization server)."""@@ -3304,10 +3305,10 @@ class TestRedirectHeaderStripper:headers, location):from tools.mcp_tool_errors import _make_redirect_header_stripper- client_cls = _make_redirect_header_stripper(+ build_client = _make_redirect_header_stripper(httpx, httpx.URL("https://origin.example.test/mcp"),strict=strict, configured_header_names=configured)- client = client_cls()+ client = build_client()request = httpx.Request("GET", "https://origin.example.test/mcp", headers=headers)response = httpx.Response(302, headers={"location": location}, request=request)return client._build_redirect_request(request, response)diff --git a/tools/mcp_tool_errors.py b/tools/mcp_tool_errors.pyindex 0ab2a74a1fe..1d73820eedf 100644--- a/tools/mcp_tool_errors.py+++ b/tools/mcp_tool_errors.py@@ -264,21 +264,27 @@ def _apply_identity_header(server_name: str, config: dict, headers: dict) -> dicdef _make_redirect_header_stripper(httpx_mod, original_url, *, strict: bool = False,configured_header_names: "set[str] | frozenset[str]" = frozenset()):- """AsyncClient subclass enforcing the redirect credential boundary: on a cross-origin redirect+ """Client factory enforcing the redirect credential boundary: on a cross-origin redirectfollow-up it strips ``Authorization``; with *strict* (Agent Plugins v1 ``strict_redirect_headers``)every configured header (lowercase names in *configured_header_names*) is stripped too — v1 forbidsforwarding them cross-origin.- This MUST live on ``_build_redirect_request``: ``response.next_request`` is unset when response- event hooks fire (httpx populates it later in the redirect loop), so a response hook can never- mutate the follow-up; and a *request* hook would fire on non-redirect traffic too — the OAuth- auth flow yields token/metadata/registration requests through the same client, often to a- different-origin authorization server whose own credentials must NOT be stripped."""+ The factory builds ``httpx_mod.AsyncClient(**kwargs)`` — resolved at call time, so the proxy+ ``mounts=`` / ``transport=`` the caller passes reach the SDK's real client class (and anything a+ caller swapped in for it) unchanged — and installs the boundary on that instance's+ ``_build_redirect_request``. This MUST live on ``_build_redirect_request``: ``response.next_request``+ is unset when response event hooks fire (httpx populates it later in the redirect loop), so a+ response hook can never mutate the follow-up; and a *request* hook would fire on non-redirect traffic+ too — the OAuth auth flow yields token/metadata/registration requests through the same client, often+ to a different-origin authorization server whose own credentials must NOT be stripped."""origin = (original_url.scheme, original_url.host, original_url.port)- class _Client(httpx_mod.AsyncClient):- def _build_redirect_request(self, request, response):- next_request = super()._build_redirect_request(request, response)+ def _build_client(**kwargs):+ client = httpx_mod.AsyncClient(**kwargs)+ base_build = getattr(type(client), "_build_redirect_request", None)++ def _build_redirect_request(request, response):+ next_request = base_build(client, request, response)target = next_request.urlif (target.scheme, target.host, target.port) != origin:headers = next_request.headers@@ -289,7 +295,10 @@ def _make_redirect_header_stripper(httpx_mod, original_url, *, strict: bool = Fadel headers[_name]return next_request- return _Client+ client._build_redirect_request = _build_redirect_request+ return client++ return _build_client# Wire-body cap, applied at the httpx transport before the SDK buffers/JSON-parses a response. Adiff --git a/tools/mcp_tool_transport.py b/tools/mcp_tool_transport.pyindex 663f61f83f4..7b30c66f620 100644--- a/tools/mcp_tool_transport.py+++ b/tools/mcp_tool_transport.py@@ -364,11 +364,11 @@ class MCPServerTransportMixin:# cross-origin hop natively, but forwards every other configured header verbatim — under# strict_redirect_headers those must not leave the configured origin on the probe either.probe_transport = _httpx.AsyncHTTPTransport(verify=ssl_verify, **_present(cert=client_cert))- _client_cls = _make_redirect_header_stripper(+ _build_client = _make_redirect_header_stripper(_httpx, _httpx.URL(url), strict=strict_redirect_headers,configured_header_names={key.lower() for key in probe_headers})try:- async with _client_cls(+ async with _build_client(follow_redirects=True, timeout=_httpx.Timeout(timeout), transport=probe_transport,**_present(mounts=_mcp_proxy_mounts(_httpx, url, ssl_verify, client_cert, self.name))) as client:resp = await client.head(url, headers=probe_headers) # cheapest; GET on 405/501@@ -477,7 +477,7 @@ class MCPServerTransportMixin:# Explicit AsyncClient matching the SDK's create_mcp_http_client defaults; MUST come from the# SDK's httpx (httpx2 on mcp >= 2.0) since the SDK sends its own Requests through it.httpx = _core.sdk_httpx()- _client_cls = _make_redirect_header_stripper(+ _build_client = _make_redirect_header_stripper(httpx, httpx.URL(url), strict=strict_cfg_headers, configured_header_names=configured_header_names)# verify/cert live on the inner transport: a custom transport= makes client-level TLS kwargs# inert — and suppresses httpx's own proxy auto-detection, hence the explicit mounts=.@@ -491,7 +491,7 @@ class MCPServerTransportMixin:@asynccontextmanagerasync def _owned_client_streams(): # the SDK skips cleanup when http_client is provided- async with _client_cls(**client_kwargs) as http_client:+ async with _build_client(**client_kwargs) as http_client:async with _core.streamable_http_client(url, http_client=http_client) as streams:yield streamsreturn _owned_client_streams()