rails-active-mcp 0.1.6 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +106 -279
  3. data/changelog.md +69 -0
  4. data/claude_desktop_config.json +12 -0
  5. data/docs/DEBUGGING.md +40 -8
  6. data/docs/GENERATOR_TESTING.md +121 -0
  7. data/docs/README.md +130 -142
  8. data/exe/rails-active-mcp-server +176 -65
  9. data/lib/generators/rails_active_mcp/install/install_generator.rb +123 -3
  10. data/lib/generators/rails_active_mcp/install/templates/README.md +34 -128
  11. data/lib/generators/rails_active_mcp/install/templates/initializer.rb +37 -38
  12. data/lib/generators/rails_active_mcp/install/templates/mcp.ru +7 -3
  13. data/lib/rails_active_mcp/configuration.rb +37 -98
  14. data/lib/rails_active_mcp/console_executor.rb +202 -78
  15. data/lib/rails_active_mcp/engine.rb +36 -8
  16. data/lib/rails_active_mcp/sdk/server.rb +183 -0
  17. data/lib/rails_active_mcp/sdk/tools/console_execute_tool.rb +103 -0
  18. data/lib/rails_active_mcp/sdk/tools/dry_run_tool.rb +73 -0
  19. data/lib/rails_active_mcp/sdk/tools/model_info_tool.rb +106 -0
  20. data/lib/rails_active_mcp/sdk/tools/safe_query_tool.rb +77 -0
  21. data/lib/rails_active_mcp/version.rb +1 -1
  22. data/lib/rails_active_mcp.rb +10 -11
  23. data/rails_active_mcp.gemspec +8 -4
  24. metadata +43 -17
  25. data/lib/rails_active_mcp/mcp_server.rb +0 -374
  26. data/lib/rails_active_mcp/railtie.rb +0 -48
  27. data/lib/rails_active_mcp/stdio_server.rb +0 -467
  28. data/lib/rails_active_mcp/tools/console_execute_tool.rb +0 -61
  29. data/lib/rails_active_mcp/tools/dry_run_tool.rb +0 -41
  30. data/lib/rails_active_mcp/tools/model_info_tool.rb +0 -70
  31. data/lib/rails_active_mcp/tools/safe_query_tool.rb +0 -41
data/docs/DEBUGGING.md CHANGED
@@ -60,10 +60,10 @@ This enables:
60
60
  1. Click the 🔌 icon to view connected servers
61
61
  2. Click the "Search and tools" 🔍 icon to view available tools
62
62
  3. Look for these Rails Active MCP tools:
63
- - `rails_console_execute`
64
- - `rails_model_info`
65
- - `rails_safe_query`
66
- - `rails_dry_run`
63
+ - `console_execute`
64
+ - `model_info`
65
+ - `safe_query`
66
+ - `dry_run`
67
67
 
68
68
  #### Viewing Claude Desktop Logs
69
69
 
