MCP PHP SDK

OAuth

FinalYes

Fluent builder for the OAuth authenticator of an {@see \Mcp\Client\Transport\HttpTransport}.

Tags
author

Christopher Hertel mail@christopher-hertel.de

Table of Contents

Constants

DEFAULT_REDIRECT_URI  : mixed = 'http://127.0.0.1:8765/callback'
A fixed port, as many authorization servers require redirect URIs to be registered up front.

Properties

$applicationType  : string
$authorizationHandler  : AuthorizationHandlerInterface|null
$clientId  : string|null
$clientMetadataUrl  : string|null
$clientName  : string
$clientSecret  : string|null
$clientUri  : string|null
$crossAppAccess  : CrossAppAccess|null
$expectedIssuer  : string|null
$grant  : Grant
$httpClient  : ClientInterface|null
$legacyDiscovery  : bool
$logger  : LoggerInterface
$offlineAccess  : bool
$privateKeyPem  : string|null
$redirectUri  : string
$requestFactory  : RequestFactoryInterface|null
$scopes  : array<string|int, string>
$signingAlgorithm  : string
$softwareId  : string|null
$softwareVersion  : string|null
$storage  : CredentialStorageInterface|null
$streamFactory  : StreamFactoryInterface|null

Methods

build()  : OAuthAuthenticator
forApplication()  : self
An application acting for a user, who approves the request in a browser.
forServiceAccount()  : self
A client acting as itself, with no user and no browser (RFC 6749 section 4.4).
setApplicationType()  : self
Whether the client runs on the user's machine ("native") or on a server ("web").
setAuthorizationHandler()  : self
How the user gets to the authorization endpoint and back.
setClientCredentials()  : self
Use a client id issued out of band instead of registering dynamically.
setClientMetadataUrl()  : self
Use the URL of a client ID metadata document as the client id where the server supports it.
setCredentialStorage()  : self
Where tokens and registrations live between requests, and between runs.
setCrossAppAccess()  : self
Reach the server through an identity the user already holds at their identity provider, rather than sending them to a consent screen.
setExpectedIssuer()  : self
Only authorize with this issuer; required with a configured client, or a hostile MCP server could receive its codes and secret.
setHttpClient()  : self
setLegacyDiscovery()  : self
Whether to fall back to pre-2025-06-18 discovery for a server without protected resource metadata (default: false).
setLogger()  : self
setOfflineAccess()  : self
Whether to request a refresh token where the authorization server offers one (default: true).
setPrivateKeyJwt()  : self
Authenticate at the token endpoint with a signed assertion instead of a secret.
setRedirectUri()  : self
Must match what the authorization handler listens on and, for a pre-registered client, what the server has on file.
setScopes()  : self
Request these scopes instead of those the server asks for; a step-up challenge still adds its own.
setSoftwareInfo()  : self
Extra identifying information shown to users and administrators.
__construct()  : mixed

Constants

DEFAULT_REDIRECT_URI

A fixed port, as many authorization servers require redirect URIs to be registered up front.

public mixed DEFAULT_REDIRECT_URI = 'http://127.0.0.1:8765/callback'

Properties

$applicationType

private string $applicationType = 'native'

$clientId

private string|null $clientId = null

$clientMetadataUrl

private string|null $clientMetadataUrl = null

$clientName read-only

private string $clientName

$clientSecret

private string|null $clientSecret = null

$clientUri

private string|null $clientUri = null

$expectedIssuer

private string|null $expectedIssuer = null

$grant

private Grant $grant = \Mcp\Client\Auth\Grant::AuthorizationCode

$httpClient

private ClientInterface|null $httpClient = null

$legacyDiscovery

private bool $legacyDiscovery = false

$logger

private LoggerInterface $logger

$offlineAccess

private bool $offlineAccess = true

$privateKeyPem

private string|null $privateKeyPem = null

$redirectUri

private string $redirectUri = self::DEFAULT_REDIRECT_URI

$requestFactory

private RequestFactoryInterface|null $requestFactory = null

$scopes

private array<string|int, string> $scopes = []

$signingAlgorithm

private string $signingAlgorithm = 'RS256'

