Module: RailsAiBridge::Mcp::HttpStructuredLog

Defined in:
lib/rails_ai_bridge/mcp/http_structured_log.rb

Overview

One-line JSON logs for the MCP HTTP Rack path when Config::Mcp#http_log_json is enabled. Does not log tokens or Rack +env+ bodies.

Constant Summary collapse

MESSAGE_KEY =
'rails_ai_bridge.mcp.http'

Class Method Summary collapse

Class Method Details

.emit(request:, event:, http_status:, **extra) ⇒ void

This method returns an undefined value.

Emits a single JSON line via +Rails.logger+ (or +$stdout+) when logging is on.

Parameters:

  • request (Rack::Request)
  • event (Symbol, String)

    logical outcome (+rate_limited+, +handled+, …); stored as string.

  • http_status (Integer)
  • extra (Hash)

    optional extra scalar fields merged into the payload (+nil+ values omitted)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_ai_bridge/mcp/http_structured_log.rb', line 20

def emit(request:, event:, http_status:, **extra)
  return unless RailsAiBridge.configuration.mcp.http_log_json

  payload = {
    msg: MESSAGE_KEY,
    event: event.to_s,
    http_status: http_status,
    path: request.path,
    client_ip: request.ip.to_s
  }
  rid = request.env['action_dispatch.request_id']
  payload[:request_id] = rid if rid.present?
  extra.each { |k, v| payload[k] = v unless v.nil? }

  target_logger.info(payload.to_json)
end