@@ -126,7 +126,7 @@ The logs show:
126
126
  "mcpServers": {
127
127
  "rails-active-mcp": {
128
128
  "command": "bundle",
129
- "args": ["exec", "rails-active-mcp-server", "stdio"],
129
+ "args": ["exec", "rails-active-mcp-server"],
130
130
  "cwd": "/path/to/rails/project",
131
131
  "env": {
132
132
  "RAILS_ENV": "development",
@@ -137,7 +137,39 @@ The logs show:
137
137
  }
138
138
  ```
139
139
 
140
- ### 3. Server Initialization Problems
140
+ **Note**: The server will use the mode configured in your Rails initializer (`:stdio` by default). You can override this by adding the mode as an argument: `["exec", "rails-active-mcp-server", "stdio"]`
141
+
142
+ ### 3. Server Mode Configuration
143
+
144
+ **Problem**: Want to change the default server mode.
145
+
146
+ **Solution**: Configure server mode in your Rails initializer:
147
+
148
+ ```ruby
149
+ # config/initializers/rails_active_mcp.rb
150
+ RailsActiveMcp.configure do |config|
151
+ # For Claude Desktop (default)
152
+ config.server_mode = :stdio
153
+
154
+ # For HTTP-based integrations
155
+ config.http_mode!(host: '0.0.0.0', port: 8080)
156
+
157
+ # Or just set stdio mode
158
+ config.stdio_mode!
159
+ end
160
+ ```
161
+
162
+ **Command line override**:
163
+ ```bash
164
+ # Use configured mode
165
+ bundle exec rails-active-mcp-server
166
+
167
+ # Force specific mode
168
+ bundle exec rails-active-mcp-server stdio
169
+ bundle exec rails-active-mcp-server http --port 8080
170
+ ```
171
+
172
+ ### 4. Server Initialization Problems
141
173
 
142
174
  **Common initialization issues:**
143
175
 
@@ -162,7 +194,7 @@ The logs show:
162
194
  chmod +x exe/rails-active-mcp-server
163
195
  ```
164
196
 
165
- ### 4. Connection Problems
197
+ ### 5. Connection Problems
166
198
 
167
199
  **Problem**: Claude Desktop can't connect to server.
168
200
 
@@ -246,7 +278,7 @@ Should return all 4 Rails Active MCP tools.
246
278
 
247
279
  ```bash
248
280
  # Test a simple tool call
249
- echo '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"rails_dry_run","arguments":{"code":"puts \"Hello World\""}}}' | bundle exec rails-active-mcp-server stdio
281
+ echo '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"dry_run","arguments":{"code":"puts \"Hello World\""}}}' | bundle exec rails-active-mcp-server
250
282
  ```
251
283
 
252
284
  ## Best Practices
@@ -0,0 +1,121 @@
1
+ # Testing the Rails Active MCP Generator
2
+
3
+ This guide explains how to test that your Rails Active MCP generator is working correctly.
4
+
5
+ ## Quick Test in a New Rails App
6
+
7
+ 1. **Create a test Rails app:**
8
+ ```bash
9
+ rails new test_mcp_app
10
+ cd test_mcp_app
11
+ ```
12
+
13
+ 2. **Add your gem to the Gemfile:**
14
+ ```ruby
15
+ # Add to Gemfile
16
+ gem 'rails-active-mcp', path: '/path/to/your/gem'
17
+ # OR if published:
18
+ gem 'rails-active-mcp'
19
+ ```
20
+
21
+ 3. **Bundle install:**
22
+ ```bash
23
+ bundle install
24
+ ```
25
+
26
+ 4. **Check if the generator is available:**
27
+ ```bash
28
+ rails generate --help
29
+ ```
30
+ You should see `rails_active_mcp:install` in the list.
31
+
32
+ 5. **Run the generator:**
33
+ ```bash
34
+ rails generate rails_active_mcp:install
35
+ ```
36
+
37
+ 6. **Verify generated files:**
38
+ - `config/initializers/rails_active_mcp.rb` should exist
39
+ - `mcp.ru` should exist
40
+ - `config/routes.rb` should have the mount line added
41
+
42
+ ## What the Generator Should Create
43
+
44
+ ### Initializer File
45
+ Location: `config/initializers/rails_active_mcp.rb`
46
+
47
+ Should contain:
48
+ - Configuration block with `RailsActiveMcp.configure`
49
+ - Environment-specific settings
50
+ - Safety and logging configuration
51
+
52
+ ### MCP Server Configuration
53
+ Location: `mcp.ru`
54
+
55
+ Should contain:
56
+ - Rack configuration for standalone MCP server
57
+ - Proper requires and server initialization
58
+
59
+ ### Route Addition
60
+ In `config/routes.rb`:
61
+ - Should add: `mount RailsActiveMcp::McpServer.new, at: '/mcp'`
62
+
63
+ ## Generator Commands to Test
64
+
65
+ ```bash
66
+ # See generator help
67
+ rails generate rails_active_mcp:install --help
68
+
69
+ # Run with verbose output
70
+ rails generate rails_active_mcp:install --verbose
71
+
72
+ # Skip files (for testing)
73
+ rails generate rails_active_mcp:install --skip-route
74
+ ```
75
+
76
+ ## Common Issues and Solutions
77
+
78
+ ### Generator Not Found
79
+ If `rails generate rails_active_mcp:install` returns "Could not find generator", check:
80
+
81
+ 1. **Gem is properly installed:** `bundle list | grep rails-active-mcp`
82
+ 2. **Generator file exists:** Check `lib/generators/rails_active_mcp/install/install_generator.rb`
83
+ 3. **Class name matches:** Ensure class is `RailsActiveMcp::Generators::InstallGenerator`
84
+ 4. **Engine is loaded:** Check that the Engine is being required
85
+
86
+ ### Files Not Generated
87
+ If files aren't created:
88
+
89
+ 1. **Check permissions:** Ensure Rails can write to the directories
90
+ 2. **Check templates:** Verify template files exist in `templates/` directory
91
+ 3. **Check source_root:** Ensure `source_root` points to correct directory
92
+
93
+ ### Route Not Added
94
+ If the route isn't added to `config/routes.rb`:
95
+
96
+ 1. **Check routes.rb exists:** Generator requires existing routes file
97
+ 2. **File permissions:** Ensure routes.rb is writable
98
+
99
+ ## Running Tests
100
+
101
+ Run the generator specs:
102
+ ```bash
103
+ bundle exec rspec spec/generators/
104
+ ```
105
+
106
+ ## Debugging Generator Issues
107
+
108
+ Add debugging to your generator:
109
+ ```ruby
110
+ def create_initializer
111
+ say "Creating initializer at: #{destination_root}/config/initializers/rails_active_mcp.rb"
112
+ template 'initializer.rb', 'config/initializers/rails_active_mcp.rb'
113
+ say "Initializer created successfully", :green
114
+ end
115
+ ```
116
+
117
+ Check Rails generator resolution:
118
+ ```ruby
119
+ # In rails console
120
+ Rails::Generators.find_by_namespace("rails_active_mcp:install")
121
+ ```
data/docs/README.md CHANGED
@@ -1,22 +1,20 @@
1
1
  # Rails Active MCP Documentation
2
2
 
3
3
  ## Introduction
4
- Rails Active MCP is a Ruby gem that integrates the Model Context Protocol (MCP) into Rails applications. It provides a secure, configurable, and extensible server for AI agents and developer tools to interact with your Rails app via a standardized protocol.
4
+ Rails Active MCP is a Ruby gem that integrates the Model Context Protocol (MCP) into Rails applications using the official MCP Ruby SDK. It provides a secure, configurable, and professional server for AI agents and developer tools to interact with your Rails app via the standardized MCP protocol.
5
5
 
6
6
  ## How It Works
7
- - **Custom MCP Server**: Implements the MCP protocol (JSON-RPC 2.0 over HTTP) as a Rack middleware, mountable in Rails or runnable standalone.
8
- - **Tooling**: Exposes a set of tools (e.g., safe console execution, model info, safe queries, code analysis) to MCP clients.
9
- - **Safety**: Advanced safety checks, read-only modes, and audit logging protect your application from dangerous operations.
10
- - **Integration**: Easily added to any Rails 7+ app via a generator, with configuration via an initializer.
7
+ - **Official MCP SDK**: Built on the official MCP Ruby SDK (`mcp` gem) for robust protocol handling
8
+ - **Professional Implementation**: Automatic instrumentation, timing, and error reporting
9
+ - **Tooling**: Exposes four powerful tools (console execution, model info, safe queries, code analysis) to MCP clients
10
+ - **Safety**: Advanced safety checks, read-only modes, and audit logging protect your application from dangerous operations
11
+ - **Integration**: Easily added to any Rails application via a generator, with simplified configuration
11
12
 
12
13
  ## MCP Protocol Overview
13
- - **MCP**: The Model Context Protocol is a standard for structured, secure, and auditable access to application internals for AI agents and developer tools.
14
- - **Supported Methods**:
15
- - `initialize`: Returns server capabilities and protocol version.
16
- - `tools/list`: Lists available tools and their schemas.
17
- - `tools/call`: Executes a tool with given arguments.
18
- - `resources/list` and `resources/read`: (Stubbed, for future resource access.)
19
- - **JSON-RPC 2.0**: All communication uses JSON-RPC 2.0 over HTTP POST.
14
+ - **MCP**: The Model Context Protocol is a standard for structured, secure, and auditable access to application internals for AI agents and developer tools
15
+ - **Official SDK**: Uses the official MCP Ruby SDK for protocol compliance and future-proof compatibility
16
+ - **STDIO Transport**: Perfect for Claude Desktop integration
17
+ - **JSON-RPC 2.0**: All communication uses JSON-RPC 2.0 with automatic protocol handling
20
18
 
21
19
  ## Installation & Setup
22
20
  1. Add to your Gemfile:
@@ -28,158 +26,148 @@ Rails Active MCP is a Ruby gem that integrates the Model Context Protocol (MCP)
28
26
  bundle install
29
27
  rails generate rails_active_mcp:install
30
28
  ```
31
- This creates an initializer, mounts the MCP server at `/mcp`, and sets up audit logging.
29
+ This creates an initializer and sets up the Rails integration.
32
30
  3. Start the server:
33
- - Rails: `rails server` (MCP at `/mcp`)
34
- - Standalone: `bundle exec rails-active-mcp-server`
35
- - Rack: `rackup mcp.ru -p 3001`
31
+ ```bash
32
+ bundle exec rails-active-mcp-server
33
+ ```
36
34
 
37
35
  ## Configuration
38
36
  Edit `config/initializers/rails_active_mcp.rb`:
39
- - Enable/disable the server
40
- - Safety mode (production = strict)
41
- - Allowed/blocked models
42
- - Enable/disable mutation tools
43
- - Audit log location
44
- - Environment presets: `production_mode!`, `strict_mode!`, `permissive_mode!`
45
37
 
46
- ## Available Tools
47
- - **rails_console_execute**: Execute Ruby code in Rails with safety checks
48
- - **rails_model_info**: Inspect model schema and associations
49
- - **rails_safe_query**: Run safe, read-only queries on models
50
- - **rails_dry_run**: Analyze code for safety without executing
38
+ ```ruby
39
+ RailsActiveMcp.configure do |config|
40
+ # Core configuration options
41
+ config.allowed_commands = %w[
42
+ ls pwd cat head tail grep find wc
43
+ rails console rails runner
44
+ bundle exec rspec bundle exec test
45
+ git status git log git diff
46
+ ]
47
+ config.command_timeout = 30
48
+ config.enable_logging = true
49
+ config.log_level = :info
50
+ end
51
+ ```
51
52
 
52
- Each tool is described in the MCP `tools/list` response and can be extended or customized.
53
+ ## Available Tools
53
54
 
54
- ## Security & Safety
55
- - **Safety Checker**: Blocks dangerous operations (e.g., mass deletions, system commands, file access)
56
- - **Read-Only Mode**: Enforced in production
57
- - **Audit Logging**: All executions are logged to `log/rails_active_mcp.log`
58
- - **Configurable**: Fine-tune what is allowed per environment
55
+ ### 1. console_execute
56
+ **Purpose:**
57
+ Execute Ruby code in the Rails console context with safety checks and timeout protection.
59
58
 
60
- ## Extending the Gem
61
- - Add custom tools by calling `RailsActiveMcp.server.register_tool` in an initializer or plugin.
62
- - Tools must define a name, description, input schema, and a handler block.
59
+ **Features:**
60
+ - Built-in dangerous operation detection
61
+ - Configurable execution timeout
62
+ - All executions logged for audit
63
+ - Safe execution environment
63
64
 
64
- ## Testing & Specification Adherence
65
- - All features are covered by RSpec tests in the `spec/` directory.
66
- - Follows standard Ruby/Rails conventions for gems, engines, and generators.
67
- - MCP protocol compliance is maintained as per [modelcontextprotocol.io](https://modelcontextprotocol.io/introduction).
65
+ **Example Usage in Claude:**
66
+ > "Execute `User.where(active: true).count`"
68
67
 
69
- ## Per-Tool Breakdown
68
+ ---
70
69
 
71
- ### 1. rails_console_execute
70
+ ### 2. model_info
72
71
  **Purpose:**
73
- Execute arbitrary Ruby code in the Rails console context, with safety checks and output capture.
74
-
75
- **Input Parameters:**
76
- - `code` (string, required): Ruby code to execute.
77
- - `safe_mode` (boolean, optional): Enable safety checks (default: true).
78
- - `timeout` (integer, optional): Timeout in seconds (default: 30).
79
- - `capture_output` (boolean, optional): Capture console output (default: true).
80
-
81
- **Output:**
82
- - On success: Code, result, output (if any), execution time, and notes.
83
- - On error: Error message and class.
84
-
85
- **Example Usage:**
86
- ```json
87
- {
88
- "method": "tools/call",
89
- "params": {
90
- "name": "rails_console_execute",
91
- "arguments": {
92
- "code": "User.count"
93
- }
94
- }
95
- }
96
- ```
72
+ Get detailed information about Rails models including schema, associations, and validations.
73
+
74
+ **Features:**
75
+ - Schema information (column types, constraints, indexes)
76
+ - Association details (has_many, belongs_to, has_one relationships)
77
+ - Validation rules and constraints
78
+ - Available instance and class methods
79
+
80
+ **Example Usage in Claude:**
81
+ > "Show me the User model structure"
97
82
 
98
83
  ---
99
84
 
100
- ### 2. rails_model_info
85
+ ### 3. safe_query
101
86
  **Purpose:**
102
- Get detailed information about a Rails model, including schema, associations, and validations.
103
-
104
- **Input Parameters:**
105
- - `model` (string, required): Model class name (e.g., "User").
106
- - `include_schema` (boolean, optional): Include database schema info (default: true).
107
- - `include_associations` (boolean, optional): Include model associations (default: true).
108
- - `include_validations` (boolean, optional): Include model validations (default: true).
109
-
110
- **Output:**
111
- - Model name, table, primary key, schema (columns, types, null/default), associations, validations.
112
- - On error: Error message (e.g., model not found).
113
-
114
- **Example Usage:**
115
- ```json
116
- {
117
- "method": "tools/call",
118
- "params": {
119
- "name": "rails_model_info",
120
- "arguments": {
121
- "model": "User"
122
- }
123
- }
124
- }
125
- ```
87
+ Execute safe, read-only database queries with automatic safety analysis.
88
+
89
+ **Features:**
90
+ - Read-only operations only (SELECT queries)
91
+ - Automatic query analysis for safety
92
+ - Result limiting to prevent large data dumps
93
+ - Works within your model definitions
94
+
95
+ **Example Usage in Claude:**
96
+ > "Get the 10 most recent orders"
126
97
 
127
98
  ---
128
99
 
129
- ### 3. rails_safe_query
100
+ ### 4. dry_run
130
101
  **Purpose:**
131
- Execute safe, read-only database queries on Rails models.
132
-
133
- **Input Parameters:**
134
- - `model` (string, required): Model class name (e.g., "User").
135
- - `method` (string, required): Query method (e.g., "where", "count").
136
- - `args` (array, optional): Arguments for the query method.
137
- - `limit` (integer, optional): Limit results (default: 100).
138
-
139
- **Output:**
140
- - Query string, count, and result (as inspected Ruby object).
141
- - On error: Error message.
142
-
143
- **Example Usage:**
144
- ```json
145
- {
146
- "method": "tools/call",
147
- "params": {
148
- "name": "rails_safe_query",
149
- "arguments": {
150
- "model": "User",
151
- "method": "where",
152
- "args": [{"active": true}],
153
- "limit": 10
154
- }
155
- }
156
- }
102
+ Analyze Ruby code for safety without executing it.
103
+
104
+ **Features:**
105
+ - Risk assessment and categorization
106
+ - Safety analysis with detailed feedback
107
+ - Recommendations for safer alternatives
108
+ - Zero execution guarantee
109
+
110
+ **Example Usage in Claude:**
111
+ > "Analyze this code for safety: `User.delete_all`"
112
+
113
+ ## Security & Safety
114
+ - **Safety Checker**: Blocks dangerous operations (mass deletions, system commands, file access)
115
+ - **Read-Only Detection**: Identifies and promotes safe read-only operations
116
+ - **Audit Logging**: All executions are logged with detailed context
117
+ - **Configurable**: Fine-tune safety levels per environment
118
+
119
+ ## Architecture
120
+
121
+ ### Built on Official MCP Ruby SDK
122
+ Rails Active MCP leverages the official MCP Ruby SDK for:
123
+
124
+ - **Professional Protocol Handling**: Robust JSON-RPC 2.0 implementation
125
+ - **Built-in Instrumentation**: Automatic timing and error reporting
126
+ - **Future-Proof**: Automatic updates as MCP specification evolves
127
+ - **Standards Compliance**: Full MCP protocol compatibility
128
+
129
+ ### Server Implementation
130
+ The server is implemented in `lib/rails_active_mcp/sdk/server.rb` and provides:
131
+
132
+ - **STDIO Transport**: Perfect for Claude Desktop integration
133
+ - **Tool Registration**: Automatic discovery of available tools
134
+ - **Error Handling**: Comprehensive error reporting and recovery
135
+ - **Rails Integration**: Deep integration with Rails applications
136
+
137
+ ### Tool Architecture
138
+ Each tool is implemented as a separate class in `lib/rails_active_mcp/sdk/tools/`:
139
+
140
+ - `ConsoleExecuteTool`: Safe code execution
141
+ - `ModelInfoTool`: Model introspection
142
+ - `SafeQueryTool`: Read-only database access
143
+ - `DryRunTool`: Code safety analysis
144
+
145
+ ## Testing & Development
146
+
147
+ ### Running Tests
148
+ ```bash
149
+ $ bundle exec rspec
157
150
  ```
158
151
 
159
- ---
152
+ ### Testing MCP Integration
153
+ ```bash
154
+ $ ./bin/test-mcp-output
155
+ ```
160
156
 
161
- ### 4. rails_dry_run
162
- **Purpose:**
163
- Analyze Ruby code for safety and risk without executing it.
164
-
165
- **Input Parameters:**
166
- - `code` (string, required): Ruby code to analyze.
167
-
168
- **Output:**
169
- - Code, safety status, read-only status, risk level, summary, violations, and recommendations.
170
-
171
- **Example Usage:**
172
- ```json
173
- {
174
- "method": "tools/call",
175
- "params": {
176
- "name": "rails_dry_run",
177
- "arguments": {
178
- "code": "User.delete_all"
179
- }
180
- }
181
- }
157
+ ### Debugging
158
+ Set the debug environment variable for detailed logging:
159
+ ```bash
160
+ $ RAILS_MCP_DEBUG=1 bundle exec rails-active-mcp-server
182
161
  ```
183
162
 
184
- ---
185
- For more details, see the main project README or source code.
163
+ ## Extending the Gem
164
+ The gem is designed to be extensible. You can add custom tools by:
165
+
166
+ 1. Creating new tool classes in your application
167
+ 2. Following the MCP tool interface pattern
168
+ 3. Registering tools with the server during initialization
169
+
170
+ Each tool must implement the standard MCP tool interface with proper input schemas and error handling.
171
+
172
+
173
+ For more details, see the main project README or explore the source code in `lib/rails_active_mcp/sdk/`.