Module: Legion::LLM::Inference::Steps::McpDiscovery

Includes:
Logging, Legion::Logging::Helper
Defined in:
lib/legion/llm/inference/steps/mcp_discovery.rb

Instance Method Summary collapse

Instance Method Details

#step_mcp_discoveryObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/llm/inference/steps/mcp_discovery.rb', line 14

def step_mcp_discovery
  @discovered_tools ||= []
  start_time = Time.now
  log_step_debug(:mcp_discovery, :start, existing_tool_count: @discovered_tools.size)

  discover_server_tools
  discover_client_tools

  total = @discovered_tools.size
  if total.positive?
    sources = @discovered_tools.filter_map { |t| t.dig(:source, :server) || t.dig(:source, :type) }.uniq
    @enrichments['mcp:tool_discovery'] = {
      content:   "#{total} tools from #{sources.length} sources",
      data:      { tool_count: total, sources: sources },
      timestamp: Time.now
    }
  end

  record_mcp_timeline(total, start_time)
  log_step_debug(:mcp_discovery, :complete, tool_count: total)
rescue StandardError => e
  @warnings << "MCP discovery error: #{e.message}"
  handle_exception(e, level: :warn, operation: 'llm.pipeline.steps.mcp_discovery')
  record_mcp_timeline(0)
end