= MCP (Model Context Protocol)

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.

https://modelcontextprotocol.io/[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 xref:usage/server.adoc[Server Mode] and xref:usage/lsp.adoc[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`:

[source,ruby]
----
gem 'mcp', '~> 0.6'
----

Then run:

[source,console]
----
$ bundle install
----

If you do not use Bundler:

[source,console]
----
$ gem install mcp
----

== Available Tools

The MCP server exposes two tools:

`rubocop_inspection`::
Inspect Ruby code for offenses. Accepts a `path` to check files on disk or `source_code` to check inline code. Returns detected offenses as JSON.

`rubocop_autocorrection`::
Autocorrect RuboCop offenses in Ruby code. Accepts `path` or `source_code` like the inspection tool, plus a `safety` boolean (defaults to `true`). When `safety` is `true`, only safe corrections are applied; set it to `false` to include unsafe corrections.

== Client Configuration

For a list of tools that support MCP, see https://modelcontextprotocol.io/clients[MCP Clients].

=== Claude Code

[source,console]
----
$ 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.

[source,json]
----
{
  "mcpServers": {
    "rubocop": {
      "type": "stdio",
      "command": "bundle",
      "args": [
        "exec",
        "rubocop",
        "--mcp"
      ],
      "cwd": "/path/to/your/project"
    }
  }
}
----

NOTE: `rubocop --mcp` starts the MCP server and is intended to be invoked by an MCP client, not run manually by users.
