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::GetGems,
  Tools::SearchCode,
  Tools::SearchSemantic,
  Tools::GetConventions,
  Tools::GetControllers,
  Tools::GetConfig,
  Tools::GetTestInfo,
  Tools::GetView,
  Tools::GetStimulus,
  Tools::ListRegistry,
  Tools::ResolveSkill
].freeze

Instance Attribute 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)



52
53
54
55
# File 'lib/rails_ai_bridge/server.rb', line 52

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#transport_typeObject (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

Instance Method Details

#buildMCP::Server

Build and return the configured MCP::Server instance.

Returns:

  • (MCP::Server)

    configured MCP server



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

def build
  config = RailsAiBridge.configuration

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

#startvoid

This method returns an undefined value.

Start the MCP server with the configured transport.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails_ai_bridge/server.rb', line 80

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_classesArray<Class, ToolResultCache::CachedTool>

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

Returns:



62
63
64
65
66
# File 'lib/rails_ai_bridge/server.rb', line 62

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