From majestic-rails
Implements MCP server authentication using OAuth dynamic client registration (RFC 7591/8414), PKCE, bearer tokens, and API keys for admin UIs. Supports per-agent credentials, metadata discovery, token exchange, and tool sync for providers like Linear, Sentry.
How this skill is triggered — by the user, by Claude, or both
Slash command
/majestic-rails:mcp-oauth-setupThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Implement flexible authentication for MCP (Model Context Protocol) server connections.
Implement flexible authentication for MCP (Model Context Protocol) server connections. For OAuth providers, auto-discover endpoints and dynamically register as a client — the user just provides the MCP server URL and clicks "Connect." For bearer/API key providers, support both admin-shared and per-agent credentials so different agents can authenticate with different accounts.
The OAuth implementation relies on three RFCs:
.well-known/oauth-authorization-serverNot all MCP servers use OAuth. Some (e.g., Render) use bearer tokens with API keys and handle account/workspace selection at the MCP protocol level. The credential system must be auth-type-agnostic.
credential_mode applies to all auth types (bearer, api_key_header, oauth), not
just OAuth. Different agents may need their own credentials for the same MCP server.
credential_mode = "shared" → Admin provides one credential, all agents use it
credential_mode = "per_agent" → Each agent has its own credential
Admin clicks "Connect"
|
v
Discover OAuth metadata (RFC 8414)
| GET /.well-known/oauth-authorization-server
v
Register as OAuth client (RFC 7591)
| POST /oauth/register
v
Redirect to provider consent screen
| GET /oauth/authorize?client_id=...&code_challenge=...
v
Provider redirects back with code
| GET /callback?code=...&state=...
v
Exchange code for tokens
| POST /oauth/token
v
Store tokens, sync tools
Two tables: MCP server configuration (OAuth metadata + shared tokens) and per-agent credentials (any auth type).
See: references/schema.md
Key decisions:
encrypts :oauth_client_id, etc.)credential_mode ("shared" or "per_agent") applies to ALL auth typesdiscovered_tools as JSON arrayAgentMcpConnection.access_token stores OAuth tokens, bearer tokens, or API keysThree model methods on the MCP server record.
See: references/oauth_flow.md
discover_oauth_metadata!): Derive .well-known/oauth-authorization-server URL, parse JSON response, skip if already configured, handle 404 gracefully (not all servers support RFC 8414)register_oauth_client!): POST to registration endpoint, store client_id and client_secret, skip if already presentdiscover_and_register_oauth!): Run discovery then registration in sequenceCreate an OAuth controller with authorize and callback actions.
See: references/oauth_flow.md
Critical pitfalls:
Turbo Drive cross-origin redirects: redirect_to with an external URL is silently swallowed by Turbo Drive — browser stays on current page. Use HTML with <meta http-equiv="refresh" content="0;url=..."> for the external redirect instead.
State parameter: Use a signed, expiring message (Rails message_verifier) with connector ID, PKCE code verifier, optional agent ID, and timestamp. Set 10-minute expiry.
String keys from message verifier: After verifying the state token, payload uses string keys not symbol keys. Use payload["connector_id"], not payload[:connector_id].
PKCE (S256): Generate a random code_verifier, compute code_challenge as URL-safe Base64 of SHA-256 digest with no padding.
Error redirects: When agent_id is present in state, redirect errors to the agent edit page, not the connectors index.
Auto-sync on first agent connection: For per-agent OAuth, when the callback stores the first per-agent token, auto-sync tools using that agent's token if tools haven't been discovered yet.
resources :connectors do
member do
get "oauth/authorize", to: "mcp_oauth#authorize", as: :mcp_oauth_authorize
end
end
get "mcp_oauth/callback", to: "mcp_oauth#callback", as: :mcp_oauth_callback
Route helper naming: A member route mcp_oauth_authorize on resources :connectors generates mcp_oauth_authorize_connector_path(connector) — resource name comes last. Common source of NoMethodError.
See: references/oauth_flow.md (ensure_token_fresh! pattern)
token_expires_at < 5.minutes.from_now)with_lock for thread-safe updates on shared tokensSee: references/tool_sync.md
Two-step handshake:
initialize JSON-RPC request → get Mcp-Session-Id headertools/list with session ID headerCritical details:
Accept: application/json, text/event-stream — some servers return 406 without thissync_tools! must accept agent: parameter for per-agent authSee: references/ui_patterns.md
Connector form:
credential_mode radio applies to ALL auth typesauth_type AND credential_modeAgent edit form — three states for per-agent connectors:
| Provider | URL | Tools | Auth | Notes |
|---|---|---|---|---|
| Linear | https://mcp.linear.app/mcp | 45 | OAuth | SSE response format |
| Sentry | https://mcp.sentry.dev/mcp | 14 | OAuth | Standard JSON |
| Granola | https://mcp.granola.ai/mcp | 4 | OAuth | Standard JSON |
| Render | https://mcp.render.com/mcp | 24 | Bearer token | No OAuth, per-agent API keys |
| Symptom | Root Cause | Fix |
|---|---|---|
| Page stays on form, no redirect | Turbo Drive swallows cross-origin 302 | Use HTML meta refresh instead of redirect_to |
NoMethodError on route helper | Wrong helper name ordering | Member route generates mcp_oauth_authorize_connector_path |
payload[:connector_id] returns nil | Message verifier returns string keys | Use payload["connector_id"] |
| 406 from MCP server | Missing Accept header | Add Accept: application/json, text/event-stream |
| 400 "Mcp-Session-Id required" | Skipped initialize handshake | Send initialize first, use returned session ID |
| JSON parse error on tool sync | Server returns SSE format | Detect and parse both formats |
| Token exchange fails silently | Missing code_verifier | Include PKCE verifier from signed state |
| OAuth discovery 404 | Server doesn't use OAuth | Use bearer or API key auth instead |
| Per-agent connector shows no tools | Admin can't sync without token | Tools auto-sync on first agent connection |
| Error redirect goes to wrong page | agent_id not checked in rescue | Redirect to agent edit when agent_id present |
npx claudepluginhub majesticlabs-dev/majestic-marketplace --plugin majestic-railsImplements OAuth 2.0 Dynamic Client Registration (RFC 7591) for MCP clients, enabling automatic registration with authorization servers without manual setup.
Declares auth scopes, configures JWT/OAuth modes, and enforces multi-tenancy on tools and resources in `@cyanheads/mcp-ts-core` projects.
Manages Model Context Protocol (MCP) servers for Claude Code projects: installs/configures .mcp.json, OAuth remotes, runtime enable/disable, troubleshooting connections.