Authenticate Agent Registry MCP servers with 3-legged OAuth (3LO)

When an agent invokes a Model Context Protocol (MCP) server registered in Agent Registry that accesses data on behalf of an individual user, you can combine 3-legged OAuth (3LO) through the Agent Identity auth manager with egress governance through Agent Gateway and Identity and Access Management (IAM) Unified Access Policies (UAPs).

This document explains how workload egress authorization and user-delegated 3LO authentication work together, and how to configure an IAM UAP for a 3LO-authenticated MCP server.

How 3LO authentication and egress governance work together

When an agent deployed on Agent Runtime on Gemini Enterprise Agent Platform calls a registered MCP server on behalf of an end user, two independent security layers operate on the request:

  1. Workload authorization at Agent Gateway: Your agent presents its cryptographic workload identity (principal://agents.global.org-...) over mutual TLS (mTLS) to Agent Gateway. Identity-Aware Proxy (IAP) evaluates your UAPs (iap.googleapis.com/resources.egressViaIAP) to verify whether that agent identity is authorized to reach the registered MCP server and invoke the requested MCP method or tool.

  2. User-delegated authentication at the MCP server: The Agent Development Kit (ADK) resolves the MCP server's outbound auth_provider_binding in Agent Registry and uses the Agent Identity auth manager (GcpAuthProvider) to manage the end-user OAuth 2.0 authorization code flow. After the user grants consent, the auth manager stores the user's OAuth tokens in a Google-managed credential vault, and the ADK attaches the user's bearer token to the outbound MCP tool invocation.

Before you begin

Before you configure 3LO authentication and egress governance for a registered MCP server, complete the following prerequisites:

  1. If you plan to enforce egress policies with UAPs, turn off enforcement for the Disable binding access policy to resource (constraints/iam.managed.disableAccessPolicyBindings) organization policy constraint. For instructions, see Updating policies with boolean rules.

  2. Enable the required APIs in your Google Cloud project:

    gcloud services enable \
        agentidentity.googleapis.com \
        agentregistry.googleapis.com \
        iam.googleapis.com \
        iap.googleapis.com \
        networkservices.googleapis.com \
        --project=PROJECT_ID
    

    Replace PROJECT_ID with your Google Cloud project ID.

  3. Register your MCP server in Agent Registry and record its resource ID (MCP_SERVER_ID) and uniform resource name (URN, TARGET_MCP_URN).

  4. Deploy or provision an agent with Agent Identity enabled and record its agent resource ID (ENGINE_ID).

  5. Install or upgrade the ADK with the Agent Identity and MCP extras in your agent environment:

    pip install --upgrade "google-adk[agent-identity,mcp]>=1.29.0"
    

Required roles

To get the permissions that you need to configure 3LO authentication and egress governance for Agent Registry MCP servers, ask your administrator to grant you the following IAM roles:

  • Create and manage 3LO auth providers: Agent Identity Admin (roles/agentidentity.admin) on the project
  • Retrieve end-user OAuth credentials from the auth provider at runtime (grant to the agent's workload identity):
  • Create and manage Agent Registry auth provider bindings: Agent Registry Admin (roles/agentregistry.admin) on the project
  • Discover MCP servers and resolve bindings at runtime (grant to the agent's workload identity): Agent Registry Viewer (roles/agentregistry.viewer) on the project
  • Create and bind IAM UAPs:

For more information about granting roles, see Manage access to projects, folders, and organizations.

You might also be able to get the required permissions through custom roles or other predefined roles.

Step 1: Create a 3LO auth provider

Create a 3-legged OAuth auth provider in the Agent Identity auth manager to store your OAuth 2.0 client credentials and broker the authorization code exchange, and grant your agent's workload identity (principal://agents.global.org-...) the Agent Identity User (roles/agentidentity.user), Service Usage Consumer (roles/serviceusage.serviceUsageConsumer), and Agent Registry Viewer (roles/agentregistry.viewer) roles.

For step-by-step instructions to register the OAuth callback URI, create the 3LO auth provider (gcloud agent-identity auth-providers create), and grant runtime roles to your agent's workload identity, see Create a 3-legged auth provider in Authenticate using 3-legged OAuth with auth manager.

Step 2: Bind the 3LO auth provider to the registered MCP server

Create an outbound binding in Agent Registry that links your registered MCP server (--target-identifier) to the 3LO auth provider (--auth-provider-binding), required OAuth 2.0 scopes (--auth-provider-binding-scopes), and post-consent redirect URI (--auth-provider-binding-continue-uri). When your agent fetches the MCP toolset from Agent Registry, the ADK automatically detects this binding without requiring hardcoded auth provider IDs in your agent code.

For complete gcloud agent-registry bindings create and Terraform (google_agent_registry_binding) instructions, see Access tools on behalf of a user and Create an auth provider binding.

Step 3: Authorize outbound MCP server calls with a UAP

When your agent routes outbound traffic through Agent Gateway with iapPolicyVersion set to V2, IAP blocks outbound calls by default until you bind an IAM UAP that grants the iap.googleapis.com/resources.egressViaIAP permission.

For a 3LO-authenticated MCP server, configure your UAP to authorize the following:

  • Registered MCP server access: Allow your agent's workload identity to reach the registered MCP server (destination.agent_registry.mcp_server.name). You can optionally scope the condition to read-only tools (destination.agent_registry.mcp_server.tool.annotations.read_only_hint == true) or an explicit list of tool names (destination.agent_registry.mcp_server.tool.name in [...]).

  • Control-plane API access: If all outbound agent traffic routes through Agent Gateway, allow your agent's workload identity to reach agentregistry.googleapis.com, agentidentity.googleapis.com, and agentidentitycredentials.googleapis.com so that the ADK can resolve the MCP binding and retrieve 3LO tokens at runtime.

The following example creates and binds a project-scoped UAP that allows a specific agent to invoke read-only tools on a registered MCP server and reach the required Agent Identity and Agent Registry control-plane domains:

gcloud

  1. Create the UAP in the project that hosts your Agent Gateway:

    gcloud iam access-policies create POLICY_ID \
        --project=PROJECT_ID \
        --location=global \
        --display-name="Allow 3LO MCP server egress" \
        --details-rules='[
          {
            "description": "Allow agent to call read-only tools on the registered 3LO MCP server",
            "effect": "ALLOW",
            "principals": [
              "principal://agents.global.org-ORGANIZATION_ID.system.id.goog/resources/aiplatform/projects/PROJECT_NUMBER/locations/LOCATION/reasoningEngines/ENGINE_ID"
            ],
            "operation": {
              "permissions": [
                "iap.googleapis.com/resources.egressViaIAP"
              ]
            },
            "conditions": {
              "iap.googleapis.com": {
                "title": "Allow registered MCP server read-only tools",
                "expression": "(destination.is_registered == true) && (destination.agent_registry.mcp_server.name == '\''projects/PROJECT_ID/locations/LOCATION/mcpServers/MCP_SERVER_ID'\'') && ((destination.agent_registry.mcp_server.method != '\''tools/call'\'') || (destination.agent_registry.mcp_server.tool.annotations.read_only_hint == true))"
              }
            }
          },
          {
            "description": "Allow agent to reach Agent Registry and Agent Identity token endpoints",
            "effect": "ALLOW",
            "principals": [
              "principal://agents.global.org-ORGANIZATION_ID.system.id.goog/resources/aiplatform/projects/PROJECT_NUMBER/locations/LOCATION/reasoningEngines/ENGINE_ID"
            ],
            "operation": {
              "permissions": [
                "iap.googleapis.com/resources.egressViaIAP"
              ]
            },
            "conditions": {
              "iap.googleapis.com": {
                "title": "Scoped allowlist for registry and 3LO token broker APIs",
                "expression": "(destination.is_registered == false) && (destination.unregistered.host in ['\''agentregistry.googleapis.com'\'', '\''agentidentity.googleapis.com'\'', '\''agentidentitycredentials.googleapis.com'\''])"
              }
            }
          }
        ]'
    
  2. Bind the UAP to the project that hosts your Agent Gateway:

    gcloud iam policy-bindings create POLICY_BINDING_ID \
        --project=PROJECT_ID \
        --location=global \
        --policy-kind=ACCESS \
        --target-resource="//cloudresourcemanager.googleapis.com/projects/PROJECT_ID" \
        --policy="projects/PROJECT_ID/locations/global/accessPolicies/POLICY_ID"
    

Terraform

To declaratively create the UAP and bind it to the project that hosts your Agent Gateway, configure the google_iam_project_access_policy and google_iam_projects_policy_binding resources:

resource "google_iam_project_access_policy" "mcp_3lo_egress_policy" {
  project          = "PROJECT_ID"
  location         = "global"
  access_policy_id = "POLICY_ID"
  display_name     = "Allow 3LO MCP server egress"

  details {
    rules {
      description = "Allow agent to call read-only tools on the registered 3LO MCP server"
      effect      = "ALLOW"

      principals = [
        "principal://agents.global.org-ORGANIZATION_ID.system.id.goog/resources/aiplatform/projects/PROJECT_NUMBER/locations/LOCATION/reasoningEngines/ENGINE_ID"
      ]

      operation {
        permissions = ["iap.googleapis.com/resources.egressViaIAP"]
      }

      conditions {
        service    = "iap.googleapis.com"
        expression = "(destination.is_registered == true) && (destination.agent_registry.mcp_server.name == 'projects/PROJECT_ID/locations/LOCATION/mcpServers/MCP_SERVER_ID') && ((destination.agent_registry.mcp_server.method != 'tools/call') || (destination.agent_registry.mcp_server.tool.annotations.read_only_hint == true))"
      }
    }

    rules {
      description = "Allow agent to reach Agent Registry and Agent Identity token endpoints"
      effect      = "ALLOW"

      principals = [
        "principal://agents.global.org-ORGANIZATION_ID.system.id.goog/resources/aiplatform/projects/PROJECT_NUMBER/locations/LOCATION/reasoningEngines/ENGINE_ID"
      ]

      operation {
        permissions = ["iap.googleapis.com/resources.egressViaIAP"]
      }

      conditions {
        service    = "iap.googleapis.com"
        expression = "(destination.is_registered == false) && (destination.unregistered.host in ['agentregistry.googleapis.com', 'agentidentity.googleapis.com', 'agentidentitycredentials.googleapis.com'])"
      }
    }
  }
}

resource "google_iam_projects_policy_binding" "mcp_3lo_egress_binding" {
  project           = "PROJECT_ID"
  location          = "global"
  policy_binding_id = "POLICY_BINDING_ID"
  policy_kind       = "ACCESS"
  policy            = "projects/PROJECT_ID/locations/global/accessPolicies/${google_iam_project_access_policy.mcp_3lo_egress_policy.access_policy_id}"

  target {
    resource = "//cloudresourcemanager.googleapis.com/projects/PROJECT_ID"
  }
}

Replace the following:

  • POLICY_ID: a unique ID for the UAP, such as mcp-3lo-egress-policy.

  • POLICY_BINDING_ID: a unique ID for the policy binding, such as mcp-3lo-egress-binding.

  • PROJECT_ID: the ID of the project where your Agent Gateway and MCP server are configured.

  • ORGANIZATION_ID: your Google Cloud organization ID.

  • PROJECT_NUMBER: the project number of the project where your agent is deployed.

  • LOCATION: the region where your agent and MCP server are deployed, such as us-central1.

  • ENGINE_ID: the resource ID of your deployed reasoning engine agent.

  • MCP_SERVER_ID: the resource ID of your registered MCP server in Agent Registry. Don't include a leading slash before projects/ in destination.agent_registry.mcp_server.name.

Step 4: Resolve the MCP toolset and complete the 3LO consent flow

After you configure the 3LO auth provider, Agent Registry binding, and UAP, use the AgentRegistry client in the ADK to fetch the MCP toolset with continue_uri, and register GcpAuthProvider() inside your AdkApp wrapper's set_up() method so that the provider is initialized when your deployed container starts:

import os
from agentplatform.frameworks import AdkApp
from google.adk.agents import Agent
from google.adk.auth.credential_manager import CredentialManager
from google.adk.integrations.agent_identity import GcpAuthProvider
from google.adk.integrations.agent_registry import AgentRegistry

PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
LOCATION = os.environ.get("GOOGLE_CLOUD_LOCATION", "LOCATION")

registry = AgentRegistry(project_id=PROJECT_ID, location=LOCATION)
mcp_toolset = registry.get_mcp_toolset(
    mcp_server_name=f"projects/{PROJECT_ID}/locations/{LOCATION}/mcpServers/MCP_SERVER_ID",
    continue_uri="CONTINUE_URI",
)

root_agent = Agent(
    name="mcp_3lo_agent",
    model="gemini-2.5-flash",
    instruction="Use the registered MCP tools to assist the user.",
    tools=[mcp_toolset],
)


class AuthenticatedAdkApp(AdkApp):

  def set_up(self):
    CredentialManager.register_auth_provider(GcpAuthProvider())
    super().set_up()


app = AuthenticatedAdkApp(agent=root_agent)

Replace the following:

  • LOCATION: the regional location of your auth provider and MCP server, such as us-central1.

  • MCP_SERVER_ID: the resource ID of your registered MCP server in Agent Registry.

  • CONTINUE_URI: the HTTPS validation endpoint where users are redirected after granting consent.

When a user query triggers an MCP tool that requires 3LO consent and no valid token exists in the Agent Identity vault for that user, your client application completes a three-step consent handoff:

  1. Intercept adk_request_credential: Record the consent_nonce, auth_config, and function call id from the returned adk_request_credential function call, and redirect the user's browser to auth_uri.

  2. Finalize credentials at continue_uri: When the /oauthcallback endpoint redirects the user to your continue_uri, verify the user's session and call the credentials:finalize endpoint with userId, userIdValidationState, and consentNonce.

  3. Resume the agent turn: Send a FunctionResponse with name="adk_request_credential", the original function call id, and the stored auth_config so the agent retrieves the finalized 3LO access token and invokes the MCP tool through Agent Gateway.

For complete code examples, see Resolve bindings in your ADK code and Update your client-side application.

What's next