Repository navigation
Conversation
|
Warning Gemini encountered an error creating the review. You can try again by commenting |
|
Here is the summary of changes. You are about to add 5 region tags.
This comment is generated by snippet-bot.
|
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces several new Python scripts and corresponding tests under agentregistry/adk/ demonstrating how to authenticate and resolve bindings for MCP toolsets and remote A2A agents using the Agent Registry ADK. Feedback on the changes suggests simplifying the custom authentication flow in auth_remote_a2a_agent.py by utilizing the official google.auth.transport.httpx.Auth class instead of implementing a custom httpx.Auth class, which also helps streamline the imported dependencies.
| from collections.abc import Iterator | ||
| import os | ||
|
|
||
| from google.adk.agents.remote_a2a_agent import RemoteA2aAgent | ||
| from google.adk.integrations.agent_registry import AgentRegistry | ||
| import google.auth | ||
| from google.auth.transport.requests import Request | ||
| import httpx |
There was a problem hiding this comment.
Instead of importing collections.abc.Iterator and google.auth.transport.requests.Request to build a custom authentication flow, you can use the official google.auth.transport.httpx.Auth class. This simplifies the imports and avoids pulling in requests transport dependencies when using httpx.
| from collections.abc import Iterator | |
| import os | |
| from google.adk.agents.remote_a2a_agent import RemoteA2aAgent | |
| from google.adk.integrations.agent_registry import AgentRegistry | |
| import google.auth | |
| from google.auth.transport.requests import Request | |
| import httpx | |
| import os | |
| from google.adk.agents.remote_a2a_agent import RemoteA2aAgent | |
| from google.adk.integrations.agent_registry import AgentRegistry | |
| import google.auth | |
| import google.auth.transport.httpx | |
| import httpx |
There was a problem hiding this comment.
Not applying this change: google.auth.transport.httpx does not exist in google-auth (the available transports are requests, urllib3, grpc, and _aiohttp_requests), so importing it would fail with ModuleNotFoundError. The custom GoogleAuth(httpx.Auth) subclass using self.creds.before_request(Request(), ...) is required for httpx.
There was a problem hiding this comment.
+1, agreed — google.auth.transport.httpx doesn't exist, and using self.creds.before_request(Request(), ...) here is actually a nice improvement over the manual token refresh in our current doc
| # [START agentregistry_adk_auth_remote_a2a_agent] | ||
| class GoogleAuth(httpx.Auth): | ||
| def __init__(self) -> None: | ||
| self.creds, _ = google.auth.default() | ||
|
|
||
| def auth_flow(self, request: httpx.Request) -> Iterator[httpx.Request]: | ||
| self.creds.before_request( | ||
| Request(), | ||
| request.method, | ||
| str(request.url), | ||
| request.headers, | ||
| ) | ||
| yield request | ||
|
|
||
| # Initialize the registry client | ||
| project_id = os.environ.get("GOOGLE_CLOUD_PROJECT") | ||
| location = os.environ.get("GOOGLE_CLOUD_LOCATION", "global") | ||
|
|
||
| if not project_id: | ||
| raise ValueError("GOOGLE_CLOUD_PROJECT environment variable not set.") | ||
|
|
||
| registry = AgentRegistry( | ||
| project_id=project_id, | ||
| location=location, | ||
| ) | ||
|
|
||
| # Configure the HTTP client with GoogleAuth and a 60-second timeout | ||
| httpx_client = httpx.AsyncClient( | ||
| auth=GoogleAuth(), | ||
| timeout=httpx.Timeout(60.0), | ||
| ) |
There was a problem hiding this comment.
By using the official google.auth.transport.httpx.Auth helper, you can completely remove the custom GoogleAuth class boilerplate. This makes the code cleaner, more robust, and easier to maintain.
# [START agentregistry_adk_auth_remote_a2a_agent]
# Initialize the registry client
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
location = os.environ.get("GOOGLE_CLOUD_LOCATION", "global")
if not project_id:
raise ValueError("GOOGLE_CLOUD_PROJECT environment variable not set.")
registry = AgentRegistry(
project_id=project_id,
location=location,
)
# Configure the HTTP client with Google credentials and a 60-second timeout
credentials, _ = google.auth.default()
httpx_client = httpx.AsyncClient(
auth=google.auth.transport.httpx.Auth(credentials),
timeout=httpx.Timeout(60.0),
)There was a problem hiding this comment.
Not applying this change: google.auth.transport.httpx does not exist in google-auth (the available transports are requests, urllib3, grpc, and _aiohttp_requests), so importing it would fail with ModuleNotFoundError. The custom GoogleAuth(httpx.Auth) subclass using self.creds.before_request(Request(), ...) is required for httpx.
evelyn-goo
left a comment
There was a problem hiding this comment.
Thanks for putting together the auth samples
| def resolve_auth_bindings( | ||
| mcp_server_name: str = "mcpServers/SERVER_ID", | ||
| model: str = "gemini-2.5-flash", | ||
| ) -> LlmAgent: | ||
| """Fetches an authenticated MCP toolset with resolved auth provider bindings.""" | ||
| # [START agentregistry_adk_resolve_auth_bindings] |
There was a problem hiding this comment.
issue (blocking): Across four of the five samples in this PR (auth_mcp_toolset.py, auth_remote_a2a_agent.py, resolve_auth_bindings.py, and resolve_auth_bindings_3lo.py), # [START ...] starts inside the function body, which hides:
- Critical imports like CredentialManager, GcpAuthProvider, AgentRegistry, and LlmAgent.
- All default placeholder arguments ("mcpServers/SERVER_ID", "agents/AGENT_ID", "https://REDIRECT_URI", and "gemini-2.5-flash"), so the rendered snippets in authenticate-toolsets.md will reference undefined variables (mcp_server_name=mcp_server_name, continue_uri=continue_uri, model=model) and won't match the "Replace the following:" bullet lists in the doc.
Could we expand the region tags (or define the placeholder variables inside the tags, similar to how mcp_custom_headers.py includes def get_external_mcp_toolset(...)) so readers see the imports and placeholders on public docs?
| location=location, | ||
| ) | ||
|
|
||
| # Retrieve an MCP toolset using its resource name |
There was a problem hiding this comment.
nit (non-blocking): In authenticate-toolsets.md, both auth_remote_a2a_agent and auth_mcp_toolset include a brief inline comment explaining that agent_name / mcp_server_name accept either short ("mcpServers/SERVER_ID") or full resource name format. Keeping that comment (or leaving it in the doc prose) is nice for readers.
| import os | ||
| from typing import Any | ||
|
|
||
| from google.adk.integrations.agent_registry import AgentRegistry | ||
| from google.adk.tools.mcp_tool.mcp_toolset import McpToolset | ||
|
|
||
|
|
||
| # [START agentregistry_adk_mcp_custom_headers] |
There was a problem hiding this comment.
suggestion (non-blocking): Nice job including def my_auth_headers and def get_external_mcp_toolset inside the region tag here so EXTERNAL_SERVER_ID is visible! Could we also move # [START agentregistry_adk_mcp_custom_headers] above the imports so AgentRegistry / os / Any are shown in the doc snippet too?
| from collections.abc import Iterator | ||
| import os | ||
|
|
||
| from google.adk.agents.remote_a2a_agent import RemoteA2aAgent | ||
| from google.adk.integrations.agent_registry import AgentRegistry | ||
| import google.auth | ||
| from google.auth.transport.requests import Request | ||
| import httpx |
There was a problem hiding this comment.
+1, agreed — google.auth.transport.httpx doesn't exist, and using self.creds.before_request(Request(), ...) here is actually a nice improvement over the manual token refresh in our current doc
Description
Adds the Agent Registry authentication samples and pytest suites under
agentregistry/adk/(PR B of migrating inline Agent Registry documentation snippets to GitHub; builds on #15002; internal tracking:b/581932768).Files added
agentregistry/adk/auth_remote_a2a_agent.py+auth_remote_a2a_agent_test.py(agentregistry_adk_auth_remote_a2a_agent)agentregistry/adk/auth_mcp_toolset.py+auth_mcp_toolset_test.py(agentregistry_adk_auth_mcp_toolset)agentregistry/adk/resolve_auth_bindings.py+resolve_auth_bindings_test.py(agentregistry_adk_resolve_auth_bindings)agentregistry/adk/resolve_auth_bindings_3lo.py+resolve_auth_bindings_3lo_test.py(agentregistry_adk_resolve_auth_bindings_3lo)agentregistry/adk/mcp_custom_headers.py+mcp_custom_headers_test.py(agentregistry_adk_mcp_custom_headers)API & Test Infrastructure Notes
agentregistry/adk/requirements.txt,noxfile_config.py, andagentregistry/README.mdfrom feat(agentregistry): add ADK and Agent Engine samples with CI scaffolding #15002, plusagentregistry.googleapis.comenablement and fixture setup on the shared test project (AGENTREGISTRY_TEST_AGENT_ID,AGENTREGISTRY_TEST_MCP_SERVER_ID).AGENTREGISTRY_TEST_3LO_MCP_SERVER_IDandAGENTREGISTRY_TEST_EXTERNAL_MCP_SERVER_ID(these tests skip cleanly when not set;test_my_auth_headersruns as a unit test without API calls).Checklist
Testing
agentregistry.googleapis.comenablement, and fixture setup in the shared test project.)Compliance & Style
Post-Approval Actions