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/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/legion/mcp/tools/do_action.rb', line 38

def call(intent:, params: {}, context: {}) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  request_id = LoggingSupport.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)

  LoggingSupport.info(
    'do_action.start',
    request_id:   request_id,
    intent:       LoggingSupport.summarize_text(intent),
    params:       LoggingSupport.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)

  if tier_result
    LoggingSupport.info(
      'do_action.tier_result',
      request_id: request_id,
      tier:       tier_result[:tier],
      reason:     tier_result[:reason],
      confidence: tier_result[:pattern_confidence] || tier_result.dig(:pattern, :confidence),
      tool_chain: Array(tier_result.dig(:pattern, :tool_chain))
    )
  else
    LoggingSupport.info('do_action.tier_result', request_id: request_id, tier: 'none', reason: 'tier router unavailable')
  end

  case tier_result&.dig(:tier)
  when 0
    response = text_response(tier_result[:response].merge(
                               _meta: { tier:       0,
                                        latency_ms: tier_result[:latency_ms],
                                        confidence: tier_result[:pattern_confidence] }
                             ))
    LoggingSupport.info(
      'do_action.complete',
      request_id: request_id,
      path:       'tier0',
      result:     LoggingSupport.summarize_result(response),
      matched:    Array(tier_result.dig(:pattern, :tool_chain))
    )
    return response
  when 1
    llm_result = try_tier1(intent, tier_result[:pattern], request_id: request_id)
    if llm_result
      response = text_response({ result: llm_result,
                                 _meta:  { tier: 1, pattern_hint: tier_result[:pattern][:intent_text] } })
      LoggingSupport.info(
        'do_action.complete',
        request_id: request_id,
        path:       'tier1',
        matched:    Array(tier_result.dig(:pattern, :tool_chain)),
        result:     LoggingSupport.summarize_result(response)
      )
      return response
    end
  when 2
    llm_result = try_tier2(intent, request_id: request_id)
    if llm_result
      response = text_response({ result: llm_result, _meta: { tier: 2 } })
      LoggingSupport.info(
        'do_action.complete',
        request_id: request_id,
        path:       'tier2',
        result:     LoggingSupport.summarize_result(response)
      )
      return response
    end
  end

  # Fall back to ContextCompiler tool matching
  matched = ContextCompiler.match_tool(intent)
  if matched.nil?
    LoggingSupport.warn(
      'do_action.no_match',
      request_id: request_id,
      intent:     LoggingSupport.summarize_text(intent)
    )
    return error_response("No matching tool found for intent: #{intent}")
  end

  matched_name = matched.respond_to?(:tool_name) ? matched.tool_name : matched.to_s
  LoggingSupport.info(
    'do_action.match',
    request_id:   request_id,
    matched_tool: matched_name,
    params:       LoggingSupport.summarize_params(tool_params)
  )

  result = tool_params.empty? ? matched.call : matched.call(**tool_params)
  LoggingSupport.info(
    'do_action.complete',
    request_id: request_id,
    path:       'context_compiler',
    matched:    matched_name,
    result:     LoggingSupport.summarize_result(result)
  )

  record_feedback(intent, matched_name, success: true)
  result
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'legion.mcp.tools.do_action.call')
  LoggingSupport.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