Class: RailsAiBridge::Tools::SearchSemantic

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_bridge/tools/search_semantic.rb

Overview

MCP tool for semantic code search using rubydex.

Searches declarations (classes, modules, methods, constants) by name using rubydex's semantic index. Unlike +rails_search_code+ which greps file contents, this tool understands Ruby code structure and returns semantically meaningful results with declaration types and locations.

Requires rubydex to be installed and enabled in configuration.

Constant Summary collapse

MAX_RESULTS_CAP =

Hard upper bound for +max_results+ regardless of client input.

50

Class Method Summary collapse

Methods inherited from BaseTool

cached_context, cached_section, config, rails_app, reset_cache!, text_response

Class Method Details

.call(query:, path: nil, max_results: 20) ⇒ MCP::Tool::Response

Parameters:

  • query (String)

    declaration name search query (required)

  • path (String, nil) (defaults to: nil)

    optional file path filter

  • max_results (Integer) (defaults to: 20)

    capped at MAX_RESULTS_CAP

Returns:

  • (MCP::Tool::Response)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rails_ai_bridge/tools/search_semantic.rb', line 46

def self.call(query:, path: nil, max_results: 20)
  unless rubydex_available?
    return text_response('Rubydex is not available. Install the rubydex gem and set ' \
                         'config.rubydex_enabled = true to enable semantic search.')
  end

  max_results = normalize_max_results(max_results)

  if path
    results = adapter.file_declarations(path)
    results = results.select { |result| result[:name].to_s.downcase.include?(query.downcase) }
  else
    results = adapter.search(query, max_results: max_results)
  end

  results = results.first(max_results)
  text_response(format_results(results, query, path))
end