rails-ai-context 5.3.0 → 5.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/README.md +55 -9
- data/app/controllers/rails_ai_context/mcp_controller.rb +43 -0
- data/config/routes.rb +5 -0
- data/docs/GUIDE.md +22 -0
- data/lib/rails_ai_context/instrumentation.rb +22 -0
- data/lib/rails_ai_context/live_reload.rb +1 -1
- data/lib/rails_ai_context/resources.rb +34 -1
- data/lib/rails_ai_context/server.rb +7 -1
- data/lib/rails_ai_context/tools/base_tool.rb +26 -0
- data/lib/rails_ai_context/version.rb +1 -1
- data/lib/rails_ai_context/vfs.rb +162 -0
- data/lib/rails_ai_context.rb +1 -1
- data/server.json +2 -2
- metadata +5 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 43e8a71c793404bd950627b8f3319b4e909a0ba7a2cfe937a8d1027e2ede4af0
|
|
4
|
+
data.tar.gz: f296960edc1f3f336e90b9549618fd4b48d51d31330d95a65de9304ba47aa0af
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bc10e825ef820ad955e89ded454469104160f954b967958866876794b1c114d9b708564e99781f1252cf4faa683d468bf5143823ee3418080de43ef745508ab2
|
|
7
|
+
data.tar.gz: 127d4159c5d88da79ae7fcb7389ffb84cca28621e0181da7b7e495e6af6fe0f8c0073b37dff3087a67be78c236e6f09e36aaef82e070461406e8a2027d88d2a8
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,32 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [5.4.0] — 2026-04-08
|
|
9
|
+
|
|
10
|
+
### Added — Phase 3: Dynamic VFS & Live Resource Architecture (Ground Truth Engine Blueprint #39)
|
|
11
|
+
|
|
12
|
+
Live Virtual File System replaces static resource handling. Every MCP resource is introspected fresh on every request — zero stale data.
|
|
13
|
+
|
|
14
|
+
- **VFS URI Dispatcher** (`lib/rails_ai_context/vfs.rb`) — Pattern-matched routing for `rails-ai-context://` URIs. Resolves models, controllers, controller actions, views, and routes. Each call introspects fresh. Path traversal protection for view reads.
|
|
15
|
+
|
|
16
|
+
- **4 new MCP Resource Templates:**
|
|
17
|
+
- `rails-ai-context://controllers/{name}` — controller details with actions, filters, strong params
|
|
18
|
+
- `rails-ai-context://controllers/{name}/{action}` — action source code and applicable filters
|
|
19
|
+
- `rails-ai-context://views/{path}` — view template content (path traversal protected)
|
|
20
|
+
- `rails-ai-context://routes/{controller}` — live route map filtered by controller name
|
|
21
|
+
|
|
22
|
+
- **MCP Controller** (`app/controllers/rails_ai_context/mcp_controller.rb`) — Native Rails controller for Streamable HTTP transport. Alternative to Rack middleware — integrates with Rails routing, authentication, and middleware stack. Mount via `mount RailsAiContext::Engine, at: "/mcp"`.
|
|
23
|
+
|
|
24
|
+
- **output_schema on all 38 tools** — Default `MCP::Tool::OutputSchema` set via `BaseTool.inherited` hook. Every tool now declares its output format in the MCP protocol. Individual tools can override with custom schemas.
|
|
25
|
+
|
|
26
|
+
- **Instrumentation** (`lib/rails_ai_context/instrumentation.rb`) — Bridges MCP gem instrumentation to `ActiveSupport::Notifications`. Events: `rails_ai_context.tools.call`, `rails_ai_context.resources.read`, etc. Subscribe with standard Rails notification patterns.
|
|
27
|
+
|
|
28
|
+
- **Server instructions** — MCP server now includes `instructions:` field describing the ground truth engine capabilities.
|
|
29
|
+
|
|
30
|
+
- **Enhanced LiveReload** — Full cache sweep on file changes via `reset_all_caches!` (includes AST, tool, and fingerprint caches).
|
|
31
|
+
|
|
32
|
+
- **82 new specs** covering VFS resolution (models, controllers, actions, views, routes), instrumentation callback, McpController (thread safety, delegation, subclass isolation), resource templates (5 total), output_schema on all 38 tools, and server configuration.
|
|
33
|
+
|
|
8
34
|
## [5.3.0] — 2026-04-07
|
|
9
35
|
|
|
10
36
|
### Added — Phase 2: Cross-Tool Semantic Hydration (Ground Truth Engine Blueprint #38)
|
data/README.md
CHANGED
|
@@ -17,9 +17,10 @@
|
|
|
17
17
|
[](https://rubygems.org/gems/rails-ai-context)
|
|
18
18
|
[](https://github.com/crisnahine/rails-ai-context/actions)
|
|
19
19
|
[](https://registry.modelcontextprotocol.io)
|
|
20
|
+
<br>
|
|
20
21
|
[](https://github.com/crisnahine/rails-ai-context)
|
|
21
22
|
[](https://github.com/crisnahine/rails-ai-context)
|
|
22
|
-
[](https://github.com/crisnahine/rails-ai-context/actions)
|
|
23
24
|
[](LICENSE)
|
|
24
25
|
|
|
25
26
|
</div>
|
|
@@ -66,7 +67,7 @@ rails-ai-context serve # start MCP server
|
|
|
66
67
|
|
|
67
68
|
</div>
|
|
68
69
|
|
|
69
|
-
Now your AI doesn't guess — it **asks your app directly.** 38 tools that query your schema, models, routes, controllers, views, and conventions on demand. Model introspection uses Prism AST parsing — every result carries a `[VERIFIED]` or `[INFERRED]` confidence tag so AI knows what's ground truth and what needs runtime checking.
|
|
70
|
+
Now your AI doesn't guess — it **asks your app directly.** 38 tools and 5 resource templates that query your schema, models, routes, controllers, views, and conventions on demand. Model introspection uses Prism AST parsing — every result carries a `[VERIFIED]` or `[INFERRED]` confidence tag so AI knows what's ground truth and what needs runtime checking.
|
|
70
71
|
|
|
71
72
|
<br>
|
|
72
73
|
|
|
@@ -122,13 +123,13 @@ Compare what AI outputs with and without these tools wired in. The difference is
|
|
|
122
123
|
|
|
123
124
|
<br>
|
|
124
125
|
|
|
125
|
-
##
|
|
126
|
+
## Three ways to use it
|
|
126
127
|
|
|
127
128
|
<table>
|
|
128
129
|
<tr>
|
|
129
|
-
<td width="
|
|
130
|
+
<td width="33%">
|
|
130
131
|
|
|
131
|
-
### MCP Server
|
|
132
|
+
### MCP Server (stdio)
|
|
132
133
|
|
|
133
134
|
AI calls tools directly via the protocol. Auto-discovered through `.mcp.json`.
|
|
134
135
|
|
|
@@ -143,7 +144,21 @@ rails ai:serve
|
|
|
143
144
|
```
|
|
144
145
|
|
|
145
146
|
</td>
|
|
146
|
-
<td width="
|
|
147
|
+
<td width="33%">
|
|
148
|
+
|
|
149
|
+
### MCP Server (HTTP)
|
|
150
|
+
|
|
151
|
+
Mount inside your Rails app — inherits routing, auth, and middleware.
|
|
152
|
+
|
|
153
|
+
```ruby
|
|
154
|
+
# config/routes.rb
|
|
155
|
+
mount RailsAiContext::Engine, at: "/mcp"
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Native Rails controller transport. No separate process needed.
|
|
159
|
+
|
|
160
|
+
</td>
|
|
161
|
+
<td width="33%">
|
|
147
162
|
|
|
148
163
|
### CLI
|
|
149
164
|
|
|
@@ -342,6 +357,22 @@ Every tool is **read-only** and returns data verified against your actual app
|
|
|
342
357
|
|
|
343
358
|
<br>
|
|
344
359
|
|
|
360
|
+
## Live Resources (VFS)
|
|
361
|
+
|
|
362
|
+
AI clients can also read structured data through **resource templates** — `rails-ai-context://` URIs that introspect fresh on every request. Zero stale data.
|
|
363
|
+
|
|
364
|
+
| Resource Template | What it returns |
|
|
365
|
+
|:------------------|:---------------|
|
|
366
|
+
| `rails-ai-context://controllers/{name}` | Actions, inherited filters, strong params |
|
|
367
|
+
| `rails-ai-context://controllers/{name}/{action}` | Action source code with applicable filters |
|
|
368
|
+
| `rails-ai-context://views/{path}` | View template content (path traversal protected) |
|
|
369
|
+
| `rails-ai-context://routes/{controller}` | Live route map filtered by controller |
|
|
370
|
+
| `rails://models/{name}` | Per-model details: associations, validations, schema |
|
|
371
|
+
|
|
372
|
+
Plus 9 static resources (schema, routes, conventions, gems, controllers, config, tests, migrations, engines) that AI clients read directly.
|
|
373
|
+
|
|
374
|
+
<br>
|
|
375
|
+
|
|
345
376
|
## Anti-Hallucination Protocol
|
|
346
377
|
|
|
347
378
|
Every generated context file ships with **6 rules that force AI verification** before writing code. The protocol targets the exact cognitive failures that produce confident-wrong code: statistical priors overriding observed facts, pattern completion beating verification, stale context lies.
|
|
@@ -376,14 +407,15 @@ Enabled by default. Disable with `config.anti_hallucination_rules = false` if yo
|
|
|
376
407
|
┌─────────────────────────────────────────────────────────┐
|
|
377
408
|
│ rails-ai-context │
|
|
378
409
|
│ Prism AST parsing. Cached. Confidence-tagged results. │
|
|
410
|
+
│ VFS: rails-ai-context:// URIs introspected fresh. │
|
|
379
411
|
└────────┬──────────────────┬──────────────┬──────────────┘
|
|
380
412
|
│ │ │
|
|
381
413
|
▼ ▼ ▼
|
|
382
414
|
┌──────────────────┐ ┌────────────┐ ┌────────────────────┐
|
|
383
415
|
│ Static Files │ │ MCP Server │ │ CLI Tools │
|
|
384
416
|
│ CLAUDE.md │ │ 38 tools │ │ Same 38 tools │
|
|
385
|
-
│ .cursor/rules/ │ │
|
|
386
|
-
│ .github/instr... │ │
|
|
417
|
+
│ .cursor/rules/ │ │ 5 templates│ │ No server needed │
|
|
418
|
+
│ .github/instr... │ │ stdio/HTTP │ │ rails 'ai:tool[X]' │
|
|
387
419
|
└──────────────────┘ └────────────┘ └────────────────────┘
|
|
388
420
|
```
|
|
389
421
|
|
|
@@ -458,6 +490,20 @@ end
|
|
|
458
490
|
|
|
459
491
|
<br>
|
|
460
492
|
|
|
493
|
+
## Observability
|
|
494
|
+
|
|
495
|
+
Every MCP tool call and resource read fires an `ActiveSupport::Notifications` event. Subscribe with standard Rails patterns:
|
|
496
|
+
|
|
497
|
+
```ruby
|
|
498
|
+
ActiveSupport::Notifications.subscribe("rails_ai_context.tools.call") do |event|
|
|
499
|
+
Rails.logger.info "[MCP] #{event.payload[:method]} — #{event.duration}ms"
|
|
500
|
+
end
|
|
501
|
+
```
|
|
502
|
+
|
|
503
|
+
Events: `rails_ai_context.tools.call`, `rails_ai_context.resources.read`, and more. All 38 tools declare `output_schema` in the MCP protocol, so clients know the response format before calling.
|
|
504
|
+
|
|
505
|
+
<br>
|
|
506
|
+
|
|
461
507
|
## Requirements
|
|
462
508
|
|
|
463
509
|
- **Ruby** >= 3.2 **Rails** >= 7.1
|
|
@@ -472,7 +518,7 @@ end
|
|
|
472
518
|
## About
|
|
473
519
|
|
|
474
520
|
Built by a Rails developer with 10+ years of production experience.<br>
|
|
475
|
-
|
|
521
|
+
1813 tests. 38 tools. 5 resource templates. 31 introspectors. Standalone or in-Gemfile.<br>
|
|
476
522
|
MIT licensed. [Contributions welcome.](CONTRIBUTING.md)
|
|
477
523
|
|
|
478
524
|
<br>
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
# Rails controller for serving MCP over Streamable HTTP.
|
|
5
|
+
# Alternative to the Rack middleware — integrates with Rails routing,
|
|
6
|
+
# authentication, and middleware stack.
|
|
7
|
+
#
|
|
8
|
+
# Mount in routes: mount RailsAiContext::Engine, at: "/mcp"
|
|
9
|
+
class McpController < ActionController::API
|
|
10
|
+
def handle
|
|
11
|
+
rack_response = self.class.mcp_transport.handle_request(request)
|
|
12
|
+
self.status = rack_response[0]
|
|
13
|
+
rack_response[1].each { |k, v| response.headers[k] = v }
|
|
14
|
+
self.response_body = rack_response[2]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class << self
|
|
18
|
+
# Class-level memoization — transport persists across requests.
|
|
19
|
+
# Thread-safe: MCP::Server and transport are stateless for reads.
|
|
20
|
+
def mcp_transport
|
|
21
|
+
@mcp_transport || @transport_mutex.synchronize do
|
|
22
|
+
@mcp_transport ||= begin
|
|
23
|
+
server = RailsAiContext::Server.new(Rails.application, transport: :http).build
|
|
24
|
+
MCP::Server::Transports::StreamableHTTPTransport.new(server)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def reset_transport!
|
|
30
|
+
@transport_mutex.synchronize { @mcp_transport = nil }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def inherited(subclass)
|
|
36
|
+
super
|
|
37
|
+
subclass.instance_variable_set(:@transport_mutex, Mutex.new)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
@transport_mutex = Mutex.new
|
|
42
|
+
end
|
|
43
|
+
end
|
data/config/routes.rb
ADDED
data/docs/GUIDE.md
CHANGED
|
@@ -1010,6 +1010,17 @@ In addition to tools, the gem registers static MCP resources that AI clients can
|
|
|
1010
1010
|
| `rails://engines` | Mounted engines with paths and descriptions (JSON) |
|
|
1011
1011
|
| `rails://models/{name}` | Per-model details (resource template) |
|
|
1012
1012
|
|
|
1013
|
+
### Dynamic Resource Templates (VFS)
|
|
1014
|
+
|
|
1015
|
+
Live resources introspected fresh on every request — zero stale data:
|
|
1016
|
+
|
|
1017
|
+
| Resource Template | Description |
|
|
1018
|
+
|-------------------|-------------|
|
|
1019
|
+
| `rails-ai-context://controllers/{name}` | Controller details with actions, filters, strong params |
|
|
1020
|
+
| `rails-ai-context://controllers/{name}/{action}` | Specific action source code and applicable filters |
|
|
1021
|
+
| `rails-ai-context://views/{path}` | View template content (path traversal protected) |
|
|
1022
|
+
| `rails-ai-context://routes` | Live route map (optionally filter by controller) |
|
|
1023
|
+
|
|
1013
1024
|
---
|
|
1014
1025
|
|
|
1015
1026
|
## MCP Server Setup
|
|
@@ -1126,6 +1137,17 @@ end
|
|
|
1126
1137
|
|
|
1127
1138
|
Both transports are **read-only** — they expose the same 38 tools and never modify your app.
|
|
1128
1139
|
|
|
1140
|
+
### Controller Transport (Alternative)
|
|
1141
|
+
|
|
1142
|
+
For tighter Rails integration (authentication, routing, middleware stack), mount the engine instead of using Rack middleware:
|
|
1143
|
+
|
|
1144
|
+
```ruby
|
|
1145
|
+
# config/routes.rb
|
|
1146
|
+
mount RailsAiContext::Engine, at: "/mcp"
|
|
1147
|
+
```
|
|
1148
|
+
|
|
1149
|
+
This provides a native Rails controller (`RailsAiContext::McpController`) that delegates to the Streamable HTTP transport.
|
|
1150
|
+
|
|
1129
1151
|
---
|
|
1130
1152
|
|
|
1131
1153
|
## Configuration — All Options
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RailsAiContext
|
|
4
|
+
# Bridges MCP gem instrumentation to ActiveSupport::Notifications.
|
|
5
|
+
# Enables Rails apps to subscribe to MCP events (tool calls, resource reads, etc.).
|
|
6
|
+
module Instrumentation
|
|
7
|
+
EVENT_PREFIX = "rails_ai_context"
|
|
8
|
+
|
|
9
|
+
# Returns a lambda for MCP::Configuration#instrumentation_callback.
|
|
10
|
+
# Instruments each MCP method call as an ActiveSupport::Notifications event.
|
|
11
|
+
def self.callback
|
|
12
|
+
->(data) {
|
|
13
|
+
return unless defined?(ActiveSupport::Notifications)
|
|
14
|
+
|
|
15
|
+
method = data[:method] || "unknown"
|
|
16
|
+
event_name = "#{EVENT_PREFIX}.#{method.tr("/", ".")}"
|
|
17
|
+
|
|
18
|
+
ActiveSupport::Notifications.instrument(event_name, data)
|
|
19
|
+
}
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -70,6 +70,34 @@ module RailsAiContext
|
|
|
70
70
|
mime_type: "application/json"
|
|
71
71
|
).freeze
|
|
72
72
|
|
|
73
|
+
CONTROLLER_TEMPLATE = MCP::ResourceTemplate.new(
|
|
74
|
+
uri_template: "rails-ai-context://controllers/{name}",
|
|
75
|
+
name: "Controller Details",
|
|
76
|
+
description: "Controller with actions, filters, strong params, and schema hints",
|
|
77
|
+
mime_type: "application/json"
|
|
78
|
+
).freeze
|
|
79
|
+
|
|
80
|
+
CONTROLLER_ACTION_TEMPLATE = MCP::ResourceTemplate.new(
|
|
81
|
+
uri_template: "rails-ai-context://controllers/{name}/{action}",
|
|
82
|
+
name: "Controller Action",
|
|
83
|
+
description: "Source code and metadata for a specific controller action",
|
|
84
|
+
mime_type: "application/json"
|
|
85
|
+
).freeze
|
|
86
|
+
|
|
87
|
+
VIEW_TEMPLATE = MCP::ResourceTemplate.new(
|
|
88
|
+
uri_template: "rails-ai-context://views/{path}",
|
|
89
|
+
name: "View Template",
|
|
90
|
+
description: "View template content for a specific path relative to app/views",
|
|
91
|
+
mime_type: "text/html"
|
|
92
|
+
).freeze
|
|
93
|
+
|
|
94
|
+
ROUTES_TEMPLATE = MCP::ResourceTemplate.new(
|
|
95
|
+
uri_template: "rails-ai-context://routes/{controller}",
|
|
96
|
+
name: "Live Routes",
|
|
97
|
+
description: "Application routes introspected fresh on each request, optionally filtered by controller name",
|
|
98
|
+
mime_type: "application/json"
|
|
99
|
+
).freeze
|
|
100
|
+
|
|
73
101
|
class << self
|
|
74
102
|
def static_resources
|
|
75
103
|
STATIC_RESOURCES.map do |uri, meta|
|
|
@@ -83,7 +111,7 @@ module RailsAiContext
|
|
|
83
111
|
end
|
|
84
112
|
|
|
85
113
|
def resource_templates
|
|
86
|
-
[ MODEL_TEMPLATE ]
|
|
114
|
+
[ MODEL_TEMPLATE, CONTROLLER_TEMPLATE, CONTROLLER_ACTION_TEMPLATE, VIEW_TEMPLATE, ROUTES_TEMPLATE ]
|
|
87
115
|
end
|
|
88
116
|
|
|
89
117
|
def register(server)
|
|
@@ -100,6 +128,11 @@ module RailsAiContext
|
|
|
100
128
|
|
|
101
129
|
def handle_read(params)
|
|
102
130
|
uri = params[:uri]
|
|
131
|
+
|
|
132
|
+
# Delegate rails-ai-context:// URIs to the VFS dispatcher
|
|
133
|
+
return VFS.resolve(uri) if uri.start_with?("#{VFS::SCHEME}://")
|
|
134
|
+
|
|
135
|
+
# Legacy rails:// URI handling
|
|
103
136
|
context = RailsAiContext.introspect
|
|
104
137
|
|
|
105
138
|
if STATIC_RESOURCES.key?(uri)
|
|
@@ -67,11 +67,17 @@ module RailsAiContext
|
|
|
67
67
|
end
|
|
68
68
|
end
|
|
69
69
|
|
|
70
|
+
mcp_config = MCP::Configuration.new(
|
|
71
|
+
instrumentation_callback: Instrumentation.callback
|
|
72
|
+
)
|
|
73
|
+
|
|
70
74
|
server = MCP::Server.new(
|
|
71
75
|
name: config.server_name,
|
|
72
76
|
version: config.server_version,
|
|
77
|
+
instructions: "Ground truth engine for Rails apps. Live Prism AST introspection. Zero stale data.",
|
|
73
78
|
tools: active_tools(config) + validated_custom_tools,
|
|
74
|
-
resource_templates: Resources.resource_templates
|
|
79
|
+
resource_templates: Resources.resource_templates,
|
|
80
|
+
configuration: mcp_config
|
|
75
81
|
)
|
|
76
82
|
|
|
77
83
|
Resources.register(server)
|
|
@@ -8,6 +8,32 @@ module RailsAiContext
|
|
|
8
8
|
# Inherits from the official MCP::Tool to get schema validation,
|
|
9
9
|
# annotations, and protocol compliance for free.
|
|
10
10
|
class BaseTool < MCP::Tool
|
|
11
|
+
# Default output schema for all tools. MCP::Tool.inherited resets
|
|
12
|
+
# @output_schema_value to nil on each subclass, so we re-set it
|
|
13
|
+
# via our own inherited hook.
|
|
14
|
+
DEFAULT_OUTPUT_SCHEMA = MCP::Tool::OutputSchema.new(
|
|
15
|
+
type: "object",
|
|
16
|
+
properties: {
|
|
17
|
+
content: {
|
|
18
|
+
type: "array",
|
|
19
|
+
items: {
|
|
20
|
+
type: "object",
|
|
21
|
+
properties: {
|
|
22
|
+
type: { type: "string", enum: [ "text" ] },
|
|
23
|
+
text: { type: "string", description: "Tool response as Markdown-formatted text" }
|
|
24
|
+
},
|
|
25
|
+
required: [ "type", "text" ]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
required: [ "content" ]
|
|
30
|
+
).freeze
|
|
31
|
+
|
|
32
|
+
def self.inherited(subclass)
|
|
33
|
+
super
|
|
34
|
+
subclass.instance_variable_set(:@output_schema_value, DEFAULT_OUTPUT_SCHEMA)
|
|
35
|
+
end
|
|
36
|
+
|
|
11
37
|
# Shared cache across all tool subclasses, protected by a Mutex
|
|
12
38
|
# for thread safety in multi-threaded servers (e.g., Puma).
|
|
13
39
|
SHARED_CACHE = { mutex: Mutex.new }
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module RailsAiContext
|
|
6
|
+
# Virtual File System — pattern-matched URI routing for MCP resources.
|
|
7
|
+
# Each resolve call introspects fresh (zero stale data).
|
|
8
|
+
module VFS
|
|
9
|
+
SCHEME = "rails-ai-context"
|
|
10
|
+
|
|
11
|
+
PATTERNS = [
|
|
12
|
+
{ pattern: %r{\Arails-ai-context://controllers/([^/]+)/([^/]+)\z}, handler: :resolve_controller_action },
|
|
13
|
+
{ pattern: %r{\Arails-ai-context://controllers/([^/]+)\z}, handler: :resolve_controller },
|
|
14
|
+
{ pattern: %r{\Arails-ai-context://models/(.+)\z}, handler: :resolve_model },
|
|
15
|
+
{ pattern: %r{\Arails-ai-context://views/(.+)\z}, handler: :resolve_view },
|
|
16
|
+
{ pattern: %r{\Arails-ai-context://routes/(.+)\z}, handler: :resolve_routes }
|
|
17
|
+
].freeze
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
# Resolve a rails-ai-context:// URI to MCP resource content.
|
|
21
|
+
# Returns an array of content hashes: [{uri:, mime_type:, text:}]
|
|
22
|
+
def resolve(uri)
|
|
23
|
+
PATTERNS.each do |entry|
|
|
24
|
+
match = uri.match(entry[:pattern])
|
|
25
|
+
next unless match
|
|
26
|
+
|
|
27
|
+
return send(entry[:handler], uri, *match.captures)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
raise RailsAiContext::Error, "Unknown VFS URI: #{uri}"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def resolve_model(uri, name)
|
|
36
|
+
context = RailsAiContext.introspect
|
|
37
|
+
models = context[:models] || {}
|
|
38
|
+
|
|
39
|
+
# Case-insensitive lookup
|
|
40
|
+
key = models.keys.find { |k| k.to_s.casecmp?(name) } || name
|
|
41
|
+
data = models[key]
|
|
42
|
+
|
|
43
|
+
unless data
|
|
44
|
+
available = models.keys.sort.first(20)
|
|
45
|
+
content = JSON.pretty_generate(error: "Model '#{name}' not found", available: available)
|
|
46
|
+
return [ { uri: uri, mime_type: "application/json", text: content } ]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Enrich with schema columns if available
|
|
50
|
+
table_name = data[:table_name]
|
|
51
|
+
schema = context.dig(:schema, :tables, table_name) if table_name
|
|
52
|
+
enriched = data.merge(schema: schema).compact
|
|
53
|
+
|
|
54
|
+
[ { uri: uri, mime_type: "application/json", text: JSON.pretty_generate(enriched) } ]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def resolve_controller(uri, name)
|
|
58
|
+
context = RailsAiContext.introspect
|
|
59
|
+
controllers = context.dig(:controllers, :controllers) || {}
|
|
60
|
+
|
|
61
|
+
# Flexible matching: "posts", "PostsController", "postscontroller"
|
|
62
|
+
input_snake = name.underscore.delete_suffix("_controller")
|
|
63
|
+
key = controllers.keys.find { |k|
|
|
64
|
+
k.underscore.delete_suffix("_controller") == input_snake ||
|
|
65
|
+
k.downcase.delete_suffix("controller") == name.downcase.delete_suffix("controller")
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
unless key
|
|
69
|
+
available = controllers.keys.sort.first(20)
|
|
70
|
+
content = JSON.pretty_generate(error: "Controller '#{name}' not found", available: available)
|
|
71
|
+
return [ { uri: uri, mime_type: "application/json", text: content } ]
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
[ { uri: uri, mime_type: "application/json", text: JSON.pretty_generate(controllers[key]) } ]
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def resolve_controller_action(uri, controller_name, action_name)
|
|
78
|
+
context = RailsAiContext.introspect
|
|
79
|
+
controllers = context.dig(:controllers, :controllers) || {}
|
|
80
|
+
|
|
81
|
+
input_snake = controller_name.underscore.delete_suffix("_controller")
|
|
82
|
+
key = controllers.keys.find { |k|
|
|
83
|
+
k.underscore.delete_suffix("_controller") == input_snake
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
unless key
|
|
87
|
+
content = JSON.pretty_generate(error: "Controller '#{controller_name}' not found")
|
|
88
|
+
return [ { uri: uri, mime_type: "application/json", text: content } ]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
info = controllers[key]
|
|
92
|
+
actions = info[:actions] || []
|
|
93
|
+
action = actions.find { |a| a.to_s.casecmp?(action_name) }
|
|
94
|
+
|
|
95
|
+
unless action
|
|
96
|
+
content = JSON.pretty_generate(error: "Action '#{action_name}' not found in #{key}", available: actions)
|
|
97
|
+
return [ { uri: uri, mime_type: "application/json", text: content } ]
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Build action-specific data
|
|
101
|
+
action_data = {
|
|
102
|
+
controller: key,
|
|
103
|
+
action: action.to_s,
|
|
104
|
+
filters: (info[:filters] || []).select { |f|
|
|
105
|
+
if f[:only]&.any?
|
|
106
|
+
f[:only].map(&:to_s).include?(action.to_s)
|
|
107
|
+
elsif f[:except]&.any?
|
|
108
|
+
!f[:except].map(&:to_s).include?(action.to_s)
|
|
109
|
+
else
|
|
110
|
+
true
|
|
111
|
+
end
|
|
112
|
+
},
|
|
113
|
+
strong_params: info[:strong_params]
|
|
114
|
+
}.compact
|
|
115
|
+
|
|
116
|
+
[ { uri: uri, mime_type: "application/json", text: JSON.pretty_generate(action_data) } ]
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def resolve_view(uri, path)
|
|
120
|
+
# Block path traversal
|
|
121
|
+
if path.include?("..") || path.start_with?("/")
|
|
122
|
+
raise RailsAiContext::Error, "Path not allowed: #{path}"
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
views_dir = Rails.root.join("app", "views")
|
|
126
|
+
full_path = views_dir.join(path)
|
|
127
|
+
|
|
128
|
+
unless File.exist?(full_path)
|
|
129
|
+
content = JSON.pretty_generate(error: "View not found: #{path}")
|
|
130
|
+
return [ { uri: uri, mime_type: "application/json", text: content } ]
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Verify resolved path is still under views_dir
|
|
134
|
+
unless File.realpath(full_path).start_with?(File.realpath(views_dir))
|
|
135
|
+
raise RailsAiContext::Error, "Path not allowed: #{path}"
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
max_size = RailsAiContext.configuration.max_file_size
|
|
139
|
+
if File.size(full_path) > max_size
|
|
140
|
+
content = JSON.pretty_generate(error: "File too large: #{path} (#{File.size(full_path)} bytes)")
|
|
141
|
+
return [ { uri: uri, mime_type: "application/json", text: content } ]
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
view_content = RailsAiContext::SafeFile.read(full_path) || ""
|
|
145
|
+
mime = path.end_with?(".rb") ? "text/x-ruby" : "text/html"
|
|
146
|
+
[ { uri: uri, mime_type: mime, text: view_content } ]
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def resolve_routes(uri, controller)
|
|
150
|
+
context = RailsAiContext.introspect
|
|
151
|
+
routes_data = context[:routes] || {}
|
|
152
|
+
|
|
153
|
+
filtered = (routes_data[:routes] || []).select { |r|
|
|
154
|
+
r[:controller].to_s.include?(controller)
|
|
155
|
+
}
|
|
156
|
+
data = routes_data.merge(routes: filtered, filtered_by: controller)
|
|
157
|
+
|
|
158
|
+
[ { uri: uri, mime_type: "application/json", text: JSON.pretty_generate(data) } ]
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
data/lib/rails_ai_context.rb
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
require "zeitwerk"
|
|
4
4
|
|
|
5
5
|
loader = Zeitwerk::Loader.for_gem(warn_on_extra_files: false)
|
|
6
|
-
loader.inflector.inflect("devops_introspector" => "DevOpsIntrospector", "cli" => "CLI")
|
|
6
|
+
loader.inflector.inflect("devops_introspector" => "DevOpsIntrospector", "cli" => "CLI", "vfs" => "VFS")
|
|
7
7
|
loader.ignore("#{__dir__}/generators")
|
|
8
8
|
loader.ignore("#{__dir__}/rails-ai-context.rb")
|
|
9
9
|
loader.setup
|
data/server.json
CHANGED
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
"url": "https://github.com/crisnahine/rails-ai-context",
|
|
8
8
|
"source": "github"
|
|
9
9
|
},
|
|
10
|
-
"version": "5.
|
|
10
|
+
"version": "5.4.0",
|
|
11
11
|
"packages": [
|
|
12
12
|
{
|
|
13
13
|
"registryType": "mcpb",
|
|
14
|
-
"identifier": "https://github.com/crisnahine/rails-ai-context/releases/download/v5.
|
|
14
|
+
"identifier": "https://github.com/crisnahine/rails-ai-context/releases/download/v5.4.0/rails-ai-context-mcp.mcpb",
|
|
15
15
|
"fileSha256": "0000000000000000000000000000000000000000000000000000000000000000",
|
|
16
16
|
"transport": {
|
|
17
17
|
"type": "stdio"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails-ai-context
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 5.
|
|
4
|
+
version: 5.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- crisnahine
|
|
@@ -207,6 +207,8 @@ files:
|
|
|
207
207
|
- README.md
|
|
208
208
|
- Rakefile
|
|
209
209
|
- SECURITY.md
|
|
210
|
+
- app/controllers/rails_ai_context/mcp_controller.rb
|
|
211
|
+
- config/routes.rb
|
|
210
212
|
- demo/demo-trace.gif
|
|
211
213
|
- demo/demo-trace.tape
|
|
212
214
|
- demo/demo.gif
|
|
@@ -229,6 +231,7 @@ files:
|
|
|
229
231
|
- lib/rails_ai_context/hydrators/hydration_formatter.rb
|
|
230
232
|
- lib/rails_ai_context/hydrators/schema_hint_builder.rb
|
|
231
233
|
- lib/rails_ai_context/hydrators/view_hydrator.rb
|
|
234
|
+
- lib/rails_ai_context/instrumentation.rb
|
|
232
235
|
- lib/rails_ai_context/introspector.rb
|
|
233
236
|
- lib/rails_ai_context/introspectors/action_mailbox_introspector.rb
|
|
234
237
|
- lib/rails_ai_context/introspectors/action_text_introspector.rb
|
|
@@ -333,6 +336,7 @@ files:
|
|
|
333
336
|
- lib/rails_ai_context/tools/session_context.rb
|
|
334
337
|
- lib/rails_ai_context/tools/validate.rb
|
|
335
338
|
- lib/rails_ai_context/version.rb
|
|
339
|
+
- lib/rails_ai_context/vfs.rb
|
|
336
340
|
- lib/rails_ai_context/watcher.rb
|
|
337
341
|
- server.json
|
|
338
342
|
homepage: https://github.com/crisnahine/rails-ai-context
|