MCP PHP SDK

AuthorizationMiddleware implements MiddlewareInterface uses ReadsBoundedBody

FinalYes

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
see
https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization
author

Volodymyr Panivko sveneld300@gmail.com

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

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
ResponseInterface

escapeHeaderValue()

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
string

parseBearerToken()

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|null

readBoundedBody()

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
Return values
string|null
On this page

Search results