|
Note
|
The built-in MCP server was introduced in RuboCop 1.85. |
|
Warning
|
This feature is experimental and should not be considered stable. Changes to its behavior or interface may occur. |
Model Context Protocol is an open-source standard for connecting AI applications to external systems.
This feature enables interactions through MCP clients that communicate with RuboCop via the Model Context Protocol.
Through MCP, RuboCop operations are initiated by an MCP client, with decisions made by an LLM.
The MCP server runs as a long-lived process over stdio, allowing an MCP client to request analysis and autocorrection without spawning a new RuboCop process for each request. This is based on the same principles as Server Mode and LSP.
Setup
The mcp gem is required but is not included as a dependency of RuboCop. You must install it separately.
If you use Bundler, add the following to your Gemfile:
gem 'mcp'
Then run:
$ bundle install
If you do not use Bundler:
$ gem install mcp
Available Tools
The MCP server exposes these tools:
rubocop_inspection-
Inspect Ruby code for offenses. Accepts a
pathto check files on disk orsource_codeto check inline code. Each offense is reported the way--format jsonreports it, with 1-based lines and columns and the edits its correction would make. Either way the result has the samefilesandsummary: inline code is reported as a single file, named afterpathwhen one is given and <code>example.rb</code> otherwise. An optionalmax_offenses_per_copcaps how many offenses each cop reports across the files inspected, as--max-offenses-per-copdoes, and the summary’sunreported_offensescounts what each capped cop left out. A file missing fromfilesunder a cap can still have offenses; they were just counted rather than listed. A cop that crashes on a file does not fail the request: the crash is listed in that file’serrors, counted in the summary’serror_count, and the other cops still report. Warnings a cop raises are listed the same way, underwarningsandwarning_count. rubocop_autocorrection-
Autocorrect RuboCop offenses in Ruby code. Accepts
pathorsource_codelike the inspection tool, plus a requiredsafetyboolean. Whensafetyistrue, only safe corrections are applied; set it tofalseto include unsafe corrections. Settingcontextualtotruealso applies the corrections held back while code is being edited, described below. Withdry_runset totruenothing is written: each file that would be corrected comes with thediffthat would be applied, and the offenses left over are located in the code as it would be after it.When correcting files, the result lists each file that was corrected or still has offenses, with the offenses left over in the inspection tool’s format, located in the corrected file. Those are what an agent has to fix itself, so it doesn’t need to inspect again. They come back after the files are written, so each cop lists at most five of them unless
max_offenses_per_copsays otherwise, and the summary’sunreported_offensescounts the rest. Files with nothing to report are only counted, and a file with nothing to correct isn’t written.A cop that crashes while correcting a file is listed in its
errorsand the other cops' corrections are still applied. A file whose corrections go back and forth between cops is left unchanged, with the loop listed in itserrorsand the offenses it still has. Inlinesource_codecomes back as plain corrected text, so a crash there fails the call instead and nothing is written. rubocop_explain-
Explain what cops do, the way
--explaindoes: the description, the bad and good examples from the cop’s own documentation, its configuration as the project resolves it, and whether it autocorrects. Takes acop_nameslist, plus an optionalpathto explain the configuration that applies to that file, which can differ from the project root’s. An agent can ask about the cops an inspection reported instead of guessing their intent from a one-line message.
An inspected offense tells an agent what autocorrection will do with it. correctable says whether rubocop_autocorrection fixes it at all, and <code>correction.safe</code> whether that takes <code>safety: false</code>. An offense can carry a correction without being correctable: cops with <code>AutoCorrect: contextual</code>, such as <code>Lint/UselessAssignment</code>, are not corrected while code is being edited, since the variable may be about to be used. Their edits are still reported, and an agent that has finished editing can have them applied by passing <code>contextual: true</code> to rubocop_autocorrection. Corrections covers what to watch for when applying edits directly.
Both rubocop_inspection and rubocop_autocorrection can narrow what they do, the way the matching command-line options do. only and except take lists of cop or department names, as --only and --except do, so an agent can check just Lint, or apply a single cop’s corrections. When checking files, changed sticks to the files git says changed, like --changed: true for changes since the last commit, or a revision to compare against, such as main.
Client Configuration
For a list of tools that support MCP, see MCP Clients.
Claude Code
$ claude mcp add rubocop -- bundle exec rubocop --mcp
JSON Configuration
Many MCP clients (e.g. VS Code, Cursor, Windsurf) accept a JSON configuration file. The exact file path varies by client — consult your client’s documentation.
{
"mcpServers": {
"rubocop": {
"type": "stdio",
"command": "bundle",
"args": [
"exec",
"rubocop",
"--mcp"
],
"cwd": "/path/to/your/project"
}
}
}
|
Note
|
<code>rubocop --mcp</code> starts the MCP server and is intended to be invoked by an MCP client, not run manually by users. |