Skip to content

Commit 7b7f94a

Browse files
Fix OAuth consent event forwarding in as_tool()
Fixes microsoft#4499 Adds exception-based OAuth consent event forwarding to enable proper OAuth flows when using agents as tools. Changes: - Added OAuthConsentRequiredException to exceptions.py - Modified as_tool() to detect oauth_consent_request events in both streaming and non-streaming modes - Raises OAuthConsentRequiredException with consent_url when OAuth consent is required - Parent agents can now catch this exception and forward the consent request to the user This implements the exception-based approach (Option A) discussed in issue microsoft#4499, allowing proper OAuth flow handling when wrapping agents as tools. The fix ensures that OAuth consent requests are not silently swallowed when using as_tool(), enabling proper multi-agent OAuth workflows.
1 parent ded32f3 commit 7b7f94a

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

‎python/packages/core/agent_framework/_agents.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
map_chat_to_agent_update,
5252
normalize_messages,
5353
)
54-
from .exceptions import AgentInvalidResponseException
54+
from .exceptions import AgentInvalidResponseException, OAuthConsentRequiredException
5555
from .observability import AgentTelemetryLayer
5656

5757
if sys.version_info >= (3, 13):
@@ -532,11 +532,25 @@ async def agent_wrapper(**kwargs: Any) -> str:
532532

533533
if stream_callback is None:
534534
# Use non-streaming mode
535-
return (await self.run(input_text, stream=False, session=parent_session, **forwarded_kwargs)).text
535+
response = await self.run(input_text, stream=False, session=parent_session, **forwarded_kwargs)
536+
# Check for OAuth consent request in response
537+
for content in response.contents:
538+
if content.type == 'oauth_consent_request':
539+
consent_url = content.get('consent_link') or content.get('url') or ''
540+
from .exceptions import OAuthConsentRequiredException
541+
raise OAuthConsentRequiredException(consent_url)
542+
return response.text
536543

537544
# Use streaming mode - accumulate updates and create final response
538545
response_updates: list[AgentResponseUpdate] = []
539546
async for update in self.run(input_text, stream=True, session=parent_session, **forwarded_kwargs):
547+
# Check for OAuth consent request in update
548+
for content in update.contents:
549+
if content.type == 'oauth_consent_request':
550+
consent_url = content.get('consent_link') or content.get('url') or ''
551+
from .exceptions import OAuthConsentRequiredException
552+
raise OAuthConsentRequiredException(consent_url)
553+
540554
response_updates.append(update)
541555
if is_async_callback:
542556
await stream_callback(update) # type: ignore[misc]

‎python/packages/core/agent_framework/exceptions.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,46 @@ class SettingNotFoundError(AgentFrameworkException):
204204

205205
# endregion
206206

207+
208+
209+
class OAuthConsentRequiredException(AgentException):
210+
"""Raised when a sub-agent tool requires OAuth consent.
211+
212+
This exception is raised by as_tool() when a wrapped agent returns
213+
an oauth_consent_request event. The parent agent should catch this
214+
exception and forward the consent request to the user.
215+
216+
Attributes:
217+
consent_url: The OAuth consent URL that the user must visit.
218+
"""
219+
220+
def __init__(self, consent_url: str):
221+
self.consent_url = consent_url
222+
super().__init__(
223+
f'OAuth consent required. Please visit: {consent_url}',
224+
log_level=logging.INFO,
225+
)
226+
227+
228+
229+
class OAuthConsentRequiredException(AgentException):
230+
"""Raised when a sub-agent tool requires OAuth consent.
231+
232+
This exception is raised by as_tool() when a wrapped agent returns
233+
an oauth_consent_request event. The parent agent should catch this
234+
exception and forward the consent request to the user.
235+
236+
Attributes:
237+
consent_url: The OAuth consent URL that the user must visit.
238+
"""
239+
240+
def __init__(self, consent_url: str):
241+
self.consent_url = consent_url
242+
super().__init__(
243+
f'OAuth consent required. Please visit: {consent_url}',
244+
log_level=logging.INFO,
245+
)
246+
207247
# region Workflow Exceptions
208248

209249

0 commit comments

Comments
 (0)