Skip to content
Merged
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
11 changes: 2 additions & 9 deletions agentregistry/adk/auth_mcp_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,18 @@
# limitations under the License.

# [START agentregistry_adk_auth_mcp_toolset]
import os

from google.adk.integrations.agent_registry import AgentRegistry
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset


# mcp_server_name accepts short ("mcpServers/SERVER_ID") or full
# ("projects/{project_id}/locations/{location}/mcpServers/SERVER_ID") format.
def auth_mcp_toolset(
project_id: str = "PROJECT_ID",
Comment thread
fatoboist marked this conversation as resolved.
Resolved
location: str = "global",
mcp_server_name: str = "mcpServers/SERVER_ID",
) -> McpToolset:
"""Retrieves an MCP toolset authenticated with Application Default Credentials."""
# 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,
Expand Down
10 changes: 4 additions & 6 deletions agentregistry/adk/auth_mcp_toolset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@
import pytest


def test_auth_mcp_toolset(monkeypatch: pytest.MonkeyPatch) -> None:
def test_auth_mcp_toolset() -> None:
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
mcp_server_id = os.environ.get("AGENTREGISTRY_TEST_MCP_SERVER_ID")
if not project_id or not mcp_server_id:
pytest.skip(
"GOOGLE_CLOUD_PROJECT and AGENTREGISTRY_TEST_MCP_SERVER_ID must be set."
)

monkeypatch.setenv("GOOGLE_CLOUD_PROJECT", project_id)
monkeypatch.setenv(
"GOOGLE_CLOUD_LOCATION", os.environ.get("GOOGLE_CLOUD_LOCATION", "global")
)

location = os.environ.get("GOOGLE_CLOUD_LOCATION", "global")
toolset = auth_mcp_toolset.auth_mcp_toolset(
project_id=project_id,
location=location,
mcp_server_name=f"mcpServers/{mcp_server_id}",
)
assert toolset is not None
10 changes: 2 additions & 8 deletions agentregistry/adk/auth_remote_a2a_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

# [START agentregistry_adk_auth_remote_a2a_agent]
from collections.abc import Iterator
import os

from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
from google.adk.integrations.agent_registry import AgentRegistry
Expand All @@ -26,6 +25,8 @@
# agent_name accepts short ("agents/AGENT_ID") or full
# ("projects/{project_id}/locations/{location}/agents/AGENT_ID") format.
def auth_remote_a2a_agent(
project_id: str = "PROJECT_ID",
Comment thread
fatoboist marked this conversation as resolved.
Resolved
location: str = "global",
agent_name: str = "agents/AGENT_ID",
) -> RemoteA2aAgent:
"""Authenticates a remote A2A agent with Application Default Credentials."""
Expand All @@ -42,13 +43,6 @@ def auth_flow(self, request: httpx.Request) -> Iterator[httpx.Request]:
)
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,
Expand Down
10 changes: 4 additions & 6 deletions agentregistry/adk/auth_remote_a2a_agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@
import pytest


def test_auth_remote_a2a_agent(monkeypatch: pytest.MonkeyPatch) -> None:
def test_auth_remote_a2a_agent() -> None:
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT")
agent_id = os.environ.get("AGENTREGISTRY_TEST_AGENT_ID")
if not project_id or not agent_id:
pytest.skip(
"GOOGLE_CLOUD_PROJECT and AGENTREGISTRY_TEST_AGENT_ID must be set."
)

monkeypatch.setenv("GOOGLE_CLOUD_PROJECT", project_id)
monkeypatch.setenv(
"GOOGLE_CLOUD_LOCATION", os.environ.get("GOOGLE_CLOUD_LOCATION", "global")
)

location = os.environ.get("GOOGLE_CLOUD_LOCATION", "global")
remote_agent = auth_remote_a2a_agent.auth_remote_a2a_agent(
project_id=project_id,
location=location,
agent_name=f"agents/{agent_id}",
)
assert remote_agent is not None
9 changes: 2 additions & 7 deletions agentregistry/adk/clone_remote_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

# [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
Expand All @@ -26,16 +25,12 @@
# agent_name accepts short ("agents/AGENT_ID") or full
# ("projects/{project_id}/locations/{location}/agents/AGENT_ID") format.
def clone_remote_agent(
project_id: str = "PROJECT_ID",
Comment thread
fatoboist marked this conversation as resolved.
Resolved
location: str = "global",
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,
Expand Down
7 changes: 2 additions & 5 deletions agentregistry/adk/clone_remote_agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@
# 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(
project_id=project_id,
location=location,
agent_name=f"projects/{project_id}/locations/{location}/agents/{agent_id}",
)

Expand Down
21 changes: 11 additions & 10 deletions agentregistry/adk/compose_multi_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

# [START agentregistry_adk_initialize_client]
from collections.abc import Iterator
import os

from google.adk.agents.llm_agent import LlmAgent
from google.adk.integrations.agent_registry import AgentRegistry
Expand All @@ -23,14 +22,11 @@
import httpx


