Sponsored by Deepsite.site

Rails AI Context — Zero-config MCP server for Rails apps

Created By
crisnahine4 days ago
Automatically introspects your Rails application and exposes schema, models, routes, controllers, views, auth, tests, config, and conventions to AI assistants via MCP. 9 read-only tools, zero configuration. Works with Claude Code, Cursor, Windsurf, and GitHub Copilot.
Overview

rails-ai-context

Turn any Rails app into an AI-ready codebase — one gem install.

Gem Version MCP Registry CI License: MIT

Built by a Rails dev who got tired of burning tokens explaining his app to AI assistants every single session.


The Problem

You open Claude Code, Cursor, or Copilot and ask: "Add a draft status to posts with a scheduled publish date."

The AI doesn't know your schema, your Devise setup, your Sidekiq jobs, or that Post already has an enum :status. It generates generic code that doesn't match your app.

rails-ai-context fixes this. It auto-introspects your entire Rails app and feeds everything to your AI assistant — schema, models, routes, controllers, jobs, gems, auth, API, tests, config, and conventions — through the Model Context Protocol (MCP).


Proof: 37% Token Savings (Real Benchmark)

Same task — "Add status and date range filters to the Cooks index page" — 4 scenarios in parallel, same Rails app:

SetupTokensSavedWhat it knows
rails-ai-context (full)28,83437%13 MCP tools + generated docs + rules
rails-ai-context CLAUDE.md only33,10627%Generated docs + rules, no MCP tools
Normal Claude /init40,70011%Generic CLAUDE.md only
No rails-ai-context at all45,477baselineNothing — discovers everything from scratch
No rails-ai-context          45,477 tk  █████████████████████████████████████████████
Normal Claude /init           40,700 tk  █████████████████████████████████████████     -11%
rails-ai-context CLAUDE.md    33,106 tk  █████████████████████████████████             -27%
rails-ai-context (full)       28,834 tk  █████████████████████████████                 -37%

https://github.com/user-attachments/assets/14476243-1210-4e62-9dc5-9d4aa9caef7e

What each layer gives you:

Normal /initrails-ai-context CLAUDE.mdrails-ai-context full
Knows it's Rails + TailwindYesYesYes
Knows model names, columns, associationsNoYesYes
Knows controller actions, filtersNoYesYes
Discovery overhead~8 calls0 calls0 calls
Structured MCP queriesNoNoYes — 5 MCP calls replace file reads

~16,600 fewer tokens per task vs no gem at all.

This was a simple task on a small 5-model app. Real-world tasks are 3-10x more complex. A feature touching auth + payments + mailers + tests on a 50-model app? Without the gem, Claude reads db/schema.rb (2,000+ lines), every model file, every controller, every view — easily 200K+ tokens per session. With rails-ai-context, MCP tools return only what's needed: rails_get_schema(table:"users") returns 25 lines instead of 2,000. The bigger your app and the harder the task, the more you save.

App sizeWithout gemWith rails-ai-contextSavings
Small (5 models)45K tokens29K tokens37%
Medium (30 models)~150K tokens~60K tokens~60%
Large (100+ models)~500K+ tokens~100K tokens~80%

Medium/large estimates based on schema.rb scaling (40 lines/table), model file scaling, and MCP summary-first workflow eliminating full-file reads.


Quick Start

bundle add rails-ai-context
rails generate rails_ai_context:install
rails ai:context

That's it. Three commands. Your AI assistant now understands your entire Rails app.

The install generator creates .mcp.json for auto-discovery — Claude Code and Cursor detect it automatically. No manual MCP config needed.

Full Guide — complete documentation with every command, parameter, and configuration option.


How It Saves Tokens

Token Comparison

  • /init saves 11% — knows the framework but wastes tokens discovering models and tables
  • CLAUDE.md saves 27% — complete Rails-specific map, zero discovery overhead
  • Full MCP saves 37% — structured queries replace expensive full-file reads
  • MCP tools return detail:"summary" first (~55 tokens), then drill into specifics
  • Split rule files only activate in relevant directories

