Labsco
GoodPie logo

Rails Active MCP

โ˜… 16

from GoodPie

A Ruby gem providing secure Rails console access through MCP for AI agents and development tools.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedFreeQuick setup

Gem Version

Note: This is just a personal project and while it works for the most part, I am still developing it and actively trying to make it a bit more useful for my uses.

Rails Active MCP

A Ruby gem that provides secure Rails console access through Model Context Protocol (MCP) for AI agents and development tools. Built using the official MCP Ruby SDK for professional protocol handling and future-proof compatibility.

Works with any MCP-compatible client, including Claude Desktop, Claude Code, VS Code (GitHub Copilot), Cursor, Windsurf, ChatGPT, Gemini CLI, Amazon Q Developer, JetBrains IDEs, Zed, Warp, Cline, and many more.

Features

  • ๐Ÿ”’ Safe Execution: Advanced safety checks prevent dangerous operations
  • ๐Ÿš€ Official MCP SDK: Built with the official MCP Ruby SDK for robust protocol handling
  • ๐Ÿ“Š Read-Only Queries: Safe database querying with automatic result limiting
  • ๐Ÿ” Code Analysis: Dry-run capabilities to analyze code before execution
  • ๐Ÿ“ Audit Logging: Complete execution logging for security and debugging
  • โš™๏ธ Configurable: Flexible configuration for different environments
  • ๐Ÿ›ก๏ธ Production Ready: Strict safety modes for production environments
  • โšก Professional Implementation: Built-in instrumentation, timing, and error handling

Available MCP Tools

The Rails Active MCP server provides four powerful tools that appear automatically in any connected MCP client:

1. console_execute

Execute Ruby code with safety checks and timeout protection:

  • Purpose: Run Rails console commands safely
  • Safety: Built-in dangerous operation detection
  • Timeout: Configurable execution timeout
  • Logging: All executions are logged for audit

Example prompt:

"Execute User.where(active: true).count"

2. model_info

Get detailed information about Rails models:

  • Schema Information: Column types, constraints, indexes
  • Associations: Has_many, belongs_to, has_one relationships
  • Validations: All model validations and rules
  • Methods: Available instance and class methods

Example prompt:

"Show me the User model structure"

3. safe_query

Execute safe, read-only database queries:

  • Read-Only: Only SELECT operations allowed
  • Safe Execution: Automatic query analysis
  • Result Limiting: Prevents large data dumps
  • Model Context: Works within your model definitions

Example prompt:

"Get the 10 most recent orders" "Count active users"

You can pass an optional where hash to filter records before invoking the method. For example, safe_query(model: "User", method: "count", where: { active: true }) runs User.where(active: true).count. The same applies to sum, average, minimum, maximum, pluck, and exists?.

4. dry_run

Analyze Ruby code for safety without executing:

  • Risk Assessment: Categorizes code by danger level
  • Safety Analysis: Identifies potential issues
  • Recommendations: Suggests safer alternatives
  • Zero Execution: Never runs the actual code

Example prompt:

"Analyze this code for safety: User.delete_all"

Safety Features

Automatic Detection of Dangerous Operations

The gem automatically detects and blocks:

  • Mass deletions (delete_all, destroy_all)
  • System commands (system, exec, backticks)
  • File operations (File.delete, FileUtils)
  • Raw SQL execution
  • Code evaluation (eval, send)
  • Process manipulation (exit, fork)

Safety Levels

  • Critical: Never allowed (system commands, file deletion)
  • High: Blocked in safe mode (mass deletions, eval)
  • Medium: Logged but allowed (raw SQL, update_all)
  • Low: Generally safe (environment access, require)

Read-Only Mode

The gem can detect read-only operations and provide additional safety:

# These are considered safe read-only operations
User.find(1)
User.where(active: true).count
Post.includes(:comments).limit(10)

Architecture

Built on Official MCP Ruby SDK

Rails Active MCP uses the official MCP Ruby SDK (mcp gem) for:

  • Professional Protocol Handling: Robust JSON-RPC 2.0 implementation
  • Built-in Instrumentation: Automatic timing and error reporting
  • Future-Proof: Automatic updates as MCP specification evolves
  • Standards Compliance: Full MCP protocol compatibility

Server Implementation

The server is implemented in lib/rails_active_mcp/sdk/server.rb and provides:

  • STDIO Transport: Compatible with all major MCP clients
  • Tool Registration: Automatic discovery of available tools
  • Error Handling: Comprehensive error reporting and recovery
  • Rails Integration: Deep integration with Rails applications

Tool Architecture

Each tool is implemented as a separate class in lib/rails_active_mcp/sdk/tools/:

  • ConsoleExecuteTool: Safe code execution
  • ModelInfoTool: Model introspection
  • SafeQueryTool: Read-only database access
  • DryRunTool: Code safety analysis

Error Handling

The gem provides specific error types:

  • RailsActiveMcp::SafetyError: Code failed safety checks
  • RailsActiveMcp::TimeoutError: Execution timed out
  • RailsActiveMcp::ExecutionError: General execution failure

All errors are properly reported through the MCP protocol with detailed messages.

Development and Testing

# Run tests
bundle exec rspec

# Test MCP server protocol compliance
./bin/test-mcp-output