Class: RailsActiveMcp::Sdk::Tools::SafeQueryTool

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/rails_active_mcp/sdk/tools/safe_query_tool.rb

Class Method Summary collapse

Class Method Details

.call(model:, method:, server_context:, args: [], limit: 100) ⇒ Object



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
# File 'lib/rails_active_mcp/sdk/tools/safe_query_tool.rb', line 41

def self.call(model:, method:, server_context:, args: [], limit: 100)
  config = RailsActiveMcp.config

  executor = RailsActiveMcp::ConsoleExecutor.new(config)

  result = executor.execute_safe_query(
    model: model,
    method: method,
    args: args,
    limit: limit
  )

  if result[:success]
    output = []
    output << "Query: #{model}.#{method}(#{args.join(', ')})"
    output << "Count: #{result[:count]}" if result[:count]
    output << "Result: #{result[:result].inspect}"

    MCP::Tool::Response.new([
                              { type: 'text', text: output.join("\n") }
                            ])
  else
    error_response(result[:error])
  end
end

.error_response(message) ⇒ Object



67
68
69
70
71
72
# File 'lib/rails_active_mcp/sdk/tools/safe_query_tool.rb', line 67

def self.error_response(message)
  MCP::Tool::Response.new(
    [{ type: 'text', text: message }],
    error: true
  )
end