- fix `lib/mcp-ruby` auto-import path - implement server resources - add basic listing of resources to Server - allow setting handlers on Server - wrap handler method results - make tool definition more declarative - make prompt definition more declarative - bump dependency versions
70 lines
1.7 KiB
Ruby
70 lines
1.7 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:, is_error: }.compact
|
|
end
|
|
end
|
|
|
|
class << self
|
|
attr_reader :description_value
|
|
attr_reader :input_schema_value
|
|
|
|
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)
|
|
@name_value = value
|
|
end
|
|
|
|
def name_value
|
|
@name_value || name.demodulize.underscore
|
|
end
|
|
|
|
def description(value)
|
|
@description_value = value
|
|
end
|
|
|
|
def input_schema(value)
|
|
@input_schema_value = value
|
|
end
|
|
|
|
def define(name: nil, description: nil, input_schema: nil, &block)
|
|
new(name:, description:, input_schema:).tap do |tool|
|
|
tool.define_singleton_method(:call) do |*args|
|
|
instance_exec(*args, &block)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
attr_reader :name, :description, :input_schema
|
|
|
|
def initialize(name: nil, description: nil, input_schema: nil)
|
|
@name = name || self.class.name_value
|
|
@description = description || self.class.description_value
|
|
@input_schema = input_schema || self.class.input_schema_value
|
|
end
|
|
|
|
def call(*args)
|
|
raise NotImplementedError, "Subclasses must implement call"
|
|
end
|
|
|
|
def to_h
|
|
{ name:, description:, inputSchema: input_schema }
|
|
end
|
|
end
|
|
end
|