MCP: least-privilege OAuth scopes + 2026-07-28 protocol support

BU
8d ago
+1264-60

Problem

Every client connecting to /mcp — Claude, ChatGPT, a custom agent, anything — got the same all-or-nothing access:

  • Dynamic Client Registration (POST /register) hardcoded every new OAuth client to the read_write scope, even though Doorkeeper already had a read scope configured (config/initializers/doorkeeper.rb) that nothing used.
  • McpController#authenticate_via_doorkeeper explicitly required read_write, so a read-scoped token was rejected outright.
  • There was no way to give an external assistant read-only access to financial data without also handing it every write-capable tool (update_transaction, update_budget, create_goal, …).
  • The protocol layer only spoke 2025-03-26/2025-06-18, with no support for 2026-07-28's stateless dialect (no initialize/session handshake, server/discover, resultType envelopes).

What changed

Reuses Doorkeeper's existing read/read_write scopes end to end rather than introducing a parallel authorization system:

  • OAuth scopes drive MCP access: read → MCP read-only, read_write → full access (historical behavior, unchanged). POST /register now defaults new clients to read (least privilege) and only grants read_write when a client explicitly asks for it; unknown scopes are rejected with invalid_client_metadata. The 201 response now echoes the granted scope (RFC 7591 §3.2.1), and requesting "read read_write" keeps both rather than collapsing to just read_write.
  • One allowlist, two enforcement points: Assistant::Function.read_only? (one line per tool class, defaulting to false) is the single source of truth consulted by both tools/list and tools/call — a write tool hidden from discovery is equally unreachable by its exact name, not just absent from a listing. All 19 read-only classes were read end to end before being marked; SearchFamilyFiles is deliberately excluded despite being non-mutating (it can surface whole uploaded documents, a broader exposure than the other read tools).
  • Static token gets the same knob: MCP_API_TOKEN (the existing self-hosted fallback) gains an optional MCP_API_TOKEN_SCOPE (read / read_write), defaulting to read_write so existing installs see no behavior change. An invalid value fails auth rather than silently granting full access, and now fails loud at boot too (config/initializers/mcp.rb) instead of surfacing only as confusing 401s.
  • Session privilege preservation: Mcp-Session-Id (legacy 2025 dialects only — 2026-07-28 is stateless) now stores {user_id, access_mode} instead of a bare id, so a session minted read-only can never become read-write by being reused with a different credential. Pre-existing cache entries are treated as the read-write they always were (fail-closed on the ambiguous case, not fail-open).
  • MCP 2026-07-28: server/discover (session-less capability probe), resultType: "complete" plus _meta.serverInfo on tools/list/tools/call, ttlMs/cacheScope on list results, Mcp-Method/Mcp-Name header validation, strict version resolution (a MCP-Protocol-Version header disagreeing with _meta.protocolVersion is a 400, not a silent pick). Notifications reply 202 Accepted. 2025-03-26 and 2025-06-18 are unchanged.
  • RFC 9728 metadata: the root well-known path reports resource: "<base>", the resource-scoped path (.../oauth-protected-resource/mcp) reports resource: "<base>/mcp" — each internally consistent with the identifier a client constructs that URL from (§3.3). WWW-Authenticate on a 401 points at the scoped document and advertises scope="read" so a client's first authorization request already asks for the minimum it needs.
  • Settings → MCP now shows each connection's access level (Read Only / Read/Write), not just its name and a revoke button.

Behavior changes worth knowing about

Two things this PR changes for existing installs, not just new ones — surfacing them here since there's no CHANGELOG in this repo to put them in:

  1. A pre-existing read-scoped OAuth token gains MCP access it didn't have before. /mcp used to require read_write and reject a read token outright; it now authenticates it with the read-only tool surface. Doorkeeper's default_scopes :read means such tokens can already exist in the wild for other purposes. The exposure is comparable to what /api/v1 already grants a read token, so it reads as acceptable to me — but it's a widening, in a PR framed entirely as narrowing, and worth a maintainer's own judgment call.
  2. Removing and re-adding an MCP connector silently downgrades it. Existing tokens are unaffected (their app row isn't touched), but the moment a user disconnects and reconnects Claude/ChatGPT, Dynamic Client Registration hands back a read-scoped client by default, and every write tool disappears with "Unknown tool" as the only symptom. Worth a heads-up in whatever release notes accompany this.

Validation

  • bin/rails test test/controllers/mcp_controller_test.rb test/controllers/oauth_metadata_controller_test.rb test/controllers/oauth_registration_controller_test.rb test/controllers/settings/mcp_controller_test.rb — 105 runs, 0 failures
  • bin/rails test (full suite) — 9678 runs, 0 failures (1 pre-existing, unrelated infra flake in RecurringTransaction::PipelineTest)
  • bin/rubocop, bin/brakeman, erb_lint — clean
  • Independent validation against the official MCP Inspector (v2 CLI, real TypeScript SDK client, not this repo's own test harness) over a live server: full initialize → notifications/initialized → tools/list → tools/call handshake, and the adversarial case — a real read-scoped OAuth token calling update_budget by its exact name gets "Tool not found", identical to a nonexistent tool, with no database mutation.

Test plan

  • Existing MCP/OAuth test suites pass
  • Full test suite passes (minus one pre-existing unrelated flake)
  • Rubocop / Brakeman / erb_lint clean
  • Manually verified against the official MCP Inspector against a live dev server
  • SearchFamilyFiles exclusion and "hide as unknown tool" (vs. 403 insufficient_scope) — reviewed independently by jjmata, who agreed with both for this specific case
  • Maintainer judgment call on the two behavior changes above (pre-existing read tokens gaining access; DCR reconnect silently downgrading scope)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added support for MCP protocol version 2026-07-28, including stateless discovery, metadata, and improved notification handling.
    • Added OAuth and static-token read/read_write scopes with least-privilege defaults and validation.
    • Added read-only access controls, tool filtering, and a global read-only configuration option.
    • Added resource-specific OAuth metadata discovery for MCP.
    • Expanded authentication, protocol-version, and session handling.
  • Documentation

    • Updated MCP hosting, deployment, configuration, and localization documentation for scopes, read-only mode, and the new protocol version.