A banking agent needs two things on every MCP call: the right tool and a credential that represents the customer. When the agent connects to the MCP server directly, the agent can attach that credential itself. When the same agent runs as a Microsoft Foundry hosted agent behind a Foundry Toolbox, Azure authentication occupies the Authorization header on the Toolbox hop, and the credential that reaches the MCP server comes from the Toolbox's project connection.
The two paths therefore differ in who holds the customer's credential and where consent happens. This article compares them using one application that supports both: direct MCP for local execution and a Toolbox with OAuth identity passthrough for the hosted agent. The Account and Transaction MCP services stay the same in both cases.
Direct MCP: Microsoft Agent Framework resolves a per-request
Authorizationheader in the agent process.Foundry Toolbox: Foundry stores a per-user OAuth grant on the project connection, and the agent never handles the customer's token.
Consent: the hosted path returns a native
oauth_consent_requestthat the application must treat as a protocol challenge, not a tool failure.Hosted behavior observed with two independently consented customers.
Starting point
The implementation extends Microsoft's public banking-assistant sample. The BFF, the Identity service acting as the OAuth authorization server, the Toolbox wiring and the consent middleware are this repository's additions, not behavior of the original sample.
The examples are taken from the repository at the revision this article describes. The agent environment uses agent-framework-core 1.26.0, agent-framework-foundry 1.16.2, the prerelease agent-framework-foundry-hosting 1.0.0b261112 and mcp 2.3.0. The MCP services use FastMCP 3.4.7. Toolbox and hosting behavior is version-sensitive.
The reader should be familiar with Python, MCP tool clients and the OAuth authorization code flow. The snippets are adapted fragments, not standalone applications.
Two paths to the same MCP services
Aspect | Direct MCP | Foundry Toolbox |
|---|---|---|
Agent Framework type |
|
|
Who calls the MCP server | The agent process | The Toolbox proxy |
Customer credential source | Built by the agent from a signed BFF envelope | Per-user grant stored by Foundry on the project connection |
Credential inside the agent | Yes, for the duration of the call | No |
Where consent happens | The BFF runs the authorization code flow before chat | The platform returns a consent link during the run |
MCP token validation | Shared RS256 verifier | The same verifier |