13 Live MCP Tools

The gem exposes 13 read-only tools via MCP that AI clients call on-demand:

ToolWhat it returns
rails_get_schemaTables, columns, indexes, foreign keys
rails_get_model_detailsAssociations, validations, scopes, enums, callbacks
rails_get_routesHTTP verbs, paths, controller actions
rails_get_controllersActions, filters, strong params, concerns
rails_get_configCache, session, timezone, middleware, initializers
rails_get_test_infoTest framework, factories, CI config, coverage
rails_get_gemsNotable gems categorized by function
rails_get_conventionsArchitecture patterns, directory structure
rails_search_codeRipgrep-powered regex search across the codebase
rails_get_viewView templates, partials, Stimulus references
rails_get_stimulusStimulus controllers — targets, values, actions, outlets
rails_get_edit_contextSurgical edit helper — returns code around a match with line numbers
rails_validateBatch syntax validation for Ruby, ERB, and JavaScript files

Smart Detail Levels

Schema, routes, models, and controllers tools support a detail parameter — critical for large apps:

LevelReturnsDefault limit
summaryNames + counts50
standardNames + key details (default)15
fullEverything (indexes, FKs, constraints)5
rails_get_schema(detail: "summary")           # → all tables with column counts
rails_get_schema(table: "users")              # → full detail for one table
rails_get_routes(controller: "users")         # → routes for one controller
rails_get_model_details(model: "User")        # → associations, validations, scopes

A safety net (max_tool_response_chars, default 120K) truncates oversized responses with hints to use filters.


What Gets Generated

rails ai:context generates context files tailored to each AI assistant:

your-rails-app/
├── 🟣 Claude Code
│   ├── CLAUDE.md                                         ≤150 lines (compact)
│   └── .claude/rules/
│       ├── rails-context.md                              app overview
│       ├── rails-schema.md                               table listing + column types
│       ├── rails-models.md                               model listing
│       ├── rails-ui-patterns.md                          CSS/Tailwind component patterns
│       └── rails-mcp-tools.md                            full tool reference
├── 🟢 Cursor
│   └── .cursor/rules/
│       ├── rails-project.mdc                             alwaysApply: true
│       ├── rails-models.mdc                              globs: app/models/**
│       ├── rails-controllers.mdc                         globs: app/controllers/**
│       ├── rails-ui-patterns.mdc                         globs: app/views/**
│       └── rails-mcp-tools.mdc                           alwaysApply: true
├── ⚡ OpenCode
│   ├── AGENTS.md                                        native OpenCode context
│   ├── app/models/AGENTS.md                             auto-loaded when editing models
│   └── app/controllers/AGENTS.md                        auto-loaded when editing controllers
├── 🔵 Windsurf
│   ├── .windsurfrules                                    ≤5,800 chars (6K limit)
│   └── .windsurf/rules/
│       ├── rails-context.md                              project overview
│       ├── rails-ui-patterns.md                          CSS component patterns
│       └── rails-mcp-tools.md                            tool reference
├── 🟠 GitHub Copilot
│   ├── .github/copilot-instructions.md                   ≤500 lines (compact)
│   └── .github/instructions/
│       ├── rails-context.instructions.md                 applyTo: **/*
│       ├── rails-models.instructions.md                  applyTo: app/models/**
│       ├── rails-controllers.instructions.md             applyTo: app/controllers/**
│       ├── rails-ui-patterns.instructions.md             applyTo: app/views/**
│       └── rails-mcp-tools.instructions.md               applyTo: **/*
├── 📋 .ai-context.json                                   full JSON (programmatic)
└── .mcp.json                                             MCP auto-discovery

Root files (CLAUDE.md, AGENTS.md, etc.) use section markers — your custom content outside the markers is preserved on re-generation. Set config.generate_root_files = false to only generate split rules.


