Add notification support

Co-authored-by: Kevin Fischer <kfischer_okarin@yahoo.co.jp>
This commit is contained in:
Riley Shott 2025-06-02 13:46:18 -07:00
commit 55b03fc7aa
3 changed files with 711 additions and 6 deletions

View file

@ -21,6 +21,11 @@ module MCP
SAMPLING_CREATE_MESSAGE = "sampling/createMessage"
# Notification methods
NOTIFICATIONS_TOOLS_LIST_CHANGED = "notifications/tools/list_changed"
NOTIFICATIONS_PROMPTS_LIST_CHANGED = "notifications/prompts/list_changed"
NOTIFICATIONS_RESOURCES_LIST_CHANGED = "notifications/resources/list_changed"
class MissingRequiredCapabilityError < StandardError
attr_reader :method
attr_reader :capability

View file

@ -98,6 +98,30 @@ module MCP
@prompts[prompt.name_value] = prompt
end
def notify_tools_list_changed
return unless @transport
@transport.send_notification(Methods::NOTIFICATIONS_TOOLS_LIST_CHANGED)
rescue => e
report_exception(e, { notification: "tools_list_changed" })
end
def notify_prompts_list_changed
return unless @transport
@transport.send_notification(Methods::NOTIFICATIONS_PROMPTS_LIST_CHANGED)
rescue => e
report_exception(e, { notification: "prompts_list_changed" })
end
def notify_resources_list_changed
return unless @transport
@transport.send_notification(Methods::NOTIFICATIONS_RESOURCES_LIST_CHANGED)
rescue => e
report_exception(e, { notification: "resources_list_changed" })
end
def resources_list_handler(&block)
@capabilities.support_resources
@handlers[Methods::RESOURCES_LIST] = block

View file

@ -43,10 +43,18 @@ module MCP
$stdout.flush
end
def send_response(message)
json_message = message.is_a?(String) ? message : JSON.generate(message)
$stdout.puts(json_message)
$stdout.flush
def send_notification(method, params = nil)
notification = {
jsonrpc: "2.0",
method: method,
}
notification[:params] = params if params
send_response(notification)
true
rescue => e
MCP.configuration.exception_reporter.call(e, { error: "Failed to send notification" })
false
end
end
end