rails-mcp-server 1.2.3 → 1.4.0

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 (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +168 -166
  3. data/docs/AGENT.md +345 -0
  4. data/exe/rails-mcp-config +1411 -0
  5. data/exe/rails-mcp-server +23 -10
  6. data/exe/rails-mcp-setup-claude +1 -1
  7. data/lib/rails-mcp-server/analyzers/analyze_controller_views.rb +253 -0
  8. data/lib/rails-mcp-server/analyzers/analyze_environment_config.rb +79 -0
  9. data/lib/rails-mcp-server/analyzers/analyze_models.rb +251 -0
  10. data/lib/rails-mcp-server/analyzers/base_analyzer.rb +42 -0
  11. data/lib/rails-mcp-server/analyzers/get_file.rb +40 -0
  12. data/lib/rails-mcp-server/analyzers/get_routes.rb +212 -0
  13. data/lib/rails-mcp-server/analyzers/get_schema.rb +216 -0
  14. data/lib/rails-mcp-server/analyzers/list_files.rb +43 -0
  15. data/lib/rails-mcp-server/analyzers/load_guide.rb +84 -0
  16. data/lib/rails-mcp-server/analyzers/project_info.rb +136 -0
  17. data/lib/rails-mcp-server/tools/base_tool.rb +2 -0
  18. data/lib/rails-mcp-server/tools/execute_ruby.rb +409 -0
  19. data/lib/rails-mcp-server/tools/execute_tool.rb +115 -0
  20. data/lib/rails-mcp-server/tools/search_tools.rb +186 -0
  21. data/lib/rails-mcp-server/tools/switch_project.rb +16 -1
  22. data/lib/rails-mcp-server/version.rb +1 -1
  23. data/lib/rails_mcp_server.rb +19 -53
  24. metadata +65 -18
  25. data/lib/rails-mcp-server/extensions/resource_templating.rb +0 -182
  26. data/lib/rails-mcp-server/extensions/server_templating.rb +0 -333
  27. data/lib/rails-mcp-server/tools/analyze_controller_views.rb +0 -239
  28. data/lib/rails-mcp-server/tools/analyze_environment_config.rb +0 -427
  29. data/lib/rails-mcp-server/tools/analyze_models.rb +0 -116
  30. data/lib/rails-mcp-server/tools/get_file.rb +0 -55
  31. data/lib/rails-mcp-server/tools/get_routes.rb +0 -24
  32. data/lib/rails-mcp-server/tools/get_schema.rb +0 -141
  33. data/lib/rails-mcp-server/tools/list_files.rb +0 -54
  34. data/lib/rails-mcp-server/tools/load_guide.rb +0 -370
  35. data/lib/rails-mcp-server/tools/project_info.rb +0 -86
data/docs/AGENT.md ADDED
@@ -0,0 +1,345 @@
1
+ # Rails MCP Server - AI Agent Guide
2
+
3
+ This guide helps AI agents (Claude, GPT, etc.) use the Rails MCP Server effectively. It covers tool selection, common patterns, and troubleshooting.
4
+
5
+ ## Quick Start
6
+
7
+ **Always start with these two steps:**
8
+
9
+ ```
10
+ # 1. Switch to the project
11
+ railsMcpServer:switch_project project_name: "your-project-name"
12
+
13
+ # 2. Get project overview
14
+ railsMcpServer:execute_tool tool_name: "project_info"
15
+ ```
16
+
17
+ **If unsure what tools are available:**
18
+
19
+ ```
20
+ railsMcpServer:search_tools
21
+ railsMcpServer:search_tools category: "models"
22
+ railsMcpServer:search_tools query: "routes"
23
+ ```
24
+
25
+ ---
26
+
27
+ ## Tool Selection Guide
28
+
29
+ ### Reading Files
30
+
31
+ **Primary method** - Use `execute_ruby` with `read_file()`:
32
+
33
+ ```
34
+ railsMcpServer:execute_ruby code: "puts read_file('config/routes.rb')"
35
+ railsMcpServer:execute_ruby code: "puts read_file('app/models/user.rb')"
36
+ railsMcpServer:execute_ruby code: "puts read_file('app/controllers/users_controller.rb')"
37
+ ```
38
+
39
+ **Alternative** - Use `get_file` tool:
40
+
41
+ ```
42
+ railsMcpServer:execute_tool tool_name: "get_file" params: { path: "config/routes.rb" }
43
+ ```
44
+
45
+ > ⚠️ **Important:** Do NOT use Claude's built-in `view` tool for Rails project files. It cannot access the project directory. Always use Rails MCP tools.
46
+
47
+ ---
48
+
49
+ ### Finding Files
50
+
51
+ **Use `execute_ruby` with `Dir.glob()`:**
52
+
53
+ ```
54
+ # Find all models
55
+ railsMcpServer:execute_ruby code: "puts Dir.glob('app/models/**/*.rb').join('\n')"
56
+
57
+ # Find all controllers
58
+ railsMcpServer:execute_ruby code: "puts Dir.glob('app/controllers/**/*.rb').join('\n')"
59
+
60
+ # Find files by name pattern
61
+ railsMcpServer:execute_ruby code: "puts Dir.glob('app/**/*user*').join('\n')"
62
+
63
+ # Find all view templates
64
+ railsMcpServer:execute_ruby code: "puts Dir.glob('app/views/**/*.erb').join('\n')"
65
+
66
+ # Find Stimulus controllers
67
+ railsMcpServer:execute_ruby code: "puts Dir.glob('app/javascript/controllers/**/*.js').join('\n')"
68
+ ```
69
+
70
+ **Using `list_files` helper** (glob pattern):
71
+
72
+ ```
73
+ # List Ruby files in models directory
74
+ railsMcpServer:execute_ruby code: "puts list_files('app/models/**/*.rb')"
75
+ ```
76
+
77
+ ---
78
+
79
+ ### Analyzing Models
80
+
81
+ ```
82
+ # List all models
83
+ railsMcpServer:execute_tool tool_name: "analyze_models"
84
+
85
+ # Analyze specific model with associations, validations, callbacks
86
+ railsMcpServer:execute_tool tool_name: "analyze_models" params: { model_name: "User" }
87
+
88
+ # Analyze multiple models
89
+ railsMcpServer:execute_tool tool_name: "analyze_models" params: { model_names: ["User", "Post", "Comment"] }
90
+
91
+ # Quick list (names only)
92
+ railsMcpServer:execute_tool tool_name: "analyze_models" params: { detail_level: "names" }
93
+
94
+ # With Prism static analysis (callbacks, scopes, methods)
95
+ railsMcpServer:execute_tool tool_name: "analyze_models" params: { model_name: "User", analysis_type: "full" }
96
+ ```
97
+
98
+ ---
99
+
100
+ ### Getting Database Schema
101
+
102
+ ```
103
+ # List all tables
104
+ railsMcpServer:execute_tool tool_name: "get_schema" params: { detail_level: "tables" }
105
+
106
+ # Get specific table schema
107
+ railsMcpServer:execute_tool tool_name: "get_schema" params: { table_name: "users" }
108
+
109
+ # Get multiple tables
110
+ railsMcpServer:execute_tool tool_name: "get_schema" params: { table_names: ["users", "posts"] }
111
+
112
+ # Full schema with indexes
113
+ railsMcpServer:execute_tool tool_name: "get_schema"
114
+ ```
115
+
116
+ ---
117
+
118
+ ### Getting Routes
119
+
120
+ ```
121
+ # All routes
122
+ railsMcpServer:execute_tool tool_name: "get_routes"
123
+
124
+ # Filter by controller
125
+ railsMcpServer:execute_tool tool_name: "get_routes" params: { controller: "users" }
126
+
127
+ # Filter by HTTP verb
128
+ railsMcpServer:execute_tool tool_name: "get_routes" params: { verb: "POST" }
129
+
130
+ # Filter by path
131
+ railsMcpServer:execute_tool tool_name: "get_routes" params: { path_contains: "api" }
132
+
133
+ # Named routes only
134
+ railsMcpServer:execute_tool tool_name: "get_routes" params: { named_only: true }
135
+ ```
136
+
137
+ **Fallback if `get_routes` fails:**
138
+
139
+ ```
140
+ railsMcpServer:execute_ruby code: "puts read_file('config/routes.rb')"
141
+ ```
142
+
143
+ ---
144
+
145
+ ### Analyzing Controllers
146
+
147
+ ```
148
+ # List all controllers
149
+ railsMcpServer:execute_tool tool_name: "analyze_controller_views" params: { detail_level: "names" }
150
+
151
+ # Analyze specific controller (actions, callbacks, views)
152
+ railsMcpServer:execute_tool tool_name: "analyze_controller_views" params: { controller_name: "users" }
153
+
154
+ # With Prism analysis (filters, strong params, instance variables)
155
+ railsMcpServer:execute_tool tool_name: "analyze_controller_views" params: { controller_name: "users", analysis_type: "full" }
156
+ ```
157
+
158
+ ---
159
+
160
+ ### Loading Framework Guides
161
+
162
+ ```
163
+ railsMcpServer:execute_tool tool_name: "load_guide" params: { guides: "rails", guide: "getting_started" }
164
+ railsMcpServer:execute_tool tool_name: "load_guide" params: { guides: "rails", guide: "active_record_basics" }
165
+ railsMcpServer:execute_tool tool_name: "load_guide" params: { guides: "turbo" }
166
+ railsMcpServer:execute_tool tool_name: "load_guide" params: { guides: "stimulus" }
167
+ railsMcpServer:execute_tool tool_name: "load_guide" params: { guides: "kamal" }
168
+ ```
169
+
170
+ ---
171
+
172
+ ### Environment Configuration
173
+
174
+ ```
175
+ # Compare environment configs, find inconsistencies
176
+ railsMcpServer:execute_tool tool_name: "analyze_environment_config"
177
+ ```
178
+
179
+ ---
180
+
181
+ ## Helper Methods in `execute_ruby`
182
+
183
+ When using `execute_ruby`, these helper methods are available:
184
+
185
+ | Helper | Usage | Description |
186
+ |--------|-------|-------------|
187
+ | `read_file(path)` | `read_file('config/routes.rb')` | Read file contents (relative to project root) |
188
+ | `file_exists?(path)` | `file_exists?('app/models/user.rb')` | Check if file exists (returns boolean) |
189
+ | `list_files(pattern)` | `list_files('app/models/*.rb')` | Glob pattern to find files |
190
+ | `project_root` | `project_root` | Returns the project root path |
191
+
192
+ **Critical:** Always use `puts` to see output:
193
+
194
+ ```
195
+ # ❌ Bad - returns "Code executed successfully (no output)"
196
+ railsMcpServer:execute_ruby code: "read_file('Gemfile')"
197
+
198
+ # ✅ Good - returns file contents
199
+ railsMcpServer:execute_ruby code: "puts read_file('Gemfile')"
200
+ ```
201
+
202
+ ---
203
+
204
+ ## Tool Selection Summary
205
+
206
+ | Task | Tool to Use |
207
+ |------|-------------|
208
+ | Read a project file | `execute_ruby` with `read_file()` or `get_file` |
209
+ | Find files by pattern | `execute_ruby` with `Dir.glob()` |
210
+ | Analyze models | `analyze_models` |
211
+ | Get database schema | `get_schema` |
212
+ | Get routes | `get_routes` (fallback: read routes.rb) |
213
+ | Analyze controllers | `analyze_controller_views` |
214
+ | Compare environments | `analyze_environment_config` |
215
+ | Load documentation | `load_guide` |
216
+ | Custom Ruby queries | `execute_ruby` |
217
+
218
+ ---
219
+
220
+ ## Rails MCP vs Claude Built-in Tools
221
+
222
+ | Task | Use This | NOT This |
223
+ |------|----------|----------|
224
+ | Read Rails project files | `railsMcpServer:execute_ruby` with `read_file()` | Claude's `view` tool |
225
+ | Edit files in Neovim | `nvimMcpServer:update_buffer` | Claude's `str_replace` |
226
+ | Create new files | Claude's `create_file` | — |
227
+ | View images | Claude's `view` tool | — |
228
+
229
+ **Key distinction:**
230
+ - **Rails MCP tools** operate within the Rails project context (after `switch_project`)
231
+ - **Claude's built-in tools** operate on a container filesystem, not your project directory
232
+ - **Neovim MCP tools** operate on files currently open in your editor
233
+
234
+ ---
235
+
236
+ ## Recommended Exploration Workflow
237
+
238
+ When starting work on an unfamiliar codebase:
239
+
240
+ ```
241
+ # 1. Get the lay of the land
242
+ railsMcpServer:execute_tool tool_name: "project_info"
243
+
244
+ # 2. Find relevant files
245
+ railsMcpServer:execute_ruby code: "puts Dir.glob('app/**/*transaction*').join('\n')"
246
+
247
+ # 3. Understand the data model
248
+ railsMcpServer:execute_tool tool_name: "analyze_models" params: { model_name: "Transaction" }
249
+ railsMcpServer:execute_tool tool_name: "get_schema" params: { table_name: "transactions" }
250
+
251
+ # 4. Check the routes
252
+ railsMcpServer:execute_tool tool_name: "get_routes" params: { controller: "transactions" }
253
+
254
+ # 5. Read the controller
255
+ railsMcpServer:execute_ruby code: "puts read_file('app/controllers/transactions_controller.rb')"
256
+
257
+ # 6. Check existing views
258
+ railsMcpServer:execute_ruby code: "puts Dir.glob('app/views/transactions/**/*').join('\n')"
259
+ ```
260
+
261
+ ---
262
+
263
+ ## Common Pitfalls
264
+
265
+ ### ❌ Don't
266
+
267
+ - Use Claude's `view` tool for Rails project files
268
+ - Forget `puts` in `execute_ruby` calls
269
+ - Use absolute paths (always use paths relative to project root)
270
+ - Skip `switch_project` before using other tools
271
+
272
+ ### ✅ Do
273
+
274
+ - Call `switch_project` before any other MCP tool
275
+ - Use `execute_ruby` with `read_file()` as your primary file reading method
276
+ - Use `puts` to output results in `execute_ruby`
277
+ - Fall back to `execute_ruby` when specialized tools fail
278
+ - Use `search_tools` when unsure what's available
279
+
280
+ ---
281
+
282
+ ## Error Handling & Fallbacks
283
+
284
+ ### "undefined method" errors from analyzers
285
+
286
+ Some analyzers may fail with certain Rails versions. Fall back to `execute_ruby`:
287
+
288
+ ```
289
+ # If get_routes fails:
290
+ railsMcpServer:execute_ruby code: "puts read_file('config/routes.rb')"
291
+
292
+ # If analyze_models fails:
293
+ railsMcpServer:execute_ruby code: "puts read_file('app/models/user.rb')"
294
+ ```
295
+
296
+ ### "Path not found" errors
297
+
298
+ 1. Ensure you've called `switch_project` first
299
+ 2. Use relative paths, not absolute paths
300
+ 3. Check if path exists:
301
+ ```
302
+ railsMcpServer:execute_ruby code: "puts file_exists?('app/models/user.rb')"
303
+ ```
304
+
305
+ ### "wrong number of arguments" errors
306
+
307
+ The `list_files()` helper takes a glob pattern as a single argument:
308
+ ```
309
+ # Correct usage
310
+ railsMcpServer:execute_ruby code: "puts list_files('app/models/**/*.rb')"
311
+ ```
312
+
313
+ ### No output from `execute_ruby`
314
+
315
+ Add `puts` before your expression:
316
+ ```
317
+ # Before (no output)
318
+ railsMcpServer:execute_ruby code: "User.count"
319
+
320
+ # After (shows result)
321
+ railsMcpServer:execute_ruby code: "puts User.count"
322
+ ```
323
+
324
+ ---
325
+
326
+ ## Integration with Neovim MCP
327
+
328
+ If using alongside Neovim MCP Server:
329
+
330
+ ```
331
+ # Check what files are open in Neovim
332
+ nvimMcpServer:get_project_buffers project_name: "your-project"
333
+
334
+ # Update a file that's open in Neovim
335
+ nvimMcpServer:update_buffer project_name: "your-project" file_path: "/full/path/to/file.rb" content: "new content"
336
+ ```
337
+
338
+ **Use Neovim MCP when:**
339
+ - You need to edit a file that's already open in the editor
340
+ - You want changes to appear immediately in the user's editor
341
+
342
+ **Use Rails MCP when:**
343
+ - You need to read/analyze project files
344
+ - You need Rails-specific analysis (models, routes, schema)
345
+ - The file isn't open in Neovim