Skip to content

Backport MCP servers and OAuth to Craft 5.12 - #20145

Merged
brandonkelly merged 62 commits into
5.12from
feat/backport-mcp
Oct 29, 2026
Merged

brandonkelly merged 62 commits into
5.12from
feat/backport-mcp

Conversation

@riasvdv

@riasvdv riasvdv commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

MCP client ──▶ OAuth discovery ──▶ registration ──▶ PKCE consent ──▶ scoped token
                                                                      │
          /{cpTrigger}/mcp/auth, /{cpTrigger}/mcp  ◀───────────────────┘  authenticated MCP server
          (/mcp/auth, /mcp when public registration is on)                 acts as the user
Visitor ──▶ /mcp ──▶ public MCP server: approved capabilities and content only
CLI     ──▶ php craft mcp/serve --user=… ──▶ stdio MCP server
GraphQL ──▶ Authorization: Bearer <OAuth or personal token> ──▶ schema built from the user's permissions

Backports the MCP servers and an OAuth authorization server to Craft 5.12, with authenticated GraphQL on top. This branch includes the work merged from:

  • [5.12] Separate authenticated and public MCP servers #20189: separate authenticated and public MCP servers, each turned on and given an endpoint from Settings → MCP, which has Public and Authenticated tabs.
    • There's no useCraftMcp permission anymore. Any active user can authorize a client, and tools check permissions per record.
    • Without public registration, the authenticated endpoints default to /{cpTrigger}/mcp and /{cpTrigger}/mcp/auth. With it, they default to /mcp and /mcp/auth. Endpoints in the control panel require control panel access.
    • Public content approvals are grouped as Sites, Entries, Nested Entry Fields, Assets, Addresses, and Users. Categories, tags, and global sets aren't available through MCP.
  • Authenticated GraphQL queries via OAuth #20147: OAuth-authenticated GraphQL requests (CMS-2559) and the My Account → Tokens screen.
    • The graphql OAuth resource has graphql:read and graphql:write levels.
    • Each request gets a schema built from the components available to the user. Schema components opt in with an authenticated key.
    • The new currentUser query returns the user that authenticated the request.
    • Users can create personal access tokens for APIs that offer them, and revoke tokens and apps they've authorized.
  • Use Laravel Passport's OAuth table layout and scope-bound clients #20154: the OAuth tables use Passport 13's layout, so Craft 6 can adopt 5.12 tokens.

The release notes are in CHANGELOG-5.12.md. docs/mcp.md and docs/oauth.md cover setup, configuration, and plugin extension points.

