Labsco
MCP SERVER

Rails PG Extras MCP

by pawurb

Ask an LLM why your Rails app's PostgreSQL is slow — table bloat, unused indexes, outlier queries, locks and cache hit rates — and optionally run EXPLAIN ANALYZE on the culprits.

Relational SQL Databases
Summary
Create the read-only role first; the SQL in the README is the setup step, not an appendix.

The metadata tools need nothing more than SELECT on a handful of `pg_stat_*` views and a few size functions, and the README spells out exactly which grants that is. Doing it that way removes the risk from the whole integration, which matters because the EXPLAIN ANALYZE tools — the ones that make the diagnosis genuinely useful — do let an LLM run arbitrary queries, transaction-wrapped or not.

What it is

An MCP interface over the `rails-pg-extras` gem, mounted into your own Rails application as a route. It exposes PostgreSQL's diagnostic metadata — the queries that tell you about bloat, index usage, slow statements, locks and connections — so an LLM can explore them and explain what it finds, instead of you running each query by hand and interpreting the output yourself.

What you get
  • PostgreSQL metadata and performance metrics as MCP tools: table bloat, slow queries, unused indexes and the rest of the pg-extras set
  • Bottleneck queries surfaced through the gem's `calls` and `outliers` reports
  • Optional EXPLAIN and EXPLAIN ANALYZE on those bottlenecks — `explain` and `explain_analyze`, each behind its own environment flag
  • The connected database user readable through the `connections` tool, so you can confirm which role the LLM is actually using
  • An HTTP SSE interface mounted at a path you choose in `config/routes.rb`, with an optional auth token
  • An execution context wrapped in a transaction and rolled back, so in theory data modification is not possible through the explain path
Requirements

A Rails application with PostgreSQL. Add the gem with `bundle add rails-pg-extras-mcp` and mount `RailsPgExtrasMcp::App.build` in `config/routes.rb`; `RailsPgExtrasMcp::App.build(opts)` passes options straight through to fast-mcp, and a production mount typically needs `allowed_origins`, `allowed_ips`, `auth_token` and `localhost_only: false`. Clients reach it through `mcp-remote` — `npm install -g mcp-remote` — pointed at the SSE path with an `Authorization` header. The EXPLAIN tools are off unless `PG_EXTRAS_MCP_EXPLAIN_ENABLED` and `PG_EXTRAS_MCP_EXPLAIN_ANALYZE_ENABLED` are set to `true`; enabling them means an LLM can run arbitrary queries against your database, so the README asks you to use a read-only role and offers `RAILS_PG_EXTRAS_MCP_DATABASE_URL` to override the default ActiveRecord connection. The project describes itself as early beta.

Setup effort

Build from source — clone the repository and build it, then point your client at the binary