Skip to content

Commit abaa929

Browse files
wukathcopybara-github
authored andcommitted
feat: Agent Registry in ADK
Client library for the Agent Registry API that allows users to discover, look up, and connect to agents and MCP servers cataloged in the registry. Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 873073675
1 parent 77df6d8 commit abaa929

File tree

5 files changed

+376
-0
lines changed

5 files changed

+376
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Agent Registry Sample
2+
3+
This sample demonstrates how to use the `AgentRegistry` client to discover agents and MCP servers registered in Google Cloud.
4+
5+
## Setup
6+
7+
1. Ensure you have Google Cloud credentials configured (e.g., `gcloud auth application-default login`).
8+
2. Set the following environment variables:
9+
10+
```bash
11+
export GOOGLE_CLOUD_PROJECT=your-project-id
12+
export GOOGLE_CLOUD_LOCATION=global # or your specific region
13+
```
14+
15+
3. Obtain the full resource names for the agents and MCP servers you want to use. You can do this by running the sample script once to list them:
16+
17+
```bash
18+
python3 agent.py
19+
```
20+
21+
Alternatively, use `gcloud` to list them:
22+
23+
```bash
24+
# For agents
25+
gcloud alpha agent-registry agents list --project=$GOOGLE_CLOUD_PROJECT --location=$GOOGLE_CLOUD_LOCATION
26+
27+
# For MCP servers
28+
gcloud alpha agent-registry mcp-servers list --project=$GOOGLE_CLOUD_PROJECT --location=$GOOGLE_CLOUD_LOCATION
29+
```
30+
31+
4. Replace `AGENT_NAME` and `MCP_SERVER_NAME` in `agent.py` with the last part of the resource names (e.g., if the name is `projects/.../agents/my-agent`, use `my-agent`).
32+
33+
## Running the Sample
34+
35+
Run the sample script to list available agents and MCP servers:
36+
37+
```bash
38+
python3 agent.py
39+
```
40+
41+
## How it Works
42+
43+
The sample uses `AgentRegistry` to:
44+
- List registered agents using `list_agents()`.
45+
- List registered MCP servers using `list_mcp_servers()`.
46+
47+
It also shows (in comments) how to:
48+
- Get a `RemoteA2aAgent` instance using `get_remote_a2a_agent(name)`.
49+
- Get an `McpToolset` instance using `get_mcp_toolset(name)`.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import agent
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Sample agent demonstrating Agent Registry discovery."""
16+
17+
import os
18+
19+
from google.adk.agents.llm_agent import LlmAgent
20+
from google.adk.integrations.agent_registry import AgentRegistry
21+
22+
# Project and location can be set via environment variables:
23+
# GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION
24+
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
25+
location = os.environ.get("GOOGLE_CLOUD_LOCATION", "global")
26+
27+
# Initialize Agent Registry client
28+
registry = AgentRegistry(project_id=project_id, location=location)
29+
30+
print(f"Listing agents in {project_id}/{location}...")
31+
agents = registry.list_agents()
32+
for agent in agents.get("agents", []):
33+
print(f"- Agent: {agent.get('displayName')} ({agent.get('name')})")
34+
35+
print(f"\nListing MCP servers in {project_id}/{location}...")
36+
mcp_servers = registry.list_mcp_servers()
37+
for server in mcp_servers.get("mcpServers", []):
38+
print(f"- MCP Server: {server.get('displayName')} ({server.get('name')})")
39+
40+
# Example of using a specific agent or MCP server from the registry:
41+
# (Note: These names should be full resource names as returned by list methods)
42+
43+
# 1. Using a Remote A2A Agent as a sub-agent
44+
# TODO: Replace AGENT_NAME with your agent name
45+
remote_agent = registry.get_remote_a2a_agent(
46+
f"projects/{project_id}/locations/{location}/agents/AGENT_NAME"
47+
)
48+
49+
# 2. Using an MCP Server in a toolset
50+
# TODO: Replace MCP_SERVER_NAME with your MCP server name
51+
mcp_toolset = registry.get_mcp_toolset(
52+
f"projects/{project_id}/locations/{location}/mcpServers/MCP_SERVER_NAME"
53+
)
54+
55+
root_agent = LlmAgent(
56+
model="gemini-2.5-flash",
57+
name="discovery_agent",
58+
instruction=(
59+
"You have access to tools and sub-agents discovered via Registry."
60+
),
61+
tools=[mcp_toolset],
62+
sub_agents=[remote_agent],
63+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest.mock import MagicMock
16+
from unittest.mock import patch
17+
18+
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
19+
from google.adk.integrations.agent_registry import AgentRegistry
20+
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
21+
import httpx
22+
import pytest
23+
24+
25+
class TestAgentRegistry:
26+
27+
@pytest.fixture
28+
def registry(self):
29+
with patch("google.auth.default", return_value=(MagicMock(), "project-id")):
30+
return AgentRegistry(project_id="test-project", location="global")
31+
32+
def test_init_raises_value_error_if_params_missing(self):
33+
with pytest.raises(
34+
ValueError, match="project_id and location must be provided"
35+
):
36+
AgentRegistry(project_id=None, location=None)
37+
38+
def test_get_connection_uri_mcp_interfaces_top_level(self, registry):
39+
resource_details = {
40+
"interfaces": [
41+
{"url": "https://mcp-v1main.com", "protocolBinding": "JSONRPC"}
42+
]
43+
}
44+
uri = registry._get_connection_uri(
45+
resource_details, protocol_binding="JSONRPC"
46+
)
47+
assert uri == "https://mcp-v1main.com"
48+
49+
def test_get_connection_uri_agent_nested_protocols(self, registry):
50+
resource_details = {
51+
"protocols": [{
52+
"type": "A2A_AGENT",
53+
"interfaces": [{
54+
"url": "https://my-agent.com",
55+
"protocolBinding": "JSONRPC",
56+
}],
57+
}]
58+
}
59+
uri = registry._get_connection_uri(
60+
resource_details, protocol_type="A2A_AGENT"
61+
)
62+
assert uri == "https://my-agent.com"
63+
64+
def test_get_connection_uri_filtering(self, registry):
65+
resource_details = {
66+
"protocols": [
67+
{
68+
"type": "CUSTOM",
69+
"interfaces": [{"url": "https://custom.com"}],
70+
},
71+
{
72+
"type": "A2A_AGENT",
73+
"interfaces": [{
74+
"url": "https://my-agent.com",
75+
"protocolBinding": "HTTP_JSON",
76+
}],
77+
},
78+
]
79+
}
80+
# Filter by type
81+
uri = registry._get_connection_uri(
82+
resource_details, protocol_type="A2A_AGENT"
83+
)
84+
assert uri == "https://my-agent.com"
85+
86+
# Filter by binding
87+
uri = registry._get_connection_uri(
88+
resource_details, protocol_binding="HTTP_JSON"
89+
)
90+
assert uri == "https://my-agent.com"
91+
92+
# No match
93+
uri = registry._get_connection_uri(
94+
resource_details, protocol_type="A2A_AGENT", protocol_binding="JSONRPC"
95+
)
96+
assert uri is None
97+
98+
def test_get_connection_uri_returns_none_if_no_interfaces(self, registry):
99+
resource_details = {}
100+
uri = registry._get_connection_uri(resource_details)
101+
assert uri is None
102+
103+
def test_get_connection_uri_returns_none_if_no_url_in_interfaces(
104+
self, registry
105+
):
106+
resource_details = {"interfaces": [{"protocolBinding": "HTTP"}]}
107+
uri = registry._get_connection_uri(resource_details)
108+
assert uri is None
109+
110+
@patch("httpx.Client")
111+
def test_list_agents(self, mock_httpx, registry):
112+
mock_response = MagicMock()
113+
mock_response.json.return_value = {"agents": []}
114+
mock_response.raise_for_status = MagicMock()
115+
mock_httpx.return_value.__enter__.return_value.get.return_value = (
116+
mock_response
117+
)
118+
119+
# Mock auth refresh
120+
registry._credentials.token = "token"
121+
registry._credentials.refresh = MagicMock()
122+
123+
agents = registry.list_agents()
124+
assert agents == {"agents": []}
125+
126+
@patch("httpx.Client")
127+
def test_get_mcp_server(self, mock_httpx, registry):
128+
mock_response = MagicMock()
129+
mock_response.json.return_value = {"name": "test-mcp"}
130+
mock_response.raise_for_status = MagicMock()
131+
mock_httpx.return_value.__enter__.return_value.get.return_value = (
132+
mock_response
133+
)
134+
135+
registry._credentials.token = "token"
136+
registry._credentials.refresh = MagicMock()
137+
138+
server = registry.get_mcp_server("test-mcp")
139+
assert server == {"name": "test-mcp"}
140+
141+
@patch("httpx.Client")
142+
def test_get_mcp_toolset(self, mock_httpx, registry):
143+
mock_response = MagicMock()
144+
mock_response.json.return_value = {
145+
"displayName": "TestPrefix",
146+
"interfaces": [
147+
{"url": "https://mcp.com", "protocolBinding": "JSONRPC"}
148+
],
149+
}
150+
mock_response.raise_for_status = MagicMock()
151+
mock_httpx.return_value.__enter__.return_value.get.return_value = (
152+
mock_response
153+
)
154+
155+
registry._credentials.token = "token"
156+
registry._credentials.refresh = MagicMock()
157+
158+
toolset = registry.get_mcp_toolset("test-mcp")
159+
assert isinstance(toolset, McpToolset)
160+
assert toolset.tool_name_prefix == "TestPrefix"
161+
162+
@patch("httpx.Client")
163+
def test_get_remote_a2a_agent(self, mock_httpx, registry):
164+
mock_response = MagicMock()
165+
mock_response.json.return_value = {
166+
"displayName": "TestAgent",
167+
"description": "Test Desc",
168+
"agentSpec": {
169+
"a2aAgentCardUrl": "https://my-agent.com/agent-card.json"
170+
},
171+
}
172+
mock_response.raise_for_status = MagicMock()
173+
mock_httpx.return_value.__enter__.return_value.get.return_value = (
174+
mock_response
175+
)
176+
177+
registry._credentials.token = "token"
178+
registry._credentials.refresh = MagicMock()
179+
180+
agent = registry.get_remote_a2a_agent("test-agent")
181+
assert isinstance(agent, RemoteA2aAgent)
182+
assert agent.name == "TestAgent"
183+
assert agent.description == "Test Desc"
184+
assert agent._agent_card_source == "https://my-agent.com/agent-card.json"
185+
186+
def test_get_auth_headers(self, registry):
187+
registry._credentials.token = "fake-token"
188+
registry._credentials.refresh = MagicMock()
189+
registry._credentials.quota_project_id = "quota-project"
190+
191+
headers = registry._get_auth_headers()
192+
assert headers["Authorization"] == "Bearer fake-token"
193+
assert headers["x-goog-user-project"] == "quota-project"
194+
195+
@patch("httpx.Client")
196+
def test_make_request_raises_http_status_error(self, mock_httpx, registry):
197+
mock_response = MagicMock()
198+
mock_response.status_code = 404
199+
mock_response.text = "Not Found"
200+
error = httpx.HTTPStatusError(
201+
"Error", request=MagicMock(), response=mock_response
202+
)
203+
mock_httpx.return_value.__enter__.return_value.get.side_effect = error
204+
205+
registry._credentials.token = "token"
206+
registry._credentials.refresh = MagicMock()
207+
208+
with pytest.raises(
209+
RuntimeError, match="API request failed with status 404"
210+
):
211+
registry._make_request("test-path")
212+
213+
@patch("httpx.Client")
214+
def test_make_request_raises_request_error(self, mock_httpx, registry):
215+
error = httpx.RequestError("Connection failed", request=MagicMock())
216+
mock_httpx.return_value.__enter__.return_value.get.side_effect = error
217+
218+
registry._credentials.token = "token"
219+
registry._credentials.refresh = MagicMock()
220+
221+
with pytest.raises(
222+
RuntimeError, match="API request failed \(network error\)"
223+
):
224+
registry._make_request("test-path")
225+
226+
@patch("httpx.Client")
227+
def test_make_request_raises_generic_exception(self, mock_httpx, registry):
228+
mock_httpx.return_value.__enter__.return_value.get.side_effect = Exception(
229+
"Generic error"
230+
)
231+
232+
registry._credentials.token = "token"
233+
registry._credentials.refresh = MagicMock()
234+
235+
with pytest.raises(RuntimeError, match="API request failed: Generic error"):
236+
registry._make_request("test-path")

0 commit comments

Comments
 (0)