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'
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::GetConventions,
  Tools::GetControllers,
  Tools::GetConfig,
  Tools::GetTestInfo,
  Tools::GetView,
  Tools::GetStimulus
].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)



44
45
46
47
# File 'lib/rails_ai_bridge/server.rb', line 44

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



57
58
59
60
61
62
63
# File 'lib/rails_ai_bridge/server.rb', line 57

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.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rails_ai_bridge/server.rb', line 67

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>

Returns all available tool classes including additional configured tools.

Returns:

  • (Array<Class>)

    list of tool classes



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

def tool_classes
  TOOLS + RailsAiBridge.configuration.additional_tools
end