Figure 1. Coral marks where the customer credential lives on each path. The custom banking header does not cross the Toolbox, and the consent link returns to the BFF as run output. Implementation schematic, not a runtime trace.
The last row is the useful constant. In OAuth mode, both paths deliver an Identity-issued RS256 access token with the exact mcp.access scope, and the shared verifier checks its signature, issuer, audience, customer claims and lifetime. Only the client_id claim differs, because each path uses its own confidential client. The Account and Transaction services still restrict every lookup to the token's customer_id; neither path moves authorization into the agent.
Direct MCP: headers resolved per request
In local execution, the runtime adapter builds an MCPStreamableHTTPTool with a header provider:
def build_mcp_tool(
name: str,
url: str,
identity_secret: str,
mcp_client: Client | None = None,
) -> MCPStreamableHTTPTool:
# The SDK rejects dynamic header providers for caller-supplied MCP sessions.
return MCPStreamableHTTPTool(
name=name,
url=url if mcp_client is None else None,
mcp_client=mcp_client,
header_provider=mcp_header_provider(identity_secret) if mcp_client is None else None,
)The provider ignores model-supplied tool arguments. It reads the request context, verifies the BFF's HMAC-signed envelope and returns an Authorization header. The identity helper supports two modes:
hmac: the agent signs a fresh internal bearer containingsub,customer_idand a 60-second expiry.oauth: the BFF has already completed the authorization code flow with Identity, keeps the grant server-side and places it inside the signed envelope. The agent forwards that access token as the bearer.
A missing, expired or malformed grant raises an error instead of falling back to HMAC. Synthetic test sessions passed through mcp_client receive no provider, because the SDK cannot establish the identity of a session it did not create.
This path keeps the customer's credential inside infrastructure the application owns. It also makes the agent process responsible for carrying that credential correctly on every call.
Why the signed header cannot cross a Toolbox
The first Toolbox attempt kept the HMAC envelope and only changed the route. Azure authentication owns Authorization on the agent-to-Toolbox hop, so the adapter sent the envelope in a separate x-banking-mcp-authorization header. Read-only Account calls through the Toolbox returned the MCP service's missing-identity rejection: the custom header did not reach the service as a usable credential, and the service failed closed.
Microsoft's Toolbox authentication guide explains why. Authentication is a property of the project connection, not code in the agent. For OAuth identity passthrough, FoundryToolbox forwards the current hosted request's x-agent-foundry-call-id, and the Toolbox proxy resolves the caller and injects that caller's token.
The fix was to move the customer credential out of the agent. Each MCP service got an OAuth2 project connection that points at Identity's authorization and token endpoints, requests mcp.access and uses its own confidential client. Identity issues the same kind of token it issues to the BFF; the Toolbox, not the agent, holds it.
Foundry Toolbox: one Toolbox per connection
The agent manifest declares each Toolbox as an azure.ai.toolbox service that references the connection and the tools the agent may use:
account-tools:
host: azure.ai.toolbox
tools:
- type: mcp
server_label: account
server_url: ${ACCOUNT_MCP_URL}
project_connection_id: account-mcp
allowed_tools:
- getAccountsByUserName
- getAccountDetailsThe agent side builds one FoundryToolbox per service. This adapted fragment shows the header provider, which deliberately adds nothing:
def header_provider(runtime_kwargs: dict[str, Any]) -> dict[str, str]:
# The platform's per-user OAuth connection supplies the MCP bearer.
if not get_request_context().call_id:
raise ValueError("Native OAuth Toolbox requires a platform request call ID")
return {}
account_toolbox = FoundryToolbox(
credential,
url=f"{project_endpoint}/toolboxes/account-tools/mcp?api-version=v1",
name="account Toolbox",
allowed_tools=[f"account___{name}" for name in ACCOUNT_TOOLS],
header_provider=header_provider,
)The agent sends no banking grant and no signer. It only refuses to run without a platform call ID, because a missing call ID would leave the Toolbox unable to resolve a user, and the adapter never fabricates one.
The repository started with one Toolbox, banking-tools, that listed both connections. Consent in Foundry is scoped per connection, per user and per project, and the Toolbox resolves its sources on the server. The client-side allowed_tools filter only narrows which tools the agent sees after listing; it does not decide which connections the Toolbox authenticates. Opening the combined Toolbox therefore asked for consent to every connection it listed. Splitting it into account-tools and transaction-tools means an agent asks only for the connections it opens. The Account agent opens one Toolbox; the Transaction agent opens both, because it resolves cards through Account tools such as discoverCardsBySuffix before reading movements.
Two configuration details mattered in practice:
The MCP services redirect
/mcpto/mcp/, and Toolbox discovery does not follow that redirect, so the connection targets must use the trailing-slash URL.The agent
usesboth Toolbox services, and hosted CD supplies their endpoints as deployment inputs.
Why not reference the connection directly?
Agent Framework offers a third route to the same connection. FoundryChatClient.get_mcp_tool(name=..., project_connection_id="account-mcp") declares a hosted MCP tool: the agent only sends the tool definition with its model request, and the Foundry model service calls the MCP server with the connection's credentials. No Toolbox is involved.
This application uses the Toolbox for two reasons:
Per-user identity in a hosted agent. The Toolbox authentication guide directs user delegation for MCP tools through a Toolbox, consumed with
FoundryToolboxin Agent Framework hosted agents, because that wrapper forwards the hosted request's call ID. A hosted agent's model request is made with the agent's own identity, and the guidance documents no equivalent way to carry the end user into a hosted MCP tool call.Tool calls stay in the agent's function loop. A hosted MCP tool runs inside the model service, so its calls bypass Agent Framework function middleware. The consent detection described next, and the agent's handling of ownership denials, both run as middleware around each tool call.
Consent arrives at two different moments
A user who has not consented triggers the challenge either during tool discovery or during a tool call, and the two surface differently. The consent middleware handles both:
Moment | What fails | Handling |
|---|---|---|
Discovery ( | Connecting the Toolbox raises a consent error with a JSON payload |
|
Call ( | The tool call fails with |
|
Deferral is necessary because the workflow host connects every agent's MCP tools before the run starts. Without it, a consent error would fail run preparation instead of producing output. Both middlewares emit Agent Framework's native Content.from_oauth_consent_request, which the Agent Framework Toolbox integration describes as a user-input request carrying a consent_link. The Foundry Responses host then ends the turn as incomplete with the link.
The middleware recognizes a call-time consent only by error code -32006, including when it is wrapped in another exception. Plain text that merely contains a URL is never treated as consent. When several connections need consent during discovery, all challenges are returned together and deduplicated by link. A call-time challenge surfaces one at a time, because the failed call cancels its sibling calls.
The link goes to the client, not to the model, and the paused run is not replayed. The chat opens the link in a popup and, when the popup closes, resends the customer's last message once. That resend is safe here because the paused turn executed no business action.
What the hosted runs showed
Two customers used the deployed banking chat through the BFF against the hosted agent:
Each customer was asked to consent to Account and to Transaction separately. The second customer was prompted even though the first customer's grants already existed.
Consecutive Account and Transaction consents worked in the same hosted session.
For each consent, the platform callback completed Identity's
/oauth/authorizeand/oauth/tokenexchange.After consent, each customer's tool results contained only that customer's name and accounts.
Both customers' requests reached Foundry with the same BFF Azure identity. The per-customer input in those requests is x-ms-user-identity, a stable pseudonymous value the BFF derives with an HMAC from the customer's subject, so the platform never receives the customer's email or ID.
The same runs exposed a diagnostic trap: the agent's telemetry user.id was identical for both customers. It identifies the calling service, not the banking user, so it cannot be used to show which customer a tool call ran for. The tool results themselves are the evidence.
The focused offline regressions cover header resolution in both modes, the call-ID requirement, consent detection at both moments and the host ending the turn with the challenge:
$env:OTEL_SDK_DISABLED = "true"
uv run --directory app\agent --no-sync python -m pytest tests\test_internal_identity.py tests\test_oauth_delegation.py tests\test_runtime_tools.py tests\test_toolbox_caller_context.py tests\test_toolbox_consent_middleware.py tests\test_native_consent_boundary.py tests\test_toolbox_definition.py -qThe selection passed with 67 tests and 12 deprecation warnings from installed packages.
Takeaways
Direct MCP keeps the customer credential in application-owned infrastructure and gives the agent the job of attaching it to every call. A Toolbox moves the credential to the project connection and keeps it out of the agent.
Custom headers are not a credential channel through a Toolbox. Per-user access comes from the connection's authentication type plus the forwarded call ID.
Give each MCP connection its own Toolbox when consent should match what an agent actually uses; client-side tool filters do not scope consent.
Treat
oauth_consent_requestas protocol output with its own UI, never as a failed read and never as text for the model. The MCP service's ownership checks remain the authorization boundary on both paths.
A follow-up article covers how this challenge travels from the hosted agent through the BFF into a custom React chat, including the popup and the single resend.
Implementation and references
Supporting tests: consent middleware tests, runtime adapter tests and OAuth delegation tests.


Deepak TewatiaPosted Nov 23, 2026, 10:24 AM EST
Thanks for sharing