MCP PHP SDK

AuthorizationMiddleware implements MiddlewareInterface

FinalYes

Enforces MCP HTTP authorization requirements.

This middleware:

  • Validates Bearer tokens via the configured validator
  • Returns 401 with WWW-Authenticate header on missing/invalid tokens
  • Returns 403 on insufficient scope
Tags
see
https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization
author

Volodymyr Panivko sveneld300@gmail.com

Table of Contents

Interfaces

MiddlewareInterface

Properties

$resourceMetadata  : ProtectedResourceMetadata
$responseFactory  : ResponseFactoryInterface
$validator  : AuthorizationTokenValidatorInterface

Methods

__construct()  : mixed
process()  : ResponseInterface
applyAttributes()  : ServerRequestInterface
buildAuthenticateHeader()  : string
buildErrorResponse()  : ResponseInterface
escapeHeaderValue()  : string
normalizeScopes()  : array<int, string>|null
parseBearerToken()  : string|null
resolveResourceMetadataUrl()  : string
resolveScopes()  : array<int, string>|null

Properties

Methods

__construct()

public __construct(AuthorizationTokenValidatorInterface $validator, ProtectedResourceMetadata $resourceMetadata[, ResponseFactoryInterface|null $responseFactory = null ]) : mixed
Parameters
$validator : AuthorizationTokenValidatorInterface

Token validator implementation

$resourceMetadata : ProtectedResourceMetadata

Protected resource metadata object used for challenge hints

$responseFactory : ResponseFactoryInterface|null = null

PSR-17 response factory (auto-discovered if null)

process()

public process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
Parameters
$request : ServerRequestInterface
$handler : RequestHandlerInterface
Return values
ResponseInterface

applyAttributes()

private applyAttributes(ServerRequestInterface $request, array<string, mixed> $attributes) : ServerRequestInterface
Parameters
$request : ServerRequestInterface
$attributes : array<string, mixed>
Return values
ServerRequestInterface

escapeHeaderValue()

private escapeHeaderValue(string $value) : string
Parameters
$value : string
Return values
string

normalizeScopes()

private normalizeScopes(array<int, string>|null $scopes) : array<int, string>|null
Parameters
$scopes : array<int, string>|null
Return values
array<int, string>|null

parseBearerToken()

private parseBearerToken(string $authorization) : string|null
Parameters
$authorization : string
Return values
string|null

resolveResourceMetadataUrl()

private resolveResourceMetadataUrl(ServerRequestInterface $request) : string
Parameters
$request : ServerRequestInterface
Return values
string

        
On this page

Search results