8000 Add dynamicAuthConfig to ExecuteCustomQuery. Add sample agent for App… · nag763/adk-python@62a543b · GitHub
[go: up one dir, main page]

Skip to content

Commit 62a543b

Browse files
google-genai-botcopybara-github
authored andcommitted
Add dynamicAuthConfig to ExecuteCustomQuery. Add sample agent for ApplicationIntegrationToolset which uses Integration Connectors with end user credentials.
PiperOrigin-RevId: 761356343
1 parent cbdb5fc commit 62a543b

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Application Integration Agent Sample with End-User Credentials
2+
3+
## Introduction
4+
5+
This sample demonstrates how to use the `ApplicationIntegrationToolset` within
6+
an ADK agent to interact with external applications using **end-user OAuth 2.0
7+
credentials**. Specifically, this agent (`agent.py`) is configured to interact
8+
with Google Calendar using a pre-configured Application Integration connection
9+
and authenticating as the end user.
10+
11+
## Prerequisites
12+
13+
1. **Set up Integration Connection:**
14+
* You need an existing
15+
[Integration connection](https://cloud.google.com/integration-connectors/docs/overview)
16+
configured to interact with Google Calendar APIs. Follow the
17+
[documentation](https://google.github.io/adk-docs/tools/google-cloud-tools/#use-integration-connectors)
18+
to provision the Integration Connector in Google Cloud. You will need
19+
the `Connection Name`, `Project ID`, and `Location` of your connection.
20+
* Ensure the connection is configured to use Google Calendar (e.g., by
21+
enabling the `google-calendar-connector` or a similar connector).
22+
23+
2. **Configure OAuth 2.0 Client:**
24+
* You need an OAuth 2.0 Client ID and Client Secret that is authorized to
25+
access the required Google Calendar scopes (e.g.,
26+
`https://www.googleapis.com/auth/calendar.readonly`). You can create
27+
OAuth credentials in the Google Cloud Console under "APIs & Services"
28+
-> "Credentials".
29+
30+
3. **Configure Environment Variables:**
31+
* Create a `.env` file in the same directory as `agent.py` (or add to
32+
your existing one).
33+
* Add the following variables to the `.env` file, replacing the
34+
placeholder values with your actual connection details:
35+
36+
```dotenv
37+
CONNECTION_NAME=<YOUR_CALENDAR_CONNECTION_NAME>
38+
CONNECTION_PROJECT=<YOUR_GOOGLE_CLOUD_PROJECT_ID>
39+
CONNECTION_LOCATION=<YOUR_CONNECTION_LOCATION>
40+
CLIENT_ID=<YOUR_OAUTH_CLIENT_ID>
41+
CLIENT_SECRET=<YOUR_OAUTH_CLIENT_SECRET>
42+
```
43+
44+
## End-User Authentication (OAuth 2.0)
45+
46+
This agent utilizes the `AuthCredential` and `OAuth2Auth` classes from the ADK
47+
to handle authentication.
48+
* It defines an OAuth 2.0 scheme (`oauth2_scheme`) based on Google Cloud's
49+
OAuth endpoints and required scopes.
50+
* It uses the `CLIENT_ID` and `CLIENT_SECRET` from the environment variables
51+
(or hardcoded values in the sample) to configure `OAuth2Auth`.
52+
* This `AuthCredential` is passed to the `ApplicationIntegrationToolset`,
53+
enabling the tool to make authenticated API calls to Google Calendar on
54+
behalf of the user running the agent. The ADK framework will typically
55+
handle the OAuth flow (e.g., prompting the user for consent) when the tool
56+
is first invoked.
57+
58+
## How to Use
59+
60+
1. **Install Dependencies:** Ensure you have the necessary libraries installed
61+
(e.g., `google-adk`, `python-dotenv`).
62+
2. **Run the Agent:** Execute the agent script from your terminal:
63+
```bash
64+
python agent.py
65+
```
66+
3. **Interact:** Once the agent starts, you can interact with it. If it's the
67+
first time using the tool requiring OAuth, you might be prompted to go
68+
through the OAuth consent flow in your browser. After successful
69+
authentication, you can ask the agent to perform tasks.
70+
71+
## Sample Prompts
72+
73+
Here are some examples of how you can interact with the agent:
74+
75+
* `Can you list events from my primary calendar?`
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import agent
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from dotenv import load_dotenv
18+
from google.adk import Agent
19+
from google.adk.auth import AuthCredential
20+
from google.adk.auth import AuthCredentialTypes
21+
from google.adk.auth import OAuth2Auth
22+
from google.adk.tools.application_integration_tool.application_integration_toolset import ApplicationIntegrationToolset
23+
from google.adk.tools.openapi_tool.auth.auth_helpers import dict_to_auth_scheme
24+
from google.genai import types
25+
26+
# Load environment variables from .env file
27+
load_dotenv()
28+
29+
connection_name = os.getenv("CONNECTION_NAME")
30+
connection_project = os.getenv("CONNECTION_PROJECT")
31+
connection_location = os.getenv("CONNECTION_LOCATION")
32+
client_secret = os.getenv("CLIENT_SECRET")
33+
client_id = os.getenv("CLIENT_ID")
34+
35+
36+
oauth2_data_google_cloud = {
37+
"type": "oauth2",
38+
"flows": {
39+
"authorizationCode": {
40+
"authorizationUrl": "https://accounts.google.com/o/oauth2/auth",
41+
"tokenUrl": "https://oauth2.googleapis.com/token",
42+
"scopes": {
43+
"https://www.googleapis.com/auth/cloud-platform": (
44+
"View and manage your data across Google Cloud Platform"
45+
" services"
46+
),
47+
"https://www.googleapis.com/auth/calendar.readonly": (
48+
"View your calendars"
49+
),
50+
},
51+
}
52+
},
53+
}
54+
55+
oauth2_scheme = dict_to_auth_scheme(oauth2_data_google_cloud)
56+
57+
auth_credential = AuthCredential(
58+
auth_type=AuthCredentialTypes.OAUTH2,
59+
oauth2=OAuth2Auth(
60+
client_id=client_id,
61+
client_secret=client_secret,
62+
),
63+
)
64+
65+
calendar_tool = ApplicationIntegrationToolset(
66+
project=connection_project,
67+
location=connection_location,
68+
tool_name_prefix="calendar_tool",
69+
connection=connection_name,
70+
actions=["GET_calendars/%7BcalendarId%7D/events"],
71+
tool_instructions="""
72+
Use this tool to list events in a calendar. Get calendarId from the user and use it in tool as following example:
73+
connectorInputPayload: { "Path parameters": { "calendarId": "primary" } }. Follow the schema correctly. Note its "Path parameters" and not "Path_parameters".
74+
""",
75+
auth_scheme=oauth2_scheme,
76+
auth_credential=auth_credential,
77+
)
78+
79+
root_agent = Agent(
80+
model="gemini-2.0-flash",
81+
name="data_processing_agent",
82+
description="Agent that can list events in a calendar.",
83+
instruction="""
84+
Helps you with calendar related tasks.
85+
""",
86+
tools=calendar_tool.get_tools(),
87+
generate_content_config=types.GenerateContentConfig(
88+
safety_settings=[
89+
types.SafetySetting(
90+
category=types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
91+
threshold=types.HarmBlockThreshold.OFF,
92+
),
93+
]
94+
),
95+
)

src/google/adk/tools/application_integration_tool/clients/connections_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,9 @@ def execute_custom_query_request() -> Dict[str, Any]:
728728
"query": {"$ref": "#/components/schemas/query"},
729729
"timeout": {"$ref": "#/components/schemas/timeout"},
730730
"pageSize": {"$ref": "#/components/schemas/pageSize"},
731+
"dynamicAuthConfig": {
732+
"$ref": "#/components/schemas/dynamicAuthConfig"
733+
},
731734
},
732735
}
733736

0 commit comments

Comments
 (0)
0