Module: RailsAiBridge::Resources

Defined in:
lib/rails_ai_bridge/resources.rb

Overview

Registers MCP resources and resource templates that expose static introspection data AI clients can read directly.

Architectural role: intentional protocol facade

Resources is deliberately the single read-side gateway between every data-producing subsystem and the MCP resource surface. Its edges to ContextProvider (cached context), Registry::RegistryManifest (dynamic context-provider resources), Serializers::ContextSummary (sanitization), ViewFileAnalyzer, Server, and Doctor::Checkers::BridgeMetadataChecker exist because MCP requires one registration and dispatch point — the subsystems only meet here.

It is NOT a god module despite high betweenness centrality in graph analyses: a protocol boundary that declares URIs, dispatches reads, and sanitizes output is expected to sit between otherwise unrelated communities. Splitting it would scatter URI ownership and the sanitization guard across multiple files.

If a future graph analysis flags this module, check the betweenness against the "facade at a protocol boundary" pattern before proposing a split. The one edge to watch instead: sanitize_conventions_section reaches into Serializers::ContextSummary internals; extract a shared sanitizer if serializers are refactored.

Constant Summary collapse

MODEL_URI_PATTERN =

URI pattern for matching model resource URIs (rails://models/name)

%r{\Arails://models/(.+)\z}
VIEW_URI_PATTERN =

URI pattern for matching view resource URIs (rails://views/path)

%r{\Arails://views/(.+)\z}
STIMULUS_URI_PATTERN =

URI pattern for matching stimulus controller URIs (rails://stimulus/name)

%r{\Arails://stimulus/(.+)\z}
CONTEXT_PROVIDER_URI_PATTERN =

URI pattern for matching context provider URIs (rails://context-providers/name)

%r{\Arails://context-providers/(.+)\z}
JSON_MIME_TYPE =

Standard MIME type for all JSON resources

'application/json'
RESOURCE_SECTION_SANITIZERS =

Section-specific sanitizers applied before exposing static MCP resources.

{
  conventions: :sanitize_conventions_section
}.freeze
UNKNOWN_RESOURCE_ERROR =

Error message template for unknown resources

'Unknown resource: %s'
MODEL_NOT_FOUND_ERROR =

Error message template for missing models

"Model '%s' not found"
VIEW_NOT_FOUND_ERROR =

Error message template for missing views

"View '%s' not found"
STIMULUS_NOT_FOUND_ERROR =

Error message template for missing stimulus controllers

"Stimulus controller '%s' not found"
STATIC_RESOURCES =

Static resource definitions for built-in MCP resources Maps URIs to metadata including name, description, and context keys

{
  'rails://bridge/meta' => {
    name: 'Bridge Metadata',
    description: 'Bridge runtime metadata including version, enabled introspectors, tools, ' \
                 'resources, and cache settings',
    mime_type: JSON_MIME_TYPE
  },
  'rails://schema' => {
    name: 'Database Schema',
    description: 'Full database schema including tables, columns, indexes, and foreign keys',
    mime_type: JSON_MIME_TYPE,
    key: :schema
  },
  'rails://routes' => {
    name: 'Application Routes',
    description: 'All routes with HTTP verbs, paths, and controller actions',
    mime_type: JSON_MIME_TYPE,
    key: :routes
  },
  'rails://conventions' => {
    name: 'Conventions & Patterns',
    description: 'Detected architecture patterns, conventions, and directory structure',
    mime_type: JSON_MIME_TYPE,
    key: :conventions
  },
  'rails://gems' => {
    name: 'Notable Gems',
    description: 'Gem dependencies categorized by function with explanations',
    mime_type: JSON_MIME_TYPE,
    key: :gems
  },
  'rails://controllers' => {
    name: 'Controllers',
    description: 'All controllers with actions, filters, strong params, and concerns',
    mime_type: JSON_MIME_TYPE,
    key: :controllers
  },
  'rails://config' => {
    name: 'Application Config',
    description: 'Application configuration including cache, sessions, middleware, and initializers',
    mime_type: JSON_MIME_TYPE,
    key: :config
  },
  'rails://tests' => {
    name: 'Test Infrastructure',
    description: 'Test framework, factories, fixtures, CI, and coverage configuration',
    mime_type: JSON_MIME_TYPE,
    key: :tests
  },
  'rails://migrations' => {
    name: 'Migrations',
    description: 'Migration history, pending migrations, and migration statistics',
    mime_type: JSON_MIME_TYPE,
    key: :migrations
  },
  'rails://engines' => {
    name: 'Mounted Engines',
    description: 'Mounted Rails engines and Rack apps with paths and descriptions',
    mime_type: JSON_MIME_TYPE,
    key: :engines
  },
  'rails://views' => {
    name: 'Views',
    description: 'View layer structure including layouts, templates, partials, helpers, and components',
    mime_type: JSON_MIME_TYPE,
    key: :views
  },
  'rails://stimulus' => {
    name: 'Stimulus Controllers',
    description: 'Stimulus controller inventory with targets, values, actions, outlets, and classes',
    mime_type: JSON_MIME_TYPE,
    key: :stimulus
  },
  'rails://semantic/analysis' => {
    name: 'Semantic Analysis',
    description: 'Rubydex-powered semantic analysis including pattern detection, ' \
                 'relationship mapping, complexity hotspots, and codebase statistics',
    mime_type: JSON_MIME_TYPE,
    key: :semantic
  }
}.freeze

Class Method Summary collapse

Class Method Details

.build_resources ⇒ Array<MCP::Resource>

Builds the list of static MCP::Resource objects for all registered URIs. Intended to be passed to MCP::Server.new(resources: ...) at construction time.

Returns:

  • (Array<MCP::Resource>)


162
163
164
165
166
167
168
169
170
171
# File 'lib/rails_ai_bridge/resources.rb', line 162

def build_resources
  resource_definitions.map do |uri, meta|
    MCP::Resource.new(
      uri: uri,
      name: meta[:name],
      description: meta[:description],
      mime_type: meta[:mime_type]
    )
  end
end

.build_templates ⇒ Array<MCP::ResourceTemplate>

Builds the list of MCP::ResourceTemplate objects for URI-template resources. Intended to be passed to MCP::Server.new(resource_templates: ...) at construction time.

Returns:

  • (Array<MCP::ResourceTemplate>)


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/rails_ai_bridge/resources.rb', line 177

def build_templates
  [
    MCP::ResourceTemplate.new(
      uri_template: 'rails://models/{name}',
      name: 'Model Details',
      description: 'Detailed information about a specific ActiveRecord model',
      mime_type: JSON_MIME_TYPE
    ),
    MCP::ResourceTemplate.new(
      uri_template: 'rails://views/{path}',
      name: 'View Details',
      description: 'Detailed information about a specific view template or partial',
      mime_type: JSON_MIME_TYPE
    ),
    MCP::ResourceTemplate.new(
      uri_template: 'rails://stimulus/{name}',
      name: 'Stimulus Controller Details',
      description: 'Detailed information about a specific Stimulus controller',
      mime_type: JSON_MIME_TYPE
    ),
    MCP::ResourceTemplate.new(
      uri_template: 'rails://context-providers/{name}',
      name: 'Context Provider Details',
      description: 'Detailed information about a specific context provider declared in the registry manifest',
      mime_type: JSON_MIME_TYPE
    )
  ]
end

.register(server) ⇒ void

This method returns an undefined value.

Registers the resources/read handler on an already-constructed MCP server. Resources and templates must be passed to MCP::Server.new before calling this — see build_resources and build_templates.

Parameters:

  • server (MCP::Server) —

    server instance to register the handler on



212
213
214
215
216
217
218
# File 'lib/rails_ai_bridge/resources.rb', line 212

def register(server)
  require 'json'

  server.resources_read_handler do |params|
    handle_read(params)
  end
end

.resource_definitions ⇒ Hash{String => Hash}

Returns built-in and user-defined MCP resource definitions.

Returns:

  • (Hash{String => Hash}) —

    resource definitions keyed by URI



152
153
154
155
156
# File 'lib/rails_ai_bridge/resources.rb', line 152

def resource_definitions
  STATIC_RESOURCES
    .merge(context_provider_resources)
    .merge(RailsAiBridge.configuration.additional_resources)
end