MCP auth: OAuth 2.1 + DCR + scopes (closes #45) - #59
Merged
Conversation
Adds an OAuth 2.1 authorization server for the per-agent MCP endpoint, plus scope-based ability gating, RFC 7591 Dynamic Client Registration, RFC 7662 introspection, RFC 7009 revocation, and a Connected Clients admin page. The legacy bearer-token auth path stays live behind OPENCLAWP_MCP_LEGACY_AUTH for one minor version (logs a deprecation per request). Scope -> effect tiers: mcp:read -> read mcp:write -> read + write mcp:destructive -> read + write + destructive mcp:external -> read + write + destructive + external Scope enforcement is two-layered: tools/list is filtered so clients only see what they can call, and tools/call hard-gates per-call. Tokens are stored hashed (SHA-256); plaintext is shown once. Refresh-token rotation and .well-known/oauth-protected-resource are deferred to a follow-up — issue body documents the cut. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
lezama
force-pushed
the
ship/issue-45-oauth-mcp
branch
from
May 18, 2026 11:11
81a86d9 to
399b1cd
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #45
What ships
/wp-json/openclawp/v1/oauth/:GET /authorize— interactive consent screen, PKCE-required (S256 default,plaintolerated).POST /token—authorization_codegrant with PKCE verifier;client_secret_basic/client_secret_post/none(public client).POST /introspect— RFC 7662, requires client auth.POST /revoke— RFC 7009, always 200 (no enumeration).POST /register— RFC 7591 Dynamic Client Registration. By default DCR may only requestmcp:read/mcp:write(override viaopenclawp_oauth_allowed_dcr_scopesfilter).GET /.well-known/oauth-authorization-server— discovery document.mcp:read->readmcp:write->read+writemcp:destructive->read+write+destructivemcp:external-> all fourOpenclaWP_Mcp_Rest:tools/listis filtered so clients only see what they can call.tools/callhard-gates per-call and returnsinsufficient_scopewhen denied.OpenclaWP_Mcp_Server_Storepattern.openclaWP -> Connected Clients) lists clients, their scopes, last-used timestamp, and access tokens with revoke buttons. DCR-self-registered clients show up the same way.Legacy migration
Legacy bearer-token auth remains live behind an opt-in switch (one minor version):
When the flag is set, the per-server hashed bearer (from
OpenclaWP_Mcp_Server_Store::verify_token) still works, and anerror_log()deprecation warning is emitted on every request. With the flag off (the default), legacy bearers are rejected.Assumed API contracts for the dependent issues
This PR doesn't wait for #40 (effect-tagged abilities) or #42 (mcp-adapter migration). Documented assumptions:
OpenclaWP_Oauth_Scope::effect_for_ability( $name )resolves an ability's effect tier. Today it runs a name-based heuristic (delete-*/destroy-*-> destructive,update-*/create-*-> write,send-whatsapp/send-sms-> external, default read) and passes that through theopenclawp_ability_effectfilter. When Tool confirmation gate with "always allow" memory #40 lands, the registrar should hook that filter (or extendOpenclaWP_Oauth_Scope::effect_for_abilityto readability->get_meta('effect')directly) so the explicit metadata wins over the heuristic. No call-site changes needed downstream — the contract is(string $ability_name) -> string $effect.OpenclaWP_Mcp_Restroute. When Migrate per-agent MCP server to Abilities + official mcp-adapter #42 swaps the JSON-RPC body handling to delegate to the officialWordPress/mcp-adapter, the only requirement is that the adapter's request entry point either reusesOpenclaWP_Mcp_Rest::check_permissionor calls intoOpenclaWP_Oauth_Store::find_token_by_value( $bearer, KIND_ACCESS )itself and consultsOpenclaWP_Oauth_Scope::scopes_permit_effect( $scopes, $effect )before invoking an ability. The token table is the source of truth for both paths.Merge order doesn't matter — #40 / #42 can land before or after.
Dependency-weight analysis (league/oauth2-server)
Evaluated
league/oauth2-serveras the issue suggested. Not adopted in v1:psr/http-message,psr/http-server-handler,psr/http-factory,defuse/php-encryption,lcobucci/jwt,lcobucci/clock,lcobucci/parser,lcobucci/signer,nyholm/psr7,nyholm/psr7-server,paragonie/random_compat,symfony/polyfill-php83). ~1.5MB invendor/, no Composer prefixing — collides if a host site has a differentpsr/http-messageversion.authorization_code+ PKCE + DCR + introspection + revocation. The bespoke implementation here is ~900 lines of PHP across 4 well-scoped classes, easy to audit, no PSR-7 / JWT machinery.WP_REST_Request. Bridging both ways added more code than the bespoke server.The trade-off is documented in
OpenclaWP_Oauth_Server's class-level docblock.Deferred to follow-up (out of v1 scope)
/.well-known/oauth-protected-resource— the MCP server side of the discovery story. The authorization server doc is live; the protected-resource doc that points clients back at the AS can be a follow-up since current MCP clients (Claude Desktop, Cursor, VS Code MCP) all support DCR + the AS discovery doc alone./authorizeshows the consent screen. Adding a remembered-consent table is a small follow-up.Validation
php -lclean on all changed files.vendor/bin/phpunit --testsuite unit-> 44 tests / 91 assertions (existing 33 + 11 new inOauthScopeTest).npm run lintclean (no JS changes).tests/smoke.phpextended with a full OAuth flow integration test:mcp:readtoken via the full auth-code + PKCE exchangemcp:read-> assertsinsufficient_scopemcp:read-> asserts successmcp:write-> asserts the write tool now succeedstools/listis rejected (401)Test plan
vendor/bin/phpunit --testsuite unitpassesstudio wp eval-file tests/smoke.phppasses inside a Studio site with openclaWP activemcp:read-> see only read-effect tools in the catalogdelete-*ability with that token -> client reports the tool isn't available, server logsinsufficient_scopeopenclaWP -> Connected Clients, revoke the client's token -> next call from Claude returns 401 within a minuteOPENCLAWP_MCP_LEGACY_AUTH=1and re-test with the legacy bearer fromOpenclaWP_Mcp_Server_Store-> still works,error_logshows the deprecation