MCP: least-privilege OAuth scopes + 2026-07-28 protocol support
BU
8d ago+1264+1264+0123456789012345678901234567890123456789-60-60-01234567890123456789
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 theread_writescope, even though Doorkeeper already had areadscope configured (config/initializers/doorkeeper.rb) that nothing used. McpController#authenticate_via_doorkeeperexplicitly requiredread_write, so aread-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,resultTypeenvelopes).
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 /registernow defaults new clients toread(least privilege) and only grantsread_writewhen a client explicitly asks for it; unknown scopes are rejected withinvalid_client_metadata. The 201 response now echoes the grantedscope(RFC 7591 §3.2.1), and requesting"read read_write"keeps both rather than collapsing to justread_write. - One allowlist, two enforcement points:
Assistant::Function.read_only?(one line per tool class, defaulting tofalse) is the single source of truth consulted by bothtools/listandtools/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;SearchFamilyFilesis 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 optionalMCP_API_TOKEN_SCOPE(read/read_write), defaulting toread_writeso 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.serverInfoontools/list/tools/call,ttlMs/cacheScopeon list results,Mcp-Method/Mcp-Nameheader validation, strict version resolution (aMCP-Protocol-Versionheader disagreeing with_meta.protocolVersionis a 400, not a silent pick). Notifications reply202 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) reportsresource: "<base>/mcp"— each internally consistent with the identifier a client constructs that URL from (§3.3).WWW-Authenticateon a 401 points at the scoped document and advertisesscope="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:
- A pre-existing
read-scoped OAuth token gains MCP access it didn't have before./mcpused to requireread_writeand reject areadtoken outright; it now authenticates it with the read-only tool surface. Doorkeeper'sdefault_scopes :readmeans such tokens can already exist in the wild for other purposes. The exposure is comparable to what/api/v1already grants areadtoken, 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. - 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 failuresbin/rails test(full suite) — 9678 runs, 0 failures (1 pre-existing, unrelated infra flake inRecurringTransaction::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/callhandshake, and the adversarial case — a realread-scoped OAuth token callingupdate_budgetby 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
-
SearchFamilyFilesexclusion 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
readtokens 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_writescopes 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.