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,19 @@ def notify_resources_list_changed
138140 report_exception ( e , { notification : "resources_list_changed" } )
139141 end
140142
143+ def notify_logging_message ( logger : nil , data : nil )
144+ return unless @transport
145+ raise LoggingMessageNotification ::NotSpecifiedLevelError unless logging_message_notification &.level
146+
147+ params = { level : logging_message_notification . level }
148+ params [ :logger ] = logger if logger
149+ params [ :data ] = data if data
150+
151+ @transport . send_notification ( Methods ::NOTIFICATIONS_MESSAGE , params )
152+ rescue => e
153+ report_exception ( e , { notification : "logging_message_notification" } )
154+ end
155+
141156 def resources_list_handler ( &block )
142157 @handlers [ Methods ::RESOURCES_LIST ] = block
143158 end
@@ -211,6 +226,7 @@ def default_capabilities
211226 tools : { listChanged : true } ,
212227 prompts : { listChanged : true } ,
213228 resources : { listChanged : true } ,
229+ logging : { } ,
214230 }
215231 end
216232
@@ -230,6 +246,13 @@ def init(request)
230246 } . compact
231247 end
232248
249+ def logging_level = ( level )
250+ logging_message_notification = LoggingMessageNotification . new ( level : level )
251+ raise LoggingMessageNotification ::InvalidLevelError unless logging_message_notification . valid_level?
252+
253+ @logging_message_notification = logging_message_notification
254+ end
255+
233256 def list_tools ( request )
234257 @tools . map { |_ , tool | tool . to_h }
235258 end
0 commit comments