Skip to content

fix(mcp): preserve the OAuth client and resource through exchange - #2665

Merged
rapids-bot[bot] merged 1 commit into
NVIDIA:developfrom
playb0t:fix/oauth-callback-resource
Dec 1, 2026
Merged

rapids-bot[bot] merged 1 commit into
NVIDIA:developfrom
playb0t:fix/oauth-callback-resource

Conversation

@playb0t

@playb0t playb0t commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

MCPAuthenticationFlowHandler never stored the OAuth client in its per-flow state. The redirect route inherited from the console handler then called fetch_token on None, and the MCP browser sign-in failed at the callback. #2165 moved the client into the per-flow state for the console handler. This applies the same to the MCP override.

When the authorization request carries a resource parameter, the code exchange and the token refresh now send the same value. A flow without a resource sends the same parameters as before. The client secret handling from #2626 is unchanged.

On develop three of the five targeted callback and refresh tests fail. With this change all five pass. ci/scripts/run_tests.py passes the same tests in the MCP project (2) and the core project (3). The tests use a stub token endpoint and an in-process ASGI transport. A live authorization server was not used.

Closes #2662

By Submitting this PR I confirm:

  • I am familiar with the Contributing Guidelines.
  • We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license.
    • Any contribution which contains commits that are not Signed-Off will not be accepted.
  • When the PR is ready for review, new or existing tests cover these changes.
  • When the PR is ready for review, the documentation is up to date with these changes.

Summary by CodeRabbit

  • Bug Fixes
    • OAuth authorization-code exchanges and token refreshes now include a configured resource value when it is a non-empty string. This keeps resource-specific settings consistent across the authentication flow while leaving requests unchanged when no resource is configured.

@playb0t
playb0t requested a review from a team as a code owner November 26, 2026 09:26
@copy-pr-bot

copy-pr-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions

Copy link
Copy Markdown

Warning

⚠️ Deprecation Notice: NVIDIA NeMo Agent Toolkit has been deprecated as of October 2026 and will reach end of life (EOL) by end of year 2026. Depending on your needs, migrate to: NeMo Relay for observability, middleware, and governance. NeMo Helix for a unified CLI with evaluation, optimization, and hardening capabilities. NeMo Fabric for a consistent interface to run agent harnesses and custom agents.
We recommend completing your migration before the EOL date.

Pull request policy: Only bug fixes may be opened as pull requests.

@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The console and MCP OAuth authorization-code flows now retain an optional resource value and pass it to token exchange when present. The MCP flow also stores its OAuth client in flow state. Token refresh includes the resource when it is a nonempty string.

Changes

OAuth resource forwarding

Layer / File(s) Summary
Authorization-code exchange
packages/nvidia_nat_core/src/nat/front_ends/console/authentication_flow_handler.py, packages/nvidia_nat_mcp/src/nat/plugins/mcp/auth/auth_flow_handler.py, packages/nvidia_nat_mcp/tests/client/test_mcp_auth_flow_handler.py
Flow state stores the OAuth client and a nonempty resource value. The redirect handler passes the resource to token exchange when present. The MCP callback test covers cases with and without a resource.
Refresh request
packages/nvidia_nat_core/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider.py, packages/nvidia_nat_core/tests/nat/authentication/test_oauth_exchanger.py
Refresh requests always include client_id and include resource only when it is a nonempty string. The test covers requests with and without a resource.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant Callback
  participant MCPAuthenticationFlowHandler
  participant flow_state.client
  participant TokenEndpoint
  Callback->>MCPAuthenticationFlowHandler: authorization code and OAuth state
  MCPAuthenticationFlowHandler->>flow_state.client: fetch_token with code, state, and optional resource
  flow_state.client->>TokenEndpoint: token request
  TokenEndpoint-->>flow_state.client: token response
Loading

Possibly related PRs

  • NVIDIA/NeMo-Agent-Toolkit#2626: Both changes update refresh-request construction in _attempt_token_refresh; this change adds optional resource forwarding.
Merge Risk: Minimal · up to 21c3f

This change lets the MCP OAuth callback complete the code exchange and carries the authorization request's resource value through code exchange and token refresh. Flows without a resource keep their previous parameters. No concrete merge-blocking risk was found. The only failing check is the label checker, which needs category and breaking labels applied.

Security architecture risk: Moderate · up to 21c3f

The MCP sign-in fix allows the browser callback to contact a token endpoint selected through discovery. If that discovery is compromised, the restored exchange can send an authorization code and, for a confidential client, its client secret to an unintended endpoint. The callback can also start overlapping exchanges for the same pending sign-in. These risks are limited to affected OAuth flows; the change does not add a listener or grant broader privileges.

Retained concerns

  • Medium · security · inferred: The restored built-in MCP callback can now send a code and configured client authentication to a discovery-selected token endpoint. The inspected path accepts an HTTP(S) endpoint from metadata without binding it to a trusted issuer or to the provisioned credential. If an untrusted party controls effective discovery metadata and a user reaches the callback, the receiving endpoint can obtain the code and, when secret-based authentication is configured, the client secret. The built-in MCP callback could not make this request at the merge base.
  • Low · security · inferred: MCP now inherits an active token exchange without an atomic claim of the pending flow. Callbacks that present the same valid state before cleanup can initiate competing exchanges; a callback already in flight can finish after its waiting future is cancelled. A browser-launch exception also bypasses flow cleanup and can leave exchange-capable state registered. These lifecycle gaps predate the PR, but at the merge base they could not cause outbound exchanges on this MCP path. Consequences are bounded to the affected flow and its token endpoint; duplicate token issuance is not established.
