Implement OAuth 2.1 authorization support for poem-mcpserver as specified in the
https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization.
Background
The Model Context Protocol (MCP) specification defines authorization capabilities for HTTP-based
transports, enabling MCP clients to make requests to restricted MCP servers on behalf of
resource owners. This is based on OAuth 2.1 and related RFCs.
Requirements
According to the MCP specification, protected MCP servers (resource servers) MUST implement:
- OAuth 2.0 Protected Resource Metadata (https://datatracker.ietf.org/doc/html/rfc9728)
- Serve metadata at /.well-known/oauth-protected-resource
- Include authorization_servers field with at least one authorization server URL
- Return WWW-Authenticate header on HTTP 401 responses indicating resource metadata URL
- Access Token Validation
- Error Responses
| Status Code |
Usage |
| 401 Unauthorized |
Authorization required or token invalid |
| 403 Forbidden |
Invalid scopes or insufficient permissions |
| 400 Bad Request |
Malformed authorization request |
Proposed Implementation
The implementation should provide:
- Extractor for validating Bearer tokens on incoming requests and inject to tool methods.
- Configuration for specifying:
- Authorization server URL(s)
- Token validation endpoint or JWT validation keys
- Resource identifier (canonical URI of this MCP server)
- Protected Resource Metadata endpoint at /.well-known/oauth-protected-resource
- WWW-Authenticate header handling on 401 responses
use poem_mcpserver::oauth::{OAuthConfig, ProtectedResource};
let oauth_config = OAuthConfig::new()
.authorization_server("https://auth.example.com")
.resource_uri("https://mcp.example.com")
.token_validation(TokenValidation::Jwt {
jwks_url: "https://auth.example.com/.well-known/jwks.json"
});
OAuthProtection::new(oauth_config);
Standards Compliance
Implementation should follow these specifications (subset of features for
security/interoperability):
Benefits
- Enable secure, multi-tenant MCP servers
- Standard OAuth 2.1 integration for enterprise environments
- Support for various authorization servers (Auth0, Okta, Keycloak, etc.)
- Proper token audience validation to prevent token misuse
- Compliance with MCP specification
References
Implement OAuth 2.1 authorization support for poem-mcpserver as specified in the
https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization.
Background
The Model Context Protocol (MCP) specification defines authorization capabilities for HTTP-based
transports, enabling MCP clients to make requests to restricted MCP servers on behalf of
resource owners. This is based on OAuth 2.1 and related RFCs.
Requirements
According to the MCP specification, protected MCP servers (resource servers) MUST implement:
https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-13#section-5.2
https://www.rfc-editor.org/rfc/rfc8707.html#section-2
Proposed Implementation
The implementation should provide:
- Authorization server URL(s)
- Token validation endpoint or JWT validation keys
- Resource identifier (canonical URI of this MCP server)
Standards Compliance
Implementation should follow these specifications (subset of features for
security/interoperability):
Benefits
References