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 ) ,
@@ -74,12 +76,12 @@ def initialize(
7476 Methods ::PROMPTS_GET => method ( :get_prompt ) ,
7577 Methods ::INITIALIZE => method ( :init ) ,
7678 Methods ::PING => -> ( _ ) { { } } ,
79+ Methods ::LOGGING_SET_LEVEL => method ( :logging_level= ) ,
7780
7881 # No op handlers for currently unsupported methods
7982 Methods ::RESOURCES_SUBSCRIBE => -> ( _ ) { } ,
8083 Methods ::RESOURCES_UNSUBSCRIBE => -> ( _ ) { } ,
8184 Methods ::COMPLETION_COMPLETE => -> ( _ ) { } ,
82- Methods ::LOGGING_SET_LEVEL => -> ( _ ) { } ,
8385 }
8486 @transport = transport
8587 end
@@ -138,6 +140,22 @@ def notify_resources_list_changed
138140 report_exception ( e , { notification : "resources_list_changed" } )
139141 end
140142
143+ def notify_logging_message ( notification_level : nil , logger : nil , data : nil )
144+ return unless @transport
145+ raise LoggingMessageNotification ::NotSpecifiedLevelError unless logging_message_notification &.level
146+
147+ current_level = notification_level || logging_message_notification . level
148+ return unless logging_message_notification . should_notify? ( notification_level : current_level )
149+
150+ params = { level : current_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+
141159 def resources_list_handler ( &block )
142160 @handlers [ Methods ::RESOURCES_LIST ] = block
143161 end
@@ -211,6 +229,7 @@ def default_capabilities
211229 tools : { listChanged : true } ,
212230 prompts : { listChanged : true } ,
213231 resources : { listChanged : true } ,
232+ logging : { } ,
214233 }
215234 end
216235
@@ -230,6 +249,13 @@ def init(request)
230249 } . compact
231250 end
232251
252+ def logging_level = ( level )
253+ logging_message_notification = LoggingMessageNotification . new ( level : level )
254+ raise LoggingMessageNotification ::InvalidLevelError unless logging_message_notification . valid_level?
255+
256+ @logging_message_notification = logging_message_notification
257+ end
258+
233259 def list_tools ( request )
234260 @tools . map { |_ , tool | tool . to_h }
235261 end
0 commit comments