Class: TalkToYourApp::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/talk_to_your_app/railtie.rb

Overview

Wires the gem into the Rails boot sequence. The headline behavior is the fail-closed validation initializer: every required piece of configuration is checked once, at the end of boot, so misconfiguration surfaces at deploy time rather than on the first MCP request.

Class Method Summary collapse

Class Method Details

.validate_api_key_values!Object

A blank API key can never authenticate anything (an empty Bearer token is rejected before comparison), so an entry with one is a dead credential — yet it counts toward auth_configured? and the app would boot serving 401 to every request. This is the config.api_keys = { "x" => ENV["TTYA_KEY"] } footgun with the variable unset: fail at deploy, like every other missing piece of configuration, rather than shipping an endpoint nobody can reach.

Raises:



65
66
67
68
69
70
71
72
73
# File 'lib/talk_to_your_app/railtie.rb', line 65

def self.validate_api_key_values!
  blank = TalkToYourApp.configuration.api_keys.select { |_name, key| key.to_s.strip.empty? }.keys
  return if blank.empty?

  raise ConfigurationError,
    "talk_to_your_app: API key(s) #{blank.map(&:to_s).inspect} have a blank value — a blank key can " \
    "never authenticate, so the endpoint would reject every request. Check that the ENV var or " \
    "credential backing each key in `config.api_keys` is actually set."
end

.validate_auth!Object

At least one auth mechanism must be configured once the endpoint serves tools. With no plugins enabled (the default) there is nothing to protect, so the gem still boots cleanly with no configuration at all.

Raises:



48
49
50
51
52
53
54
55
56
57
# File 'lib/talk_to_your_app/railtie.rb', line 48

def self.validate_auth!
  return if TalkToYourApp.configuration.enabled_plugins.empty?

  validate_api_key_values!
  return if TalkToYourApp.configuration.auth_configured?

  raise ConfigurationError,
    "talk_to_your_app: no authentication configured. Set `config.api_keys` or `config.basic_auth` " \
    "in config/initializers/talk_to_your_app.rb (the MCP endpoint must not be exposed unauthenticated)."
end

.validate_boot!Object

Aggregated fail-closed validation. With nothing enabled (the default), validation is a no-op and the gem boots silently. With config.enabled false the gem serves nothing, so there is nothing to validate — boot silently. (Re-enabling means a reboot, where this runs again and fails closed on missing auth or connections.)



37
38
39
40
41
42
43
# File 'lib/talk_to_your_app/railtie.rb', line 37

def self.validate_boot!
  return unless TalkToYourApp.configuration.enabled

  TalkToYourApp::PluginRegistry.validate_enabled!
  TalkToYourApp::ConnectionRegistry.validate!(TalkToYourApp.required_connections)
  validate_auth!
end