diff --git a/agentregistry/README.md b/agentregistry/README.md index e69de29bb2d..d4106abb32d 100644 --- a/agentregistry/README.md +++ b/agentregistry/README.md @@ -0,0 +1,43 @@ +Agent Registry Python Samples +============================= + +Code samples for `Google Cloud Agent Registry`_, using the Agent Development Kit (ADK) and Vertex AI Agent Engine. + +.. _Google Cloud Agent Registry: https://cloud.google.com/agent-registry + +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`_. +5. Set the required environment variables and install dependencies: + + .. code-block:: bash + + export GOOGLE_CLOUD_PROJECT="your-project-id" + export GOOGLE_CLOUD_LOCATION="global" + cd adk + pip install -r requirements.txt + +.. _Application Default Credentials: https://googleapis.dev/python/google-api-core/latest/auth.html + +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. diff --git a/agentregistry/adk/clone_remote_agent.py b/agentregistry/adk/clone_remote_agent.py new file mode 100644 index 00000000000..802b449708f --- /dev/null +++ b/agentregistry/adk/clone_remote_agent.py @@ -0,0 +1,81 @@ +# 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 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, + ) + + # [START agentregistry_adk_clone_remote_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 + + 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, + ) + + 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()], + ) + # [END agentregistry_adk_clone_remote_agent] + return flight_orchestrator, hotel_orchestrator + + +if __name__ == "__main__": + clone_remote_agent() diff --git a/agentregistry/adk/clone_remote_agent_test.py b/agentregistry/adk/clone_remote_agent_test.py new file mode 100644 index 00000000000..bf8d4f7f7be --- /dev/null +++ b/agentregistry/adk/clone_remote_agent_test.py @@ -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] diff --git a/agentregistry/adk/compose_multi_agent.py b/agentregistry/adk/compose_multi_agent.py new file mode 100644 index 00000000000..3e5543ef114 --- /dev/null +++ b/agentregistry/adk/compose_multi_agent.py @@ -0,0 +1,84 @@ +# 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 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.""" + # [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, + ) + # [END agentregistry_adk_initialize_client] + + # [START agentregistry_adk_compose_multi_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 + + 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, + ) + my_mcp_toolset = registry.get_mcp_toolset(mcp_server_name=mcp_server_name) + + main_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], + ) + # [END agentregistry_adk_compose_multi_agent] + return main_agent + + +if __name__ == "__main__": + compose_multi_agent() diff --git a/agentregistry/adk/compose_multi_agent_test.py b/agentregistry/adk/compose_multi_agent_test.py new file mode 100644 index 00000000000..352bcfe47a1 --- /dev/null +++ b/agentregistry/adk/compose_multi_agent_test.py @@ -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 diff --git a/agentregistry/adk/conftest.py b/agentregistry/adk/conftest.py new file mode 100644 index 00000000000..792dfb7d242 --- /dev/null +++ b/agentregistry/adk/conftest.py @@ -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 diff --git a/agentregistry/adk/deploy_agent_runtime.py b/agentregistry/adk/deploy_agent_runtime.py new file mode 100644 index 00000000000..4a29b30a69e --- /dev/null +++ b/agentregistry/adk/deploy_agent_runtime.py @@ -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. + +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 + + # [START agentregistry_deploy_agent_runtime] + # 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, + ) + # [END agentregistry_deploy_agent_runtime] + return remote_agent + + +if __name__ == "__main__": + deploy_agent_runtime() diff --git a/agentregistry/adk/deploy_agent_runtime_test.py b/agentregistry/adk/deploy_agent_runtime_test.py new file mode 100644 index 00000000000..c460551af57 --- /dev/null +++ b/agentregistry/adk/deploy_agent_runtime_test.py @@ -0,0 +1,45 @@ +# 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 uuid + +import deploy_agent_runtime +import pytest +import vertexai + + +def test_deploy_agent_runtime( + monkeypatch: pytest.MonkeyPatch, + project_id: str, + staging_bucket: str, +) -> None: + location = "us-central1" + monkeypatch.setenv("GOOGLE_CLOUD_PROJECT", project_id) + monkeypatch.setenv("GOOGLE_CLOUD_LOCATION", location) + + display_name = f"agentregistry-test-{uuid.uuid4().hex[:8]}" + remote_agent = None + try: + remote_agent = deploy_agent_runtime.deploy_agent_runtime( + staging_bucket=staging_bucket, + display_name=display_name, + ) + assert remote_agent.api_resource.name + finally: + if remote_agent is not None and getattr(remote_agent, "api_resource", None): + client = vertexai.Client(project=project_id, location=location) + client.agent_engines.delete( + name=remote_agent.api_resource.name, + force=True, + ) diff --git a/agentregistry/adk/discover_agents_and_toolsets.py b/agentregistry/adk/discover_agents_and_toolsets.py new file mode 100644 index 00000000000..b159c5cc5b4 --- /dev/null +++ b/agentregistry/adk/discover_agents_and_toolsets.py @@ -0,0 +1,54 @@ +# 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 +from typing import Any + +from google.adk.agents.remote_a2a_agent import RemoteA2aAgent +from google.adk.integrations.agent_registry import AgentRegistry +from google.adk.tools.mcp_tool.mcp_toolset import McpToolset + + +def discover_agents_and_toolsets( + agent_name: str = "agents/AGENT_ID", + mcp_server_name: str = "mcpServers/SERVER_ID", + filter_str: str = 'skills.tags:"translation"', +) -> tuple[dict[str, Any], RemoteA2aAgent, McpToolset]: + """Discovers agents by skill tag and fetches a remote agent and MCP toolset.""" + # [START agentregistry_adk_discover_agents_and_toolsets] + 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, + ) + + # Discover agents by a required A2A skill, for example, translation + translator_agents = registry.list_agents(filter_str=filter_str) + + # Connect to a remote A2A agent using its resource name + summarizer_agent = registry.get_remote_a2a_agent(agent_name=agent_name) + + # Retrieve an MCP toolset using its resource name + bq_toolset = registry.get_mcp_toolset(mcp_server_name=mcp_server_name) + # [END agentregistry_adk_discover_agents_and_toolsets] + return translator_agents, summarizer_agent, bq_toolset + + +if __name__ == "__main__": + discover_agents_and_toolsets() diff --git a/agentregistry/adk/discover_agents_and_toolsets_test.py b/agentregistry/adk/discover_agents_and_toolsets_test.py new file mode 100644 index 00000000000..edd622a420a --- /dev/null +++ b/agentregistry/adk/discover_agents_and_toolsets_test.py @@ -0,0 +1,38 @@ +# 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 discover_agents_and_toolsets +import pytest + + +def test_discover_agents_and_toolsets( + 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) + + agents_response, remote_agent, mcp_toolset = ( + discover_agents_and_toolsets.discover_agents_and_toolsets( + agent_name=f"agents/{agent_id}", + mcp_server_name=f"mcpServers/{mcp_server_id}", + ) + ) + + assert isinstance(agents_response, dict) + assert remote_agent is not None + assert mcp_toolset is not None diff --git a/agentregistry/adk/noxfile_config.py b/agentregistry/adk/noxfile_config.py new file mode 100644 index 00000000000..0c7d3273fa6 --- /dev/null +++ b/agentregistry/adk/noxfile_config.py @@ -0,0 +1,29 @@ +# 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. + +# Default TEST_CONFIG_OVERRIDE for python repos. + +TEST_CONFIG_OVERRIDE = { + # You can opt out from the test for specific Python versions. + "ignored_versions": ["3.8", "3.9", "3.10", "3.11", "3.13"], + # Old samples are opted out of enforcing Python type hints. + # All new samples should feature them. + "enforce_type_hints": True, + # An envvar key for determining the project id to use. + "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", + # A dictionary you want to inject into your test. + "envs": { + "GOOGLE_CLOUD_LOCATION": "global", + }, +} diff --git a/agentregistry/adk/requirements-test.txt b/agentregistry/adk/requirements-test.txt new file mode 100644 index 00000000000..c9e154ba440 --- /dev/null +++ b/agentregistry/adk/requirements-test.txt @@ -0,0 +1 @@ +pytest==9.0.3; python_version >= "3.10" diff --git a/agentregistry/adk/requirements.txt b/agentregistry/adk/requirements.txt new file mode 100644 index 00000000000..10ce56b7b64 --- /dev/null +++ b/agentregistry/adk/requirements.txt @@ -0,0 +1,4 @@ +google-adk[a2a,agent-identity,mcp]>=1.29.0 +google-cloud-aiplatform[agent_engines]>=1.112.0 +httpx>=0.28.0 +google-auth>=2.0.0