Module: Legion::MCP::Patterns::Compiler

Extended by:
Logging::Helper
Defined in:
lib/legion/mcp/patterns/compiler.rb

Class Method Summary collapse

Class Method Details

.compile_tool_definitionsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/legion/mcp/patterns/compiler.rb', line 11

def compile_tool_definitions
  return [] unless defined?(Legion::MCP::Server)

  log.debug('[mcp][pattern_compiler] action=compile_tool_definitions ' \
            "registry_size=#{Legion::MCP::Server.tool_registry.size}")
  Legion::MCP::Server.tool_registry.map do |klass|
    name = klass.respond_to?(:tool_name) ? klass.tool_name : klass.name
    desc = klass.respond_to?(:description) ? klass.description : ''
    params = extract_params(klass)

    compressed = "#{name}(#{params.join(', ')}) -- #{desc.split('.').first}"
    { name: name, compressed: compressed.slice(0, 200), full_description: desc }
  end
end

.compile_workflowsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/mcp/patterns/compiler.rb', line 26

def compile_workflows
  log.debug("[mcp][pattern_compiler] action=compile_workflows patterns=#{Patterns::Store.size}")
  Patterns::Store.patterns.filter_map do |_hash, pattern|
    next if (pattern[:confidence] || 0) < 0.6

    {
      intent:     pattern[:intent_text],
      tools:      pattern[:tool_chain],
      confidence: pattern[:confidence],
      template:   pattern[:response_template]
    }
  end
end

.extract_params(klass) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/legion/mcp/patterns/compiler.rb', line 40

def extract_params(klass)
  return [] unless klass.respond_to?(:input_schema)

  schema = klass.input_schema
  props = if schema.is_a?(Hash)
            schema[:properties] || schema['properties']
          elsif schema.respond_to?(:to_h)
            schema.to_h[:properties]
          end
  return [] unless props

  props.keys.map(&:to_s)
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'legion.mcp.pattern_compiler.extract_params')
  []
end