Skip to content

Python: Show more authentication methods in Foundry Toolbox MCP#5719

Open
TaoChenOSU wants to merge 9 commits into
mainfrom
feature/add-more-foundry-toolbox-mcp-auth-methods-in-sample
Open

Python: Show more authentication methods in Foundry Toolbox MCP#5719
TaoChenOSU wants to merge 9 commits into
mainfrom
feature/add-more-foundry-toolbox-mcp-auth-methods-in-sample

Conversation

@TaoChenOSU
Copy link
Copy Markdown
Contributor

@TaoChenOSU TaoChenOSU commented May 8, 2026

Motivation and Context

Foundry toolbox supports multiple authentication scenarios for MCPs: https://github.com/microsoft-foundry/foundry-samples/tree/main/samples/python/toolbox/azd#supported-scenarios. Update our sample to show and verify that we support all.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

@TaoChenOSU TaoChenOSU self-assigned this May 8, 2026
Copilot AI review requested due to automatic review settings May 8, 2026 18:45
@TaoChenOSU TaoChenOSU added the samples Issue relates to the samples label May 8, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates the Foundry-hosted agent samples to demonstrate more Foundry Toolbox/MCP authentication scenarios and to use the newer Toolbox endpoint + request auth plumbing.

Changes:

  • Switched toolbox endpoint construction from /toolsets/... to /toolboxes/... and moved request auth to an httpx.Auth implementation.
  • Updated samples to instantiate MCPStreamableHTTPTool with an httpx.AsyncClient and expose more managed tools (e.g., web_search).
  • Expanded the toolbox sample manifest/README to document and configure multiple MCP authentication methods (no-auth, PAT, OAuth2, agent identity, user Entra token).

Reviewed changes

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

Show a summary per file
File Description
python/samples/04-hosting/foundry-hosted-agents/responses/06_files/main.py Uses httpx.Auth + AsyncClient for toolbox calls and updates toolbox endpoint path.
python/samples/04-hosting/foundry-hosted-agents/responses/06_files/README.md Documents the additional toolbox-provided tools used by the sample.
python/samples/04-hosting/foundry-hosted-agents/responses/04_foundry_toolbox/main.py Aligns toolbox connection/auth approach with httpx and new endpoint path.
python/samples/04-hosting/foundry-hosted-agents/responses/04_foundry_toolbox/agent.manifest.yaml Adds connection definitions + parameters to demonstrate multiple MCP auth methods.
python/samples/04-hosting/foundry-hosted-agents/responses/04_foundry_toolbox/README.md Adds an “Authentication Methods” section describing the supported scenarios.

Comment thread python/samples/04-hosting/foundry-hosted-agents/responses/06_files/main.py Outdated
Comment thread python/samples/04-hosting/foundry-hosted-agents/responses/06_files/main.py Outdated
@github-actions github-actions Bot changed the title Show more authentication methods in Foundry Toolbox MCP Python: Show more authentication methods in Foundry Toolbox MCP May 8, 2026
@moonbox3 moonbox3 added the documentation Improvements or additions to documentation label May 8, 2026
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 83% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by TaoChenOSU's agents

@moonbox3
Copy link
Copy Markdown
Contributor

moonbox3 commented May 12, 2026

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/foundry_hosting/agent_framework_foundry_hosting
   _responses.py67710584%183–186, 251, 328–329, 339, 376, 431, 445, 497, 500–504, 523, 526, 532, 534, 555–557, 586–588, 593, 595, 602, 604–605, 607, 609, 615–618, 620–622, 626, 629, 634–640, 643–644, 646–647, 655–660, 939, 952, 1418–1420, 1422, 1469–1470, 1472–1473, 1475–1476, 1478–1479, 1484, 1493, 1496–1498, 1500, 1518, 1521, 1563–1564, 1566, 1570–1574, 1576, 1583–1584, 1586–1587, 1593, 1595–1599, 1606, 1612, 1634, 1640
TOTAL34524393288% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
6934 30 💤 0 ❌ 0 🔥 1m 43s ⏱️

Comment thread python/samples/04-hosting/foundry-hosted-agents/responses/06_files/main.py Outdated
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
Comment thread python/packages/foundry_hosting/agent_framework_foundry_hosting/_responses.py Outdated
@eavanvalkenburg
Copy link
Copy Markdown
Member

Pushed 149bb64 while Tao is OOF. Verified both samples run end-to-end against a real Foundry toolbox (research_toolbox) — MCPStreamableHTTPTool connects, the agent lists the toolbox tools (api_specs___fetch_azure_rest_api_docs etc.), and the request completes cleanly.

What changed:

@eavanvalkenburg

  • Dropped the branch-pinned git+https://...@feature/... entries from 04_foundry_toolbox/requirements.txt; restored the simple comment + mcp runtime dep.
  • Renamed _responses.is_consent_errorconsent_url_from_error. The helper returns str | None (the consent URL), so the new name matches behavior. Test class renamed to TestConsentUrlFromError.
  • Tightened _handle_inner_agent's lazy-entry catch from except Exception to except AgentFrameworkException — that's the exception class the MCP layer actually uses to wrap consent errors (MCPStreamableHTTPTool.__aenter__ToolExecutionException(inner_exception=mcp_error)). Network/cancellation errors now propagate normally instead of being briefly caught and re-raised. Updated _make_consent_error test helper to use ToolExecutionException so it matches the real-world wrapping.
  • Re: "considering this sample uses Foundry directly, wouldn't it make sense to use the toolboxes directly?" — kept as-is for this PR. Switching to a Foundry-native toolbox reference (hosted-MCP tool style) would change the sample's structure significantly and is a separate design decision; happy to follow up if you want me to investigate FoundryChatClient's hosted-MCP-tool support as a follow-up PR.

