Method: ActionMCP::Configuration#load_profiles

Defined in:
lib/action_mcp/configuration.rb

#load_profilesObject

Load custom configuration from Rails configuration



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/action_mcp/configuration.rb', line 113

def load_profiles
  # First load defaults from the gem
  @profiles = default_profiles

  # Preserve any settings that were already set via Rails config
  preserved_name = @name

  # Try to load from config/mcp.yml in the Rails app using Rails.config_for
  begin
    app_config = Rails.application.config_for(:mcp)

    raise "Invalid MCP config file" unless app_config.is_a?(Hash)

    # Extract authentication configuration if present
    # Handle both symbol and string keys
    @authentication_methods = Array(app_config[:authentication] || app_config["authentication"]) if app_config[:authentication] || app_config["authentication"]

    # Extract other top-level configuration settings
    extract_top_level_settings(app_config)

    # Extract profiles configuration - merge with defaults instead of replacing
    # Rails.config_for returns OrderedOptions which uses symbol keys
    if app_config[:profiles] || app_config["profiles"]
      # Get profiles with either symbol or string key
      app_profiles = app_config[:profiles] || app_config["profiles"]

      # Convert to regular hash and deep symbolize keys
      if app_profiles.is_a?(ActiveSupport::OrderedOptions)
        app_profiles = app_profiles.to_h.deep_symbolize_keys
      elsif app_profiles.respond_to?(:deep_symbolize_keys)
        app_profiles = app_profiles.deep_symbolize_keys
      end

      Rails.logger.debug "[Configuration] Merging profiles: #{app_profiles.inspect}" if @verbose_logging
      @profiles = @profiles.deep_merge(app_profiles)
    end
  rescue StandardError => e
    # If the config file doesn't exist in the Rails app, just use the defaults
    Rails.logger.warn "[Configuration] Failed to load MCP config: #{e.class} - #{e.message}"
    # No MCP config found in Rails app, using defaults from gem
  end

  # Apply the active profile
  Rails.logger.info "[ActionMCP] Loaded profiles: #{@profiles.keys.join(', ')}" if @verbose_logging
  Rails.logger.info "[ActionMCP] Using profile: #{@active_profile}" if @verbose_logging
  use_profile(@active_profile)

  # Restore preserved settings
  @name = preserved_name if preserved_name

  self
end