Evidence

  • Before: origin/5.12 has no src/mcp/, src/oauth/, or MCP and OAuth controllers. After: a Craft/PostgreSQL install served OAuth discovery and the protected MCP endpoint. Browser consent issued a token only with the correct PKCE verifier. A wrong verifier and a replayed code returned 400.

  • Read, write, and full-access tokens listed 19, 40, and 50 tools. A write token updated a user preference and restored it, and a read token saw the change but couldn't write. Missing or invalid credentials, another resource's token, and a suspended user were rejected. Refreshing rotated both the access and refresh tokens.

  • Stdio initialized, listed tools, and called preferences.get. The public server returned 404 until enabled. With one approved tool, only that tool was listed.

  • Split servers ([5.12] Separate authenticated and public MCP servers #20189), against a Pro install with real tokens:

    Request No permissions accessCp
    tools/list at /mcp 200, 26 tools 200, 30 tools
    tools/list at /admin/mcp 403 200, 30 tools
    POST /mcp/auth without a token 401 with resource_metadata
    POST /mcp without a token, shared endpoint public server's initialize result
  • Authenticated GraphQL (Authenticated GraphQL queries via OAuth #20147): AuthenticatedSchemaTest and GraphqlOAuthCest cover permission-limited queries and mutations, drafts, per-user caching, and real OAuth and personal tokens through GraphqlController. In a browser, a new personal token answered {ping} and got 401 after it was revoked.

  • After the latest merges, these tests pass: OAuth unit (22), MCP unit (29), GraphQL unit (257), and functional (38). PHPStan and ECS pass in CI.

Screenshots of the settings, consent, and Tokens screens are in #20189 and #20147.

Related issues

  • CMS-2559

Amp threads: https://ampcode.com/threads/T-01a18252-9c08-75c3-9c6b-f581cf3dda42, https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f

Backports the edition part of aa19b0e from 6.x.
Covers the field layout filtering backported from 2d5aa64.
Replace the single mcp:use scope with mcp:read, mcp:write, and mcp:admin,
modeled as generic ordered scope levels on craft\oauth\Resource. Users
choose the access level on the consent screen, defaulting to read only,
and only administrators are offered full access. Discovery hides
capabilities the token's scope doesn't cover, and clients that authorize
again keep the earlier level without another prompt. Stdio stays unscoped.

Backports e905d29 from 6.x.
OAuth metadata, client registration, token, and MCP preflight requests
carry no Craft session, so they failed while the system was offline and
clients couldn't refresh tokens. The MCP endpoint still requires the
token's user to have access to the control panel while the system is
offline.
Browser GET requests to the admin MCP endpoint, and to the public one
while it's enabled, now render a page with the server URL and copyable
setup snippets for common AI assistants. Other GET requests get a 405
without requiring a token.

Backports 1bcc66a from 6.x.
MCP is now listed after Plugins in the System settings group, rather than
first, and is hidden when admin changes are disallowed.

Adapts 3ea2209 from 6.x.
Add the light robot icon that the MCP settings item references.

Backports ada9a99 from 6.x.
@riasvdv
riasvdv requested a review from brandonkelly October 28, 2026 13:07
riasvdv and others added 2 commits October 28, 2026 13:08
GraphQL requests with an OAuth access token for the new `graphql` resource now
get a schema built from the components available to the token's user.
Schema components opt in with an `authenticated` key, and built-in
components map to user permissions. Entry and asset queries use the
`editable` param, and mutations check the user's element permissions.

Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
Co-authored-by: Amp <amp@ampcode.com>
brandonkelly and others added 13 commits October 28, 2026 23:56
Craft 6 uses Passport, so 5.12’s OAuth tables now use Passport’s column
names and layout (plus resource_id), and clients record their grant
types. Clients can only use the grant types they were registered with.

Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
Co-authored-by: Amp <amp@ampcode.com>
… client

Personal tokens now share one client per resource with grant_types
["personal_access"], and store their name on the token row, as Laravel
Passport does. Revoking a personal token revokes just that token.
…by scope like Passport

Tokens belong to the resource that defines their scopes, scope IDs must be
unique across resources, and registered clients store their resource's scopes
in Passport's optional oauth_clients.scopes column.

Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
Co-authored-by: Amp <amp@ampcode.com>
Passport 13 only matches a client with no provider when auth.guards.api.provider
is the user's provider, which Craft 6 doesn't configure, so it wouldn't find a
provider-less 5.12 client.

Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
Co-authored-by: Amp <amp@ampcode.com>
Use Laravel Passport's OAuth table layout and scope-bound clients
The authenticated server now has its own enable toggle, endpoint, and
authorization endpoint, and any active user can use it. Endpoints in the
control panel go through the usual control panel access check. The
useCraftMcp permission is gone, and capabilities that only make sense in
the control panel require accessCp.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
brandonkelly and others added 12 commits October 29, 2026 17:26
Scopes now sit in one field with a group per API. The Tokens table gains
a Type column and an action menu with Edit and Revoke, and the edit
modal shows only the scopes a token has, along with its expiration date.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
The page is titled "MCP Settings", and the public server's capabilities,
content, and element states sit inside its pane. Labels, option wording,
content order, and "Select all" buttons now match Craft 6.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
They're deprecated, so MCP no longer lists them as element types, resolves
them in element tools or public queries, or moves categories within
structures.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
Content groups are now Sites, Entries, Nested Entry Fields, Assets,
Addresses, and Users. User group checkboxes live under Users.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
Without public registration, the authenticated endpoints default to
/{cpTrigger}/mcp and /{cpTrigger}/mcp/auth, leaving /mcp for the public
server.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-ae777030-d792-49ae-8853-87e83195c88f
[5.12] Separate authenticated and public MCP servers
@linear-code

linear-code Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

CMS-2559

CMS-2557

@brandonkelly
brandonkelly merged commit 168fc5d into 5.12 Oct 29, 2026
11 checks passed
@brandonkelly
brandonkelly deleted the feat/backport-mcp branch October 29, 2026 21:20
@brandonkelly brandonkelly mentioned this pull request Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants