AuthorizationMiddleware implements MiddlewareInterface uses ReadsBoundedBody
Enforces MCP HTTP authorization: the MCP server as OAuth 2.1 resource server.
This middleware:
- Validates Bearer tokens via the configured validator
- Returns 401 with WWW-Authenticate header on missing/invalid tokens
- Returns 403 insufficient_scope when the token lacks a scope the ScopePolicy requires
- Returns 413 when a body the ScopePolicy inspects exceeds the byte cap
- Returns 503 without a challenge when the validator cannot check the token
- Hands the validated AccessToken to the transport as request attribute, from where handlers read it via RequestContext::getAccessToken()
Tags
Table of Contents
Interfaces
- MiddlewareInterface
Properties
- $logger : LoggerInterface
- $maxBodyBytes : int
- $resourceMetadata : ProtectedResourceMetadata
- $responseFactory : ResponseFactoryInterface
- $scopePolicy : ScopePolicy|null
- $streamFactory : StreamFactoryInterface
- $validator : AuthorizationTokenValidatorInterface
Methods
- __construct() : mixed
- process() : ResponseInterface
- buildErrorResponse() : ResponseInterface
- escapeHeaderValue() : string
- Quoted-string per RFC 9110 §5.6.4. Error descriptions come from validators, so control characters - CR and LF above all - are stripped to keep them from injecting headers.
- parseBearerToken() : string|null
- The b64token of an `Authorization: Bearer` header (RFC 6750 §2.1), null if it has none or a malformed one.
- readBoundedBody() : string|null
- Returns the body contents, or `null` when the payload exceeds $maxBytes.
Properties
$logger read-only
private
LoggerInterface
$logger
= new NullLogger()
$maxBodyBytes read-only
private
int
$maxBodyBytes
= StreamableHttpTransport::DEFAULT_MAX_BODY_BYTES
$resourceMetadata read-only
private
ProtectedResourceMetadata
$resourceMetadata
$responseFactory
private
ResponseFactoryInterface
$responseFactory
$scopePolicy read-only
private
ScopePolicy|null
$scopePolicy
= null
$streamFactory
private
StreamFactoryInterface
$streamFactory
$validator read-only
private
AuthorizationTokenValidatorInterface
$validator
Methods
__construct()
public
__construct(AuthorizationTokenValidatorInterface $validator, ProtectedResourceMetadata $resourceMetadata[, ScopePolicy|null $scopePolicy = null ][, ResponseFactoryInterface|null $responseFactory = null ][, StreamFactoryInterface|null $streamFactory = null ][, int $maxBodyBytes = StreamableHttpTransport::DEFAULT_MAX_BODY_BYTES ][, LoggerInterface $logger = new NullLogger() ]) : mixed
Parameters
- $validator : AuthorizationTokenValidatorInterface
-
Token validator implementation
- $resourceMetadata : ProtectedResourceMetadata
-
Metadata of this resource, its URL is advertised in challenges
- $scopePolicy : ScopePolicy|null = null
-
Scopes required per request, none if null
- $responseFactory : ResponseFactoryInterface|null = null
- $streamFactory : StreamFactoryInterface|null = null
- $maxBodyBytes : int = StreamableHttpTransport::DEFAULT_MAX_BODY_BYTES
-
Upper bound for reading the body when the scope policy inspects it, a larger body gets 413; set it together with the transport's
maxBodyBytes - $logger : LoggerInterface = new NullLogger()
-
Receives a warning when a body exceeds
maxBodyBytes
process()
public
process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
- $request : ServerRequestInterface
- $handler : RequestHandlerInterface
Return values
ResponseInterfacebuildErrorResponse()
private
buildErrorResponse(AuthorizationResult $result) : ResponseInterface
Parameters
- $result : AuthorizationResult
Return values
ResponseInterfaceescapeHeaderValue()
Quoted-string per RFC 9110 §5.6.4. Error descriptions come from validators, so control characters - CR and LF above all - are stripped to keep them from injecting headers.
private
escapeHeaderValue(string $value) : string
Parameters
- $value : string
Return values
stringparseBearerToken()
The b64token of an `Authorization: Bearer` header (RFC 6750 §2.1), null if it has none or a malformed one.
private
parseBearerToken(string $authorization) : string|null
Parameters
- $authorization : string
Return values
string|nullreadBoundedBody()
Returns the body contents, or `null` when the payload exceeds $maxBytes.
private
readBoundedBody(StreamInterface $body, int $maxBytes) : string|null
A stream advertising its size is rejected up front; otherwise the read is incremental and stops at the cap, so an unbounded stream cannot exhaust memory.
Parameters
- $body : StreamInterface
- $maxBytes : int