def initialize_registry_client() -> AgentRegistry:
"""Initializes the AgentRegistry client from environment variables."""
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.")

def initialize_registry_client(
project_id: str = "PROJECT_ID",
Comment thread
fatoboist marked this conversation as resolved.
Resolved
location: str = "global",
) -> AgentRegistry:
"""Initializes the AgentRegistry client."""
# Initialize the client
registry = AgentRegistry(
project_id=project_id,
Expand All @@ -44,12 +40,17 @@ def initialize_registry_client() -> AgentRegistry:
# agent_name and mcp_server_name accept short ("agents/AGENT_ID",
# "mcpServers/SERVER_ID") or full resource name format.
def compose_multi_agent(
project_id: str = "PROJECT_ID",
Comment thread
fatoboist marked this conversation as resolved.
Resolved
location: str = "global",
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()
registry = initialize_registry_client(
project_id=project_id,
location=location,
)

class GoogleAuth(httpx.Auth):
def __init__(self) -> None:
Expand Down
7 changes: 2 additions & 5 deletions agentregistry/adk/compose_multi_agent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@
# 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(
project_id=project_id,
location=location,
agent_name=f"agents/{agent_id}",
mcp_server_name=f"mcpServers/{mcp_server_id}",
)
Expand Down
9 changes: 2 additions & 7 deletions agentregistry/adk/deploy_agent_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,19 @@
# 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(
project_id: str = "PROJECT_ID",
Comment thread
fatoboist marked this conversation as resolved.
Resolved
location: str = "us-central1",
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()

Expand Down
7 changes: 2 additions & 5 deletions agentregistry/adk/deploy_agent_runtime_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@
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(
project_id=project_id,
location=location,
staging_bucket=staging_bucket,
display_name=display_name,
)
Expand Down
9 changes: 2 additions & 7 deletions agentregistry/adk/discover_agents_and_toolsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

# [START agentregistry_adk_discover_agents_and_toolsets]
import os
from typing import Any

from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
Expand All @@ -24,17 +23,13 @@
# agent_name and mcp_server_name accept short ("agents/AGENT_ID",
# "mcpServers/SERVER_ID") or full resource name format.
def discover_agents_and_toolsets(
project_id: str = "PROJECT_ID",
Comment thread
fatoboist marked this conversation as resolved.
Resolved
location: str = "global",
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."""
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,
Expand Down
7 changes: 2 additions & 5 deletions agentregistry/adk/discover_agents_and_toolsets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
# 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(
project_id=project_id,
location=location,
agent_name=f"agents/{agent_id}",
mcp_server_name=f"mcpServers/{mcp_server_id}",
)
Expand Down
8 changes: 2 additions & 6 deletions agentregistry/adk/mcp_custom_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@ def my_auth_headers(context: Any) -> dict[str, str]:


def get_external_mcp_toolset(
project_id: str = "PROJECT_ID",
Comment thread
fatoboist marked this conversation as resolved.
Resolved
location: str = "global",
mcp_server_name: str = (
"projects/PROJECT_ID/locations/LOCATION/mcpServers/EXTERNAL_SERVER_ID"
),
) -> McpToolset:
"""Initializes AgentRegistry with header_provider and fetches an MCP toolset."""
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 registry client.
# The header_provider is sent to the target MCP server during invocation,
# not to authenticate against the Agent Registry API (which uses ADC).
Expand Down
4 changes: 2 additions & 2 deletions agentregistry/adk/mcp_custom_headers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def test_get_external_mcp_toolset(monkeypatch: pytest.MonkeyPatch) -> None:
)

location = os.environ.get("GOOGLE_CLOUD_LOCATION", "global")
monkeypatch.setenv("GOOGLE_CLOUD_PROJECT", project_id)
monkeypatch.setenv("GOOGLE_CLOUD_LOCATION", location)
monkeypatch.setenv("API_KEY", "test-api-key")

toolset = mcp_custom_headers.get_external_mcp_toolset(
project_id=project_id,
location=location,
mcp_server_name=(
f"projects/{project_id}/locations/{location}"
f"/mcpServers/{external_server_id}"
Expand Down
10 changes: 2 additions & 8 deletions agentregistry/adk/resolve_auth_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,19 @@
# limitations under the License.

# [START agentregistry_adk_resolve_auth_bindings]
import os

from google.adk.agents.llm_agent import LlmAgent
from google.adk.auth.credential_manager import CredentialManager
from google.adk.integrations.agent_identity import GcpAuthProvider
from google.adk.integrations.agent_registry import AgentRegistry


def resolve_auth_bindings(
project_id: str = "PROJECT_ID",
Comment thread
fatoboist marked this conversation as resolved.
Resolved
location: str = "global",
mcp_server_name: str = "mcpServers/SERVER_ID",
model: str = "gemini-2.5-flash",
) -> LlmAgent:
"""Fetches an authenticated MCP toolset with resolved auth provider bindings."""
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.")

# Register the auth provider
CredentialManager.register_auth_provider(GcpAuthProvider())

Expand Down
Loading