Class: Legion::MCP::Tools::DoAction

Inherits:
MCP::Tool
  • Object
show all
Extended by:
Logging::Helper
Defined in:
lib/legion/mcp/tools/do_action.rb

Overview

rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Class Method Details

.call(intent:, params: {}, context: {}) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



37
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
67
68
69
70
# File 'lib/legion/mcp/tools/do_action.rb', line 37

def call(intent:, params: {}, context: {}) # rubocop:disable Metrics/CyclomaticComplexity
  request_id = Utils.request_id_from(context, params) || "mcp_#{SecureRandom.hex(6)}"
  normalized_context = symbolize_hash(context).merge(request_id: request_id)
  tool_params = params.transform_keys(&:to_sym)

  mcp_log :info, 'do_action.start',
          request_id: request_id, intent: Utils.summarize_text(intent),
          params: Utils.summarize_params(tool_params),
          context_keys: normalized_context.keys.map(&:to_s)

  tier_result = try_tier0(intent, tool_params, normalized_context, request_id: request_id)
  log_tier_result(request_id, tier_result)

  case tier_result&.dig(:tier)
  when 0
    return handle_tier0(tier_result, request_id)
  when 1
    response = handle_tier1(intent, tier_result, request_id)
    return response if response
  when 2
    response = handle_tier2(intent, request_id)
    return response if response
  end

  fallback_context_compiler(intent, tool_params, request_id)
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'legion.mcp.tools.do_action.call')
  mcp_log :warn, 'do_action.failed',
          request_id: defined?(request_id) ? request_id : nil,
          matched:    defined?(matched_name) ? matched_name : nil,
          error:      e.message
  record_feedback(intent, matched_name, success: false) if defined?(matched_name)
  error_response("Failed: #{e.message}")
end