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.

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}
JSON_MIME_TYPE =

Standard MIME type for all JSON resources

'application/json'
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
  }
}.freeze

Class Method Summary collapse

Class Method Details

.build_resourcesArray<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>)


123
124
125
126
127
128
129
130
131
132
# File 'lib/rails_ai_bridge/resources.rb', line 123

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_templatesArray<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>)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rails_ai_bridge/resources.rb', line 138

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
    )
  ]
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



167
168
169
170
171
172
173
# File 'lib/rails_ai_bridge/resources.rb', line 167

def register(server)
  require 'json'

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

.resource_definitionsHash{String => Hash}

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

Returns:

  • (Hash{String => Hash})

    resource definitions keyed by URI



115
116
117
# File 'lib/rails_ai_bridge/resources.rb', line 115

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