rails-active-mcp 0.1.7 → 2.0.8

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 (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +106 -279
  3. data/changelog.md +69 -0
  4. data/docs/DEBUGGING.md +5 -5
  5. data/docs/README.md +130 -142
  6. data/examples/rails_app_integration.md +405 -0
  7. data/exe/rails-active-mcp-server +153 -76
  8. data/gemfiles/rails_7.1.gemfile +34 -0
  9. data/lib/generators/rails_active_mcp/install/install_generator.rb +19 -39
  10. data/lib/generators/rails_active_mcp/install/templates/README.md +134 -188
  11. data/lib/generators/rails_active_mcp/install/templates/initializer.rb +65 -28
  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 +13 -3
  15. data/lib/rails_active_mcp/engine.rb +36 -24
  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/tasks.rake +236 -80
  22. data/lib/rails_active_mcp/version.rb +1 -1
  23. data/lib/rails_active_mcp.rb +5 -11
  24. data/rails_active_mcp.gemspec +62 -11
  25. metadata +83 -24
  26. data/app/controllers/rails_active_mcp/mcp_controller.rb +0 -80
  27. data/lib/rails_active_mcp/mcp_server.rb +0 -383
  28. data/lib/rails_active_mcp/railtie.rb +0 -70
  29. data/lib/rails_active_mcp/stdio_server.rb +0 -517
  30. data/lib/rails_active_mcp/tools/console_execute_tool.rb +0 -61
  31. data/lib/rails_active_mcp/tools/dry_run_tool.rb +0 -41
  32. data/lib/rails_active_mcp/tools/model_info_tool.rb +0 -70
  33. data/lib/rails_active_mcp/tools/safe_query_tool.rb +0 -41
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/`.