Security review details

Security Blast Radius

  • inferred — The added exposure is scoped to MCP provider instances using the restored built-in browser callback and the credentials loaded into each pending flow. The largest supported credential impact is on confidential clients with a provisioned secret; any further impact depends on that credential's actual privileges and reuse. The changed path does not establish cross-tenant access or additional tool permissions.

Security Findings and Attack Paths

  • inferred — MCP discovery can derive its issuer from protected-resource metadata and take the token URL from the resulting authorization-server metadata. If effective metadata is untrusted, the restored callback can direct credential-bearing exchange to an unintended destination. The browser callback itself does not select the token host or resource.

Trust Boundaries and Controls

  • observed — The callback rejects unknown state and uses the matching flow's client, resource, token URL, and PKCE verifier when enabled. These controls associate a browser callback with a pending flow; they do not authorize the discovery-selected destination to receive client credentials.

Resilience and Maintainability Implications

  • observed — Once the MCP flow enters the wait region, completion, failure, timeout, or cancellation leads to unregistering state and requesting listener shutdown. Unregistering blocks subsequent lookups but does not revoke state already obtained by a callback awaiting the token endpoint.

Hardening Proposals

  • proposed — Require a trusted issuer-to-token-endpoint binding before sending manually provisioned credentials, while allowing explicitly trusted cross-origin delegation where required.
  • proposed — Claim each pending flow before starting the exchange, include browser launch in the cleanup region, and make late callback completion safe after cancellation.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix(mcp): preserve the OAuth client and resource through exchange" matches the main change. The PR stores the OAuth client in MCP flow state so the redirect route can redeem the authorizati…
Linked Issues check Passed Linked issue #2662 has two coding requirements. (1) The MCP callback must complete the code exchange. The diff at auth_flow_handler.py sets flow_state.client = client in `MCPAuthenticationFlowHandle…
Out of Scope Changes check Passed Every changed file serves #2662. The MCP handler change sets the client and resource in flow state. The console handler change adds the resource field to _FlowState and forwards it to the token ex…
Full details: Title check

Explanation

The title "fix(mcp): preserve the OAuth client and resource through exchange" matches the main change. The PR stores the OAuth client in MCP flow state so the redirect route can redeem the authorization code, and it carries the authorization request's resource through code exchange and token refresh. The title uses a conventional prefix, is 65 characters, and stays under the roughly 72-character limit. It uses an imperative verb (preserve). The title is specific and descriptive.

Full details: Linked Issues check

Explanation

Linked issue #2662 has two coding requirements. (1) The MCP callback must complete the code exchange. The diff at auth_flow_handler.py sets flow_state.client = client in MCPAuthenticationFlowHandler._handle_oauth2_auth_code_flow. The inherited console redirect handler then calls flow_state.client.fetch_token(...) on a non-None client. (2) The authorization request resource must reach both the code exchange and the token refresh. The MCP handler copies a non-empty string authorization_kwargs["resource"] into flow_state.resource. The console redirect handler (authentication_flow_handler.py) adds resource to the fetch_token kwargs when set. OAuth2AuthCodeFlowProvider._attempt_token_refresh (oauth2_auth_code_flow_provider.py) adds the same resource to refresh_token kwargs under the same condition. client_id stays in the refresh call. Flows without resource pass the same parameters as before. The added tests in test_mcp_auth_flow_handler.py (callback, with and without resource) and test_oauth_exchanger.py (refresh, with and without resource) cover both requirements. I did not run the tests. The assessment rests on the reviewed diff.

Full details: Out of Scope Changes check

Explanation

Every changed file serves #2662. The MCP handler change sets the client and resource in flow state. The console handler change adds the resource field to _FlowState and forwards it to the token exchange. The provider change forwards it to refresh. The test changes cover the callback and refresh paths. The state and PKCE code_verifier arguments of the token exchange are unchanged. No unrelated files are modified in the whole-PR diff against the merge base.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9934005 and 21c3fe6.

Files selected for processing: 5

  • packages/nvidia_nat_core/src/nat/authentication/oauth2/oauth2_auth_code_flow_provider.py
  • packages/nvidia_nat_core/src/nat/front_ends/console/authentication_flow_handler.py
  • packages/nvidia_nat_core/tests/nat/authentication/test_oauth_exchanger.py
  • packages/nvidia_nat_mcp/src/nat/plugins/mcp/auth/auth_flow_handler.py
  • packages/nvidia_nat_mcp/tests/client/test_mcp_auth_flow_handler.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

@willkill07 willkill07 added bug Something isn't working non-breaking Non-breaking change labels Nov 27, 2026
@willkill07

Copy link
Copy Markdown
Member

/ok to test 21c3fe6

@willkill07

Copy link
Copy Markdown
Member

/merge

1 similar comment
@willkill07

Copy link
Copy Markdown
Member

/merge

@rapids-bot
rapids-bot Bot merged commit 16e99ac into NVIDIA:develop Dec 1, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP browser sign-in fails at the redirect callback: the flow state has no OAuth client

2 participants