4
5
6
7
8
9
10
11
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
|
# File 'lib/rails-mcp-server/analyzers/list_files.rb', line 4
def call(directory: "", pattern: "*.rb")
unless current_project
message = "No active project. Please switch to a project first."
log(:warn, message)
return message
end
if directory.empty?
full_path = active_project_path
else
validated_dir = PathValidator.validate_path(directory, active_project_path)
if validated_dir.nil?
message = "Access denied: directory '#{directory}' is outside the project or is sensitive."
log(:warn, "Directory access blocked: #{directory}")
return message
end
full_path = validated_dir
end
unless File.directory?(full_path)
message = "Directory '#{directory}' not found in the project."
log(:warn, message)
return message
end
sanitized_pattern = pattern.gsub(/[^a-zA-Z0-9*?.\/_-]/, "")
if sanitized_pattern != pattern
log(:warn, "Pattern sanitized from '#{pattern}' to '#{sanitized_pattern}'")
end
files = collect_files(full_path, sanitized_pattern, directory)
files = PathValidator.filter_sensitive_files(files, active_project_path)
log(:debug, "Found #{files.size} files matching pattern (after filtering)")
"Files in #{directory.empty? ? "project root" : directory} matching '#{sanitized_pattern}':\n\n#{files.join("\n")}"
end
|