ruby-console-mcp

ruby-console-mcp

Enables AI assistants to execute Ruby console commands (Rails console, IRB, etc.) with persistent session support for interacting with Ruby/Rails applications through natural language.

Category
访问服务器

README

Ruby Console MCP Server

A Model Context Protocol (MCP) server that provides access to Ruby console functionality for AI assistants. Execute Rails console, IRB, or Racksh commands, query models, and interact with your Ruby/Rails application through natural language with persistent session support.

Features

  • 🚀 Execute Rails console, IRB, or Racksh commands through MCP
  • 💾 Persistent session - variables and state are preserved between commands
  • ⚙️ Configurable console command (supports Rails, IRB, Racksh, or any Ruby REPL)
  • 🔌 Easy integration with MCP-compatible AI assistants (Claude, Cursor, etc.)
  • 📝 Clear error messages and helpful diagnostics
  • 🎯 Uses PTY for proper TTY support (works with Rails 8+, IRB, Racksh)

Installation

Option 1: Install via npm (Recommended)

# Install globally
npm install -g ruby-console-mcp

# Or use with npx (no installation needed)
npx ruby-console-mcp

Option 2: Install from source

# Clone or navigate to the project directory
git clone https://github.com/tuhalang/ruby-console-mcp.git
cd ruby-console-mcp

# Install dependencies
npm install

# Build the project
npm run build

Configuration

Create a configuration file or set environment variables:

Environment Variables

  • RUBY_APP_PATH: Path to your Rails/Rack application (default: current directory). Optional if using Docker/remote commands or IRB.
  • RUBY_CONSOLE_COMMAND: Command to start console (default: bundle exec rails c). Can be Rails console, IRB, Racksh, or any Ruby REPL.
  • COMMAND_TIMEOUT: Timeout for command execution in milliseconds (default: 30000)

Example Configuration

Local Rails app:

export RUBY_APP_PATH=/path/to/your/rails/app
export RUBY_CONSOLE_COMMAND="bundle exec rails c"

Docker (no RUBY_APP_PATH needed):

export RUBY_CONSOLE_COMMAND="docker-compose exec -T web bundle exec rails c"

Running from Rails directory (no RUBY_APP_PATH needed):

# Just use default command, it will use current directory
export RUBY_CONSOLE_COMMAND="bundle exec rails c"

Custom Console Commands

You can customize the command used to start the console. Examples for Rails, IRB, and Racksh:

# Production environment
RUBY_CONSOLE_COMMAND="bundle exec rails c production"

# Sandbox mode (changes are rolled back)
RUBY_CONSOLE_COMMAND="bundle exec rails c --sandbox"

# Using Docker (no RUBY_APP_PATH needed)
RUBY_CONSOLE_COMMAND="docker-compose exec -T web bundle exec rails c"

# Using Kubernetes (no RUBY_APP_PATH needed)
RUBY_CONSOLE_COMMAND="kubectl exec -it rails-pod -- bundle exec rails c"

# Using specific Ruby version
RUBY_CONSOLE_COMMAND="rbenv exec bundle exec rails c"

# Remote server via SSH (no RUBY_APP_PATH needed)
RUBY_CONSOLE_COMMAND="ssh user@server 'cd /app && bundle exec rails c'"

# IRB (standalone Ruby)
RUBY_CONSOLE_COMMAND="irb"

# Racksh (Rack console)
RUBY_CONSOLE_COMMAND="bundle exec racksh"

Note: When using Docker, Kubernetes, or remote commands, you typically don't need to set RUBY_APP_PATH since the command itself handles the context.

Usage with MCP Clients

Claude Desktop

Add to your Claude Desktop configuration file:

MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%/Claude/claude_desktop_config.json

Using npm package (Recommended):

{
  "mcpServers": {
    "ruby-console": {
      "command": "npx",
      "args": ["-y", "ruby-console-mcp"],
      "env": {
        "RUBY_APP_PATH": "/path/to/your/rails/app"
      }
    }
  }
}

Or using globally installed package:

{
  "mcpServers": {
    "ruby-console": {
      "command": "ruby-console-mcp",
      "env": {
        "RUBY_APP_PATH": "/path/to/your/rails/app"
      }
    }
  }
}

Local Rails app (from source):

{
  "mcpServers": {
    "ruby-console": {
      "command": "node",
      "args": ["/path/to/ruby-console-mcp/build/index.js"],
      "env": {
        "RUBY_APP_PATH": "/path/to/your/rails/app"
      }
    }
  }
}

Docker (no RUBY_APP_PATH needed):

{
  "mcpServers": {
    "ruby-console": {
      "command": "npx",
      "args": ["-y", "ruby-console-mcp"],
      "env": {
        "RUBY_CONSOLE_COMMAND": "docker-compose exec -T web bundle exec rails c"
      }
    }
  }
}

Other MCP Clients

Using npm package:

npx -y ruby-console-mcp

Or if installed globally:

ruby-console-mcp

From source:

node /path/to/ruby-console-mcp/build/index.js

How It Works

Command Execution

