Skip to content

fix(mcp): scope stored OAuth grants to the client registration - #2666

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

rapids-bot[bot] merged 1 commit into
NVIDIA:developfrom
playb0t:fix/oauth-grant-client-binding

Conversation

@playb0t

@playb0t playb0t commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

Stored MCP OAuth grants were keyed by user id alone, so a grant saved by one client registration was returned to a later registration that shared the object store. Grants are now stored under a namespace derived from the registration: the MCP server, the authorization server issuer and endpoints, the client id, the resource and the requested scopes.

Reopening the same registration reuses its grant, and rotating the client secret keeps it. A changed client id, resource, issuer or server starts a new authorization. MCP workers write tokens/v2/{namespace_hash}/{user_hash}; callers that construct ObjectStoreTokenStorage without a namespace keep reading and writing tokens/{user_hash}. Existing records are left untouched, and the first use after the upgrade authorizes again. The token storage guide describes mixed-version workers and the retirement of old keys.

ci/scripts/run_tests.py passes 68 selected tests in the MCP project and 29 in the core project. The tests use synthetic credentials and in-memory object storage; a deployed provider was not used.

This touches auth_provider.py, as does the issuer check proposed for #2664. Whichever lands second needs a rebase, and I will do it.

Closes #2663

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

  • New Features
    • OAuth grants are now kept separate across MCP server and client identities, reducing the chance of reusing credentials for a different connection.
    • Rotating a client secret or reordering scopes preserves an existing grant when the other identity details remain unchanged. Changes to the server, issuer, resource, or scopes require a new grant.
    • Existing stored tokens remain readable during upgrades.
  • Documentation
    • Updated guidance explains authorization behavior, token migration, and cleanup during upgrades.

@playb0t
playb0t requested a review from a team as a code owner November 26, 2026 09:30
@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

MCP OAuth token storage now supports keys scoped to server and client identity fields. OAuth discovery records issuer and resource metadata. The provider prepares authentication with the current endpoint and registration state, and tests cover namespace isolation, registration changes, and grant reuse.

Changes

MCP OAuth grant storage and authentication

Layer / File(s) Summary
Namespaced token keys and upgrade behavior
packages/nvidia_nat_core/src/nat/authentication/token_storage.py, packages/nvidia_nat_mcp/tests/client/test_mcp_token_storage.py, docs/source/components/auth/mcp-auth/mcp-auth-token-storage.md
ObjectStoreTokenStorage can hash namespace components into a tokens/v2 key prefix. Without a namespace, it retains the legacy key format. Tests and documentation describe namespace isolation and legacy-record handling.
OAuth discovery metadata
packages/nvidia_nat_mcp/src/nat/plugins/mcp/auth/auth_provider.py, packages/nvidia_nat_mcp/tests/client/test_mcp_auth_provider.py
Discovered endpoints now include issuer and resource metadata. Tests check issuer discovery and fallback behavior.
Prepared authentication and scoped grants
packages/nvidia_nat_mcp/src/nat/plugins/mcp/auth/auth_provider.py, packages/nvidia_nat_mcp/tests/client/test_mcp_auth_provider.py, packages/nvidia_nat_mcp/tests/client/test_mcp_token_storage.py
Authentication prepares an auth-code provider under the discovery and registration lock. A redirect-URI registration rejection triggers one forced refresh and retry. Token storage uses server and OAuth identity fields as its namespace. Tests cover registration rotation and grant reuse.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Low

Sequence Diagram(s)

sequenceDiagram
  participant MCPOAuth2Provider
  participant OAuth2AuthCodeFlowProvider
  participant ObjectStoreTokenStorage
  participant ObjectStore
  MCPOAuth2Provider->>OAuth2AuthCodeFlowProvider: prepare provider and authenticate
  OAuth2AuthCodeFlowProvider->>ObjectStoreTokenStorage: retrieve grant for user
  ObjectStoreTokenStorage->>ObjectStore: read namespaced token key
  ObjectStore-->>ObjectStoreTokenStorage: stored token
  ObjectStoreTokenStorage-->>OAuth2AuthCodeFlowProvider: grant
Loading
Merge Risk: Minimal · up to e39ec

Stored MCP OAuth grants are now scoped to the registration that obtained them. Changing the client ID, issuer, resource, scopes, or server requires a new authorization, while rotating the client secret keeps the existing grant. Users authorize once more after upgrading because legacy records are left untouched, and the documentation covers rolling upgrades. No blocking issue was found.

Security architecture risk: Moderate · up to e39ec

The change improves grant isolation: an upgraded worker no longer reuses a grant from a different client registration. The remaining risks are that replaced registrations can leave additional credential records in storage, and a late retry can displace a healthy replacement registration, making affected users authorize again.

Retained concerns

  • Medium · security · inferred: After a registration identity change and successful reauthorization, the new write no longer replaces the previous credential object as it did under the user-only key. Deletion through the current storage instance reaches only its own namespace. The guide covers retiring legacy keys, not superseded v2 namespaces. Without separately configured retention or cleanup, repeated registration changes can leave additional bearer and refresh-token records accessible to principals with store read access. Whether those tokens remain usable depends on the authorization server.
  • Medium · reliability · inferred: A late redirect rejection can force a second dynamic registration even after another call has published a healthy replacement. The lock serializes replacement but the retry does not check whether the failed provider is still current. Unconditional invalidation predates the PR; the new effect is that when the second registration returns a different client ID, later lookups miss grants already saved under the healthy replacement. This can force reauthorization for users sharing that provider instance, without allowing one registration to read another's grants.
