|
| 1 | +# A2A OAuth Authentication Sample Agent |
| 2 | + |
| 3 | +This sample demonstrates the **Agent-to-Agent (A2A)** architecture with **OAuth Authentication** workflows in the Agent Development Kit (ADK). The sample implements a multi-agent system where a remote agent can surface OAuth authentication requests to the local agent, which then guides the end user through the OAuth flow before returning the authentication credentials to the remote agent for API access. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The A2A OAuth Authentication sample consists of: |
| 8 | + |
| 9 | +- **Root Agent** (`root_agent`): The main orchestrator that handles user requests and delegates tasks to specialized agents |
| 10 | +- **YouTube Search Agent** (`youtube_search_agent`): A local agent that handles YouTube video searches using LangChain tools |
| 11 | +- **BigQuery Agent** (`bigquery_agent`): A remote A2A agent that manages BigQuery operations and requires OAuth authentication for Google Cloud access |
| 12 | + |
| 13 | +## Architecture |
| 14 | + |
| 15 | +``` |
| 16 | +┌─────────────────┐ ┌────────────────────┐ ┌──────────────────┐ |
| 17 | +│ End User │───▶│ Root Agent │───▶│ BigQuery Agent │ |
| 18 | +│ (OAuth Flow) │ │ (Local) │ │ (Remote A2A) │ |
| 19 | +│ │ │ │ │ (localhost:8001) │ |
| 20 | +│ OAuth UI │◀───│ │◀───│ OAuth Request │ |
| 21 | +└─────────────────┘ └────────────────────┘ └──────────────────┘ |
| 22 | +``` |
| 23 | + |
| 24 | +## Key Features |
| 25 | + |
| 26 | +### 1. **Multi-Agent Architecture** |
| 27 | +- Root agent coordinates between local YouTube search and remote BigQuery operations |
| 28 | +- Demonstrates hybrid local/remote agent workflows |
| 29 | +- Seamless task delegation based on user request types |
| 30 | + |
| 31 | +### 2. **OAuth Authentication Workflow** |
| 32 | +- Remote BigQuery agent surfaces OAuth authentication requests to the root agent |
| 33 | +- Root agent guides end users through Google OAuth flow for BigQuery access |
| 34 | +- Secure token exchange between agents for authenticated API calls |
| 35 | + |
| 36 | +### 3. **Google Cloud Integration** |
| 37 | +- BigQuery toolset with comprehensive dataset and table management capabilities |
| 38 | +- OAuth-protected access to user's Google Cloud BigQuery resources |
| 39 | +- Support for listing, creating, and managing datasets and tables |
| 40 | + |
| 41 | +### 4. **LangChain Tool Integration** |
| 42 | +- YouTube search functionality using LangChain community tools |
| 43 | +- Demonstrates integration of third-party tools in agent workflows |
| 44 | + |
| 45 | +## Setup and Usage |
| 46 | + |
| 47 | +### Prerequisites |
| 48 | + |
| 49 | +1. **Set up OAuth Credentials**: |
| 50 | + ```bash |
| 51 | + export OAUTH_CLIENT_ID=your_google_oauth_client_id |
| 52 | + export OAUTH_CLIENT_SECRET=your_google_oauth_client_secret |
| 53 | + ``` |
| 54 | + |
| 55 | +2. **Start the Remote BigQuery Agent server**: |
| 56 | + ```bash |
| 57 | + # Start the remote a2a server that serves the BigQuery agent on port 8001 |
| 58 | + adk api_server --a2a --port 8001 contributing/samples/a2a_auth/remote_a2a |
| 59 | + ``` |
| 60 | + |
| 61 | +3. **Run the Main Agent**: |
| 62 | + ```bash |
| 63 | + # In a separate terminal, run the adk web server |
| 64 | + adk web contributing/samples/ |
| 65 | + ``` |
| 66 | + |
| 67 | +### Example Interactions |
| 68 | + |
| 69 | +Once both services are running, you can interact with the root agent: |
| 70 | + |
| 71 | +**YouTube Search (No Authentication Required):** |
| 72 | +``` |
| 73 | +User: Search for 3 Taylor Swift music videos |
| 74 | +Agent: I'll help you search for Taylor Swift music videos on YouTube. |
| 75 | +[Agent delegates to YouTube Search Agent] |
| 76 | +Agent: I found 3 Taylor Swift music videos: |
| 77 | +1. "Anti-Hero" - Official Music Video |
| 78 | +2. "Shake It Off" - Official Music Video |
| 79 | +3. "Blank Space" - Official Music Video |
| 80 | +``` |
| 81 | + |
| 82 | +**BigQuery Operations (OAuth Required):** |
| 83 | +``` |
| 84 | +User: List my BigQuery datasets |
| 85 | +Agent: I'll help you access your BigQuery datasets. This requires authentication with your Google account. |
| 86 | +[Agent delegates to BigQuery Agent] |
| 87 | +Agent: To access your BigQuery data, please complete the OAuth authentication. |
| 88 | +[OAuth flow initiated - user redirected to Google authentication] |
| 89 | +User: [Completes OAuth flow in browser] |
| 90 | +Agent: Authentication successful! Here are your BigQuery datasets: |
| 91 | +- dataset_1: Customer Analytics |
| 92 | +- dataset_2: Sales Data |
| 93 | +- dataset_3: Marketing Metrics |
| 94 | +``` |
| 95 | + |
| 96 | +**Dataset Management:** |
| 97 | +``` |
| 98 | +User: Show me details for my Customer Analytics dataset |
| 99 | +Agent: I'll get the details for your Customer Analytics dataset. |
| 100 | +[Using existing OAuth token] |
| 101 | +Agent: Customer Analytics Dataset Details: |
| 102 | +- Created: 2024-01-15 |
| 103 | +- Location: US |
| 104 | +- Tables: 5 |
| 105 | +- Description: Customer behavior and analytics data |
| 106 | +``` |
| 107 | + |
| 108 | +## Code Structure |
| 109 | + |
| 110 | +### Main Agent (`agent.py`) |
| 111 | + |
| 112 | +- **`youtube_search_agent`**: Local agent with LangChain YouTube search tool |
| 113 | +- **`bigquery_agent`**: Remote A2A agent configuration for BigQuery operations |
| 114 | +- **`root_agent`**: Main orchestrator with task delegation logic |
| 115 | + |
| 116 | +### Remote BigQuery Agent (`remote_a2a/bigquery_agent/`) |
| 117 | + |
| 118 | +- **`agent.py`**: Implementation of the BigQuery agent with OAuth toolset |
| 119 | +- **`agent.json`**: Agent card of the A2A agent |
| 120 | +- **`BigQueryToolset`**: OAuth-enabled tools for BigQuery dataset and table management |
| 121 | + |
| 122 | +## OAuth Authentication Workflow |
| 123 | + |
| 124 | +The OAuth authentication process follows this pattern: |
| 125 | + |
| 126 | +1. **Initial Request**: User requests BigQuery operation through root agent |
| 127 | +2. **Delegation**: Root agent delegates to remote BigQuery agent |
| 128 | +3. **Auth Check**: BigQuery agent checks for valid OAuth token |
| 129 | +4. **Auth Request**: If no token, agent surfaces OAuth request to root agent |
| 130 | +5. **User OAuth**: Root agent guides user through Google OAuth flow |
| 131 | +6. **Token Exchange**: Root agent sends OAuth token to BigQuery agent |
| 132 | +7. **API Call**: BigQuery agent uses token to make authenticated API calls |
| 133 | +8. **Result Return**: BigQuery agent returns results through root agent to user |
| 134 | + |
| 135 | +## Supported BigQuery Operations |
| 136 | + |
| 137 | +The BigQuery agent supports the following operations: |
| 138 | + |
| 139 | +### Dataset Operations: |
| 140 | +- **List Datasets**: `bigquery_datasets_list` - Get all user's datasets |
| 141 | +- **Get Dataset**: `bigquery_datasets_get` - Get specific dataset details |
| 142 | +- **Create Dataset**: `bigquery_datasets_insert` - Create new dataset |
| 143 | + |
| 144 | +### Table Operations: |
| 145 | +- **List Tables**: `bigquery_tables_list` - Get tables in a dataset |
| 146 | +- **Get Table**: `bigquery_tables_get` - Get specific table details |
| 147 | +- **Create Table**: `bigquery_tables_insert` - Create new table in dataset |
| 148 | + |
| 149 | +## Extending the Sample |
| 150 | + |
| 151 | +You can extend this sample by: |
| 152 | + |
| 153 | +- Adding more Google Cloud services (Cloud Storage, Compute Engine, etc.) |
| 154 | +- Implementing token refresh and expiration handling |
| 155 | +- Adding role-based access control for different BigQuery operations |
| 156 | +- Creating OAuth flows for other providers (Microsoft, Facebook, etc.) |
| 157 | +- Adding audit logging for authentication events |
| 158 | +- Implementing multi-tenant OAuth token management |
| 159 | + |
| 160 | +## Troubleshooting |
| 161 | + |
| 162 | +**Connection Issues:** |
| 163 | +- Ensure the local ADK web server is running on port 8000 |
| 164 | +- Ensure the remote A2A server is running on port 8001 |
| 165 | +- Check that no firewall is blocking localhost connections |
| 166 | +- Verify the agent.json URL matches the running A2A server |
| 167 | + |
| 168 | +**OAuth Issues:** |
| 169 | +- Verify OAuth client ID and secret are correctly set in .env file |
| 170 | +- Ensure OAuth redirect URIs are properly configured in Google Cloud Console |
| 171 | +- Check that the OAuth scopes include BigQuery access permissions |
| 172 | +- Verify the user has access to the BigQuery projects/datasets |
| 173 | + |
| 174 | +**BigQuery Access Issues:** |
| 175 | +- Ensure the authenticated user has BigQuery permissions |
| 176 | +- Check that the Google Cloud project has BigQuery API enabled |
| 177 | +- Verify dataset and table names are correct and accessible |
| 178 | +- Check for quota limits on BigQuery API calls |
| 179 | + |
| 180 | +**Agent Communication Issues:** |
| 181 | +- Check the logs for both the local ADK web server and remote A2A server |
| 182 | +- Verify OAuth tokens are properly passed between agents |
| 183 | +- Ensure agent instructions are clear about authentication requirements |
0 commit comments