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 , :instructions , :tools , :prompts , :resources , :server_context , :configuration , :capabilities , :transport
35+ attr_accessor :name , :version , :instructions , :tools , :prompts , :resources , :server_context , :configuration , :capabilities , :transport , :logging_message_notification
3536
3637 def initialize (
3738 name : "model_context_protocol" ,
@@ -63,6 +64,7 @@ def initialize(
6364 end
6465
6566 @capabilities = capabilities || default_capabilities
67+ @logging_message_notification = nil
6668
6769 @handlers = {
6870 Methods ::RESOURCES_LIST => method ( :list_resources ) ,
@@ -75,12 +77,12 @@ def initialize(
7577 Methods ::INITIALIZE => method ( :init ) ,
7678 Methods ::PING => -> ( _ ) { { } } ,
7779 Methods ::NOTIFICATIONS_INITIALIZED => -> ( _ ) { } ,
80+ Methods ::LOGGING_SET_LEVEL => method ( :logging_level= ) ,
7881
7982 # No op handlers for currently unsupported methods
8083 Methods ::RESOURCES_SUBSCRIBE => -> ( _ ) { } ,
8184 Methods ::RESOURCES_UNSUBSCRIBE => -> ( _ ) { } ,
8285 Methods ::COMPLETION_COMPLETE => -> ( _ ) { } ,
83- Methods ::LOGGING_SET_LEVEL => -> ( _ ) { } ,
8486 }
8587 @transport = transport
8688 end
@@ -139,6 +141,21 @@ def notify_resources_list_changed
139141 report_exception ( e , { notification : "resources_list_changed" } )
140142 end
141143
144+ def notify_logging_message ( log_level :, logger : nil , data : nil )
145+ return unless @transport
146+ raise LoggingMessageNotification ::NotSpecifiedLevelError unless logging_message_notification &.level
147+
148+ return unless logging_message_notification . should_notify? ( log_level :)
149+
150+ params = { level : log_level }
151+ params [ :logger ] = logger if logger
152+ params [ :data ] = data if data
153+
154+ @transport . send_notification ( Methods ::NOTIFICATIONS_MESSAGE , params )
155+ rescue => e
156+ report_exception ( e , { notification : "logging_message_notification" } )
157+ end
158+
142159 def resources_list_handler ( &block )
143160 @handlers [ Methods ::RESOURCES_LIST ] = block
144161 end
@@ -212,6 +229,7 @@ def default_capabilities
212229 tools : { listChanged : true } ,
213230 prompts : { listChanged : true } ,
214231 resources : { listChanged : true } ,
232+ logging : { } ,
215233 }
216234 end
217235
@@ -231,6 +249,13 @@ def init(request)
231249 } . compact
232250 end
233251
252+ def logging_level = ( params )
253+ logging_message_notification = LoggingMessageNotification . new ( level : params [ :level ] )
254+ raise LoggingMessageNotification ::InvalidLevelError unless logging_message_notification . valid_level?
255+
256+ @logging_message_notification = logging_message_notification
257+ end
258+
234259 def list_tools ( request )
235260 @tools . map { |_ , tool | tool . to_h }
236261 end
0 commit comments