Skip to content

[Client] Add OAuth authorization with the authorization code flow - #666

Draft
chr-hertel wants to merge 3 commits into
modelcontextprotocol:mainfrom
chr-hertel:client-auth-sign-in
Draft

chr-hertel wants to merge 3 commits into
modelcontextprotocol:mainfrom
chr-hertel:client-auth-sign-in

Conversation

@chr-hertel

Copy link
Copy Markdown
Member

Part of #316, #319, #320, #321, #322 & #323 - third slice of the client authorization in chr-hertel#53. Builds on #663 & #664, so please only look at the last commit until those are merged.

$client->connect(new HttpTransport('https://mcp.example.com/mcp', auth: OAuth::forApplication('My App')->build()));

A 401 now leads to discovery, client registration, the browser, a token and a retry - nothing else changes at the call site. Refresh, step-up and offline_access are included, and so are the checks against a hostile server, see What the Client Refuses in docs/client/authorization.md.

Client id metadata documents, the console handler & machine grants (client credentials, private_key_jwt, cross-app access) follow - their conformance scenarios stay in the baselines. Web applications finishing the flow in a later request are #630 / #660, independent of this.

This also adds the Keycloak end-to-end job.

The HTTP client gets decorated, so the transport keeps speaking plain PSR-18:
AuthenticatingHttpClient authenticates every request and retries the ones the
authenticator can act on. BearerToken sends a token you already have.

A 401 or 403 now fails with an AuthorizationException that names the challenge,
which connect() wraps in a ConnectionException without a retry.
Finds and validates the documents that say who guards an MCP server: protected
resource metadata (RFC 9728) and authorization server metadata (RFC 8414 and
OpenID Connect), plus the WWW-Authenticate challenge that points at them.

All of it is @internal, since nothing uses it until the authenticator lands.
Endpoints have to pass SecureUrl, and a document naming another issuer than it
was fetched for is discarded.
Pass OAuth::forApplication('My App')->build() as `auth` and a client answers a
401 with discovery, client registration, the authorization code flow with PKCE
and a retry. It refreshes tokens, handles step-up challenges and refuses what a
hostile server could send.

Client id metadata documents, the console handler, client credentials,
private_key_jwt and cross-app access follow in their own changes, so their
conformance scenarios stay in the baselines.
@chr-hertel chr-hertel added Client Issues & PRs related to the Client component auth Issues and PRs related to Authentication / OAuth labels Nov 5, 2026
@chr-hertel chr-hertel added this to the 1.0.0 milestone Nov 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Issues and PRs related to Authentication / OAuth Client Issues & PRs related to the Client component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant