Class: RailsAiBridge::Tools::ReadLogs

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

Overview

MCP tool returning a redacted tail of a log file under the app's log/ directory.

Security contract (strictly read-only):

  • only files under Rails.root/log are readable (expanded-path prefix check, no ../ traversal)
  • line cap (MAX_LINES) and per-line byte cap applied before returning
  • every line is redacted through Registry::MessageSanitizer
  • errors follow the message contract

Defined Under Namespace

Classes: LogLocator, TailFormatter

Constant Summary collapse

MAX_LINES =
400
MAX_LINE_BYTES =
2000
MAX_TAIL_SCAN_BYTES =
MAX_LINES * MAX_LINE_BYTES
LINES_BY_DETAIL =
{ 'summary' => 0, 'standard' => 50, 'full' => MAX_LINES }.freeze
DEFAULT_LINES =
50

Class Method Summary collapse

Methods inherited from BaseTool

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

Class Method Details

.call(file:, lines: nil, detail: 'standard') ⇒ MCP::Tool::Response

Returns the redacted tail of the requested log file as compact JSON.

Parameters:

  • file (String) —

    log file name relative to the log directory

  • lines (Integer, nil) (defaults to: nil) —

    requested tail length (hard-capped at MAX_LINES)

  • detail (String) (defaults to: 'standard') —

    summary, standard, or full

Returns:

  • (MCP::Tool::Response) —

    JSON payload with file metadata and tail lines, or "..."

Raises:

  • (StandardError) —

    never escapes; converted to an error response



57
58
59
60
61
62
63
64
# File 'lib/rails_ai_bridge/tools/read_logs.rb', line 57

def self.call(file:, lines: nil, detail: 'standard')
  file_io, error = LogLocator.new(file, Rails.root.to_s).locate
  return error_response(error) if error

  respond(file_io, lines, detail)
rescue StandardError => error
  execution_failure(error)
end