$softwareId

private string|null $softwareId = null

$softwareVersion

private string|null $softwareVersion = null

$streamFactory

private StreamFactoryInterface|null $streamFactory = null

Methods

forApplication()

An application acting for a user, who approves the request in a browser.

public static forApplication(string $clientName) : self
Parameters
$clientName : string

the name shown to the user while they approve

Return values
self

forServiceAccount()

A client acting as itself, with no user and no browser (RFC 6749 section 4.4).

public static forServiceAccount(string $clientName, string $clientId[, string|null $clientSecret = null ]) : self

The credentials have to be issued out of band: there is nobody to consent to a dynamic registration.

Parameters
$clientName : string
$clientId : string
$clientSecret : string|null = null
Return values
self

setApplicationType()

Whether the client runs on the user's machine ("native") or on a server ("web").

public setApplicationType(string $applicationType) : self
Parameters
$applicationType : string
Return values
self

setClientCredentials()

Use a client id issued out of band instead of registering dynamically.

public setClientCredentials(string $clientId[, string|null $clientSecret = null ]) : self
Parameters
$clientId : string
$clientSecret : string|null = null
Return values
self

setClientMetadataUrl()

Use the URL of a client ID metadata document as the client id where the server supports it.

public setClientMetadataUrl(string $url) : self
Parameters
$url : string
Return values
self

setCrossAppAccess()

Reach the server through an identity the user already holds at their identity provider, rather than sending them to a consent screen.

public setCrossAppAccess(CrossAppAccess $crossAppAccess) : self

Requires the client id the authorization server knows this client by, set with self::setClientCredentials().

Parameters
$crossAppAccess : CrossAppAccess
Return values
self

setExpectedIssuer()

Only authorize with this issuer; required with a configured client, or a hostile MCP server could receive its codes and secret.

public setExpectedIssuer(string $issuer) : self
Parameters
$issuer : string
Return values
self

setHttpClient()

public setHttpClient(ClientInterface $httpClient[, RequestFactoryInterface|null $requestFactory = null ][, StreamFactoryInterface|null $streamFactory = null ]) : self
Parameters
$httpClient : ClientInterface
$requestFactory : RequestFactoryInterface|null = null
$streamFactory : StreamFactoryInterface|null = null
Return values
self

setLegacyDiscovery()

Whether to fall back to pre-2025-06-18 discovery for a server without protected resource metadata (default: false).

public setLegacyDiscovery(bool $legacyDiscovery) : self
Parameters
$legacyDiscovery : bool
Return values
self

setLogger()

public setLogger(LoggerInterface $logger) : self
Parameters
$logger : LoggerInterface
Return values
self

setOfflineAccess()

Whether to request a refresh token where the authorization server offers one (default: true).

public setOfflineAccess(bool $offlineAccess) : self
Parameters
$offlineAccess : bool
Return values
self

setPrivateKeyJwt()

Authenticate at the token endpoint with a signed assertion instead of a secret.

public setPrivateKeyJwt(string $privateKeyPem[, string $algorithm = 'RS256' ]) : self

Needs the firebase/php-jwt package.

Parameters
$privateKeyPem : string

a PEM-encoded private key

$algorithm : string = 'RS256'

one of RS256, RS384, RS512, ES256, ES256K or ES384

Return values
self

setRedirectUri()

Must match what the authorization handler listens on and, for a pre-registered client, what the server has on file.

public setRedirectUri(string $redirectUri) : self
Parameters
$redirectUri : string
Return values
self

setScopes()

Request these scopes instead of those the server asks for; a step-up challenge still adds its own.

public setScopes(string ...$scopes) : self
Parameters
$scopes : string
Return values
self

setSoftwareInfo()

Extra identifying information shown to users and administrators.

public setSoftwareInfo([string|null $clientUri = null ][, string|null $softwareId = null ][, string|null $softwareVersion = null ]) : self
Parameters
$clientUri : string|null = null
$softwareId : string|null = null
$softwareVersion : string|null = null
Return values
self

__construct()

private __construct(string $clientName) : mixed
Parameters
$clientName : string
On this page

Search results