Repository navigation
feat(agentregistry): add ADK and Agent Engine samples with CI scaffolding #15002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fatoboist
wants to merge
4
commits into
main
Choose a base branch
from
feat/agentregistry-adk-samples
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+562
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e373108
feat(agentregistry): add ADK and Agent Engine samples with CI scaffol…
fatoboist 5b3d337
fix(agentregistry): use creds.before_request, align Python versions, …
fatoboist 63719a0
fix(agentregistry): convert README to Markdown, rename root_agent, ex…
fatoboist 4f184c7
docs(agentregistry): clarify short and full resource name formats in …
fatoboist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Agent Registry Python Samples | ||
|
|
||
| Code samples for [Google Cloud Agent Registry](https://cloud.google.com/agent-registry), using the Agent Development Kit (ADK) and Vertex AI Agent Engine. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| 1. Select or create a Google Cloud project. | ||
| 2. Enable billing for your project. | ||
| 3. Enable the Agent Registry API (`agentregistry.googleapis.com`) and Vertex AI API (`aiplatform.googleapis.com`). | ||
| 4. Set up [Application Default Credentials](https://googleapis.dev/python/google-api-core/latest/auth.html). | ||
| 5. Set the required environment variables and install dependencies: | ||
|
|
||
| ```bash | ||
| export GOOGLE_CLOUD_PROJECT="your-project-id" | ||
| export GOOGLE_CLOUD_LOCATION="global" | ||
| cd adk | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| ## Samples | ||
|
|
||
| * `adk/compose_multi_agent.py`: Initialize the `AgentRegistry` client and compose an `LlmAgent` orchestrator with a remote A2A agent and an MCP toolset. | ||
| * `adk/clone_remote_agent.py`: Fetch a remote A2A agent once and clone it across multiple orchestrator agents. | ||
| * `adk/discover_agents_and_toolsets.py`: Discover agents by skill tag and retrieve remote agents and MCP toolsets. | ||
| * `adk/deploy_agent_runtime.py`: Deploy an A2A agent to Vertex AI Agent Engine so it is automatically registered in Agent Registry. | ||
|
|
||
| ## Testing | ||
|
|
||
| Tests require a Google Cloud project with `agentregistry.googleapis.com` enabled and the following environment variables set: | ||
|
|
||
| * `GOOGLE_CLOUD_PROJECT`: Google Cloud project ID. | ||
| * `GOOGLE_CLOUD_LOCATION`: Agent Registry location (defaults to `global`). | ||
| * `AGENTREGISTRY_TEST_AGENT_ID`: ID of a pre-registered A2A agent fixture. | ||
| * `AGENTREGISTRY_TEST_MCP_SERVER_ID`: ID of a pre-registered MCP server fixture. | ||
| * `AGENTREGISTRY_TEST_STAGING_BUCKET`: Cloud Storage bucket URI (`gs://...`) for the Agent Engine deployment test. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # [START agentregistry_adk_clone_remote_agent] | ||
| from collections.abc import Iterator | ||
| import os | ||
|
|
||
| from google.adk.agents.llm_agent import LlmAgent | ||
| from google.adk.integrations.agent_registry import AgentRegistry | ||
| import google.auth | ||
| from google.auth.transport.requests import Request | ||
| import httpx | ||
|
|
||
|
|
||
| # agent_name accepts short ("agents/AGENT_ID") or full | ||
| # ("projects/{project_id}/locations/{location}/agents/AGENT_ID") format. | ||
| def clone_remote_agent( | ||
| agent_name: str = "projects/PROJECT_ID/locations/LOCATION/agents/AGENT_ID", | ||
| model: str = "gemini-2.5-flash", | ||
| ) -> tuple[LlmAgent, LlmAgent]: | ||
| """Fetches a remote A2A agent once and clones it across orchestrators.""" | ||
| 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, | ||
| ) | ||
|
|
||
| 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 | ||
|
fatoboist marked this conversation as resolved.
Resolved
|
||
|
|
||
| httpx_client = httpx.AsyncClient( | ||
| auth=GoogleAuth(), | ||
| timeout=httpx.Timeout(60.0), | ||
| ) | ||
|
|
||
| base_remote_agent = registry.get_remote_a2a_agent( | ||
| agent_name=agent_name, | ||
| httpx_client=httpx_client, | ||
| ) | ||
|
fatoboist marked this conversation as resolved.
Resolved
|
||
|
|
||
| flight_orchestrator = LlmAgent( | ||
| model=model, | ||
| name="flight_orchestrator", | ||
| sub_agents=[base_remote_agent.clone()], | ||
| ) | ||
|
|
||
| hotel_orchestrator = LlmAgent( | ||
| model=model, | ||
| name="hotel_orchestrator", | ||
| sub_agents=[base_remote_agent.clone()], | ||
| ) | ||
| return flight_orchestrator, hotel_orchestrator | ||
| # [END agentregistry_adk_clone_remote_agent] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| clone_remote_agent() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import clone_remote_agent | ||
| import pytest | ||
|
|
||
|
|
||
| def test_clone_remote_agent( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| project_id: str, | ||
| location: str, | ||
| agent_id: str, | ||
| ) -> None: | ||
| monkeypatch.setenv("GOOGLE_CLOUD_PROJECT", project_id) | ||
| monkeypatch.setenv("GOOGLE_CLOUD_LOCATION", location) | ||
|
|
||
| flight_orchestrator, hotel_orchestrator = clone_remote_agent.clone_remote_agent( | ||
| agent_name=f"projects/{project_id}/locations/{location}/agents/{agent_id}", | ||
| ) | ||
|
|
||
| assert flight_orchestrator.name == "flight_orchestrator" | ||
| assert hotel_orchestrator.name == "hotel_orchestrator" | ||
| assert len(flight_orchestrator.sub_agents) == 1 | ||
| assert len(hotel_orchestrator.sub_agents) == 1 | ||
| assert flight_orchestrator.sub_agents[0] is not hotel_orchestrator.sub_agents[0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from collections.abc import Iterator | ||
| import os | ||
|
|
||
| from google.adk.agents.llm_agent import LlmAgent | ||
| from google.adk.integrations.agent_registry import AgentRegistry | ||
| import google.auth | ||
| from google.auth.transport.requests import Request | ||
| import httpx | ||
|
|
||
|
|
||
| def initialize_registry_client() -> AgentRegistry: | ||
| """Initializes the AgentRegistry client from environment variables.""" | ||
| # [START agentregistry_adk_initialize_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.") | ||
|
|
||
| # Initialize the client | ||
| registry = AgentRegistry( | ||
| project_id=project_id, | ||
| location=location, | ||
| ) | ||
| return registry | ||
| # [END agentregistry_adk_initialize_client] | ||
|
|
||
|
|
||
| # [START agentregistry_adk_compose_multi_agent] | ||
| # agent_name and mcp_server_name accept short ("agents/AGENT_ID", | ||
| # "mcpServers/SERVER_ID") or full resource name format. | ||
| def compose_multi_agent( | ||
| agent_name: str = "agents/AGENT_ID", | ||
| mcp_server_name: str = "mcpServers/SERVER_ID", | ||
| model: str = "gemini-2.5-flash", | ||
| ) -> LlmAgent: | ||
| """Initializes AgentRegistry and composes an orchestrator LlmAgent.""" | ||
| registry = initialize_registry_client() | ||
|
|
||
| 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 | ||
|
fatoboist marked this conversation as resolved.
Resolved
|
||
|
|
||
| httpx_client = httpx.AsyncClient( | ||
| auth=GoogleAuth(), | ||
| timeout=httpx.Timeout(60.0), | ||
| ) | ||
| my_remote_agent = registry.get_remote_a2a_agent( | ||
| agent_name=agent_name, | ||
| httpx_client=httpx_client, | ||
| ) | ||
|
fatoboist marked this conversation as resolved.
Resolved
|
||
| my_mcp_toolset = registry.get_mcp_toolset(mcp_server_name=mcp_server_name) | ||
|
|
||
| root_agent = LlmAgent( | ||
| model=model, | ||
| name="travel_orchestrator", | ||
| instruction=( | ||
| "You are a travel coordinator. You can use your subagents to book " | ||
| "travel and your tools to query historical travel data." | ||
| ), | ||
| tools=[my_mcp_toolset], | ||
| sub_agents=[my_remote_agent], | ||
| ) | ||
| return root_agent | ||
| # [END agentregistry_adk_compose_multi_agent] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| compose_multi_agent() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import compose_multi_agent | ||
| import pytest | ||
|
|
||
|
|
||
| def test_compose_multi_agent( | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| project_id: str, | ||
| location: str, | ||
| agent_id: str, | ||
| mcp_server_id: str, | ||
| ) -> None: | ||
| monkeypatch.setenv("GOOGLE_CLOUD_PROJECT", project_id) | ||
| monkeypatch.setenv("GOOGLE_CLOUD_LOCATION", location) | ||
|
|
||
| orchestrator = compose_multi_agent.compose_multi_agent( | ||
| agent_name=f"agents/{agent_id}", | ||
| mcp_server_name=f"mcpServers/{mcp_server_id}", | ||
| ) | ||
|
|
||
| assert orchestrator.name == "travel_orchestrator" | ||
| assert len(orchestrator.sub_agents) == 1 | ||
| assert len(orchestrator.tools) == 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def project_id() -> str: | ||
| return os.environ["GOOGLE_CLOUD_PROJECT"] | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def location() -> str: | ||
| return os.environ.get("GOOGLE_CLOUD_LOCATION", "global") | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def agent_id() -> str: | ||
| value = os.environ.get("AGENTREGISTRY_TEST_AGENT_ID") | ||
| if not value: | ||
| pytest.skip("AGENTREGISTRY_TEST_AGENT_ID environment variable not set.") | ||
| return value | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def mcp_server_id() -> str: | ||
| value = os.environ.get("AGENTREGISTRY_TEST_MCP_SERVER_ID") | ||
| if not value: | ||
| pytest.skip("AGENTREGISTRY_TEST_MCP_SERVER_ID environment variable not set.") | ||
| return value | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def staging_bucket() -> str: | ||
| value = os.environ.get("AGENTREGISTRY_TEST_STAGING_BUCKET") | ||
| if not value: | ||
| pytest.skip("AGENTREGISTRY_TEST_STAGING_BUCKET environment variable not set.") | ||
| return value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # [START agentregistry_deploy_agent_runtime] | ||
| import os | ||
| from typing import Any | ||
|
|
||
| import vertexai | ||
| from vertexai.agent_engines.templates.a2a import default_a2a_agent | ||
|
|
||
|
|
||
| def deploy_agent_runtime( | ||
| staging_bucket: str | None = None, | ||
| display_name: str = "my-support-agent", | ||
| ) -> Any: | ||
| """Deploys an A2A agent to Vertex AI Agent Engine.""" | ||
| project_id = os.environ.get("GOOGLE_CLOUD_PROJECT") | ||
| location = os.environ.get("GOOGLE_CLOUD_LOCATION", "us-central1") | ||
|
|
||
| if not project_id: | ||
| raise ValueError("GOOGLE_CLOUD_PROJECT environment variable not set.") | ||
|
|
||
| client = vertexai.Client(project=project_id, location=location) | ||
| my_a2a_agent = default_a2a_agent() | ||
|
|
||
| config: dict[str, Any] = { | ||
| "display_name": display_name, | ||
| "description": "An agent that handles support tickets.", | ||
| "requirements": ["google-cloud-aiplatform[agent_engines,a2a]"], | ||
| } | ||
| if staging_bucket: | ||
| config["staging_bucket"] = staging_bucket | ||
|
|
||
| # Create and deploy the agent. | ||
| # This action automatically registers the agent in Agent Registry. | ||
| remote_agent = client.agent_engines.create( | ||
| agent=my_a2a_agent, | ||
| config=config, | ||
| ) | ||
| return remote_agent | ||
| # [END agentregistry_deploy_agent_runtime] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| deploy_agent_runtime() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this accepts full resource name, while in other files it only accepts the agents/AGENT_ID part. Would be good to normalize. For example, you could detect if the parameter is a full name vs just the id and then conditionally use as is or re-construct from environment variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not adding normalization logic here because
AgentRegistryalready handles both formats natively:AgentRegistry._make_request()checksif path.startswith("projects/"):and routes full resource names directly, while prependingprojects/{project_id}/locations/{location}/for short names (agents/AGENT_ID). Adding custom string-parsing boilerplate would clutter the doc snippet and could implyAgentRegistrydoesn't support both formats out of the box.The full-format default in
clone_remote_agent.pyis intentional to match thePROJECT_ID,LOCATION, andAGENT_IDplaceholders inresolve-endpoints-and-build-orchestrators.md. Added a 2-line comment here (and incompose_multi_agent.py) clarifying that both short and full formats are accepted.