@copilot reviewer (PR feedback)

  • httpx.AsyncClient(...) is now used inside async with so the connection pool is closed deterministically in both samples.
  • TOOLBOX_NAME consistency: previously the tool's name= silently fell back to "toolbox" while resolve_toolbox_endpoint() required TOOLBOX_NAME and would KeyError. Now both samples resolve the endpoint once and derive the tool name from the resolved URL when TOOLBOX_NAME isn't explicitly set, so the local tool name always matches the upstream toolbox identity regardless of whether the user set TOOLBOX_NAME or FOUNDRY_TOOLBOX_ENDPOINT.
  • Fixed the /toolsets//toolboxes/ URL typo in both READMEs. Verified empirically: the toolbox MCP gateway lives at /toolboxes/{name}/mcp?api-version=v1 with Foundry-Features: Toolboxes=V1Preview; /toolsets/ returns 403 with preview_feature_required: Toolsets=V1Preview (a different opt-in feature).
  • Clarified the github_pat description in agent.manifest.yaml to say it's only required when using the PAT-based connection (github-mcp-pat-conn); users selecting github-mcp-oauth-conn can leave it empty.

Validation: 119 foundry_hosting tests pass; pyright + mypy clean.

TaoChenOSU and others added 8 commits May 19, 2026 16:34
Reviewed feedback addressed:

- Drop the branch-pinned `git+https://...@feature/...` entries from
  `04_foundry_toolbox/requirements.txt`; restore the simple comment + `mcp`
  runtime dep. The git pins were only useful while iterating on the PR and
  shouldn't ship. (eavanvalkenburg)

- Fix the `/toolsets/` typo in both `04_foundry_toolbox/README.md` and
  `06_files/README.md`. Verified empirically against the
  research_toolbox in the test workspace: the toolbox MCP gateway lives at
  `/toolboxes/{name}/mcp?api-version=v1` and requires the
  `Foundry-Features: Toolboxes=V1Preview` header. `/toolsets/{name}/mcp`
  returns 403 with `preview_feature_required: Toolsets=V1Preview` (a
  different opt-in feature).

- Wrap `httpx.AsyncClient(...)` in `async with ... as http_client:` in both
  samples so the connection pool is cleaned up. (Copilot reviewer)

- Make the `TOOLBOX_NAME` env var consistent in both samples. Previously the
  tool name silently fell back to `"toolbox"` when `TOOLBOX_NAME` was unset,
  but `resolve_toolbox_endpoint()` still required `TOOLBOX_NAME` and would
  raise `KeyError`. The samples now resolve the endpoint once and derive the
  tool name from the resolved URL when `TOOLBOX_NAME` isn't set, so the
  local tool name always matches the upstream toolbox identity regardless
  of which env var the user set. (Copilot reviewer)

- Rename `_responses.is_consent_error` to `consent_url_from_error`: the
  helper returns `str | None` (the consent URL), not a bool, so the new
  name matches behavior. Update the test class accordingly. (eavanvalkenburg)

- Tighten `_handle_inner_agent`'s lazy-entry catch from `Exception` to
  `AgentFrameworkException`, the type the MCP layer actually wraps consent
  errors in via `MCPStreamableHTTPTool.__aenter__` →
  `ToolExecutionException(inner_exception=mcp_error)`. Network failures,
  cancellations, and other non-framework exceptions now propagate normally
  instead of being briefly caught and re-raised. The test helper
  `_make_consent_error` is updated to use `ToolExecutionException` so it
  matches the real-world wrapping. (eavanvalkenburg)

- Clarify the `github_pat` description in `agent.manifest.yaml` to note
  it's only needed when the PAT-based connection (`github-mcp-pat-conn`)
  is chosen; users selecting the OAuth2 connection (`github-mcp-oauth-conn`)
  can leave it empty. (Copilot reviewer)

Validation: ran both samples end-to-end against a real Foundry toolbox
(`research_toolbox`) -- the samples connect successfully and the agent
lists the toolbox's MCP tools (`api_specs___fetch_azure_rest_api_docs`,
etc.). `uv run poe test -P foundry_hosting` passes (119 tests), pyright +
mypy clean.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@eavanvalkenburg eavanvalkenburg force-pushed the feature/add-more-foundry-toolbox-mcp-auth-methods-in-sample branch from 149bb64 to 7f51de9 Compare May 19, 2026 14:35
@eavanvalkenburg eavanvalkenburg marked this pull request as ready for review May 19, 2026 14:36
Copilot AI requested a review from eavanvalkenburg May 19, 2026 14:38
The previous URL pointed to an old location of the toolbox supported-scenarios
doc; the doc moved to /samples/python/hosted-agents/SUPPORTED_TOOLBOX_SCENARIOS.md
and the old /samples/python/toolbox/azd path now 404s.

Caught by the markdown-link-check CI step.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 84% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Design Approach


Automated review by eavanvalkenburg's agents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation python samples Issue relates to the samples

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

5 participants