feat(tools): Implement toolset auth for McpToolset, OpenAPIToolset, and others

Update existing toolsets to utilize the new toolset authentication framework. Key changes:
        - McpToolset: Add _auth_config instance variable, _get_auth_headers()
          method to build auth headers from exchanged credentials, and
          get_auth_config() override. Auth headers are now included when
          creating MCP sessions.
        - OpenAPIToolset: Add _auth_config and get_auth_config() to expose
          auth configuration to the framework.
        - ApplicationIntegrationToolset: Add _auth_config and get_auth_config().
        - APIHubToolset: Add _auth_config and get_auth_config().

When ADK resolves toolset auth before calling get_tools(), it populates exchanged_auth_credential on the auth_config. Toolsets can then use this credential when making authenticated requests.

Also update test fixtures in test_apihub_toolset.py to use real auth objects instead of mocks that fail pydantic validation.

Co-authored-by: Xiang (Sean) Zhou <seanzhougoogle@google.com>
PiperOrigin-RevId: 863764941
This commit is contained in:
Xiang (Sean) Zhou
2026-01-31 16:25:46 -08:00
committed by Copybara-Service
parent ee873cae2e
commit 798f65df86
6 changed files with 443 additions and 39 deletions
@@ -14,7 +14,12 @@
from unittest.mock import MagicMock
from fastapi.openapi.models import OAuth2
from fastapi.openapi.models import OAuthFlowAuthorizationCode
from fastapi.openapi.models import OAuthFlows
from google.adk.auth.auth_credential import AuthCredential
from google.adk.auth.auth_credential import AuthCredentialTypes
from google.adk.auth.auth_credential import OAuth2Auth
from google.adk.auth.auth_schemes import AuthScheme
from google.adk.tools.apihub_tool.apihub_toolset import APIHubToolset
from google.adk.tools.apihub_tool.clients.apihub_client import BaseAPIHubClient
@@ -67,13 +72,27 @@ def lazy_apihub_toolset():
# Fixture for auth scheme
@pytest.fixture
def mock_auth_scheme():
return MagicMock(spec=AuthScheme)
return OAuth2(
flows=OAuthFlows(
authorizationCode=OAuthFlowAuthorizationCode(
authorizationUrl='https://example.com/auth',
tokenUrl='https://example.com/token',
scopes={'read': 'Read access'},
)
)
)
# Fixture for auth credential
@pytest.fixture
def mock_auth_credential():
return MagicMock(spec=AuthCredential)
return AuthCredential(
auth_type=AuthCredentialTypes.OAUTH2,
oauth2=OAuth2Auth(
client_id='test_client_id',
client_secret='test_client_secret',
),
)
# Test cases