Contents
- What it does
- Try it in your Rails app
- A closer look: the schema, the model, the inherited filters, the trace, the app that cannot boot, the six rules
- The tools
- How it works
- What it costs
- Questions you should ask first
- What it will not do
- Install options and configuration
- Troubleshooting
- Development
- Contributing and security
- Learn more
What it does
An assistant writing Rails code knows what an average Rails app looks like. Yours has its own columns,
associations, filters and test setup, and the assistant starts every session with none of that. It
writes a migration for a column that exists, calls user.posts when the association is user.articles,
scaffolds FactoryBot into a fixture suite, and misses the before_action a parent controller adds. You
catch it, fix it, re-prompt, and something next to it breaks.
rails-ai-context gives the assistant 35 tools that read the app itself, over MCP from the AI tool or
from the command line. Every answer is read from the app and says how sure it is, and most end with a
hint for the next call.
| When |
What the assistant gets |
A real line from an answer |
| It asks about a table |
Columns with types, nulls and defaults, the indexes, the model on the table |
index_users_on_email on (email) (unique) |
| Before it calls an association |
Associations with their options, validations including the implicit ones, enums, scopes, callbacks, concerns |
has_many articles dependent: destroy (foreign_key: :author_id, inverse_of: :author) |
| Before it writes an action |
The actions, every filter with where it was inherited from, strong parameters, the layout |
before authenticate_user! (from ApplicationController) (skipped on: index, show) |
| Before it writes a test |
The framework, where the factories or fixtures live, their traits, a template in the app's style |
Factories: spec/factories (3 files) |
| It traces a method |
The definition and source, what it calls, every caller grouped by file and kind, the tests |
app/services/article_publisher.rb (Service) · 11: return false unless @article.publishable? |
| When the app cannot boot |
The same tools, from config/routes.rb, the schema dump and the source, saying so |
[STATIC] Static mode (static mode requested with --no-boot). Serving static analysis; runtime-only data is marked [UNAVAILABLE]. |
Each line comes from the sample app in this repository, a blog with Devise, an admin
namespace, a JSON API, a Sidekiq job and a FactoryBot suite. The website records the whole answers
every time it is built, by running the gem at that commit: crisnahine.github.io/rails-ai-context.
The install writes the files each AI tool reads, so the assistant knows the tools exist and how to ask:
the MCP config, one root context file with six anti-hallucination rules, and rules that load when a
model, a controller or a migration is open. The tools are read-only, the SQL tool runs inside a
read-only transaction, and files that hold secrets are never served.
Try it in your Rails app
Open your AI tool at the root of your Rails app and paste this. It installs the gem, sets up the AI
tool you are in, checks the result and tells you the one step left.
Install the rails-ai-context gem in this Rails app so you can read its schema, models, routes and code through its tools instead of guessing.
1. Run `ruby -v` and `bundle exec rails -v`. rails-ai-context needs Ruby 3.1+ and Rails 7.0+. If either is older, stop and tell me.
2. Pick TOOL, the key for the AI tool you are: claude (Claude Code), cursor (Cursor), copilot (GitHub Copilot in VS Code), opencode (OpenCode), codex (Codex CLI). If you are another AI tool, stop and tell me to use the "Another MCP client" prompt instead.
3. Run `bundle add rails-ai-context --group development --version '>= 5.35.0'`. If the Gemfile already lists it, run `bundle update rails-ai-context --conservative` instead. Then run `bundle exec rails-ai-context version`: it must print 5.35.0 or newer, the first version with the flags used below. If it is older, the Gemfile pins it: change its rails-ai-context line to `gem "rails-ai-context", ">= 5.35.0", group: :development` and run `bundle update rails-ai-context --conservative`. If Bundler refuses (a frozen or deployment bundle), run `gem install rails-ai-context -v '>= 5.35.0'` instead and use `rails-ai-context init --tools TOOL --defaults` in step 4.
4. Run `bundle exec rails generate rails_ai_context:install --tools TOOL --defaults`. If it fails because the app can't boot, run `bundle exec rails-ai-context init --tools TOOL --defaults` instead; that one reads the source files when the app won't start.
5. Run `bundle exec rails-ai-context tool onboard` (or `rails-ai-context tool onboard` if you used gem install) and check it describes this app.
6. Tell me which files changed, then the "Next step" lines the install printed for TOOL, word for word. I have to do that step before your rails-ai-context MCP tools appear.
Rather type it yourself? In the Gemfile:
bundle add rails-ai-context --group development
bin/rails generate rails_ai_context:install --tools claude --defaults
Standalone, with no Gemfile change:
gem install rails-ai-context
rails-ai-context init --tools claude --defaults
As a Claude Code plugin, which adds the MCP server and gives each session the six rules, and writes no
context files. It runs the installed gem, so install that first, and run it in the app's folder:
gem install rails-ai-context
claude plugin install rails-ai-context --marketplace crisnahine/rails-ai-context --scope project
Commit the .claude/settings.json it writes; the install generator and init then leave Claude Code's
.mcp.json to the plugin (details).
--tools takes claude, cursor, copilot, opencode, codex or all. Without --defaults the
generator asks which AI tools you use and what to write. Re-running is safe: it keeps what you have,
adds what is missing, and in a CLAUDE.md you wrote it touches only the block between its
<!-- BEGIN/END rails-ai-context --> markers.
You need Ruby 3.1 or newer and Rails 7.0 to 8.1, on Linux, macOS or Windows. Optional:
brakeman for security_scan, listen for watch, ripgrep for a faster search_code.
Then one step in your AI tool
Every AI tool holds a new project MCP server back until you say yes. The install prints the step for
each tool you picked:
| Claude Code |
Cursor |
GitHub Copilot |
OpenCode |
Codex CLI |
New session in the app's folder, approve rails-ai-context. Check with /mcp |
Restart, turn it on under MCP in Customize |
Trust the workspace, send a chat message. Check with "MCP: List Servers". Copilot CLI: trust the folder, check with copilot mcp list |
Restart in the app's folder. Check with opencode mcp list |
Trust the project, start a new session. Check with /mcp |
Then ask something only your app knows, such as "what columns does the users table have?", and watch
it call rails_get_schema instead of guessing. Every other setup has a prompt: no
Gemfile change, CLI only, MCP only, a whole team, a folder of apps, nothing committed, MCP over HTTP,
Ruby in Docker, another MCP client.
A closer look
Every block below is the gem's own output for the sample app, read without booting it, trimmed: the
tier line and the report line that end every answer are cut, and each note names what else is. The
website shows each answer whole, recorded at the commit it was built from.
The schema, with the model on it
$ rails-ai-context --app-path site/sample_app tool schema --table users --no-boot
## Table: users
**Models:** User
**Primary key:** id
| Column | Type | Null | Default |
|--------|------|------|---------|
| id | bigint | **NO** | |
| email | string | **NO** | "" |
| encrypted_password | string | **NO** | "" |
| name | string | yes | |
| role | integer | **NO** | 0 |
| active | boolean | **NO** | true |
| api_token | string | yes | |
### Indexes
- `index_users_on_api_token` on (api_token) (unique)
- `index_users_on_email` on (email) (unique)
_Next: `rails_get_model_details(model:"User")` for associations, validations, scopes._
Trimmed to seven of its twelve columns and two of its three indexes. With the app booted, the columns
come from the database connection; here they come from db/schema.rb, and db/structure.sql or the
migrations would do.
Before it calls an association
$ rails-ai-context --app-path site/sample_app tool model_details --model Article --no-boot
# Article [STATIC]
**Table:** `articles`
**File:** `app/models/article.rb` (32 lines)
**Structure:** class definition(1-1) → includes(2-3) → associations(4-6) → macros(7-8) → validations(9-12) → callbacks(13-15) → scopes(16-18) → instance methods(19-22) → private(23-24) → instance methods(25-32)
## Associations
- `belongs_to` **author** (class: User) (inverse_of: :articles) (fk: author_id)
- `has_many` **comments** dependent: destroy
## Validations
- `presence` on author _(implicit from belongs_to)_
- `presence` on title
- `length` on title (maximum: 120)
- `presence` on slug
- `uniqueness` on slug
- `presence` on body (if: :published?)
## Enums
- `status`: draft(0), in_review(1), published(2) [integer]
## Scopes
- `recent` → order(published_at: :desc, created_at: :desc) [STATIC]
- `by_author` → where(author: user) [STATIC]
- `published` → where(status: :published).where.not(published_at: nil) [STATIC]
## Callbacks
- `before_validation`: :assign_slug (on: :create)
- `after_commit (on: :update)`: :notify_author (if: :saved_change_to_status?)
## Concerns
- **Publishable** - publishable?, publish!
The model is read with the Prism parser. A scope with a literal body is [VERIFIED], one built at
runtime is [INFERRED], and in the static tier every record is capped at [STATIC], since no record
can claim more than the tier that carries it. The columns, the primary key, the static-tier note on
associations, the instance methods and the next-call hint are trimmed out above. The published scope
is declared in the Publishable concern, and the Concerns section lists the methods each concern brings.
Before it writes an action
$ rails-ai-context --app-path site/sample_app tool controllers --controller admin/articles --no-boot
# Admin::ArticlesController
**Parent:** `Admin::BaseController`
**Layout:** `admin` (declared in Admin::BaseController)
## Actions
- `index`
- `publish`
- `show`
## Filters
- `before` **authenticate_user!** _(from ApplicationController)_
- `before` **require_admin** _(from Admin::BaseController)_
- `before` **set_article** (only: show, publish)
Every filter that applies, with where it was inherited from, so the assistant never misses the
authenticate_user! two classes up. The schema hints for the models the controller touches, and the
next-call hints, follow in the full answer.
Tracing a method
$ rails-ai-context --app-path site/sample_app tool search_code --pattern 'publishable?' --match_type trace --no-boot
# Trace: `publishable?`
## Definition
**app/models/concerns/publishable.rb:8** in `module Publishable`
```ruby
def publishable?
in_review? && body.present? && author.active?
end
```
## Called from (4 sites)
### app/controllers/admin/articles_controller.rb (Controller) → `POST /admin/articles/:id/publish`
13: if @article.publishable?
### app/models/concerns/publishable.rb (Model)
13: raise ArgumentError, "#{self.class.name} #{id} is not ready to publish" unless publishable?
### app/services/article_publisher.rb (Service)
11: return false unless @article.publishable?
### lib/tasks/articles.rake (Other)
5: PublishArticleJob.perform_later(article) if article.publishable?
## Tested by (1 reference)
- `spec/models/article_spec.rb` (1 reference)
One call in place of a file-by-file search: the definition, what it calls, its siblings, every caller
grouped by file and labelled by kind, with the route a controller caller answers, and the tests. The
"Calls internally" and "Sibling methods" sections are trimmed out above.
When the app cannot boot
The app-reading commands try a full boot for live reflection. When the boot fails, from a missing
environment variable, an unreachable service or a broken initializer, they answer from the static
tier instead of dying: routes from config/routes.rb, the schema from db/schema.rb,
db/structure.sql or the migrations, models and controllers from their source. Every answer carries
a tier line, a section that needs a booted app says [UNAVAILABLE] with the reason, and
--no-boot asks for that tier outright:
_[STATIC] Static mode (static mode requested with --no-boot). Serving static analysis; runtime-only data is marked [UNAVAILABLE]. Run `bundle exec rails-ai-context --app-path site/sample_app doctor` for details._
32 of the 40 introspectors answer here. Each declares which of three kinds it is: it reads files only,
it needs live reflection, or it answers from another source such as the schema dump. What a tier can
say is a declaration, so an answer never guesses at what it could not read
(ADR-0002). The routes engines and gems draw at
boot, and an association a gem adds at boot, are not listed, and the answer says so.
The six rules
The root context file carries six rules the assistant reads before writing code. That file is
AGENTS.md, which Cursor, OpenCode and Codex read and CLAUDE.md imports, or CLAUDE.md itself when
none of those three is chosen; Copilot reads its own copy in .github/copilot-instructions.md.
- Verify before you write. Never reference a column, association, route, helper, method, class, partial or gem that a tool call in this turn did not confirm.
- Mark every assumption with
[ASSUMPTION]. "I'd need to check X first" is a good answer.
- Training data describes average Rails. This app isn't average. When something feels obviously standard, query anyway.
- Check the inheritance chain before every edit: inherited filters, concerns, includes, STI parents.
- Empty tool output is information. "0 callers found" means investigate, or ask.
- Stale context lies. Re-query after writes.
On by default. Turn off with config.anti_hallucination_rules = false if you prefer your own. In an
MCP-only or CLI setup, the ground rules prompt gives
the assistant the same rules for one session.
The tools