Skip to content

Commit 564cbe6

Browse files
committed
fix(mcp): Preserve existing auth in mTLS transport
HTTP header names are case-insensitive, but the mTLS auth bridge only checked for an exact Authorization key before adding ADC credentials. httpx can pass the existing header as lowercase authorization, causing the user token to be missed. Fixes #6200
1 parent a238884 commit 564cbe6

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ async def before_request(
261261
)
262262
return
263263

264-
if 'Authorization' in headers:
264+
if any(key.lower() == 'authorization' for key in headers):
265265
logger.debug('Authorization header already present, not overwriting')
266266
return
267267

tests/unittests/tools/mcp_tool/test_mcp_session_manager.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,25 @@ def mock_refresh(req):
12981298

12991299
assert headers["Authorization"] == "Bearer refreshed_token"
13001300

1301+
@pytest.mark.skipif(not AIO_SUPPORTED, reason="google.auth.aio not supported")
1302+
@pytest.mark.asyncio
1303+
async def test_before_request_preserves_lowercase_authorization_header(self):
1304+
"""An existing lowercase authorization header prevents token injection."""
1305+
from google.adk.tools.mcp_tool.mcp_session_manager import _RefreshableAsyncCredentials
1306+
1307+
mock_creds = Mock()
1308+
mock_creds.expired = True
1309+
mock_creds.token = "service_account_token"
1310+
mock_creds.refresh = Mock()
1311+
1312+
credentials = _RefreshableAsyncCredentials(mock_creds)
1313+
headers = {"authorization": "Bearer user_token"}
1314+
1315+
await credentials.before_request(None, "GET", "http://example.com", headers)
1316+
1317+
assert headers == {"authorization": "Bearer user_token"}
1318+
mock_creds.refresh.assert_not_called()
1319+
13011320

13021321
class TestGoogleAuthAsyncByteStream:
13031322

0 commit comments

Comments
 (0)