Skip to content

Commit 3d11f9f

Browse files
google-genai-botcopybara-github
authored andcommitted
fix(mcp): respect proxy environment variables in mTLS transport
`AsyncAuthorizedSession.configure_mtls_channel()` creates an `aiohttp.ClientSession` with `trust_env=False`, ignoring proxy environment variables. Resolve `proxy` and `proxy_auth` per request via `aiohttp.helpers.get_env_proxy_for_url` and pass them to `AsyncAuthorizedSession.request` without enabling `~/.netrc` auth lookup for target hosts. PiperOrigin-RevId: 996372348
1 parent 128faab commit 3d11f9f

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

‎src/google/adk/tools/mcp_tool/mcp_session_manager.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import asyncio
1818
from collections import deque
19+
import contextlib
1920
from contextlib import AbstractAsyncContextManager
2021
from contextlib import AsyncExitStack
2122
import contextvars
@@ -56,6 +57,13 @@ class AsyncAuthorizedSession: # pylint: disable=g-bad-classes
5657

5758
_AIO_SUPPORTED = False
5859

60+
try:
61+
import aiohttp.helpers
62+
import yarl
63+
except ImportError:
64+
aiohttp = None # type: ignore[assignment]
65+
yarl = None # type: ignore[assignment]
66+
5967
from pydantic import BaseModel
6068
from pydantic import ConfigDict
6169

@@ -664,12 +672,21 @@ async def handle_async_request(
664672
# prevent aiohttp from forcibly closing the stream after sse_read_timeout.
665673
timeout_val = 0.0
666674

675+
proxy = proxy_auth = None
676+
if aiohttp is not None and yarl is not None:
677+
with contextlib.suppress(LookupError):
678+
proxy, proxy_auth = await asyncio.to_thread(
679+
aiohttp.helpers.get_env_proxy_for_url,
680+
yarl.URL(str(request.url)),
681+
)
667682
auth_response: Any = await self._auth_session.request(
668683
method=request.method,
669684
url=str(request.url),
670685
data=content if content else None,
671686
headers=headers_dict,
672687
timeout=timeout_val,
688+
proxy=proxy,
689+
proxy_auth=proxy_auth,
673690
)
674691

675692
# google-auth-aio uses aiohttp internally, which automatically handles

‎tests/unittests/tools/mcp_tool/test_mcp_session_manager.py‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1799,7 +1799,8 @@ async def test_google_auth_async_transport_handle_request(self):
17991799
"GET", "https://example.com/api", headers={"x-test": "value"}
18001800
)
18011801

1802-
response = await transport.handle_async_request(request)
1802+
with patch.dict("os.environ", {}, clear=True):
1803+
response = await transport.handle_async_request(request)
18031804

18041805
assert response.status_code == 200
18051806
assert response.headers["content-type"] == "application/json"
@@ -1810,6 +1811,79 @@ async def test_google_auth_async_transport_handle_request(self):
18101811
data=None,
18111812
headers={"x-test": "value", "host": "example.com"},
18121813
timeout=30.0,
1814+
proxy=None,
1815+
proxy_auth=None,
1816+
)
1817+
1818+
@pytest.mark.asyncio
1819+
@pytest.mark.skipif(not AIO_SUPPORTED, reason="google.auth.aio not supported")
1820+
async def test_google_auth_async_transport_forwards_env_proxy(self):
1821+
"""Test that _GoogleAuthAsyncTransport resolves HTTPS_PROXY and passes proxy and proxy_auth per request."""
1822+
import aiohttp
1823+
from google.adk.tools.mcp_tool.mcp_session_manager import _GoogleAuthAsyncTransport
1824+
import yarl
1825+
1826+
mock_session = AsyncMock()
1827+
mock_auth_response = AsyncMock()
1828+
mock_auth_response.status_code = 200
1829+
mock_auth_response.headers = {"content-type": "application/json"}
1830+
mock_auth_response.content = AsyncMock()
1831+
mock_session.request.return_value = mock_auth_response
1832+
1833+
transport = _GoogleAuthAsyncTransport(mock_session)
1834+
request = httpx.Request("GET", "https://example.com/api")
1835+
1836+
with patch.dict(
1837+
"os.environ",
1838+
{"HTTPS_PROXY": "http://user:[email protected]:8080"},
1839+
clear=True,
1840+
):
1841+
await transport.handle_async_request(request)
1842+
1843+
mock_session.request.assert_called_once_with(
1844+
method="GET",
1845+
url="https://example.com/api",
1846+
data=None,
1847+
headers={"host": "example.com"},
1848+
timeout=30.0,
1849+
proxy=yarl.URL("http://proxy.example.com:8080"),
1850+
proxy_auth=aiohttp.BasicAuth("user", "pass"),
1851+
)
1852+
1853+
@pytest.mark.asyncio
1854+
@pytest.mark.skipif(not AIO_SUPPORTED, reason="google.auth.aio not supported")
1855+
async def test_google_auth_async_transport_respects_no_proxy(self):
1856+
"""Test that _GoogleAuthAsyncTransport passes proxy=None when host matches NO_PROXY."""
1857+
from google.adk.tools.mcp_tool.mcp_session_manager import _GoogleAuthAsyncTransport
1858+
1859+
mock_session = AsyncMock()
1860+
mock_auth_response = AsyncMock()
1861+
mock_auth_response.status_code = 200
1862+
mock_auth_response.headers = {"content-type": "application/json"}
1863+
mock_auth_response.content = AsyncMock()
1864+
mock_session.request.return_value = mock_auth_response
1865+
1866+
transport = _GoogleAuthAsyncTransport(mock_session)
1867+
request = httpx.Request("GET", "https://example.com/api")
1868+
1869+
with patch.dict(
1870+
"os.environ",
1871+
{
1872+
"HTTPS_PROXY": "http://proxy.example.com:8080",
1873+
"NO_PROXY": "example.com",
1874+
},
1875+
clear=True,
1876+
):
1877+
await transport.handle_async_request(request)
1878+
1879+
mock_session.request.assert_called_once_with(
1880+
method="GET",
1881+
url="https://example.com/api",
1882+
data=None,
1883+
headers={"host": "example.com"},
1884+
timeout=30.0,
1885+
proxy=None,
1886+
proxy_auth=None,
18131887
)
18141888

18151889

0 commit comments

Comments
 (0)