What Your AI Learns

CategoryWhat's introspected
DatabaseEvery table, column, index, foreign key, and migration
ModelsAssociations, validations, scopes, enums, callbacks, concerns, macros
RoutingEvery route with HTTP verbs, paths, controller actions, API namespaces
ControllersActions, filters, strong params, concerns, API controllers
ViewsLayouts, templates, partials, helpers, template engines, view components
FrontendStimulus controllers (targets, values, actions, outlets), Turbo Frames/Streams
BackgroundActiveJob classes, mailers, Action Cable channels
Gems70+ notable gems categorized (Devise = auth, Sidekiq = jobs, Pundit = authorization)
AuthDevise modules, Pundit policies, CanCanCan, has_secure_password, CORS, CSP
APISerializers, GraphQL, versioning, rate limiting, API-only mode
TestingFramework, factories/fixtures, CI config, coverage, system tests
ConfigCache store, session store, middleware, initializers, timezone
DevOpsPuma, Procfile, Docker, deployment tools, asset pipeline
ArchitectureService objects, STI, polymorphism, state machines, multi-tenancy, engines

29 introspectors total. The :standard preset runs 13 core ones by default; use :full for 28 (database_stats is opt-in, PostgreSQL only).


MCP Server Setup

The install generator creates .mcp.jsonClaude Code and Cursor auto-detect it. No manual config needed.

This server is also listed on the official MCP Registry as io.github.crisnahine/rails-ai-context.

To start manually: rails ai:serve

Claude Desktop setup

Add to ~/Library/Application Support/Claude/claude_desktop_config.json (macOS):

{
  "mcpServers": {
    "rails-ai-context": {
      "command": "bundle",
      "args": ["exec", "rails", "ai:serve"],
      "cwd": "/path/to/your/rails/app"
    }
  }
}
HTTP transport (for remote clients)
rails ai:serve_http  # Starts at http://127.0.0.1:6029/mcp

Or auto-mount inside your Rails app:

RailsAiContext.configure do |config|
  config.auto_mount = true
  config.http_path  = "/mcp"
end

Configuration

# config/initializers/rails_ai_context.rb
RailsAiContext.configure do |config|
  # Presets: :standard (13 introspectors, default) or :full (all 28)
  config.preset = :standard

  # Cherry-pick on top of a preset
  # config.introspectors += %i[views turbo auth api]

  # Context mode: :compact (≤150 lines, default) or :full (dump everything)
  # config.context_mode = :compact

  # Exclude models from introspection
  config.excluded_models += %w[AdminUser InternalAuditLog]

  # Exclude paths from code search
  config.excluded_paths += %w[vendor/bundle]

  # Cache TTL for MCP tool responses (seconds)
  config.cache_ttl = 30

  # Live reload: auto-invalidate MCP caches on file changes
  # :auto (default), true, or false
  # config.live_reload = :auto

  # Skip root files (CLAUDE.md, .windsurfrules, etc.) — only generate split rules
  # Lets you manage root files yourself while still getting .claude/rules/, .cursor/rules/, etc.
  # config.generate_root_files = false
end
All configuration options
OptionDefaultDescription
preset:standardIntrospector preset (:standard or :full)
introspectors13 coreArray of introspector symbols
context_mode:compact:compact (≤150 lines) or :full (dump everything)
claude_max_lines150Max lines for CLAUDE.md in compact mode
max_tool_response_chars120_000Safety cap for MCP tool responses
excluded_modelsinternal Rails modelsModels to skip during introspection
excluded_pathsnode_modules tmp log vendor .gitPaths excluded from code search
auto_mountfalseAuto-mount HTTP MCP endpoint
http_path"/mcp"HTTP endpoint path
http_port6029HTTP server port
cache_ttl30Cache TTL in seconds
live_reload:auto:auto, true, or false — MCP live reload
live_reload_debounce1.5Debounce interval in seconds
generate_root_filestrueGenerate root files (CLAUDE.md, etc.) — set false for split rules only

