Module: CompletionKit::McpTools::Runs

Defined in:
app/services/completion_kit/mcp_tools/runs.rb

Constant Summary collapse

TOOLS =
{
  "runs_list" => {
    description: "List all runs",
    inputSchema: {type: "object", properties: {}, required: []},
    handler: :list
  },
  "runs_get" => {
    description: "Get a run by ID",
    inputSchema: {type: "object", properties: {id: {type: "integer"}}, required: ["id"]},
    handler: :get
  },
  "runs_create" => {
    description: "Create a run",
    inputSchema: {
      type: "object",
      properties: {
        name: {type: "string"}, prompt_id: {type: "integer"},
        dataset_id: {type: "integer"}, judge_model: {type: "string"},
        metric_ids: {type: "array", items: {type: "integer"}}
      },
      required: ["name", "prompt_id"]
    },
    handler: :create
  },
  "runs_update" => {
    description: "Update a run",
    inputSchema: {
      type: "object",
      properties: {
        id: {type: "integer"}, name: {type: "string"},
        dataset_id: {type: "integer"}, judge_model: {type: "string"},
        metric_ids: {type: "array", items: {type: "integer"}}
      },
      required: ["id"]
    },
    handler: :update
  },
  "runs_delete" => {
    description: "Delete a run",
    inputSchema: {type: "object", properties: {id: {type: "integer"}}, required: ["id"]},
    handler: :delete
  },
  "runs_generate" => {
    description: "Generate responses for a run using its prompt and dataset",
    inputSchema: {type: "object", properties: {id: {type: "integer"}}, required: ["id"]},
    handler: :generate
  },
  "runs_judge" => {
    description: "Judge responses for a run using configured metrics",
    inputSchema: {type: "object", properties: {id: {type: "integer"}}, required: ["id"]},
    handler: :judge
  }
}.freeze

Class Method Summary collapse

Class Method Details

.call(name, arguments) ⇒ Object



62
63
64
65
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 62

def self.call(name, arguments)
  tool = TOOLS.fetch(name)
  send(tool[:handler], arguments)
end

.create(args) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 75

def self.create(args)
  run = Run.new(args.slice("name", "prompt_id", "dataset_id", "judge_model"))
  if run.save
    replace_run_metrics(run, args["metric_ids"])
    text_result(run.reload.as_json)
  else
    error_result(run.errors.full_messages.join(", "))
  end
end

.definitionsObject



58
59
60
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 58

def self.definitions
  TOOLS.map { |name, config| {name: name, description: config[:description], inputSchema: config[:inputSchema]} }
end

.delete(args) ⇒ Object



95
96
97
98
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 95

def self.delete(args)
  Run.find(args["id"]).destroy!
  text_result("Run #{args["id"]} deleted")
end

.error_result(message) ⇒ Object



117
118
119
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 117

def self.error_result(message)
  {content: [{type: "text", text: message}], isError: true}
end

.generate(args) ⇒ Object



100
101
102
103
104
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 100

def self.generate(args)
  run = Run.find(args["id"])
  GenerateJob.perform_later(run.id)
  text_result(run.reload.as_json)
end

.get(args) ⇒ Object



71
72
73
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 71

def self.get(args)
  text_result(Run.find(args["id"]).as_json)
end

.judge(args) ⇒ Object



106
107
108
109
110
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 106

def self.judge(args)
  run = Run.find(args["id"])
  JudgeJob.perform_later(run.id)
  text_result(run.reload.as_json)
end

.list(_args) ⇒ Object



67
68
69
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 67

def self.list(_args)
  text_result(Run.order(created_at: :desc).map(&:as_json))
end

.replace_run_metrics(run, metric_ids) ⇒ Object



121
122
123
124
125
126
127
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 121

def self.replace_run_metrics(run, metric_ids)
  return unless metric_ids
  run.run_metrics.delete_all
  Array(metric_ids).reject(&:blank?).each_with_index do |metric_id, index|
    run.run_metrics.create!(metric_id: metric_id, position: index + 1)
  end
end

.text_result(data) ⇒ Object



112
113
114
115
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 112

def self.text_result(data)
  text = data.is_a?(String) ? data : data.to_json
  {content: [{type: "text", text: text}]}
end

.update(args) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'app/services/completion_kit/mcp_tools/runs.rb', line 85

def self.update(args)
  run = Run.find(args["id"])
  if run.update(args.except("id", "metric_ids").slice("name", "dataset_id", "judge_model"))
    replace_run_metrics(run, args["metric_ids"]) if args.key?("metric_ids")
    text_result(run.reload.as_json)
  else
    error_result(run.errors.full_messages.join(", "))
  end
end