Module: ActionMCP::Logging

Defined in:
lib/action_mcp/logging.rb,
lib/action_mcp/logging/level.rb,
lib/action_mcp/logging/mixin.rb,
lib/action_mcp/logging/state.rb,
lib/action_mcp/logging/logger.rb,
lib/action_mcp/logging/null_logger.rb

Overview

Global MCP logging interface

Defined Under Namespace

Modules: Mixin Classes: Level, Logger, NullLogger, State

Constant Summary collapse

SESSION_LEVEL_KEY =
"action_mcp_logging_level"

Class Method Summary collapse

Class Method Details

.disable!Boolean

Disable MCP logging

Returns:

  • (Boolean)

    false



40
41
42
# File 'lib/action_mcp/logging.rb', line 40

def disable!
  state.disable!
end

.enable!Boolean

Enable MCP logging

Returns:

  • (Boolean)

    true



33
34
35
36
# File 'lib/action_mcp/logging.rb', line 33

def enable!
  ActionMCP.configuration.logging_enabled = true
  state.enable!
end

.enabled?Boolean

Check if logging is enabled

Returns:

  • (Boolean)

    true if enabled



27
28
29
# File 'lib/action_mcp/logging.rb', line 27

def enabled?
  ActionMCP.configuration.logging_enabled && state.enabled?
end

.initialize_from_config!Object

Initialize logging state based on configuration



115
116
117
118
119
120
121
122
123
124
# File 'lib/action_mcp/logging.rb', line 115

def self.initialize_from_config!
  # Always set the level from configuration
  state.level = ActionMCP.configuration.logging_level

  if ActionMCP.configuration.logging_enabled
    state.enable!
  else
    state.disable!
  end
end

.levelSymbol

Get the current minimum log level

Returns:

  • (Symbol)

    current level



46
47
48
# File 'lib/action_mcp/logging.rb', line 46

def level
  state.level_symbol
end

.level=(new_level) ⇒ Symbol Also known as: set_level

Set the minimum log level

Parameters:

  • new_level (String, Symbol, Integer)

    the new level

Returns:

  • (Symbol)

    the new level as symbol



53
54
55
56
# File 'lib/action_mcp/logging.rb', line 53

def level=(new_level)
  state.level = new_level
  state.level_symbol
end

.level_for(session) ⇒ Object

Return the minimum log level negotiated for a session, falling back to the configured server default until the client sends logging/setLevel.



61
62
63
64
65
# File 'lib/action_mcp/logging.rb', line 61

def level_for(session)
  session_data = session.respond_to?(:session_data) && session.session_data
  stored_level = session_data[SESSION_LEVEL_KEY] if session_data.is_a?(Hash)
  Level.name_for(Level.coerce(stored_level || level))
end

.logger(name: nil, session:) ⇒ ActionMCP::Logging::Logger, ActionMCP::Logging::NullLogger

Create a logger for the given session

Parameters:

  • name (String, nil) (defaults to: nil)

    Optional logger name

  • session (ActionMCP::Session)

    The MCP session

Returns:



94
95
96
97
98
99
100
# File 'lib/action_mcp/logging.rb', line 94

def logger(name: nil, session:)
  if enabled? && supported_by?(session)
    Logger.new(name: name, session: session, state: state)
  else
    NullLogger.new
  end
end

.logger_for_context(name: nil, execution_context:) ⇒ ActionMCP::Logging::Logger, ActionMCP::Logging::NullLogger

Convenience method to get a logger for the current session context

Parameters:

  • name (String, nil) (defaults to: nil)

    Optional logger name

  • execution_context (Hash)

    Context containing session

Returns:



106
107
108
109
110
111
# File 'lib/action_mcp/logging.rb', line 106

def logger_for_context(name: nil, execution_context:)
  session = execution_context[:session]
  return NullLogger.new unless session

  logger(name: name, session: session)
end

.reset!void

This method returns an undefined value.

Reset the global state (for testing)



21
22
23
# File 'lib/action_mcp/logging.rb', line 21

def reset!
  @state = State.new
end

.set_level_for(session, new_level) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/action_mcp/logging.rb', line 67

def set_level_for(session, new_level)
  normalized_level = Level.name_for(Level.coerce(new_level))
  session_data = session.session_data.is_a?(Hash) ? session.session_data.deep_dup : {}
  session_data[SESSION_LEVEL_KEY] = normalized_level.to_s

  if session.respond_to?(:update!)
    session.update!(session_data: session_data)
  else
    session.session_data = session_data
    session.save! if session.respond_to?(:save!)
  end

  normalized_level
end

.stateActionMCP::Logging::State

Get the global logging state

Returns:



15
16
17
# File 'lib/action_mcp/logging.rb', line 15

def state
  @state ||= State.new
end

.supported_by?(session) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
# File 'lib/action_mcp/logging.rb', line 82

def supported_by?(session)
  return false unless session&.respond_to?(:server_capabilities)

  capabilities = session.server_capabilities
  capabilities.is_a?(Hash) &&
    (capabilities.key?("logging") || capabilities.key?(:logging))
end