Skip to content

Commit 86d542f

Browse files
committed
Skip implicit OAuth redirection for toolsets during agent invocation
1 parent 7623ff1 commit 86d542f

1 file changed

Lines changed: 11 additions & 54 deletions

File tree

src/google/adk/flows/llm_flows/base_llm_flow.py

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
from ...agents.live_request_queue import LiveRequestQueue
3737
from ...agents.readonly_context import ReadonlyContext
3838
from ...agents.run_config import StreamingMode
39-
from ...auth.auth_handler import AuthHandler
40-
from ...auth.auth_tool import AuthConfig
4139
from ...auth.credential_manager import CredentialManager
4240
from ...events.event import Event
4341
from ...models.base_llm_connection import BaseLlmConnection
@@ -51,10 +49,6 @@
5149
from ...tools.tool_context import ToolContext
5250
from ...utils.context_utils import Aclosing
5351
from .audio_cache_manager import AudioCacheManager
54-
from .functions import build_auth_request_event
55-
56-
# Prefix used by toolset auth credential IDs
57-
TOOLSET_AUTH_CREDENTIAL_ID_PREFIX = '_adk_toolset_auth_'
5852

5953
if TYPE_CHECKING:
6054
from ...agents.llm_agent import LlmAgent
@@ -115,24 +109,24 @@ def _finalize_model_response_event(
115109
async def _resolve_toolset_auth(
116110
invocation_context: InvocationContext,
117111
agent: LlmAgent,
118-
) -> AsyncGenerator[Event, None]:
112+
) -> None:
119113
"""Resolves authentication for toolsets before tool listing.
120114
121115
For each toolset with auth configured via get_auth_config():
122116
- If credential is available, populate auth_config.exchanged_auth_credential
123-
- If credential is not available, yield auth request event and interrupt
117+
- If credential is not available, log and continue — auth will be handled
118+
on demand by ToolAuthHandler when a tool is actually invoked.
119+
120+
This avoids triggering OAuth redirects on every agent invocation,
121+
including messages that don't require any tool calls.
124122
125123
Args:
126124
invocation_context: The invocation context.
127125
agent: The LLM agent.
128-
129-
Yields:
130-
Auth request events if any toolset needs authentication.
131126
"""
132127
if not agent.tools:
133128
return
134129

135-
pending_auth_requests: dict[str, AuthConfig] = {}
136130
callback_context = CallbackContext(invocation_context)
137131

138132
for tool_union in agent.tools:
@@ -161,30 +155,11 @@ async def _resolve_toolset_auth(
161155
# Populate in-place for toolset to use in get_tools()
162156
auth_config.exchanged_auth_credential = credential
163157
else:
164-
# Need auth - will interrupt
165-
toolset_id = (
166-
f'{TOOLSET_AUTH_CREDENTIAL_ID_PREFIX}{type(tool_union).__name__}'
158+
logger.debug(
159+
'No credential found for toolset %s; deferring auth to tool'
160+
' invocation.',
161+
type(tool_union).__name__,
167162
)
168-
pending_auth_requests[toolset_id] = auth_config
169-
170-
if not pending_auth_requests:
171-
return
172-
173-
# Build auth requests dict with generated auth requests
174-
auth_requests = {
175-
credential_id: AuthHandler(auth_config).generate_auth_request()
176-
for credential_id, auth_config in pending_auth_requests.items()
177-
}
178-
179-
# Yield event with auth requests using the shared helper
180-
yield build_auth_request_event(
181-
invocation_context,
182-
auth_requests,
183-
author=agent.name,
184-
)
185-
186-
# Interrupt invocation
187-
invocation_context.end_invocation = True
188163

189164

190165
async def _handle_before_model_callback(
@@ -916,14 +891,7 @@ async def _preprocess_async(
916891

917892
# Resolve toolset authentication before tool listing.
918893
# This ensures credentials are ready before get_tools() is called.
919-
async with Aclosing(
920-
self._resolve_toolset_auth(invocation_context, agent)
921-
) as agen:
922-
async for event in agen:
923-
yield event
924-
925-
if invocation_context.end_invocation:
926-
return
894+
await _resolve_toolset_auth(invocation_context, agent)
927895

928896
# Run processors for tools.
929897
await _process_agent_tools(invocation_context, llm_request)
@@ -1273,17 +1241,6 @@ def _finalize_model_response_event(
12731241
llm_request, llm_response, model_response_event
12741242
)
12751243

1276-
async def _resolve_toolset_auth(
1277-
self,
1278-
invocation_context: InvocationContext,
1279-
agent: LlmAgent,
1280-
) -> AsyncGenerator[Event, None]:
1281-
async with Aclosing(
1282-
_resolve_toolset_auth(invocation_context, agent)
1283-
) as agen:
1284-
async for event in agen:
1285-
yield event
1286-
12871244
async def _handle_before_model_callback(
12881245
self,
12891246
invocation_context: InvocationContext,

0 commit comments

Comments
 (0)