Index of every page on this site: llms.txt
All skills
obie avatar

/mcp-oauth-setup

@7372ca8
by Obie Fernandezobie/skills101 stars
6
Run it yourself

Implement MCP server authentication with OAuth Dynamic Client Registration (RFC 7591), Authorization Server Metadata Discovery (RFC 8414), and generalized per-agent credential support. This skill should be used when building admin UIs that let users connect to third-party MCP servers, whether they use OAuth (Linear, Sentry, Granola), bearer tokens (Render, custom APIs), or API keys. Covers the full flow: metadata discovery, client registration, PKCE authorization, token exchange, token refresh, tool sync, and credential storage patterns (shared vs per-agent for any auth type). Includes hard-won lessons from production implementation.

Use this Skill: https://skilld.dev/gh/obie/skills/mcp-oauth-setup

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesschema.md

≈1.6k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Database Schema for MCP Authentication

MCP Servers Table

The primary table stores MCP server configuration, OAuth metadata from discovery, dynamically registered client credentials, and shared tokens.

Migration

class CreateMcpServers < ActiveRecord::Migration
  def change
    create_table :mcp_servers do |t|
      t.string :name, null: false
      t.string :url, null: false
      t.string :auth_type, null: false, default: "none"
      t.string :auth_token                  # Bearer token or API key (shared mode)
      t.string :auth_header_name            # Custom header name for API key auth
      t.jsonb :custom_headers, default: {}
      t.jsonb :discovered_tools, default: []
      t.datetime :tools_last_synced_at

      # Credential mode: applies to ALL auth types, not just OAuth
      t.string :credential_mode, default: "shared"  # "shared" or "per_agent"

      # OAuth fields (populated by discovery + registration)
      t.string :oauth_authorization_url     # From RFC 8414 discovery
      t.string :oauth_token_url             # From RFC 8414 discovery
      t.string :oauth_client_id             # From RFC 7591 registration
      t.string :oauth_client_secret         # From RFC 7591 registration
      t.string :oauth_scopes                # Space-separated, from discovery
      t.string :oauth_access_token          # Shared token (when mode = shared)
      t.string :oauth_refresh_token         # Shared refresh token
      t.datetime :oauth_token_expires_at    # Shared token expiry

      t.timestamps
    end

    add_index :mcp_servers, :name, unique: true
  end
end

Agent MCP Connections Table (Per-Agent Credentials)

When credential_mode is "per_agent", each agent stores its own credential in this join table. The access_token field stores OAuth tokens, bearer tokens, or API keys depending on the connector's auth_type.

class CreateAgentMcpConnections < ActiveRecord::Migration
  def change
    create_table :agent_mcp_connections do |t|
      t.references :agent, null: false, foreign_key: true
      t.references :mcp_server, null: false, foreign_key: true
      t.string :access_token          # Per-agent credential (any auth type)
      t.string :refresh_token         # OAuth-specific
      t.datetime :token_expires_at    # OAuth-specific
      t.timestamps
    end

    add_index :agent_mcp_connections,
              [:agent_id, :mcp_server_id],
              unique: true
  end
end

Model: McpServer

class McpServer < ApplicationRecord
  # Encrypt all secrets at rest
  encrypts :auth_token
  encrypts :oauth_client_id
  encrypts :oauth_client_secret
  encrypts :oauth_access_token
  encrypts :oauth_refresh_token

  enum :auth_type, {
    no_auth: "none",
    bearer: "bearer",
    api_key_header: "api_key_header",
    oauth: "oauth"
  }

  has_many :agent_mcp_connections, dependent: :destroy

  validates :name, presence: true, uniqueness: true
  validates :url, presence: true,
            format: { with: /\Ahttps?:\/\/.+/i, message: "must be an HTTP(S) URL" }
  validates :auth_header_name, presence: true, if: :api_key_header?
  # credential_mode validates for ALL auth types, not just OAuth
  validates :credential_mode,
            inclusion: { in: %w[shared per_agent] }, if: :auth_configured?

  # General credential mode helpers (auth-type-agnostic)
  def shared_credentials?
    credential_mode == "shared"
  end

  def per_agent_credentials?
    credential_mode == "per_agent"
  end

  # OAuth-specific wrappers (check auth_type too)
  def oauth_shared?
    oauth? && shared_credentials?
  end

  def oauth_per_agent?
    oauth? && per_agent_credentials?
  end

  def oauth_connected?
    oauth? && oauth_access_token.present?
  end

  def auth_configured?
    !no_auth?
  end

  # Generate MCP tool names in the mcp__<server_key>__<tool> format
  def tool_names
    (discovered_tools || []).map { |tool| "mcp__#{server_key}__#{tool}" }
  end

  def server_key
    name.downcase.gsub(/[^a-z0-9]/, "_")
  end

  # Build the SDK config hash for Claude CLI's .mcp.json
  def to_sdk_config(agent: nil)
    config = { "type" => "http", "url" => url }
    headers = (custom_headers || {}).dup

    case auth_type
    when "bearer"
      token = resolve_auth_token(agent)
      headers["Authorization"] = "Bearer #{token}" if token.present?
    when "api_key_header"
      token = resolve_auth_token(agent)
      headers[auth_header_name] = token if token.present?
    when "oauth"
      token = resolve_oauth_token(agent)
      headers["Authorization"] = "Bearer #{token}" if token
    end

    config["headers"] = headers if headers.any?
    config
  end

  private

  # Resolve bearer/API key token — checks per-agent first, falls back to shared
  def resolve_auth_token(agent)
    if per_agent_credentials? && agent
      connection = agent_mcp_connections.find_by(agent: agent)
      connection&.access_token
    else
      resolve_credential(auth_token) if auth_token.present?
    end
  end
end

Model: AgentMcpConnection

class AgentMcpConnection < ApplicationRecord
  encrypts :access_token
  encrypts :refresh_token

  belongs_to :agent
  belongs_to :mcp_server

  validates :agent_id, uniqueness: { scope: :mcp_server_id }

  def connected?
    access_token.present?
  end

  def token_expiring_soon?
    return false unless token_expires_at
    token_expires_at < 5.minutes.from_now
  end

  # Only needed for OAuth per-agent tokens (bearer/API key tokens don't expire)
  def ensure_token_fresh!(mcp_server)
    return unless token_expiring_soon? && refresh_token.present?

    response = Faraday.post(mcp_server.oauth_token_url) do |req|
      req.headers["Content-Type"] = "application/x-www-form-urlencoded"
      req.body = URI.encode_www_form(
        grant_type: "refresh_token",
        refresh_token: refresh_token,
        client_id: mcp_server.oauth_client_id,
        client_secret: mcp_server.oauth_client_secret
      )
    end

    data = JSON.parse(response.body)
    raise "Token refresh failed" unless data["access_token"]

    with_lock do
      update!(
        access_token: data["access_token"],
        refresh_token: data["refresh_token"] || refresh_token,
        token_expires_at: data["expires_in"] ? Time.current + data["expires_in"].to_i.seconds : nil
      )
    end
  end
end

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

2 warnings9mo4 checks · Risk SAFE
  • Gen Agent Trust Hub9mo

    The skill consists of reference documentation and Ruby code snippets for implementing the Model Context Protocol (MCP) authentication and tool synchronization. It follows security best practices by encrypting credentials at rest and contains no malicious code or hardcoded secrets.

  • Socket9mo

    No alerts

  • Snyk9mo

    Risk: MEDIUM · No issues

  • Runlayer9mo

    5/5 files flagged

Signed by skilld at 7372ca8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 10 months ago

README badge

README badge for obie/skills/mcp-oauth-setup