Skip to content

.NET: Add sample for per-run refreshable MCP authentication headers#6624

Open
rogerbarreto wants to merge 3 commits into
microsoft:mainfrom
rogerbarreto:rogerbarreto/investigate-per-run-mcp-auth-headers
Open

.NET: Add sample for per-run refreshable MCP authentication headers#6624
rogerbarreto wants to merge 3 commits into
microsoft:mainfrom
rogerbarreto:rogerbarreto/investigate-per-run-mcp-auth-headers

Conversation

@rogerbarreto

Copy link
Copy Markdown
Member

Motivation & Context

Issue #1631 want per-run / refreshable MCP auth headers in .NET. Real need: IAM/OBO tokens expire ~1h, plus per-tenant headers. Question: does framework need new API for this? Answer: no. Existing primitives already cover it. This PR add a sample that prove the pattern, no framework change.

Description & Review Guide

  • What are the major changes?
    New sample Agent_MCP_PerRun_AuthHeaders (Foundry via Responses API + public Microsoft Learn MCP). Per-run header mechanism:

    • DelegatingHandler on MCP transport HttpClient stamp Authorization: Bearer <token> per outbound request.
    • AsyncLocal run scope (McpRunScope) carry the current-run token, set right before agent.RunAsync and cleared in finally.
    • Same agent run twice under two contexts (tenant-a, tenant-b), each mint fresh token. Header is per run, not bound at agent or connection build time.
    • Also: register project in agent-framework-dotnet.slnx, add row to ModelContextProtocol folder README.
  • What is the impact of these changes?
    Sample + docs only. No framework API surface, no header_provider analog. No new package (ModelContextProtocol already referenced). Not breaking.

  • What do you want reviewers to focus on?
    Handler security hardening: bearer attached only over HTTPS to the MCP server own origin (no leak on redirect / plaintext / other port), token never logged (label only), cookies off, cert revocation checked. And confirm the "ship a sample, not a new API" call for .NET: Feature Request: Support for Per-Run MCP Authentication Headers #1631 is right.

Related Issue

Fixes #1631

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 (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Adds a Foundry RAPI sample that attaches per-run, refreshable authentication headers to MCP requests using existing primitives: a DelegatingHandler on the MCP transport's HttpClient plus an AsyncLocal run scope. The same agent runs under two contexts, each minting a fresh token, proving the header is per run rather than bound at agent or connection creation time.

The handler attaches the bearer only over HTTPS to the MCP server's own origin, logs the non-secret label only, disables cookies, and checks certificate revocation. The README covers security considerations and production notes.

Fixes microsoft#1631
Copilot AI review requested due to automatic review settings June 19, 2026 11:41
@moonbox3 moonbox3 added documentation Improvements or additions to documentation .NET Issues related to the .NET codebase labels Jun 19, 2026
@rogerbarreto rogerbarreto marked this pull request as ready for review June 19, 2026 11:44

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

Adds a new .NET sample under the MCP samples set demonstrating how to apply per-run / refreshable authentication headers to MCP HTTP requests without introducing new framework APIs, aligning with the guidance for issue #1631 (sample-based pattern rather than new surface area).

Changes:

  • Added new sample Agent_MCP_PerRun_AuthHeaders (README + runnable Program + csproj) demonstrating AsyncLocal-scoped, per-run bearer stamping via a custom DelegatingHandler.
  • Registered the new sample project in agent-framework-dotnet.slnx.
  • Linked the new sample from the MCP samples README.

Reviewed changes

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

Show a summary per file
File Description
dotnet/samples/02-agents/ModelContextProtocol/README.md Adds the new sample to the MCP samples index table.
dotnet/samples/02-agents/ModelContextProtocol/Agent_MCP_PerRun_AuthHeaders/README.md Documents the per-run header pattern, flow, and security considerations.
dotnet/samples/02-agents/ModelContextProtocol/Agent_MCP_PerRun_AuthHeaders/Program.cs Implements the per-run header stamping pattern via AsyncLocal + DelegatingHandler.
dotnet/samples/02-agents/ModelContextProtocol/Agent_MCP_PerRun_AuthHeaders/Agent_MCP_PerRun_AuthHeaders.csproj New sample project referencing required packages and framework projects.
dotnet/agent-framework-dotnet.slnx Adds the new sample project to the solution.

Comment thread dotnet/samples/02-agents/ModelContextProtocol/README.md Outdated

@github-actions github-actions Bot 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.

Automated Code Review

Reviewers: 5 | Confidence: 89% | Result: All clear

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


Automated review by rogerbarreto's agents

…env vars

Disable AllowAutoRedirect on the shared handler so a redirect cannot carry the bearer past the origin check. Save and restore the prior run scope instead of clearing to null so the helper is safe under nesting. Note the Foundry env vars in the samples folder README row and update the sample README security notes.
@rogerbarreto rogerbarreto enabled auto-merge June 19, 2026 14:14
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 .NET Issues related to the .NET codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: Feature Request: Support for Per-Run MCP Authentication Headers

4 participants