Expand description
OAuth 2.1 Resource Server support for Turul MCP framework
This crate provides OAuth 2.1 Bearer token validation for MCP servers acting as Resource Servers (RS). It does NOT implement an Authorization Server — tokens are validated against an external AS via JWKS.
§Authorization-role posture (MCP 2026-07-28)
The MCP authorization spec assigns requirements per OAuth role. This crate implements the resource-server role only:
- RFC 9728 Protected Resource Metadata (
/.well-known/oauth-protected-resource) — the RS MUST — viaProtectedResourceMetadata/WellKnownOAuthHandler. - OAuth 2.1 §5.2 access-token validation with RFC 8707 audience binding
— via
JwtValidator/OAuthResourceMiddleware.
Client ID Metadata Documents (CIMD, draft-ietf-oauth-client-id-metadata-document-00) are a SHOULD for authorization servers and MCP clients — client registration never involves the resource server, so no CIMD surface belongs in this crate. Applications embedding an AS or building a full MCP OAuth client implement CIMD there.
Dynamic Client Registration (RFC 7591) is deprecated upstream (MAY, retained for AS back-compat; not removed — earliest removal 2027-07-28) and is likewise an AS/client concern. This crate has never implemented a DCR surface and does not add one.
§Architecture
OAuthResourceMiddleware— Pre-session middleware that validates Bearer tokens and injects claims into request extensionsJwtValidator— JWT validation with JWKS caching and kid-miss refresh. Owned byturul_jwt_validatorand re-exported herehardened_validator— builds aJwtValidatorwith this crate’s key max-age, stale window and fetch retry, and rejects a non-loopback plaintextjwks_uri. The bare constructor leaves all of those disabledProtectedResourceMetadata— RFC 9728 metadata documentWellKnownOAuthHandler— Route handler for/.well-known/oauth-protected-resource
§Usage
use std::sync::Arc;
use turul_mcp_oauth::{
JwtValidator, OAuthResourceMiddleware, ProtectedResourceMetadata,
WellKnownOAuthHandler, oauth_resource_server,
};
let metadata = ProtectedResourceMetadata::new(
"https://example.com/mcp",
vec!["https://auth.example.com".to_string()],
).unwrap();
let (auth_middleware, routes) = oauth_resource_server(
metadata,
"https://auth.example.com/.well-known/jwks.json",
).unwrap();
// Register with McpServer::builder()
// let mut builder = McpServer::builder().middleware(auth_middleware);
// for (path, handler) in routes {
// builder = builder.route(&path, handler);
// }Re-exports§
pub use error::OAuthError;pub use jwt::hardened_validator;pub use metadata::ProtectedResourceMetadata;pub use middleware::OAuthResourceMiddleware;pub use well_known::WellKnownOAuthHandler;
Modules§
- error
- OAuth error types
- jwt
- JWT validation for OAuth 2.1 Resource Server.
- metadata
- RFC 9728 Protected Resource Metadata
- middleware
- OAuth Resource Server middleware
- well_
known .well-known/oauth-protected-resourceroute handler
Structs§
- JwtValidator
- JWT validator with JWKS caching and kid-miss refresh.
- Token
Claims - Validated token claims extracted from a JWT.
Enums§
- Algorithm
- Re-exported so callers can name algorithms for
JwtValidator::with_algorithmswithout adding their ownjsonwebtokendependency. It appears in that method’s signature either way, so importing it here is what makes the method callable rather than what creates the coupling. Must track the samejsonwebtokenmajor asturul-jwt-validator. The algorithms supported for signing/verifying JWTs - Jwks
Fetch Error Kind - Categorized cause of a JWKS fetch failure.
- JwtValidation
Error - Errors from JWT validation.
Functions§
- oauth_
resource_ server - Convenience function to create both the OAuth middleware and well-known route handlers
Type Aliases§
- Route
Entry - A route entry:
(path, handler)pair for registration with the server builder.