Skip to content

Python: fix(foundry_hosting): preserve auth credentials across FoundryToolbox reconnections#7202

Open
PratikWayase wants to merge 8 commits into
microsoft:mainfrom
PratikWayase:fix/oauth-consent-request-same-session-7166
Open

Python: fix(foundry_hosting): preserve auth credentials across FoundryToolbox reconnections#7202
PratikWayase wants to merge 8 commits into
microsoft:mainfrom
PratikWayase:fix/oauth-consent-request-same-session-7166

Conversation

@PratikWayase

Copy link
Copy Markdown
Contributor

PR Description

Motivation & Context

When using the ghmcpoauth authentication method with a Foundry Toolbox, accepting an OAuth consent request triggers a close() on the underlying MCP tool. Currently, this permanently nullifies the authenticated _httpx_client. As a result, any follow-up requests in the same session fail with a server_error because the toolbox falls back to an unauthenticated transport. Users currently have to start a completely new conversation to recover from this state. This PR fixes that lifecycle bug so sessions remain valid after OAuth consent flows.

Related Issue

Fixes #7166

Description & Review Guide

  • What are the major changes?

    • Modified FoundryToolbox.__init__ to cache the credential, token_scope, and timeout as instance attributes.
    • Overrode get_mcp_client() to lazily recreate an authenticated httpx.AsyncClient if the previous one was closed.
    • Updated the close() docstring to clarify that credentials are preserved for reconnection.
    • Added regression tests in test_toolbox.py to verify credential preservation, idempotent closes, and proper context manager recreation.
  • What is the impact of these changes?
    Hosted agents using OAuth2-authenticated toolboxes will no longer crash mid-session after a user completes a consent flow. The fix is entirely internal to the FoundryToolbox lifecycle and does not alter the public API or affect non-OAuth authentication methods.

  • What do you want reviewers to focus on?
    Please review the lazy recreation logic in get_mcp_client(). I opted to rebuild the httpx.AsyncClient only when _httpx_client is None rather than trying to resurrect a closed client. I'd appreciate feedback on whether this approach aligns with the framework's expected lifecycle patterns, and whether the new regression tests adequately cover edge cases around repeated close/reconnect cycles.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue
  • This is not a breaking change.

Copilot AI review requested due to automatic review settings July 19, 2026 17:49
@giles17 giles17 added the python Usage: [Issues, PRs], Target: Python label Jul 19, 2026
@github-actions github-actions Bot changed the title fix(foundry_hosting): preserve auth credentials across FoundryToolbox reconnections Python: fix(foundry_hosting): preserve auth credentials across FoundryToolbox reconnections Jul 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the FoundryToolbox lifecycle so OAuth2-authenticated sessions can recover after an internal close() (e.g., during OAuth consent flows) by preserving credentials and recreating an authenticated httpx.AsyncClient on demand.

Changes:

  • Cache credential, token_scope, and timeout on the FoundryToolbox instance for reuse after teardown.
  • Override get_mcp_client() to lazily recreate an authenticated httpx.AsyncClient when the prior client has been cleared.
  • Add regression tests validating credential preservation and reconnection behavior across repeated close() calls.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
python/packages/foundry_hosting/agent_framework_foundry_hosting/_toolbox.py Preserve auth-related state and recreate an authenticated HTTP client on reconnection.
python/packages/foundry_hosting/tests/test_toolbox.py Add regression tests covering close/reconnect cycles and idempotent close behavior.

Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_toolbox.py Outdated
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_toolbox.py Outdated
@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/foundry_hosting/agent_framework_foundry_hosting
   _toolbox.py790100% 
TOTAL45155508988% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
9234 33 💤 0 ❌ 0 🔥 2m 28s ⏱️

@TaoChenOSU

Copy link
Copy Markdown
Contributor

@PratikWayase Thank you for your contributions! I have made some edits to the PR so that we can merge it.

@TaoChenOSU
TaoChenOSU temporarily deployed to github-app-auth July 22, 2026 18:17 — with GitHub Actions Inactive
@TaoChenOSU
TaoChenOSU temporarily deployed to github-app-auth July 22, 2026 18:41 — with GitHub Actions Inactive
@TaoChenOSU
TaoChenOSU temporarily deployed to github-app-auth July 22, 2026 21:45 — with GitHub Actions Inactive
@TaoChenOSU
TaoChenOSU temporarily deployed to github-app-auth July 22, 2026 22:33 — with GitHub Actions Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: oauth_consent_request produces error within same session

5 participants