33require "json_rpc_handler"
44require_relative "instrumentation"
55require_relative "methods"
6+ require_relative "logging_message_notification"
67
78module MCP
89 class Server
@@ -31,7 +32,7 @@ def initialize(method_name)
3132
3233 include Instrumentation
3334
34- attr_accessor :name , :version , :tools , :prompts , :resources , :server_context , :configuration , :capabilities , :transport
35+ attr_accessor :name , :version , :tools , :prompts , :resources , :server_context , :configuration , :capabilities , :transport , :logging_message_notification
3536
3637 def initialize (
3738 name : "model_context_protocol" ,
@@ -55,6 +56,7 @@ def initialize(
5556 @server_context = server_context
5657 @configuration = MCP . configuration . merge ( configuration )
5758 @capabilities = capabilities || default_capabilities
59+ @logging_message_notification = nil
5860
5961 @handlers = {
6062 Methods ::RESOURCES_LIST => method ( :list_resources ) ,
@@ -66,12 +68,12 @@ def initialize(
6668 Methods ::PROMPTS_GET => method ( :get_prompt ) ,
6769 Methods ::INITIALIZE => method ( :init ) ,
6870 Methods ::PING => -> ( _ ) { { } } ,
71+ Methods ::LOGGING_SET_LEVEL => method ( :logging_level= ) ,
6972
7073 # No op handlers for currently unsupported methods
7174 Methods ::RESOURCES_SUBSCRIBE => -> ( _ ) { } ,
7275 Methods ::RESOURCES_UNSUBSCRIBE => -> ( _ ) { } ,
7376 Methods ::COMPLETION_COMPLETE => -> ( _ ) { } ,
74- Methods ::LOGGING_SET_LEVEL => -> ( _ ) { } ,
7577 }
7678 @transport = transport
7779 end
@@ -130,6 +132,19 @@ def notify_resources_list_changed
130132 report_exception ( e , { notification : "resources_list_changed" } )
131133 end
132134
135+ def notify_logging_message ( logger : nil , data : nil )
136+ return unless @transport
137+ raise LoggingMessageNotification ::NotSpecifiedLevelError unless logging_message_notification &.level
138+
139+ params = { level : logging_message_notification . level }
140+ params [ :logger ] = logger if logger
141+ params [ :data ] = data if data
142+
143+ @transport . send_notification ( Methods ::NOTIFICATIONS_MESSAGE , params )
144+ rescue => e
145+ report_exception ( e , { notification : "logging_message_notification" } )
146+ end
147+
133148 def resources_list_handler ( &block )
134149 @handlers [ Methods ::RESOURCES_LIST ] = block
135150 end
@@ -203,6 +218,7 @@ def default_capabilities
203218 tools : { listChanged : true } ,
204219 prompts : { listChanged : true } ,
205220 resources : { listChanged : true } ,
221+ logging : { } ,
206222 }
207223 end
208224
@@ -221,6 +237,13 @@ def init(request)
221237 }
222238 end
223239
240+ def logging_level = ( level )
241+ logging_message_notification = LoggingMessageNotification . new ( level : level )
242+ raise LoggingMessageNotification ::InvalidLevelError unless logging_message_notification . valid_level?
243+
244+ @logging_message_notification = logging_message_notification
245+ end
246+
224247 def list_tools ( request )
225248 @tools . map { |_ , tool | tool . to_h }
226249 end
0 commit comments