Internal Release 0.5.0

Bump protocol version to 2025-03-26
Provide fallback value for tool_name
Make configuration per server
Add support for tool annotations
document tool annotations
This commit is contained in:
Topher Bullock 2025-03-28 09:51:32 -04:00
commit 7175d49108
7 changed files with 353 additions and 42 deletions

View file

@ -2,12 +2,57 @@
module ModelContextProtocol
class Configuration
attr_accessor :exception_reporter
attr_accessor :instrumentation_callback
attr_writer :exception_reporter, :instrumentation_callback
def initialize
@exception_reporter = ->(exception, context) {} # Default no-op reporter
@instrumentation_callback = ->(data) {} # Default no-op callback
def initialize(exception_reporter: nil, instrumentation_callback: nil)
@exception_reporter = exception_reporter
@instrumentation_callback = instrumentation_callback
end
def exception_reporter
@exception_reporter || default_exception_reporter
end
def exception_reporter?
!@exception_reporter.nil?
end
def instrumentation_callback
@instrumentation_callback || default_instrumentation_callback
end
def instrumentation_callback?
!@instrumentation_callback.nil?
end
def merge(other)
return self if other.nil?
exception_reporter = if other.exception_reporter?
other.exception_reporter
else
@exception_reporter
end
instrumentation_callback = if other.instrumentation_callback?
other.instrumentation_callback
else
@instrumentation_callback
end
Configuration.new(
exception_reporter:,
instrumentation_callback:,
)
end
private
def default_exception_reporter
@default_exception_reporter ||= ->(exception, context) {}
end
def default_instrumentation_callback
@default_instrumentation_callback ||= ->(data) {}
end
end
end

View file

@ -12,7 +12,7 @@ module ModelContextProtocol
end
def to_h
{ text:, annotations: }.compact
{ text:, annotations:, type: "text" }.compact
end
end
@ -26,7 +26,7 @@ module ModelContextProtocol
end
def to_h
{ data:, mime_type:, annotations: }.compact
{ data:, mime_type:, annotations:, type: "image" }.compact
end
end
end

View file

@ -12,7 +12,7 @@ module ModelContextProtocol
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
add_instrumentation_data(duration: end_time - start_time)
ModelContextProtocol.configuration.instrumentation_callback.call(@instrumentation_data)
configuration.instrumentation_callback.call(@instrumentation_data)
result
end

View file

@ -114,7 +114,7 @@ module ModelContextProtocol
private
def required_args
arguments_value.filter_map { |arg| arg.name if arg.required }
arguments_value.filter_map { |arg| arg.name.to_sym if arg.required }
end
end
end

View file

@ -12,19 +12,21 @@ module ModelContextProtocol
end
end
PROTOCOL_VERSION = "2024-11-05"
PROTOCOL_VERSION = "2025-03-26"
include Instrumentation
attr_accessor :name, :tools, :prompts, :resources, :context
attr_accessor :name, :tools, :prompts, :resources, :context, :configuration
def initialize(name: "model_context_protocol", tools: [], prompts: [], resources: [], context: nil)
def initialize(name: "model_context_protocol", tools: [], prompts: [], resources: [], context: nil,
configuration: nil)
@name = name
@tools = tools.to_h { |t| [t.name_value, t] }
@prompts = prompts.to_h { |p| [p.name_value, p] }
@resources = resources
@resource_index = resources.index_by(&:uri)
@context = context
@configuration = ModelContextProtocol.configuration.merge(configuration)
@handlers = {
"resources/list" => method(:list_resources),
"resources/read" => method(:read_resource),
@ -186,7 +188,7 @@ module ModelContextProtocol
end
def report_exception(exception, context = {})
ModelContextProtocol.configuration.exception_reporter.call(exception, context)
configuration.exception_reporter.call(exception, context)
end
end
end

View file

@ -15,18 +15,47 @@ module ModelContextProtocol
end
end
class Annotations
attr_reader :title, :read_only_hint, :destructive_hint, :idempotent_hint, :open_world_hint
def initialize(title: nil, read_only_hint: nil, destructive_hint: nil, idempotent_hint: nil, open_world_hint: nil)
@title = title
@read_only_hint = read_only_hint
@destructive_hint = destructive_hint
@idempotent_hint = idempotent_hint
@open_world_hint = open_world_hint
end
def to_h
{
title:,
readOnlyHint: read_only_hint,
destructiveHint: destructive_hint,
idempotentHint: idempotent_hint,
openWorldHint: open_world_hint,
}.compact
end
end
class << self
NOT_SET = Object.new
attr_reader :description_value
attr_reader :input_schema_value
attr_reader :annotations_value
def call(*args, context:)
raise NotImplementedError, "Subclasses must implement call"
end
def to_h
{ name: name_value, description: description_value, inputSchema: input_schema_value }
result = {
name: name_value,
description: description_value,
inputSchema: input_schema_value,
}
result[:annotations] = annotations_value.to_h if annotations_value
result
end
def inherited(subclass)
@ -34,11 +63,12 @@ module ModelContextProtocol
subclass.instance_variable_set(:@name_value, nil)
subclass.instance_variable_set(:@description_value, nil)
subclass.instance_variable_set(:@input_schema_value, nil)
subclass.instance_variable_set(:@annotations_value, nil)
end
def tool_name(value = NOT_SET)
if value == NOT_SET
@name_value
name_value
else
@name_value = value
end
@ -64,11 +94,20 @@ module ModelContextProtocol
end
end
def define(name: nil, description: nil, input_schema: nil, &block)
def annotations(hash = NOT_SET)
if hash == NOT_SET
@annotations_value
else
@annotations_value = Annotations.new(**hash)
end
end
def define(name: nil, description: nil, input_schema: nil, annotations: nil, &block)
Class.new(self) do
tool_name name
description description
input_schema input_schema
self.annotations(annotations) if annotations
define_singleton_method(:call) do |*args, context:|
instance_exec(*args, context:, &block)
end

View file

@ -1,5 +1,5 @@
# frozen_string_literal: true
module ModelContextProtocol
VERSION = "0.4.0"
VERSION = "0.5.0"
end