The server spawns a persistent console process (Rails console, IRB, or Racksh) using a pseudo-terminal (PTY) and communicates with it via stdin/stdout. Commands are sent to the console, and responses are captured and returned to the AI assistant.

Persistent Session

The console runs in a persistent session, which means:

  • Variables persist: Variables defined in one command are available in subsequent commands
  • State is maintained: ActiveRecord connections, loaded classes, and other state persist
  • Efficient: No need to reload the Rails environment for each command

Example Interactions

Simple query:

Command: User.count
Result: 42

Using variables across commands:

Command: a = User.first
Result: => #<User id: 1...>

Command: a.email
Result: => "user@example.com"

Complex operations:

Command: users = User.where('created_at > ?', 1.week.ago)
Result: => #<ActiveRecord::Relation...>

Command: users.count
Result: => 15

Available Tools

execute_ruby_command

Execute a single-line command in the console (Rails console, IRB, or Racksh).

Parameters:

  • command (string, required): The console command to execute

Examples:

// Query a model
{
  "command": "User.count"
}

// Complex query
{
  "command": "User.where('created_at > ?', 1.week.ago).group(:role).count"
}

// Using variables (persists across commands)
{
  "command": "user = User.first"
}

// Accessing previous variable
{
  "command": "user.email"
}

execute_ruby_script

Execute a multi-line Ruby script in the console. Useful for complex operations, method definitions, or blocks of code.

Parameters:

  • script (string, required): Multi-line Ruby script to execute

Examples:

// Multi-line script
{
  "script": "user = User.first\nputs user.email\nuser.update(name: 'New Name')"
}

// Method definition
{
  "script": "def greet(name)\n  puts \"Hello, #{name}!\"\nend\ngreet('World')"
}

check_ruby_console_health

Check if the console is healthy and responsive. Executes a simple test command and measures response time.

Returns:

  • HEALTHY: Console responds quickly (< 5s)
  • DEGRADED: Console responds but slowly (5-10s)
  • UNHEALTHY: Console fails or very slow (> 10s)

Examples:

// Check health
{}

connect_ruby_console

Connect to the Ruby console. Starts the console if it is not already running. Returns the connection status and console information.

Parameters:

  • None

Examples:

// Connect to console
{}

disconnect_ruby_console

Disconnect from the Ruby console. Stops the console process and releases resources. All variables and state will be lost after disconnecting.

Parameters:

  • None

Examples:

// Disconnect from console
{}

Features & Safety

  1. Persistent Session: Variables and state persist between commands for efficient workflow
  2. Multi-line Script Support: Execute complex Ruby scripts with multiple lines
  3. Health Monitoring: Check console health and responsiveness
  4. Connection Management: Connect and disconnect from console manually
  5. Timeout Protection: Commands timeout after 30 seconds (configurable via COMMAND_TIMEOUT) with progress feedback
  6. Error Parsing: Beautifully formatted error messages with stack traces
  7. Error Handling: Clear error messages for common issues
  8. Process Management: Automatic cleanup on server shutdown
  9. PTY Support: Uses pseudo-terminal for proper Rails console output (compatible with Rails 8+)

Troubleshooting

Console Won't Start

Problem: "Failed to start console"

Solutions:

  • Verify RUBY_APP_PATH points to a valid Rails application
  • Run bundle install in your Rails application directory
  • Check that RUBY_CONSOLE_COMMAND is correct for your setup
  • Ensure all dependencies are installed

Commands Timeout

Problem: Commands return timeout message

Solutions:

  • Increase COMMAND_TIMEOUT for long-running queries
  • Check if the Rails console is hanging (test manually)
  • Optimize the query or command

Output Not Captured

Problem: Command executes but returns "(No output)"

Solutions:

  • Some operations may not return output (this is normal)
  • Try adding .inspect or pp to the command for better output
  • Check for errors in the Rails application logs

Connection Lost

Problem: Rails console disconnects unexpectedly

Solutions:

  • Check Rails application logs for errors
  • Verify database connection is stable
  • Restart the MCP server

Development

# Clone the repository
git clone https://github.com/tuhalang/ruby-console-mcp.git
cd ruby-console-mcp

# Install dependencies
npm install

# Watch mode for development
npm run dev

# Build
npm run build

# Start the server
npm start

Architecture

┌─────────────────┐
│   MCP Client    │
│  (Claude, etc)  │
└────────┬────────┘
         │ stdio
         │
┌────────▼────────┐
│   MCP Server    │
│   (index.ts)    │
└────────┬────────┘
         │
┌────────▼────────┐
│ Ruby Console    │
│    Manager      │
│ (ruby-console)  │
└────────┬────────┘
         │ PTY (pseudo-terminal)
┌────────▼────────┐
│ Ruby Console    │
│  (rails c/irb)  │
└─────────────────┘

Security Considerations

  • This tool provides powerful access to your Rails application
  • All commands are executed immediately without confirmation
  • Consider running in sandbox mode for testing: RUBY_CONSOLE_COMMAND="bundle exec rails c --sandbox"
  • Be cautious in production environments
  • Review commands carefully before execution
  • Consider implementing additional access controls based on your needs

License

MIT

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选