Commands

CommandDescription
rails ai:contextGenerate all context files (skips unchanged)
rails ai:context:fullGenerate all files in full mode (dumps everything)
rails ai:context:claudeGenerate Claude Code files only
rails ai:context:opencodeGenerate OpenCode files only
rails ai:context:cursorGenerate Cursor files only
rails ai:context:windsurfGenerate Windsurf files only
rails ai:context:copilotGenerate Copilot files only
rails ai:serveStart MCP server (stdio)
rails ai:serve_httpStart MCP server (HTTP)
rails ai:doctorRun diagnostics and AI readiness score (0-100)
rails ai:watchAuto-regenerate context files on code changes
rails ai:inspectPrint introspection summary to stdout

Standalone CLI

The gem also ships a rails-ai-context executable — an alternative to rake tasks.

CommandEquivalent rake task
rails-ai-context serverails ai:serve
rails-ai-context serve --transport httprails ai:serve_http
rails-ai-context contextrails ai:context
rails-ai-context context --format clauderails ai:context:claude
rails-ai-context doctorrails ai:doctor
rails-ai-context watchrails ai:watch
rails-ai-context inspectrails ai:inspect
rails-ai-context version

Run from your Rails app root. Use rails-ai-context help for all options.


Stack Compatibility

Works with every Rails architecture — auto-detects what's relevant:

SetupCoverageNotes
Rails full-stack (ERB + Hotwire)29/29All introspectors relevant
Rails + Inertia.js (React/Vue)~22/29Views/Turbo partially useful, backend fully covered
Rails API + React/Next.js SPA~20/29Schema, models, routes, API, auth, jobs — all covered
Rails API + mobile app~20/29Same as SPA — backend introspection is identical
Rails engine (mountable gem)~15/29Core introspectors (schema, models, routes, gems) work

Frontend introspectors (views, Turbo, Stimulus, assets) degrade gracefully — they report nothing when those features aren't present.


Works Without a Database

The gem parses db/schema.rb as text when no database is connected. Works in CI, Docker build stages, and Claude Code sessions without a running DB.


Requirements

  • Ruby >= 3.2, Rails >= 7.1
  • mcp gem (installed automatically)
  • Optional: listen gem for watch mode, ripgrep for fast code search

vs. Other Ruby MCP Projects

ProjectApproachrails-ai-context
Official Ruby SDKLow-level protocol libraryWe use this as our foundation
fast-mcpGeneric MCP frameworkWe're a product — zero-config Rails introspection
rails-mcp-serverManual config (projects.yml)We auto-discover everything

Contributing

git clone https://github.com/crisnahine/rails-ai-context.git
cd rails-ai-context && bundle install
bundle exec rspec       # 491 examples
bundle exec rubocop     # Lint

Bug reports and pull requests welcome at github.com/crisnahine/rails-ai-context.

Sponsorship

If rails-ai-context helps your workflow, consider becoming a sponsor.

License

MIT

Server Config

{
  "mcpServers": {
    "rails-ai-context": {
      "command": "bundle",
      "args": [
        "exec",
        "rails",
        "ai:serve"
      ]
    }
  }
}
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
WindsurfThe new purpose-built IDE to harness magic
CursorThe AI Code Editor
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
RedisA Model Context Protocol server that provides access to Redis databases. This server enables LLMs to interact with Redis key-value stores through a set of standardized tools.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Tavily Mcp
Serper MCP ServerA Serper MCP Server
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
ChatWiseThe second fastest AI chatbot™
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
DeepChatYour AI Partner on Desktop
Amap Maps高德地图官方 MCP Server
Y GuiA web-based graphical interface for AI chat interactions with support for multiple AI models and MCP (Model Context Protocol) servers.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Playwright McpPlaywright MCP server