Skip to content

feat(agentregistry): add ADK authentication samples - #15003

Open
fatoboist wants to merge 1 commit into
mainfrom
feat/agentregistry-adk-auth-samples
Open

fatoboist wants to merge 1 commit into
mainfrom
feat/agentregistry-adk-auth-samples

Conversation

@fatoboist

Copy link
Copy Markdown
Contributor

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

  • Depends on agentregistry/adk/requirements.txt, noxfile_config.py, and agentregistry/README.md from feat(agentregistry): add ADK and Agent Engine samples with CI scaffolding #15002, plus agentregistry.googleapis.com enablement and fixture setup on the shared test project (AGENTREGISTRY_TEST_AGENT_ID, AGENTREGISTRY_TEST_MCP_SERVER_ID).
  • Optional fixture env vars for 3LO and external MCP server integration tests: AGENTREGISTRY_TEST_3LO_MCP_SERVER_ID and AGENTREGISTRY_TEST_EXTERNAL_MCP_SERVER_ID (these tests skip cleanly when not set; test_my_auth_headers runs as a unit test without API calls).

Checklist

Testing

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

@fatoboist
fatoboist requested review from a team as code owners November 27, 2026 17:21
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@snippet-bot

snippet-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 5 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: agentregistry Issues related to the Agent Registry API. samples Issues that are directly related to samples. labels Nov 27, 2026
@fatoboist

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +15 to +22
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+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

Comment on lines +29 to +59
# [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),
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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),
    )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 evelyn-goo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting together the auth samples

Comment on lines +23 to +28
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]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +15 to +22
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]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment on lines +15 to +22
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: agentregistry Issues related to the Agent Registry API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants