Replies: 3 comments
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: You don't call Hello! That's an excellent question about handling the complete expiration of an OAuth2 session. Based on the ADK's design, the key is to correctly handle the error when the token refresh fails. When the session idle timeout is reached and the refresh token is invalidated, your attempt to refresh it will result in an exception (e.g., Your tool or
Your client application (the ADK web interface) will receive this event and should be configured to handle it by re-initiating the user-facing authentication process, such as opening the browser popup for the user to log in again. This pattern ensures that the authentication state is correctly managed, allowing the agent to gracefully recover from a total session expiration and prompt the user for re-authentication. A very similar flow is used internally in ADK's unit tests to verify this exact behavior [9]. |
Beta Was this translation helpful? Give feedback.
-
|
@adk-bot how can i get the auth information stored given a context for checking? |
Beta Was this translation helpful? Give feedback.
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: When your API call fails due to an invalid session, catch the error within your tool function. In the error handler, clear the invalid token from Hey @guilmour, That's a great question about handling session expiry. Your intuition to use the The ADK is designed so that you don't call The way to signal this is by using Here is a conceptual workflow for your tool:
This call will stop the current tool execution and instruct the client (the ADK web interface in your case) to restart the authentication flow. Once the user re-authenticates, the ADK will re-run the tool, and this time the credentials in Here’s a simplified pseudo-code example of what your tool might look like: from google.adk.tools import ToolContext
# Assume MY_API_AUTH_CONFIG and TOKEN_CACHE_KEY are defined
def my_tool_function(tool_context: ToolContext, ...):
try:
# Your tool logic that makes an API call.
# This call will fail if the session has expired.
mcp_server_client.make_api_call(...)
except ApiUnauthorizedError: # Replace with the actual exception your client raises for a 401
# The session has expired. Time to re-authenticate.
# 1. Clear the invalid cached tokens.
print("Session expired. Clearing cached tokens.")
tool_context.state.pop(TOKEN_CACHE_KEY, None)
# 2. Request new credentials from the user.
print("Requesting new user authentication.")
tool_context.request_credential(MY_API_AUTH_CONFIG)
# You can optionally return a message to the user.
return {"status": "Your session has expired. Please log in again to continue."}This approach ensures that the re-authentication is handled gracefully within the ADK's stateful execution model. You can find more details about the Hope this helps you solve the roadblock! [1] https://google.github.io/adk-docs/tools-custom/authentication/ |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey, everyone!
I'm working on an agent that connects to an MCP server using an OAuth2 flow (via browser popup).
Currently, the first authentication works fine and I have implemented a
header_providerto handle theaccess_tokenrefresh logic before each MCP call. This works fine for standard token rotation. However, I’ve hit a roadblock regarding the "Session Idle Timeout" on the Authorization Server.But, when the session idle limit is reached, both the
access_tokenand therefresh_tokenare invalidated. At this point, I guess, I would like to make the agent to re-initiate the full authorization flow (opening the browser popup again) usingask_request_credential.So my question is:
How can I "call"
ask_request_credentialagain to make the user authenticate?I am currently testing this using the ADK web interface. Any insights or examples on handling this "hard" session expiration would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions