ruby-sdk/lib/model_context_protocol/tool.rb
Topher Bullock 2eca3681df Internal Release 0.4.0
- Added support for handle_json method from JSON RPC gem to allow consumers to pass JSON strings
- Added context parameter to prompt templating system

- Updated tool and prompt registration to use class references instead of instances
- Changed tool response is_error to isError to match protocol specification
2025-05-08 17:37:57 -04:00

79 lines
1.8 KiB
Ruby

# frozen_string_literal: true
module ModelContextProtocol
class Tool
class Response
attr_reader :content, :is_error
def initialize(content, is_error = false)
@content = content
@is_error = is_error
end
def to_h
{ content:, isError: is_error }.compact
end
end
class << self
NOT_SET = Object.new
attr_reader :description_value
attr_reader :input_schema_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 }
end
def inherited(subclass)
super
subclass.instance_variable_set(:@name_value, nil)
subclass.instance_variable_set(:@description_value, nil)
subclass.instance_variable_set(:@input_schema_value, nil)
end
def tool_name(value = NOT_SET)
if value == NOT_SET
@name_value
else
@name_value = value
end
end
def name_value
@name_value || StringUtils.handle_from_class_name(name)
end
def description(value = NOT_SET)
if value == NOT_SET
@description_value
else
@description_value = value
end
end
def input_schema(value = NOT_SET)
if value == NOT_SET
@input_schema_value
else
@input_schema_value = value
end
end
def define(name: nil, description: nil, input_schema: nil, &block)
Class.new(self) do
tool_name name
description description
input_schema input_schema
define_singleton_method(:call) do |*args, context:|
instance_exec(*args, context:, &block)
end
end
end
end
end
end