Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions agentregistry/README.md
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.
83 changes: 83 additions & 0 deletions agentregistry/adk/clone_remote_agent.py
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",

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: 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.

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 adding normalization logic here because AgentRegistry already handles both formats natively: AgentRegistry._make_request() checks if path.startswith("projects/"): and routes full resource names directly, while prepending projects/{project_id}/locations/{location}/ for short names (agents/AGENT_ID). Adding custom string-parsing boilerplate would clutter the doc snippet and could imply AgentRegistry doesn't support both formats out of the box.

The full-format default in clone_remote_agent.py is intentional to match the PROJECT_ID, LOCATION, and AGENT_ID placeholders in resolve-endpoints-and-build-orchestrators.md. Added a 2-line comment here (and in compose_multi_agent.py) clarifying that both short and full formats are accepted.

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
Comment thread
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,
)
Comment thread
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()
36 changes: 36 additions & 0 deletions agentregistry/adk/clone_remote_agent_test.py
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]
92 changes: 92 additions & 0 deletions agentregistry/adk/compose_multi_agent.py
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
Comment thread
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,
)
Comment thread
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()
36 changes: 36 additions & 0 deletions agentregistry/adk/compose_multi_agent_test.py
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
51 changes: 51 additions & 0 deletions agentregistry/adk/conftest.py
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
56 changes: 56 additions & 0 deletions agentregistry/adk/deploy_agent_runtime.py
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()
Loading