|
| 1 | +# Register an A2UI Agent deployed on Vertex AI Agent Engine with Gemini Enterprise |
| 2 | + |
| 3 | +This sample demonstrates how to deploy an A2UI Agent on **Agent Engine** and |
| 4 | +register on **Gemini Enterprise**. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +High level steps: |
| 9 | + |
| 10 | +- Config Authorization |
| 11 | +- Setup environment variables |
| 12 | +- Develop A2UI agent with ADK + A2A (sample code provided) |
| 13 | +- Deploy the agent to Agent Engine |
| 14 | +- Register the agent on Gemini Enterprise |
| 15 | + |
| 16 | +## Config Authorization |
| 17 | + |
| 18 | +Check **Before you begin** section and follow **Obtain authorization details** |
| 19 | +section on |
| 20 | +[Register and manage A2A agents](https://docs.cloud.google.com/gemini/enterprise/docs/register-and-manage-an-a2a-agent). |
| 21 | +Download JSON that looks like: |
| 22 | + |
| 23 | +``` |
| 24 | +{ |
| 25 | + "web": { |
| 26 | + "client_id": "<client id>", |
| 27 | + "project_id": "<Google Cloud project id>", |
| 28 | + "auth_uri": "https://accounts.google.com/o/oauth2/auth", |
| 29 | + "token_uri": "https://oauth2.googleapis.com/token", |
| 30 | + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", |
| 31 | + "client_secret": "<secret>", |
| 32 | + "redirect_uris": [ |
| 33 | + "https://vertexaisearch.cloud.google.com/oauth-redirect", |
| 34 | + "https://vertexaisearch.cloud.google.com/static/oauth/oauth.html" |
| 35 | + ] |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +NOTE: For this deployment, you can skip the rest of **Register and manage A2A |
| 41 | +agents** page. |
| 42 | + |
| 43 | +Replace **YOUR_CLIENT_ID** with `client_id` which can be found in the downloaded |
| 44 | +json and save the following as **authorizationUri** |
| 45 | + |
| 46 | +``` |
| 47 | +https://accounts.google.com/o/oauth2/v2/auth?client_id=<YOUR_CLIENT_ID>&redirect_uri=https%3A%2F%2Fvertexaisearch.cloud.google.com%2Fstatic%2Foauth%2Foauth.html&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform&include_granted_scopes=true&response_type=code&access_type=offline&prompt=consent |
| 48 | +``` |
| 49 | + |
| 50 | +In your console, run this: |
| 51 | + |
| 52 | +``` |
| 53 | +curl -X POST \ |
| 54 | + -H "Authorization: Bearer $(gcloud auth print-access-token)" \ |
| 55 | + -H "Content-Type: application/json" \ |
| 56 | + -H "X-Goog-User-Project: <YOUR_PROJECT_ID>" \ |
| 57 | + "https://<ENDPOINT_LOCATION>-discoveryengine.googleapis.com/v1alpha/projects/<YOUR_PROJECT_ID>/locations/<LOCATION>/authorizations?authorizationId=<AUTH_ID>" \ |
| 58 | + -d '{ |
| 59 | + "name": "projects/<YOUR_PROJECT_ID>/locations/<LOCATION>/authorizations/<AUTH_ID>", |
| 60 | + "serverSideOauth2": { |
| 61 | + "clientId": "<OAUTH_CLIENT_ID>", |
| 62 | + "clientSecret": "<OAUTH_CLIENT_SECRET>", |
| 63 | + "authorizationUri": "<OAUTH_AUTH_URI>", |
| 64 | + "tokenUri": "<OAUTH_TOKEN_URI>" |
| 65 | + } |
| 66 | + }' |
| 67 | +``` |
| 68 | + |
| 69 | +Replace the following: |
| 70 | + |
| 71 | +- **YOUR_PROJECT_ID**: the ID of your project. There are 3 occasions |
| 72 | +- **ENDPOINT_LOCATION**: the multi-region for your API request. Specify one of |
| 73 | + the following values: |
| 74 | + - *us* for the US multi-region |
| 75 | + - *eu* for the EU multi-region |
| 76 | + - *global* for the Global location |
| 77 | +- **LOCATION**: the multi-region of your data store: *global*, *us*, or *eu*. |
| 78 | + There are 2 occasions |
| 79 | +- **AUTH_ID**: The ID of the authorization resource. This is an arbitrary |
| 80 | + alphanumeric ID that you define. You need to reference this ID later when |
| 81 | + registering an Agent that requires OAuth support. There are 2 occasions. |
| 82 | +- **OAUTH_CLIENT_ID**: copy `client_id` from the downloaded JSON. |
| 83 | +- **OAUTH_CLIENT_SECRET**: copy `client_secret` from the downloaded JSON. |
| 84 | +- **OAUTH_AUTH_URI**: the value of **authorizationUri**. See above. |
| 85 | +- **OAUTH_TOKEN_URI**: copy `token_uri` from the downloaded JSON. |
| 86 | + |
| 87 | +NOTE: if `$(gcloud auth print-access-token)` does not work for you, replace it |
| 88 | +with `$(gcloud auth application-default print-access-token)` and try again. |
| 89 | + |
| 90 | +As result, you will get **AGENT_AUTHORIZATION** like this: |
| 91 | + |
| 92 | +``` |
| 93 | +projects/PROJECT_NUMBER/locations/global/authorizations/<AUTH_ID> |
| 94 | +``` |
| 95 | + |
| 96 | +The value will be used as an environment variable described below. |
| 97 | + |
| 98 | +NOTE: if you already have an agent that is deployed to Agent Engine, skip to |
| 99 | +**Manually Register An Agent** section. |
| 100 | + |
| 101 | +## Setup Environment Variables |
| 102 | + |
| 103 | +1. **Copy `.env.example`:** Duplicate the `.env.example` file and rename it to |
| 104 | + `.env`. |
| 105 | + - `cd /path/to/agent_engine` |
| 106 | + - `cp .env.example .env` |
| 107 | +2. **Fill `.env`:** Update the `.env` file with your specific Google Cloud |
| 108 | + project details: |
| 109 | + * `PROJECT_ID`: Your Google Cloud Project ID. |
| 110 | + * `LOCATION`: The Google Cloud region you want to deploy the agent in |
| 111 | + (e.g., `us-central1`). This location is **not** the same as the |
| 112 | + *location* used in the command above. |
| 113 | + * `STORAGE_BUCKET`: A Google Cloud Storage bucket name for staging. It |
| 114 | + starts with **"gs://"**. |
| 115 | + * `GEMINI_ENTERPRISE_APP_ID`: Your Gemini Enterprise Application ID. You |
| 116 | + can create a new App or use an existing one on Google Cloud Gemini |
| 117 | + Enterprise. |
| 118 | + * `AGENT_AUTHORIZATION`: the value **AGENT_AUTHORIZATION** obtained above. |
| 119 | + |
| 120 | +## Running the Script |
| 121 | + |
| 122 | +The `main.py` script performs the following actions: |
| 123 | + |
| 124 | +1. Initializes the Vertex AI client. |
| 125 | +2. Defines a sample "Contact Card Agent" skill and creates an agent card. |
| 126 | +3. Creates a local `A2aAgent` instance. |
| 127 | +4. Deploys the agent to Vertex AI Agent Engine (`client.agent_engines.create`). |
| 128 | +5. Fetches the deployed agent's card. |
| 129 | +6. Registers the agent on Gemini Enterprise using the Discovery Engine API. |
| 130 | + |
| 131 | +To run the script using `uv`: |
| 132 | + |
| 133 | +1. **Navigate to the script directory:** |
| 134 | + - `cd /path/to/agent_engine` |
| 135 | +2. **Create and activate a virtual environment:** |
| 136 | + - `uv venv` |
| 137 | + - `source .venv/bin/activate` |
| 138 | +3. **Install dependencies:** |
| 139 | + - `uv sync` |
| 140 | +4. **Run the script:** |
| 141 | + - `uv run deploy.py` |
| 142 | + - It may take 5-10 minutes to finish. |
| 143 | + |
| 144 | +## Manually Register An Agent |
| 145 | + |
| 146 | +If you have an Agent that is already deployed to Agent Engine, you can manually |
| 147 | +register it on Gemini Enterprise without running "main.py" script. |
| 148 | + |
| 149 | +1. Complete **Config Authorization** section above. |
| 150 | +2. Open Google Cloud **Gemini Enterprise**. |
| 151 | +3. Click on the **App** you want to register your agent. |
| 152 | + - If you don't see the app being listed, click **Edit** to switch location |
| 153 | +4. Select **Agents** from the left nav bar. |
| 154 | +5. Click **Add agent** and select **Add** on **A2A** card. |
| 155 | +6. Copy this following JSON to the "Agent Card JSON" input box. |
| 156 | + |
| 157 | + ``` |
| 158 | + { |
| 159 | + "name": "Test Contact Card Agent", |
| 160 | + "url": "https://<LOCATION>-aiplatform.googleapis.com/v1beta1/<RESOURCE_NAME>/a2a", |
| 161 | + "description": "A helpful assistant agent that can find contact card s.", |
| 162 | + "skills": [ |
| 163 | + { |
| 164 | + "description": "A helpful assistant agent that can find contact cards.", |
| 165 | + "tags": [ |
| 166 | + "Contact-Card" |
| 167 | + ], |
| 168 | + "name": "Contact Card Agent", |
| 169 | + "examples": [ |
| 170 | + "Who is John Doe?", |
| 171 | + "List all contact cards." |
| 172 | + ], |
| 173 | + "id": "contact_card_agent" |
| 174 | + } |
| 175 | + ], |
| 176 | + "version": "1.0.0", |
| 177 | + "capabilities": { |
| 178 | + "streaming": true, |
| 179 | + "extensions": [ |
| 180 | + { |
| 181 | + "uri": "https://a2ui.org/a2a-extension/a2ui/v0.8", |
| 182 | + "description": "Ability to render A2UI", |
| 183 | + "required": false, |
| 184 | + "params": { |
| 185 | + "supportedCatalogIds": [ |
| 186 | + "https://a2ui.org/specification/v0_8/standard_catalog_definition.json" |
| 187 | + ] |
| 188 | + } |
| 189 | + } |
| 190 | + ] |
| 191 | + }, |
| 192 | + "protocolVersion": "0.3.0", |
| 193 | + "defaultOutputModes": [ |
| 194 | + "application/json" |
| 195 | + ], |
| 196 | + "defaultInputModes": [ |
| 197 | + "text/plain" |
| 198 | + ], |
| 199 | + "supportsAuthenticatedExtendedCard": true, |
| 200 | + "preferredTransport": "HTTP+JSON" |
| 201 | + } |
| 202 | + ``` |
| 203 | +
|
| 204 | + Replace **LOCATION** and **RESOURCE_NAME**. |
| 205 | +
|
| 206 | + - LOCATION is where you deploy your agent. For example; us-central1. |
| 207 | + - RESOURCE_NAME can be found on Google Cloud **Agent Engine**: click the |
| 208 | + agent; click **Service Configuration**; select **Deployment details**; |
| 209 | + copy **Resource name**. |
| 210 | +
|
| 211 | + Update *name*, *description*, *skills*, *version* as needed. Leave other |
| 212 | + values unchanged. |
| 213 | +
|
| 214 | +7. Click **Preview Agent Details** |
| 215 | +
|
| 216 | +8. Click **Next** |
| 217 | +
|
| 218 | +9. Fill the **Agent authorization** form: |
| 219 | +
|
| 220 | + - Copy `client_id`, `client_secret`, `token_uri` from the downloaded JSON. |
| 221 | + - Copy **authorizationUri** value from the above to **Authorization URL**. |
| 222 | + - Leave **Scopes** field empty. |
| 223 | + - Click **Finish** |
| 224 | +
|
| 225 | +## Test Your Agent |
| 226 | +
|
| 227 | +1. Open Google Cloud Console and search for **"Gemini Enterprise"** and click |
| 228 | + on it. |
| 229 | +2. Open the project you used in the above setting. |
| 230 | +3. Click on the **App** you used to register your agent. |
| 231 | + - If you don't see your app being listed, click **"Edit"** to switch |
| 232 | + location |
| 233 | +4. Select **"Agents"** from the left nav bar. |
| 234 | +5. Click the three-dot button on the **"Actions"** column and select |
| 235 | + **"Previwe"** menu. |
| 236 | +6. It will open Gemini Enterprise Agent page. |
| 237 | +7. Try queries like *"Find contact card of Sarah"*. |
| 238 | + - If this is the first time you start a chat with the agent, it will ask |
| 239 | + for manual authorization. |
| 240 | +8. You should see a Contact Card being rendered on Gemini Enterprise. |
0 commit comments