Skip to content

Commit 077c167

Browse files
authored
Skip implicit OAuth redirection for toolsets during agent invocation
1 parent b8e8f6b commit 077c167

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(
@@ -914,14 +889,7 @@ async def _preprocess_async(
914889

915890
# Resolve toolset authentication before tool listing.
916891
# This ensures credentials are ready before get_tools() is called.
917-
async with Aclosing(
918-
self._resolve_toolset_auth(invocation_context, agent)
919-
) as agen:
920-
async for event in agen:
921-
yield event
922-
923-
if invocation_context.end_invocation:
924-
return
892+
await _resolve_toolset_auth(invocation_context, agent)
925893

926894
# Run processors for tools.
927895
await _process_agent_tools(invocation_context, llm_request)
@@ -1271,17 +1239,6 @@ def _finalize_model_response_event(
12711239
llm_request, llm_response, model_response_event
12721240
)
12731241

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

0 commit comments

Comments
 (0)