Class: RailsAiBridge::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/server.rb

Overview

Configures and starts an MCP server using the official Ruby SDK. Registers all introspection tools and handles transport selection. Supports both stdio and HTTP transports with proper error handling.

Constant Summary collapse

STDIO_TRANSPORT =

Transport type constants for type safety and consistency

:stdio
HTTP_TRANSPORT =
:http
STREAMABLE_HTTP_TRANSPORT =
:streamable_http
STDIO_STARTUP_MESSAGE =

Log message templates for consistent logging

'[rails-ai-bridge] MCP server started (stdio transport)'
HTTP_STARTUP_MESSAGE =
'[rails-ai-bridge] MCP server starting on %s:%s%s'
TOOLS_LIST_MESSAGE =
'[rails-ai-bridge] Tools: %s'
UNKNOWN_TRANSPORT_ERROR =

Error message template for unknown transport types

'Unknown transport: %s. Use :stdio, :http, or :streamable_http'
HTTP_AUTH_WARNING =

Warning printed when HTTP MCP starts without authentication in non-production environments.

'[rails-ai-bridge] WARNING: HTTP MCP is running without authentication. ' \
'Set config.mcp.require_http_auth = true or configure http_mcp_token, ' \
'mcp_token_resolver, or mcp_jwt_decoder before exposing this endpoint.'
TOOLS =

Built-in MCP tools that are always available These tools provide Rails application introspection capabilities

[
  Tools::GetSchema,
  Tools::GetRoutes,
  Tools::GetModelDetails,
  Tools::GetContext,
  Tools::GetGems,
  Tools::SearchCode,
  Tools::SearchSemantic,
  Tools::Query,
  Tools::ReadLogs,
  Tools::ExplainSymbol,
  Tools::GetConventions,
  Tools::GetControllers,
  Tools::GetConfig,
  Tools::GetTestInfo,
  Tools::GetView,
  Tools::GetStimulus,
  Tools::ListRegistry,
  Tools::ResolveSkill,
  Tools::UseSkill,
  Tools::UseAgent,
  Tools::ListContextProviders,
  Tools::GetProviderContext
].freeze
DATA_TOOLS =

Data-access tools reach live application data (database rows, log files) and bypass MCP introspection exclusions. They stay in TOOLS but are only registered when config.enable_data_tools is true (default: false).

Returns:

  • (Array<Class>) —

    tool classes gated behind config.enable_data_tools

[
  Tools::Query,
  Tools::ReadLogs
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, transport: STDIO_TRANSPORT) ⇒ Server

Initialize a new MCP server instance.

Parameters:

  • app (String, Object) —

    Rails application instance or name

  • transport (Symbol) (defaults to: STDIO_TRANSPORT) —

    transport type (:stdio, :http, or :streamable_http)



70
71
72
73
# File 'lib/rails_ai_bridge/server.rb', line 70

def initialize(app, transport: STDIO_TRANSPORT)
  @app = app
  @transport_type = transport
end

Instance Attribute Details

#app ⇒ Object (readonly)

Returns the value of attribute app.



10
11
12
# File 'lib/rails_ai_bridge/server.rb', line 10

def app
  @app
end

#transport_type ⇒ Object (readonly)

Returns the value of attribute transport_type.



10
11
12
# File 'lib/rails_ai_bridge/server.rb', line 10

def transport_type
  @transport_type
end

Class Method Details

.builtin_tools ⇒ Array<Class>

Built-in tools visible to clients. The data-access tools (DATA_TOOLS) are opt-in via config.enable_data_tools because they expose live data that MCP introspection exclusions cannot restrict.

Returns:

  • (Array<Class>) —

    built-in tool classes honoring the data-tool flag



80
81
82
# File 'lib/rails_ai_bridge/server.rb', line 80

def self.builtin_tools
  RailsAiBridge.configuration.enable_data_tools ? TOOLS : TOOLS - DATA_TOOLS
end

Instance Method Details

#build ⇒ MCP::Server

Build and return the configured MCP::Server instance.

Returns:

  • (MCP::Server) —

    configured MCP server



98
99
100
101
102
103
104
# File 'lib/rails_ai_bridge/server.rb', line 98

def build
  config = RailsAiBridge.configuration

  server = create_mcp_server(config)
  register_resources(server)
  server
end

#start ⇒ void

This method returns an undefined value.

Start the MCP server with the configured transport.



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rails_ai_bridge/server.rb', line 108

def start
  server = build

  case transport_type
  when STDIO_TRANSPORT
    start_stdio(server)
  when HTTP_TRANSPORT, STREAMABLE_HTTP_TRANSPORT
    start_http(server)
  else
    raise ConfigurationError, UNKNOWN_TRANSPORT_ERROR % transport_type
  end
end

#tool_classes ⇒ Array<Class, ToolResultCache::CachedTool>

Returns all available tool classes including additional configured tools. Data-access tools are only included when config.enable_data_tools is true. When tool result caching is enabled, built-in and additional tools are wrapped so identical argument fingerprints reuse cached MCP::Tool::Response objects.

Returns:



90
91
92
93
94
# File 'lib/rails_ai_bridge/server.rb', line 90

def tool_classes
  (self.class.builtin_tools + RailsAiBridge.configuration.additional_tools).map do |tool_class|
    Instrumentation::InstrumentedTool.new(ToolResultCache.maybe_wrap(tool_class))
  end
end