Security review details

Security Blast Radius

  • inferred — A stale retry affects users sharing the mutable registration on that provider instance. The separate retention risk extends to retired credential records in a shared durable store, but exploiting their contents would require store read access and credentials still accepted by the relevant authorization server or resource.

Trust Boundaries and Controls

  • observed — The hashed prefix separates lookups within the application; it is not an object-store permissions boundary. The backing store receives serialized credential material and remains responsible for protecting it.

Hardening Proposals

  • proposed — Under the registration lock, replace the registration only if the provider that failed is still current. Otherwise retry once with the replacement already published by the other call.
  • proposed — Assign an owner and a retention or revocation policy for retired v2 namespaces as well as legacy keys. Make cleanup account for workers and in-flight calls still able to write those keys, and document when rollback to the legacy layout ceases to preserve existing sessions.
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): scope stored OAuth grants to the client registration" is 62 characters, within the roughly 72-character limit, and uses an imperative verb ("scope"). It matches the main change: t…
Linked Issues check Passed [#2663] requires that a stored grant be used only by the registration that obtained it, that a changed registration authorize again, and that secret rotation keep the grant. The issue also asks for te…
Out of Scope Changes check Passed The auth_provider.py diff from the merge base to the reviewed head shows changes that support the #2663 fix. These include the namespace construction, the ObjectStore token storage wiring, the `is…
Full details: Title check

Explanation

The title "fix(mcp): scope stored OAuth grants to the client registration" is 62 characters, within the roughly 72-character limit, and uses an imperative verb ("scope"). It matches the main change: the token storage namespacing in ObjectStoreTokenStorage and the auth provider now bind stored OAuth grants to the client registration (client ID, issuer, resource, scopes, server URL), as described in PR #2666 and linked issue #2663. Conventional-commit prefix is present, and the description is concise and descriptive.

Full details: Linked Issues check

Explanation

[#2663] requires that a stored grant be used only by the registration that obtained it, that a changed registration authorize again, and that secret rotation keep the grant. The issue also asks for tests covering restart, changed client ID, secret rotation, and changed resource, issuer, or server.

At the reviewed head, MCPOAuth2Provider._get_auth_code_provider builds an ObjectStoreTokenStorage with a namespace tuple of server URL, issuer, authorization URL, token URL, client ID, resource, and sorted unique scopes. The client secret is not in the namespace, so rotation keeps the grant. Changing any other component yields a different key. ObjectStoreTokenStorage in token_storage.py hashes the namespace into tokens/v2/{namespace_hash}/{user_hash}. The no-namespace path still uses tokens/{user_hash}, so existing records remain readable through the current interface, as the issue requests. The token-storage tests cover namespace isolation, restart reuse, secret rotation, and changes to issuer, resource, scopes, and server URL. I did not run the test suite. The test coverage claim rests on the PR description and the change summary, and the test files were not inspected in this assessment.

The requirement to re-authorize after a registration change is met by the namespace design. Existing legacy grants are not read by MCP workers, so the first use after upgrade authorizes again. The PR description states this as intended behavior, and the issue does not require legacy grants to be reused by MCP workers.

Full details: Out of Scope Changes check

Explanation

The auth_provider.py diff from the merge base to the reviewed head shows changes that support the #2663 fix. These include the namespace construction, the ObjectStore token storage wiring, the issuer and resource fields on OAuth2Endpoints, the issuer copy during discovery, and the endpoint-model comparison for change detection. Those changes supply the identity fields the namespace needs.

The redirect-URI retry path is not new. _is_redirect_uri_registration_error and the single forced re-registration already exist at the merge base. The PR refactors that path into _prepare_auth_code_provider without changing its purpose. The discovery lock now snapshots the provider before user interaction. This keeps the token namespace aligned with the registration that authenticates. The JSON-before-validation parse supports reading the issuer field.

The documentation update in mcp-auth-token-storage.md describes the key format, upgrade behavior, and mixed-version workers, which supports the storage change. The test changes cover the new namespace behavior. The monotonic-clock change in the TTL test and the mock updates in the discovery tests are test-only adjustments that support the modified code paths. I found no demonstrated change unrelated to #2663.

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 e39eca7.

Files selected for processing: 5

  • docs/source/components/auth/mcp-auth/mcp-auth-token-storage.md
  • packages/nvidia_nat_core/src/nat/authentication/token_storage.py
  • packages/nvidia_nat_mcp/src/nat/plugins/mcp/auth/auth_provider.py
  • packages/nvidia_nat_mcp/tests/client/test_mcp_auth_provider.py
  • packages/nvidia_nat_mcp/tests/client/test_mcp_token_storage.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 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 e39eca7

@willkill07

Copy link
Copy Markdown
Member

/merge

@rapids-bot
rapids-bot Bot merged commit 04631ca 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.

A stored MCP OAuth grant is reused after the client registration changes

2 participants