Skip to content

McpAuthMiddleware

Bearer-token auth for the MCP HTTP endpoint.

Registered by McpPlugin immediately before McpEndpointMiddleware — only when the “http” transport is enabled and mcp.auth isn’t 'none' — so an invalid/missing token never reaches the SDK server at all. The actual validation is delegated to a McpAuthenticatorInterface resolved from the DI container (default: StaticTokenAuthenticator), so an app can swap in its own credential store via PluginRegistrar::service().

final class McpAuthMiddleware implements MiddlewareInterface

ImplementsMiddlewareInterface
SourceMiddleware/McpAuthMiddleware.php

public function __construct(string $contextName): mixed

ParameterTypeDescription
$contextNamestring

Returns mixed

MethodDescription
process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterfaceRejects requests to the MCP path that carry no valid bearer token.

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface

Rejects requests to the MCP path that carry no valid bearer token.

Passes the request straight down the pipeline when MCP is disabled, when mcp.auth is 'none', or when the path is not the configured mcp.path. Otherwise the Authorization header’s Bearer credential is handed to the container-resolved McpAuthenticatorInterface; a missing, empty or rejected token yields a 401 problem-details response carrying WWW-Authenticate: Bearer, and the inner handler is never called.

ParameterTypeDescription
$requestServerRequestInterface
$handlerRequestHandlerInterface

Returns ResponseInterface