Class: RailsVitals::MCP::RequestHandler

Inherits:
Object
  • Object
show all
Includes:
ResponseBuilder
Defined in:
lib/rails_vitals/mcp/request_handler.rb

Constant Summary collapse

SERVER_INFO =
{
  name: "railsvitals",
  version: RailsVitals::VERSION
}.freeze
PROTOCOL_VERSION =
"2025-11-25"
JSON_RPC_VERSION =
"2.0"
INSTRUCTIONS =
<<~TEXT.strip
  RailsVitals exposes Rails app performance diagnostics.
  Recommended flow: call railsvitals_get_schema_context first to understand
  the data model, then railsvitals_get_score for a high-level diagnosis,
  then drill down with the specific query tools.
TEXT

Constants included from ResponseBuilder

RailsVitals::MCP::ResponseBuilder::AUTH_ERROR, RailsVitals::MCP::ResponseBuilder::INVALID_PARAMS, RailsVitals::MCP::ResponseBuilder::INVALID_REQUEST, RailsVitals::MCP::ResponseBuilder::METHOD_NOT_FOUND, RailsVitals::MCP::ResponseBuilder::PARSE_ERROR, RailsVitals::MCP::ResponseBuilder::TOOL_EXEC_ERROR

Instance Method Summary collapse

Methods included from ResponseBuilder

error, success, tool_success

Instance Method Details

#handle(raw_body) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rails_vitals/mcp/request_handler.rb', line 21

def handle(raw_body)
  payload = parse_json(raw_body)
  return payload if payload.is_a?(Hash) && payload[:error]

  id = payload[:id]
  method = payload[:method]

  unless method.present?
    return ResponseBuilder.error(id, ResponseBuilder::INVALID_REQUEST, "Missing method")
  end

  case method
  when "initialize" then handle_initialize(id, payload[:params] || {})
  when "tools/list" then handle_tools_list(id)
  when "tools/call" then handle_tools_call(id, payload[:params] || {})
  else
    ResponseBuilder.error(id, ResponseBuilder::METHOD_NOT_FOUND, "Method not found: #{method}")
  end
end