-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
Describe the bug
When using Google ADK with FastAPI and bearer token authentication, the authentication context (bearer token) is not automatically accessible within tools. The default ADK architecture doesn't provide a built-in mechanism to pass authentication context from HTTP requests to tool execution.
To Reproduce
Steps to reproduce the behavior:
- Install Google ADK: pip install google-adk
- Create a simple tool that needs bearer token access
- Create a FastAPI server with bearer token authentication
- Send a request with bearer token
- Observe that the tool cannot access the bearer token
Example tool that demonstrates the issue:
from google.adk.tools import FunctionTool, ToolContext
def add_tool(a: float, b: float, tool_context: ToolContext) -> str:
"""
Add two numbers and return result with bearer token info.
This tool should be able to access the bearer token from the HTTP request.
"""
# ❌ PROBLEM: Cannot access bearer token from HTTP request
bearer_token = None
if hasattr(tool_context, 'state') and tool_context.state:
bearer_token = tool_context.state.get("bearer_token")
result = a + b
bearer_info = f" (Bearer: {bearer_token})" if bearer_token else " (No bearer token)"
return f"The result of {a} + {b} = {result}{bearer_info}"
add_tool = FunctionTool(func=add_tool)
Expected behavior
The tool should automatically have access to the bearer token from the HTTP request without manual intervention. The tool should be able to access authentication context like this:
Current workarounds and their limitations
- Manual session state injection: Requires manually injecting bearer token into session state
- Problem: Not secure, token persists across requests
- Problem: Requires complex implementation
- Custom tool wrappers: Wrapping tools to inject authentication context
- Problem: Complex implementation
- Problem: Not standardized
- Problem: Breaks tool signatures
- Request-level context: Using request-scoped variables
- Problem: Not accessible in async tool execution
- Problem: Breaks with different execution contexts
Desktop (please complete the following information):
OS: macOS 14.6.0
Python version: Python 3.11+
ADK version: Latest (pip show google-adk)
Model Information:
Using Google's Gemini models through ADK
Issue affects all model types as it's architectural
Additional context
Impact:
This limitation makes it difficult to build production applications where tools need to make authenticated API calls. The current architecture requires complex workarounds for a common use case.
Proposed solution:
ADK should provide a built-in mechanism to pass request context (like bearer tokens) to tools automatically, similar to how LangGraph handles configurable context.
The Critical Authentication Problem in Google ADK: A Major Development Block
The inability to automatically retrieve authentication context (bearer tokens, headers, etc.) from the FastAPI server to tools in Google ADK constitutes a major obstacle for many production use cases.
A Fundamental Architectural Problem
Currently, when a developer creates an application with Google ADK and FastAPI, they hit a wall: tools executed by the agent have no direct access to the original HTTP request's authentication context. This means that any tool requiring authenticated API calls must implement complex, non-standardized workarounds.
Impact on Development
This limitation blocks many common use cases:
- Tools that need to call external APIs with authentication
- Integrations with third-party services requiring tokens
- Enterprise applications where authentication is mandatory
- Rapid development of functional prototypes
The Official Solution That Doesn't Work
Google ADK proposes tool_context.request_credential() as the official solution, but this approach has two major problems:
- It doesn't work: In practice, this method doesn't correctly retrieve credentials from the FastAPI context
- It breaks the speed pattern: Instead of having a system that "works out of the box," developers must implement complex workarounds, losing the rapid development advantage that Google ADK normally offers