Skip to content

[v1.x] Add AuthSettings.validate_token_resource to check a bearer token's resource - #3451

Merged
maxisbey merged 1 commit into
v1.xfrom
bearer-resource-check-v1x
Sep 5, 2026
Merged

[v1.x] Add AuthSettings.validate_token_resource to check a bearer token's resource#3451
maxisbey merged 1 commit into
v1.xfrom
bearer-resource-check-v1x

Conversation

@maxisbey

@maxisbey maxisbey commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

v1.x backport of #3447.

Adds AuthSettings.validate_token_resource. When set, the bearer gate only accepts a token that the TokenVerifier reports as issued for resource_server_url (via AccessToken.resource); a token reporting another resource, or none, gets the same 401 as an unrecognized token. Leaving it unset on a resource server emits a DeprecationWarning and behaves as off; 3.0 makes it the default there. RefreshToken gains an optional resource so providers can carry the binding through the refresh grant.

Differences from #3447

  • The wiring is FastMCP.sse_app() and FastMCP.streamable_http_app() in src/mcp/server/fastmcp/server.py (main: Server.streamable_http_app() and MCPServer.sse_app()).
  • Plain DeprecationWarning, as v1.x has no MCPDeprecationWarning (same as [v1.x] Validate the authorization server metadata issuer on every discovery path #3431).
  • v1.x has no interaction suite or docs tutorials; the tests are the BearerAuthBackend unit table and the AuthSettings warning/validation tests, and the documentation lands in docs/authorization.md (the oauth_server snippet it mirrors sets the option against its endpoint URL).

How Has This Been Tested?

Unit tests as above; driven end to end under uvicorn on FastMCP with an introspection-backed verifier over Streamable HTTP (option on: a token for another resource gets 401 and one for this server 200; unset: behaviour unchanged apart from the warning); full suite with coverage, ruff, pyright.

Breaking Changes

None. Unset behaves as off (with the deprecation warning); BearerAuthBackend(verifier) and existing verifiers behave as before. Setting it to True without a resource_server_url raises at construction.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I am assigned to the linked issue (or it is labeled help wanted, or I'm a maintainer)
  • I have disclosed any AI assistance and can explain the change in my own words
  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

AI Disclaimer

…en's resource

v1.x backport. BearerAuthBackend takes an optional resource_server_url; when
it is set, only a token the verifier reports as issued for that URL
(AccessToken.resource, the RFC 8707 resource indicator, compared as a URL
with a trailing slash tolerated) is accepted, and anything else is answered
401 like an unrecognized token. AuthSettings.validate_token_resource turns
this on for FastMCP's SSE and Streamable HTTP apps; leaving it unset while
resource_server_url is set emits a DeprecationWarning and behaves as False,
and 3.0 makes True the default there. RefreshToken gains an optional
resource so a provider can carry the binding through the refresh grant.

TokenVerifier.verify_token's docstring and docs/authorization.md say where
the token's audience goes and when to enable the option versus checking the
audience in the verifier. The oauth_server snippet and the simple-auth
example set it.
@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

📚 Documentation preview

Preview https://pr-3451.mcp-python-docs.pages.dev
Deployment https://03c7f4fc.mcp-python-docs.pages.dev
Commit ebdc7e6
Triggered by @maxisbey
Updated 2026-09-05 02:26:13 UTC

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 10 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread docs/authorization.md
Comment thread examples/servers/simple-auth/mcp_simple_auth/token_verifier.py
Comment thread examples/snippets/servers/oauth_server.py
Comment thread examples/servers/simple-auth/mcp_simple_auth/server.py

@claude claude 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.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread src/mcp/server/auth/provider.py
Comment thread src/mcp/server/auth/middleware/bearer_auth.py
Comment thread examples/servers/simple-auth/mcp_simple_auth/token_verifier.py
Comment thread examples/servers/simple-auth/mcp_simple_auth/server.py
@maxisbey
maxisbey merged commit 8c2fa6e into v1.x Sep 5, 2026
33 checks passed
@maxisbey
maxisbey deleted the bearer-resource-check-v1x branch September 5, 2026 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant