Class: RailsMcpServer::ListFiles

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails-mcp-server/tools/list_files.rb

Instance Method Summary collapse

Instance Method Details

#call(directory: "", pattern: "*.rb") ⇒ Object



12
13
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rails-mcp-server/tools/list_files.rb', line 12

def call(directory: "", pattern: "*.rb")
  unless current_project
    message = "No active project. Please switch to a project first."
    log(:warn, message)

    return message
  end

  full_path = File.join(active_project_path, directory)
  unless File.directory?(full_path)
    message = "Directory '#{directory}' not found in the project."
    log(:warn, message)

    return message
  end

  # Check if this is a git repository
  is_git_repo = system("cd #{active_project_path} && git rev-parse --is-inside-work-tree > /dev/null 2>&1")

  if is_git_repo
    log(:debug, "Project is a git repository, using git ls-files")

    # Use git ls-files for tracked files
    relative_dir = directory.empty? ? "" : "#{directory}/"
    git_cmd = "cd #{active_project_path} && git ls-files --cached --others --exclude-standard #{relative_dir}#{pattern}"

    files = `#{git_cmd}`.split("\n").map(&:strip).sort # rubocop:disable Performance/ChainArrayAllocation
  else
    log(:debug, "Project is not a git repository or git not available, using Dir.glob")

    # Use Dir.glob as fallback
    files = Dir.glob(File.join(full_path, pattern))
      .map { |f| f.sub("#{active_project_path}/", "") }
      .reject { |file| file.start_with?(".git/", ".ruby-lsp/", "node_modules/", "storage/", "public/assets/", "public/packs/", ".bundle/", "vendor/bundle/", "vendor/cache/", "tmp/", "log/") } # rubocop:disable Performance/ChainArrayAllocation
      .sort # rubocop:disable Performance/ChainArrayAllocation
  end

  log(:debug, "Found #{files.size} files matching pattern (respecting .gitignore and ignoring node_modules)")

  "Files in #{directory.empty? ? "project root" : directory} matching '#{pattern}':\n\n#{files.join("\n")}"
end