Class: RailsMcpServer::Analyzers::GetFile

Inherits:
BaseAnalyzer show all
Defined in:
lib/rails-mcp-server/analyzers/get_file.rb

Instance Method Summary collapse

Instance Method Details

#call(path:) ⇒ Object



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
41
42
43
44
45
46
47
48
49
# File 'lib/rails-mcp-server/analyzers/get_file.rb', line 4

def call(path:)
  unless current_project
    message = "No active project. Please switch to a project first."
    log(:warn, message)
    return message
  end

  validated_path = PathValidator.validate_path(path, active_project_path)

  if validated_path.nil?
    message, log_msg = if !PathValidator.safe_path?(path, active_project_path)
      ["Access denied: path '#{path}' is outside the project directory.",
        "Path traversal attempt blocked: #{path}"]
    else
      ["Access denied: '#{path}' matches a sensitive file pattern.",
        "Sensitive file access blocked: #{path}"]
    end
    log(:warn, log_msg)
    return message
  end

  unless File.exist?(validated_path)
    message = "File '#{path}' not found in the project."
    log(:warn, message)
    return message
  end

  if File.directory?(validated_path)
    message = "'#{path}' is a directory, not a file. Use list_files instead."
    log(:warn, message)
    return message
  end

  log(:info, "Reading file: #{path}")

  content = File.read(validated_path)
  extension = File.extname(path).delete(".")

  <<~FILE
    File: #{path}

    ```#{extension}
    #{content}
    ```
  FILE
end