Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

10.6 KB
Raw Blame

MCP OAuth 2.1 Authorization Server

Status: Implemented — shipped with 6.0-mcp-server in one pull request. Client ID metadata documents are explicitly 6.1 work (see Migration Path step 6). Target: Spree 6.0 Depends on: 6.0-mcp-server (the tool catalog and transport this authenticates), 6.0-platform-auth (the admin user and permission models a grant binds to) Author: Damian Legawiec Last updated: 2026-10-05 Tracking: Linear — · GitHub —

Summary

The Admin MCP server shipped in 6.0 authenticates with a secret API key in a header. That reaches agents whose configuration a developer edits — Claude Code, Cursor, VS Code, in-house clients — and no further. It does not reach a merchant, because every path that carries a header to a consumer client requires either a terminal or a vendor's proprietary feature. Hosted connectors (claude.ai, ChatGPT) connect from the vendor's own servers and offer exactly one interoperable way in: OAuth 2.1.

This plan makes Spree an OAuth 2.1 authorization server for its own MCP endpoint, so a merchant connects their store by entering its URL, signing in with the admin account they already have, and approving a consent screen. The tool catalog, scope filtering, store scoping and credential filtering from 6.0-mcp-server are unchanged — this is a second front door onto the same house.

Without it the MCP server reaches only agents whose configuration a developer edits, and for those the spree api CLI is the better tool — so a key-only MCP server is a second surface that loses to one Spree already has. Consumer reach is what makes it a feature rather than a duplicate.

Key Decisions (do not deviate without discussion)

  • Spree is the authorization server, not a client of one. This is the opposite direction from 6.0-platform-auth phase 7, which makes Spree an OIDC client so staff can sign in through Okta. The two share a spec family and almost no code; neither one provides the other. A merchant on a self-hosted store has no external IdP to delegate to, and requiring one would exclude the installations this feature exists for.
  • Doorkeeper, not hand-rolled. It carries the parts that are dangerous to get wrong — PKCE S256, token issuance and rotation, grant replay detection, public-client hardening — and as of 6.0 it also carries RFC 8707 resource indicators and the port-agnostic loopback redirect that Claude Code needs. What it does not carry is the discovery layer, which is small and declarative.
  • One credential per surface: MCP takes a grant, the CLI takes a key. An agent acts for a person, so it carries that person's own permissions — a standing key names neither who approved the agent nor what they allowed, and survives the staff member who created it. Scripted callers that cannot sign in are served by the spree api CLI, which is what API keys are for. The MCP endpoint refuses a key outright rather than accepting a second, weaker way in.
  • No dynamic client registration. The 2026-07-28 revision deprecated RFC 7591 in favour of client ID metadata documents, Doorkeeper never supported DCR, and neither claude.ai nor ChatGPT requires it. Pre-registered client IDs are first in the spec's own priority order and carry the first release; CIMD follows when Doorkeeper's support lands.
  • A grant binds to a real admin user, never to a store. The token's authority is that user's own permissions at call time, so revoking the user or narrowing their role takes effect on the next request without touching the grant. This is what the API key path cannot do: one key is one blob of scopes with no person behind it.
  • A 401 is reserved for an authorization challenge. The endpoint already answers a missing or unrecognised credential with 401, which is what an OAuth-capable client needs in order to begin discovery, and a permission refusal is already not an HTTP error at all — it comes back as { error: '…' } in a 200 JSON-RPC response, which is where MCP puts tool-level failures and what lets a model correct itself. That split is what OAuth depends on, so it has to survive: a 401 on a request that authenticated would send a consumer client into a re-authorization loop.

Design Details

What the spec actually requires

Authorization is OPTIONAL in the 2026-07-28 revision — the header-only server from 6.0 is already compliant. The MUSTs bind only once authorization is implemented, and partial implementation is worse than none, so the three that matter are non-negotiable from the first release:

