Skip to content

pawurb/rails-pg-extras-mcp

Repository files navigation

Rails PG Extras MCP Gem Version GH Actions

MCP (Model Context Protocol) interface for rails-pg-extras gem. Easily explore PostgreSQL performance and metadata. Check for table bloat, slow queries, unused indexes, and more. Run EXPLAIN ANALYZE on bottlenecks and get clear, LLM-powered insights to optimize your database.

LLM interface

Installation

bundle add rails-pg-extras
bundle add rails-pg-extras-mcp

The library supports MCP protocol via HTTP SSE interface.

config/routes.rb

mount RailsPgExtrasMcp::App.build, at: "pg-extras-mcp"

with optional authorization:

opts = { auth_token: "secret" }
mount RailsPgExtrasMcp::App.build(opts), at: "pg-extras-mcp"

Refer to the fast-mcp docs for a complete list of supported options (the opts hash is passed directly as-is). For real-world deployments, you'll likely need to configure the :allowed_origins setting.

Next, install mcp-remote:

npm install -g mcp-remote

and in your LLM of choice:

{
  "mcpServers": {
    "pg-extras": {
      "command": "npx",
      "args": [
        "mcp-remote",
        "http://localhost:3000/pg-extras-mcp/sse",
        "--header",
        "Authorization: secret"
      ]
    }
  }
}

You can now ask LLM questions about the metadata and performance metrics of your database.

Optional EXPLAIN ANALYZE support

calls and outliers methods return a list of bottleneck queries. LLM can get better insights into these queries by performing EXPLAIN and EXPLAIN ANALYZE analysis. MCP server exposes two optional methods for this purpose: explain and explain_analyze.

You can enable them by setting the following ENV variables:

ENV['PG_EXTRAS_MCP_EXPLAIN_ENABLED'] = 'true' ENV['PG_EXTRAS_MCP_EXPLAIN_ANALYZE_ENABLED'] = 'true'

Enabling these features means that an LLM, can run arbitrary queries in your database. The execution context is wrapped in a transaction and rolled back, so, in theory, any data modification should not be possible. But it's advised to configure a read-only permission if you want to use these features. By specifying ENV['RAILS_PG_EXTRAS_DATABASE_URL'] you can overwrite the default Rails ActiveRecord database connection to restrict an access scope.

Status

The project is in an early beta, so proceed with caution.

About

MCP (Model Context Protocol) interface for rails-pg-extras gem

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages