Class: RailsAiContext::Server

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

Overview

Configures and starts an MCP server using the official Ruby SDK. Registers all introspection tools and handles transport selection.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, transport: :stdio) ⇒ Server

Returns a new instance of Server.



32
33
34
35
# File 'lib/rails_ai_context/server.rb', line 32

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



9
10
11
# File 'lib/rails_ai_context/server.rb', line 9

def app
  @app
end

#transport_typeObject (readonly)

Returns the value of attribute transport_type.



9
10
11
# File 'lib/rails_ai_context/server.rb', line 9

def transport_type
  @transport_type
end

Class Method Details

.builtin_toolsObject

All built-in tools, auto-discovered from Tools::BaseTool subclasses. Kept as a class method (not a constant) so auto-registration works. Legacy constant accessor preserved for backwards compatibility.



14
15
16
# File 'lib/rails_ai_context/server.rb', line 14

def self.builtin_tools
  Tools::BaseTool.registered_tools
end

.const_missing(name) ⇒ Object

Backwards-compatible constant — delegates to the registry. Existing code referencing Server::TOOLS continues to work. Emits a deprecation notice once to guide migration.



21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_ai_context/server.rb', line 21

def self.const_missing(name)
  if name == :TOOLS
    unless @tools_deprecation_warned
      @tools_deprecation_warned = true
      $stderr.puts "[rails-ai-context] DEPRECATION: Server::TOOLS is deprecated, use Server.builtin_tools instead" if ENV["DEBUG"]
    end
    return builtin_tools
  end
  super
end

Instance Method Details

#buildObject

Build and return the configured MCP::Server instance



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails_ai_context/server.rb', line 38

def build
  config = RailsAiContext.configuration

  validated_custom_tools = config.custom_tools.select do |tool|
    if tool.is_a?(Class) && tool < MCP::Tool
      true
    else
      $stderr.puts "[rails-ai-context] WARNING: Skipping invalid custom_tool #{tool.inspect} (must be an MCP::Tool subclass)"
      false
    end
  end

  mcp_config = MCP::Configuration.new(
    instrumentation_callback: Instrumentation.callback
  )

  server = MCP::Server.new(
    name: config.server_name,
    version: config.server_version,
    instructions: "Ground truth engine for Rails apps. Live Prism AST introspection. Zero stale data.",
    tools: active_tools(config) + validated_custom_tools,
    resource_templates: Resources.resource_templates,
    configuration: mcp_config
  )

  Resources.register(server)

  server
end

#startObject

Start the MCP server with the configured transport



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rails_ai_context/server.rb', line 69

def start
  server = build

  case transport_type
  when :stdio
    start_stdio(server)
  when :http, :streamable_http
    start_http(server)
  else
    raise ConfigurationError, "Unknown transport: #{transport_type}. Use :stdio or :http"
  end
end