|
| 1 | +# Eventarc Tools Sample |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +This sample agent demonstrates the Eventarc first-party tool in ADK, |
| 6 | +distributed via the `google.adk.integrations.eventarc` module. This tool suite currently includes: |
| 7 | + |
| 8 | +1. `publish_message` |
| 9 | + |
| 10 | +Publishes a structured event in CloudEvents format to a Google Cloud Eventarc message bus. |
| 11 | + |
| 12 | +## How to use |
| 13 | + |
| 14 | +Set up environment variables in your `.env` file for using |
| 15 | +[Google AI Studio](https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-ai-studio) |
| 16 | +or |
| 17 | +[Google Cloud Vertex AI](https://google.github.io/adk-docs/get-started/quickstart/#gemini---google-cloud-vertex-ai) |
| 18 | +for the LLM service for your agent. For example, for using Google AI Studio you |
| 19 | +would set: |
| 20 | + |
| 21 | +- GOOGLE_GENAI_USE_VERTEXAI=FALSE |
| 22 | +- GOOGLE_API_KEY={your api key} |
| 23 | + |
| 24 | +### With Application Default Credentials |
| 25 | + |
| 26 | +This mode is useful for quick development when the agent builder is the only |
| 27 | +user interacting with the agent. The tools are run with these credentials. |
| 28 | + |
| 29 | +1. Create application default credentials on the machine where the agent would |
| 30 | + be running by following https://cloud.google.com/docs/authentication/provide-credentials-adc. |
| 31 | + |
| 32 | +1. Set `CREDENTIALS_TYPE=None` in `agent.py` |
| 33 | + |
| 34 | +1. Run the agent |
| 35 | + |
| 36 | +### With Service Account Keys |
| 37 | + |
| 38 | +This mode is useful for quick development when the agent builder wants to run |
| 39 | +the agent with service account credentials. The tools are run with these |
| 40 | +credentials. |
| 41 | + |
| 42 | +1. Create service account key by following https://cloud.google.com/iam/docs/service-account-creds#user-managed-keys. |
| 43 | + |
| 44 | +1. Set `CREDENTIALS_TYPE=AuthCredentialTypes.SERVICE_ACCOUNT` in `agent.py` |
| 45 | + |
| 46 | +1. Download the key file and replace `"service_account_key.json"` with the path |
| 47 | + |
| 48 | +1. Run the agent |
| 49 | + |
| 50 | +### With Interactive OAuth |
| 51 | + |
| 52 | +1. Follow |
| 53 | + https://developers.google.com/identity/protocols/oauth2#1.-obtain-oauth-2.0-credentials-from-the-dynamic_data.setvar.console_name. |
| 54 | + to get your client id and client secret. Be sure to choose "web" as your client |
| 55 | + type. |
| 56 | + |
| 57 | +1. Follow https://developers.google.com/workspace/guides/configure-oauth-consent to add scope "https://www.googleapis.com/auth/cloud-platform". |
| 58 | + |
| 59 | +1. Follow https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred to add http://localhost/dev-ui/ to "Authorized redirect URIs". |
| 60 | + |
| 61 | +Note: localhost here is just a hostname that you use to access the dev ui, |
| 62 | +replace it with the actual hostname you use to access the dev ui. |
| 63 | + |
| 64 | +1. For 1st run, allow popup for localhost in Chrome. |
| 65 | + |
| 66 | +1. Configure your `.env` file to add two more variables before running the agent: |
| 67 | + |
| 68 | +- OAUTH_CLIENT_ID={your client id} |
| 69 | +- OAUTH_CLIENT_SECRET={your client secret} |
| 70 | + |
| 71 | +Note: don't create a separate .env, instead put it to the same .env file that |
| 72 | +stores your Vertex AI or Dev ML credentials |
| 73 | + |
| 74 | +1. Set `CREDENTIALS_TYPE=AuthCredentialTypes.OAUTH2` in `agent.py` and run the agent |
| 75 | + |
| 76 | +### With Agent Identity (in Agent Runtime / Vertex AI Reasoning Engine) |
| 77 | + |
| 78 | +When deploying this agent to Agent Runtime, it can use its unique SPIFFE-based Agent Identity to authenticate. This is the recommended security best practice. |
| 79 | + |
| 80 | +1. **Configure Deployment**: Create a `.agent_engine_config.json` file in this directory to specify the identity type: |
| 81 | + |
| 82 | + ```json |
| 83 | + { |
| 84 | + "identity_type": "AGENT_IDENTITY" |
| 85 | + } |
| 86 | + ``` |
| 87 | + |
| 88 | +1. **Use Default Credentials**: Leave `CREDENTIALS_TYPE = None` in `agent.py` (which is the default). This configures the agent to use Application Default Credentials (ADC), which automatically resolves to the Agent Identity in the container runtime environment. |
| 89 | + |
| 90 | +1. **Deploy the Agent**: Deploy your agent using the ADK CLI: |
| 91 | + |
| 92 | + ```bash |
| 93 | + uv run adk deploy agent_engine \ |
| 94 | + --project=YOUR_PROJECT_ID \ |
| 95 | + --region=YOUR_REGION \ |
| 96 | + --display_name=eventarc-agent-test \ |
| 97 | + contributing/samples/integrations/eventarc |
| 98 | + ``` |
| 99 | + |
| 100 | + Take note of the generated **Reasoning Engine ID** (e.g., `1234567890`) and the **Project Number** of your project. |
| 101 | + |
| 102 | +1. **Grant IAM Permissions**: Grant the Eventarc Message Bus User role (`roles/eventarc.messageBusUser`) to the Agent Identity principal at the project level: |
| 103 | + |
| 104 | + ```bash |
| 105 | + gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \ |
| 106 | + --member="principal://agents.global.org-171145599760.system.id.goog/resources/aiplatform/projects/YOUR_PROJECT_NUMBER/locations/YOUR_REGION/reasoningEngines/YOUR_REASONING_ENGINE_ID" \ |
| 107 | + --role="roles/eventarc.messageBusUser" |
| 108 | + ``` |
| 109 | + |
| 110 | + *Note: Eventarc Advanced message buses require `roles/eventarc.messageBusUser` for publishing, rather than `roles/eventarc.publisher`.* |
| 111 | + |
| 112 | +## Sample prompts |
| 113 | + |
| 114 | +- "Publish an event of type 'com.example.hello' to bus 'projects/my-project/locations/global/messageBuses/my-bus' with data 'Hello World' and source '//my/agent'" |
| 115 | +- "Send a JSON payload to Eventarc bus 'projects/my-project/locations/global/messageBuses/my-bus' representing a user sign-up event" |
0 commit comments