From 4e757e1d31088aec82eda4c4edb6e7945f594286 Mon Sep 17 00:00:00 2001 From: Beth Brown Date: Tue, 1 Dec 2026 19:29:13 +0000 Subject: [PATCH] refactor(agentregistry): pass project_id and location as sample parameters --- agentregistry/adk/auth_mcp_toolset.py | 11 ++-------- agentregistry/adk/auth_mcp_toolset_test.py | 10 ++++----- agentregistry/adk/auth_remote_a2a_agent.py | 10 ++------- .../adk/auth_remote_a2a_agent_test.py | 10 ++++----- agentregistry/adk/clone_remote_agent.py | 9 ++------ agentregistry/adk/clone_remote_agent_test.py | 7 ++----- agentregistry/adk/compose_multi_agent.py | 21 ++++++++++--------- agentregistry/adk/compose_multi_agent_test.py | 7 ++----- agentregistry/adk/deploy_agent_runtime.py | 9 ++------ .../adk/deploy_agent_runtime_test.py | 7 ++----- .../adk/discover_agents_and_toolsets.py | 9 ++------ .../adk/discover_agents_and_toolsets_test.py | 7 ++----- agentregistry/adk/mcp_custom_headers.py | 8 ++----- agentregistry/adk/mcp_custom_headers_test.py | 4 ++-- agentregistry/adk/resolve_auth_bindings.py | 10 ++------- .../adk/resolve_auth_bindings_3lo.py | 10 ++------- .../adk/resolve_auth_bindings_3lo_test.py | 10 ++++----- .../adk/resolve_auth_bindings_test.py | 10 ++++----- 18 files changed, 53 insertions(+), 116 deletions(-) diff --git a/agentregistry/adk/auth_mcp_toolset.py b/agentregistry/adk/auth_mcp_toolset.py index 85843a5e021..9efa1b37ea4 100644 --- a/agentregistry/adk/auth_mcp_toolset.py +++ b/agentregistry/adk/auth_mcp_toolset.py @@ -13,8 +13,6 @@ # 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 @@ -22,16 +20,11 @@ # 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", + 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, diff --git a/agentregistry/adk/auth_mcp_toolset_test.py b/agentregistry/adk/auth_mcp_toolset_test.py index 7afb2073427..70e057fcbbe 100644 --- a/agentregistry/adk/auth_mcp_toolset_test.py +++ b/agentregistry/adk/auth_mcp_toolset_test.py @@ -18,7 +18,7 @@ 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: @@ -26,12 +26,10 @@ def test_auth_mcp_toolset(monkeypatch: pytest.MonkeyPatch) -> None: "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 diff --git a/agentregistry/adk/auth_remote_a2a_agent.py b/agentregistry/adk/auth_remote_a2a_agent.py index 9a618da3d7a..7fa224c3454 100644 --- a/agentregistry/adk/auth_remote_a2a_agent.py +++ b/agentregistry/adk/auth_remote_a2a_agent.py @@ -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 @@ -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", + location: str = "global", agent_name: str = "agents/AGENT_ID", ) -> RemoteA2aAgent: """Authenticates a remote A2A agent with Application Default Credentials.""" @@ -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, diff --git a/agentregistry/adk/auth_remote_a2a_agent_test.py b/agentregistry/adk/auth_remote_a2a_agent_test.py index 7908a67b904..1c307b28d0e 100644 --- a/agentregistry/adk/auth_remote_a2a_agent_test.py +++ b/agentregistry/adk/auth_remote_a2a_agent_test.py @@ -18,7 +18,7 @@ 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: @@ -26,12 +26,10 @@ def test_auth_remote_a2a_agent(monkeypatch: pytest.MonkeyPatch) -> None: "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 diff --git a/agentregistry/adk/clone_remote_agent.py b/agentregistry/adk/clone_remote_agent.py index 95654d43286..b983be81148 100644 --- a/agentregistry/adk/clone_remote_agent.py +++ b/agentregistry/adk/clone_remote_agent.py @@ -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 @@ -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", + 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, diff --git a/agentregistry/adk/clone_remote_agent_test.py b/agentregistry/adk/clone_remote_agent_test.py index bf8d4f7f7be..ff1ec6b8d1c 100644 --- a/agentregistry/adk/clone_remote_agent_test.py +++ b/agentregistry/adk/clone_remote_agent_test.py @@ -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}", ) diff --git a/agentregistry/adk/compose_multi_agent.py b/agentregistry/adk/compose_multi_agent.py index be10aca83f0..c1ee29480bb 100644 --- a/agentregistry/adk/compose_multi_agent.py +++ b/agentregistry/adk/compose_multi_agent.py @@ -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 @@ -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", + location: str = "global", +) -> AgentRegistry: + """Initializes the AgentRegistry client.""" # Initialize the client registry = AgentRegistry( project_id=project_id, @@ -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", + 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: diff --git a/agentregistry/adk/compose_multi_agent_test.py b/agentregistry/adk/compose_multi_agent_test.py index 352bcfe47a1..cd3de668c41 100644 --- a/agentregistry/adk/compose_multi_agent_test.py +++ b/agentregistry/adk/compose_multi_agent_test.py @@ -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}", ) diff --git a/agentregistry/adk/deploy_agent_runtime.py b/agentregistry/adk/deploy_agent_runtime.py index a1aa80d6eab..75cfb6f645f 100644 --- a/agentregistry/adk/deploy_agent_runtime.py +++ b/agentregistry/adk/deploy_agent_runtime.py @@ -13,7 +13,6 @@ # limitations under the License. # [START agentregistry_deploy_agent_runtime] -import os from typing import Any import vertexai @@ -21,16 +20,12 @@ def deploy_agent_runtime( + project_id: str = "PROJECT_ID", + 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() diff --git a/agentregistry/adk/deploy_agent_runtime_test.py b/agentregistry/adk/deploy_agent_runtime_test.py index c460551af57..10eb43edb1a 100644 --- a/agentregistry/adk/deploy_agent_runtime_test.py +++ b/agentregistry/adk/deploy_agent_runtime_test.py @@ -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, ) diff --git a/agentregistry/adk/discover_agents_and_toolsets.py b/agentregistry/adk/discover_agents_and_toolsets.py index 6e9c86ecbeb..57592d1d52a 100644 --- a/agentregistry/adk/discover_agents_and_toolsets.py +++ b/agentregistry/adk/discover_agents_and_toolsets.py @@ -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 @@ -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", + 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, diff --git a/agentregistry/adk/discover_agents_and_toolsets_test.py b/agentregistry/adk/discover_agents_and_toolsets_test.py index edd622a420a..98ed2a0d39f 100644 --- a/agentregistry/adk/discover_agents_and_toolsets_test.py +++ b/agentregistry/adk/discover_agents_and_toolsets_test.py @@ -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}", ) diff --git a/agentregistry/adk/mcp_custom_headers.py b/agentregistry/adk/mcp_custom_headers.py index 9f5e1bcfacb..a1e6854e093 100644 --- a/agentregistry/adk/mcp_custom_headers.py +++ b/agentregistry/adk/mcp_custom_headers.py @@ -30,17 +30,13 @@ def my_auth_headers(context: Any) -> dict[str, str]: def get_external_mcp_toolset( + project_id: str = "PROJECT_ID", + 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). diff --git a/agentregistry/adk/mcp_custom_headers_test.py b/agentregistry/adk/mcp_custom_headers_test.py index 38fbe7ed13e..65f1d5f550e 100644 --- a/agentregistry/adk/mcp_custom_headers_test.py +++ b/agentregistry/adk/mcp_custom_headers_test.py @@ -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}" diff --git a/agentregistry/adk/resolve_auth_bindings.py b/agentregistry/adk/resolve_auth_bindings.py index fb38d685dd9..f29cc99d1a2 100644 --- a/agentregistry/adk/resolve_auth_bindings.py +++ b/agentregistry/adk/resolve_auth_bindings.py @@ -13,8 +13,6 @@ # 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 @@ -22,16 +20,12 @@ def resolve_auth_bindings( + project_id: str = "PROJECT_ID", + 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()) diff --git a/agentregistry/adk/resolve_auth_bindings_3lo.py b/agentregistry/adk/resolve_auth_bindings_3lo.py index 72d29021f98..41bf142afa1 100644 --- a/agentregistry/adk/resolve_auth_bindings_3lo.py +++ b/agentregistry/adk/resolve_auth_bindings_3lo.py @@ -13,8 +13,6 @@ # limitations under the License. # [START agentregistry_adk_resolve_auth_bindings_3lo] -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 @@ -22,17 +20,13 @@ def resolve_auth_bindings_3lo( + project_id: str = "PROJECT_ID", + location: str = "global", mcp_server_name: str = "mcpServers/SERVER_ID", continue_uri: str = "https://REDIRECT_URI", model: str = "gemini-2.5-flash", ) -> LlmAgent: """Fetches an MCP toolset with 3-legged OAuth bindings and continue_uri.""" - 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()) diff --git a/agentregistry/adk/resolve_auth_bindings_3lo_test.py b/agentregistry/adk/resolve_auth_bindings_3lo_test.py index e6cc5320ee1..e95d86a017c 100644 --- a/agentregistry/adk/resolve_auth_bindings_3lo_test.py +++ b/agentregistry/adk/resolve_auth_bindings_3lo_test.py @@ -18,7 +18,7 @@ import resolve_auth_bindings_3lo -def test_resolve_auth_bindings_3lo(monkeypatch: pytest.MonkeyPatch) -> None: +def test_resolve_auth_bindings_3lo() -> None: project_id = os.environ.get("GOOGLE_CLOUD_PROJECT") mcp_server_id = os.environ.get("AGENTREGISTRY_TEST_3LO_MCP_SERVER_ID") if not project_id or not mcp_server_id: @@ -27,12 +27,10 @@ def test_resolve_auth_bindings_3lo(monkeypatch: pytest.MonkeyPatch) -> None: "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") agent = resolve_auth_bindings_3lo.resolve_auth_bindings_3lo( + project_id=project_id, + location=location, mcp_server_name=f"mcpServers/{mcp_server_id}", continue_uri="https://example.com/oauth/callback", ) diff --git a/agentregistry/adk/resolve_auth_bindings_test.py b/agentregistry/adk/resolve_auth_bindings_test.py index 1f5460f8039..ee05d2c30d2 100644 --- a/agentregistry/adk/resolve_auth_bindings_test.py +++ b/agentregistry/adk/resolve_auth_bindings_test.py @@ -18,7 +18,7 @@ import resolve_auth_bindings -def test_resolve_auth_bindings(monkeypatch: pytest.MonkeyPatch) -> None: +def test_resolve_auth_bindings() -> 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: @@ -26,12 +26,10 @@ def test_resolve_auth_bindings(monkeypatch: pytest.MonkeyPatch) -> None: "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") agent = resolve_auth_bindings.resolve_auth_bindings( + project_id=project_id, + location=location, mcp_server_name=f"mcpServers/{mcp_server_id}", ) assert agent.name == "maps_agent"