Class: RailsAiBridge::Tools::SearchCode

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails_ai_bridge/tools/search_code.rb,
lib/rails_ai_bridge/tools/search_code/formatter.rb

Overview

MCP tool searching the app tree with ripgrep (+rg+) or a Ruby fallback.

Pattern size is capped by Configuration#search_code_pattern_max_bytes (default 2048). Wall-clock limits use Configuration#search_code_timeout_seconds (+0+ disables).

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

MAX_RESULTS_CAP =

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

100
DEFAULT_PATTERN_MAX_BYTES =

Fallback when +config.search_code_pattern_max_bytes+ is misconfigured (non-positive).

2048
DEFAULT_ALLOWED_FILE_TYPES =

Default extensions when no +file_type+ is given, merged with Configuration#search_code_allowed_file_types.

%w[rb erb js ts jsx tsx yml yaml json].freeze

Class Method Summary collapse

Methods inherited from BaseTool

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

Class Method Details

.call(pattern:, path: nil, file_type: nil, max_results: 30) ⇒ MCP::Tool::Response

Returns search hits as markdown or a validation error response.

Parameters:

  • pattern (String)

    regex-capable search pattern (required)

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

    subdirectory under +Rails.root+ to scope the search

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

    extension filter (must be allowlisted)

  • max_results (Integer) (defaults to: 30)

    capped at MAX_RESULTS_CAP

  • server_context (Object, nil)

    reserved for MCP transport metadata

Returns:

  • (MCP::Tool::Response)

    search hits as markdown or a validation error response



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails_ai_bridge/tools/search_code.rb', line 58

def self.call(pattern:, path: nil, file_type: nil, max_results: 30)
  root = Rails.root.to_s

  # Validate inputs
  validation_result = validate_inputs(pattern, file_type)
  return validation_result if validation_result.is_a?(MCP::Tool::Response)

  # Prepare and validate search
  search_params = prepare_search(root, path, max_results, validation_result)
  return search_params if search_params.is_a?(MCP::Tool::Response)

  # Execute search and format response
  execute_and_format_search(pattern, search_params, path)
end