Requirement Who provides it
RFC 9728 Protected Resource Metadata at /.well-known/oauth-protected-resource Hand-written. resource must equal the URL the merchant typed, path included
RFC 8414 Authorization Server Metadata, advertising code_challenge_methods_supported: ["S256"] Hand-written, or OIDC discovery via doorkeeper-openid_connect
PKCE S256, and RFC 8707 audience-restricted tokens Doorkeeper 6.0

Omitting code_challenge_methods_supported makes compliant clients refuse to connect at all, which fails closed in a way that is hard to read from the client side — so the metadata documents get their own request specs asserting each required field, not just a 200.

The challenge

The MCP endpoint's existing 401 grows a header naming where to discover the rest:

WWW-Authenticate: Bearer resource_metadata="https://store.example.com/.well-known/oauth-protected-resource", scope="read_products write_orders"

scope is a SHOULD added in this revision and worth including: it lets a client ask for the right authority up front rather than discovering the gap after consent.

Consent

The screen a merchant approves is the only part of this a person reads, so it names what it is granting in the permission catalog's own words rather than raw scope strings — "Read products", "Cancel and refund orders" — and it names the client. A grant is listed in the dashboard beside API keys, with the same revoke affordance, because a merchant who wants to disconnect an agent should not have to learn which of two mechanisms it used.

Audience validation

Every token is checked to have been issued for this resource. This is the defence against a token minted for another service being replayed here, and Doorkeeper's resource-indicator support supplies the aud to check — but the check itself lives in our endpoint and gets its own spec, because a missing audience check is invisible until it is exploited.

One audience for the whole Admin API, including MCP. The endpoint was first given an audience of its own, which could never work: a tool call reaches data by dispatching into the Admin API carrying the same token, so two audiences on one credential meant one of the two checks always refused it. The scopes are the ceiling a merchant approves, and the MCP endpoint offers nothing the Admin API does not — the path a token names adds no protection worth a surface that cannot authenticate itself.

Migration Path

  1. Doorkeeper in, migrations, no endpoints yet. The gem, its tables, and the configuration: PKCE forced, public clients, resource indicators enabled, loopback redirects port-agnostic.
  2. Discovery documents. RFC 9728 and RFC 8414, with the resource derived from the request so a store on a custom domain answers correctly.
  3. The challenge and audience validation on the MCP endpoint. At this point Claude Code can complete a flow, which makes the whole path testable before any consumer client is involved.
  4. Consent screen and dashboard management. Grants listed and revocable beside API keys.
  5. Pre-registered client IDs for claude.ai and ChatGPT, with their documented callback URLs, and the docs page rewritten around "enter your store URL and sign in".
  6. CIMD, when Doorkeeper's support ships. Until then step 5 carries the consumer clients.

Steps 1–3 are the substance; a merchant sees nothing until 4 and 5.

Constraints on Current Work

  • Do not add a key path back to MCP. A second credential would be a way to bypass consent: the grant's scopes and the approving person are the whole of what makes an agent's authority accountable.
  • Do not widen the 401. It means "no usable credential", nothing else. Permission refusals stay as { error: '…' } inside a 200, where MCP puts tool-level failures; promoting one to an HTTP status would send consumer clients into a re-authorization loop.
  • Do not add a second permission model. A grant's authority is the admin user's own, resolved at call time through the same ability the dashboard uses. Scopes on a grant narrow that authority; they never extend it.
  • The tool catalog is untouched. Spree.admin_agent_tools, the resource map and the credential filter are shared by both authentication paths and must not learn which one a request used.

Open Questions

  • Client ID metadata documents. Deferred: Doorkeeper's support is an open pull request, and pre-registered client IDs are first in the spec's own priority order, so the consumer clients work without it. A merchant registers one by picking it in the connect sheet, which fills in the callback for the clients we know. Revisit when CIMD lands — it is what would remove the registration step altogether.
  • One authorization server per installation, or per store? Settled as per-store resource identifiers over one installation-wide token store: the identifier must match the URL the merchant typed, and Spree::Api::Oauth.register_resource derives it from the store. A multi-store installation therefore serves one metadata document per store per protected resource.

References