3
3
require "json_rpc_handler"
4
4
require_relative "instrumentation"
5
5
require_relative "methods"
6
+ require_relative "logging_message_notification"
6
7
7
8
module MCP
8
9
class Server
@@ -31,7 +32,7 @@ def initialize(method_name)
31
32
32
33
include Instrumentation
33
34
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
35
36
36
37
def initialize (
37
38
name : "model_context_protocol" ,
@@ -63,6 +64,7 @@ def initialize(
63
64
end
64
65
65
66
@capabilities = capabilities || default_capabilities
67
+ @logging_message_notification = nil
66
68
67
69
@handlers = {
68
70
Methods ::RESOURCES_LIST => method ( :list_resources ) ,
@@ -74,12 +76,12 @@ def initialize(
74
76
Methods ::PROMPTS_GET => method ( :get_prompt ) ,
75
77
Methods ::INITIALIZE => method ( :init ) ,
76
78
Methods ::PING => -> ( _ ) { { } } ,
79
+ Methods ::LOGGING_SET_LEVEL => method ( :logging_level= ) ,
77
80
78
81
# No op handlers for currently unsupported methods
79
82
Methods ::RESOURCES_SUBSCRIBE => -> ( _ ) { } ,
80
83
Methods ::RESOURCES_UNSUBSCRIBE => -> ( _ ) { } ,
81
84
Methods ::COMPLETION_COMPLETE => -> ( _ ) { } ,
82
- Methods ::LOGGING_SET_LEVEL => -> ( _ ) { } ,
83
85
}
84
86
@transport = transport
85
87
end
@@ -138,6 +140,22 @@ def notify_resources_list_changed
138
140
report_exception ( e , { notification : "resources_list_changed" } )
139
141
end
140
142
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
+
141
159
def resources_list_handler ( &block )
142
160
@handlers [ Methods ::RESOURCES_LIST ] = block
143
161
end
@@ -211,6 +229,7 @@ def default_capabilities
211
229
tools : { listChanged : true } ,
212
230
prompts : { listChanged : true } ,
213
231
resources : { listChanged : true } ,
232
+ logging : { } ,
214
233
}
215
234
end
216
235
@@ -230,6 +249,13 @@ def init(request)
230
249
} . compact
231
250
end
232
251
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
+
233
259
def list_tools ( request )
234
260
@tools . map { |_ , tool | tool . to_h }
235
261
end
0 commit comments