Internal Release 0.2.0
- 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
This commit is contained in:
parent
a4df3b8688
commit
6004f42573
9 changed files with 708 additions and 164 deletions
3
lib/mcp-ruby.rb
Normal file
3
lib/mcp-ruby.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "model_context_protocol"
|
||||
|
|
@ -7,6 +7,7 @@ require "model_context_protocol/tool"
|
|||
require "model_context_protocol/content"
|
||||
require "model_context_protocol/resource"
|
||||
require "model_context_protocol/prompt"
|
||||
require "model_context_protocol/version"
|
||||
|
||||
module ModelContextProtocol
|
||||
class Annotations
|
||||
|
|
|
|||
|
|
@ -44,19 +44,59 @@ module ModelContextProtocol
|
|||
end
|
||||
end
|
||||
|
||||
class << self
|
||||
attr_reader :description_value
|
||||
attr_reader :arguments_value
|
||||
|
||||
def inherited(subclass)
|
||||
super
|
||||
subclass.instance_variable_set(:@name_value, nil)
|
||||
subclass.instance_variable_set(:@description_value, nil)
|
||||
subclass.instance_variable_set(:@arguments_value, nil)
|
||||
end
|
||||
|
||||
def prompt_name(value)
|
||||
@name_value = value
|
||||
end
|
||||
|
||||
def name_value
|
||||
@name_value || name.demodulize.underscore
|
||||
end
|
||||
|
||||
def description(value)
|
||||
@description_value = value
|
||||
end
|
||||
|
||||
def arguments(value)
|
||||
@arguments_value = value
|
||||
end
|
||||
|
||||
def define(name: nil, description: nil, arguments: [], &block)
|
||||
new(name:, description:, arguments:).tap do |prompt|
|
||||
prompt.define_singleton_method(:template) do |args|
|
||||
instance_exec(args, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :name, :description, :arguments
|
||||
|
||||
def initialize(name:, description: nil, arguments: [], &block)
|
||||
@name = name
|
||||
@description = description
|
||||
@arguments = arguments
|
||||
@template_block = block
|
||||
def initialize(name: nil, description: nil, arguments: nil)
|
||||
@name = name || self.class.name_value
|
||||
@description = description || self.class.description_value
|
||||
@arguments = arguments || self.class.arguments_value
|
||||
end
|
||||
|
||||
def template(args)
|
||||
validate_args!(args)
|
||||
result = @template_block.call(args)
|
||||
result
|
||||
raise NotImplementedError, "Prompt subclasses must implement template"
|
||||
end
|
||||
|
||||
def validate_arguments!(args)
|
||||
missing = required_args - args.keys
|
||||
return if missing.empty?
|
||||
|
||||
raise ArgumentError, "Missing required arguments: #{missing.join(", ")}"
|
||||
end
|
||||
|
||||
def to_h
|
||||
|
|
@ -68,12 +108,5 @@ module ModelContextProtocol
|
|||
def required_args
|
||||
arguments.filter_map { |arg| arg.name if arg.required }
|
||||
end
|
||||
|
||||
def validate_args!(args)
|
||||
missing = required_args - args.keys
|
||||
return if missing.empty?
|
||||
|
||||
raise ArgumentError, "Missing required arguments: #{missing.join(", ")}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,57 +2,25 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
module Resource
|
||||
class Embedded
|
||||
attr_reader :resource, :annotations
|
||||
class Resource
|
||||
attr_reader :uri, :name, :description, :mime_type, :contents
|
||||
|
||||
def initialize(resource:, annotations: nil)
|
||||
@resource = resource
|
||||
@annotations = annotations
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ resource: resource.to_h, annotations: }.compact
|
||||
end
|
||||
def initialize(uri:, name:, description:, mime_type:, contents:)
|
||||
@uri = uri
|
||||
@name = name
|
||||
@description = description
|
||||
@mime_type = mime_type
|
||||
@contents = contents
|
||||
end
|
||||
|
||||
class Contents
|
||||
attr_reader :uri, :mime_type
|
||||
|
||||
def initialize(uri:, mime_type: nil)
|
||||
@uri = uri
|
||||
@mime_type = mime_type
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ uri:, mime_type: }.compact
|
||||
end
|
||||
end
|
||||
|
||||
class TextContents < Contents
|
||||
attr_reader :text
|
||||
|
||||
def initialize(text:, uri:, mime_type:)
|
||||
super(uri: uri, mime_type: mime_type)
|
||||
@text = text
|
||||
end
|
||||
|
||||
def to_h
|
||||
super.merge(text: text)
|
||||
end
|
||||
end
|
||||
|
||||
class BlobContents < Contents
|
||||
attr_reader :data
|
||||
|
||||
def initialize(data:, uri:, mime_type:)
|
||||
super(uri: uri, mime_type: mime_type)
|
||||
@data = data
|
||||
end
|
||||
|
||||
def to_h
|
||||
super.merge(data: data)
|
||||
end
|
||||
def to_h
|
||||
{
|
||||
uri: @uri,
|
||||
name: @name,
|
||||
description: @description,
|
||||
mimeType: @mime_type,
|
||||
contents: @contents.map(&:to_h),
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
45
lib/model_context_protocol/resource/contents.rb
Normal file
45
lib/model_context_protocol/resource/contents.rb
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
class Resource
|
||||
class Contents
|
||||
attr_reader :uri, :mime_type
|
||||
|
||||
def initialize(uri:, mime_type: nil)
|
||||
@uri = uri
|
||||
@mime_type = mime_type
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ uri:, mime_type: }.compact
|
||||
end
|
||||
end
|
||||
|
||||
class TextContents < Contents
|
||||
attr_reader :text
|
||||
|
||||
def initialize(text:, uri:, mime_type:)
|
||||
super(uri: uri, mime_type: mime_type)
|
||||
@text = text
|
||||
end
|
||||
|
||||
def to_h
|
||||
super.merge(text: text)
|
||||
end
|
||||
end
|
||||
|
||||
class BlobContents < Contents
|
||||
attr_reader :data
|
||||
|
||||
def initialize(data:, uri:, mime_type:)
|
||||
super(uri: uri, mime_type: mime_type)
|
||||
@data = data
|
||||
end
|
||||
|
||||
def to_h
|
||||
super.merge(data: data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
18
lib/model_context_protocol/resource/embedded.rb
Normal file
18
lib/model_context_protocol/resource/embedded.rb
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
class Resource
|
||||
class Embedded
|
||||
attr_reader :resource, :annotations
|
||||
|
||||
def initialize(resource:, annotations: nil)
|
||||
@annotations = annotations
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ resource: resource.to_h, annotations: }.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -4,12 +4,25 @@ module ModelContextProtocol
|
|||
class Server
|
||||
PROTOCOL_VERSION = "2024-11-05"
|
||||
|
||||
attr_accessor :name, :tools, :prompts
|
||||
attr_accessor :name, :tools, :prompts, :resources
|
||||
|
||||
def initialize(name: "model_context_protocol", tools: [], prompts: [])
|
||||
def initialize(name: "model_context_protocol", tools: [], prompts: [], resources: [])
|
||||
@name = name
|
||||
@tools = tools.to_h { |t| [t.name, t] }
|
||||
@prompts = prompts.to_h { |p| [p.name, p] }
|
||||
@resources = resources
|
||||
@resource_index = resources.index_by(&:uri)
|
||||
|
||||
@handlers = {
|
||||
"resources/list" => method(:list_resources),
|
||||
"resources/read" => method(:read_resource),
|
||||
"tools/list" => method(:list_tools),
|
||||
"tools/call" => method(:call_tool),
|
||||
"prompts/list" => method(:list_prompts),
|
||||
"prompts/get" => method(:get_prompt),
|
||||
"initialize" => method(:init),
|
||||
"ping" => method(:ping),
|
||||
}
|
||||
end
|
||||
|
||||
def handle(request)
|
||||
|
|
@ -20,7 +33,7 @@ module ModelContextProtocol
|
|||
if parsed_request.notification?
|
||||
handle_notification(parsed_request)
|
||||
else
|
||||
call_method(parsed_request)
|
||||
handle_method(parsed_request)
|
||||
end
|
||||
rescue JsonRPC::Error => e
|
||||
JsonRPC::Response.new(id: parsed_request&.id, error: e)
|
||||
|
|
@ -29,35 +42,32 @@ module ModelContextProtocol
|
|||
response
|
||||
end
|
||||
|
||||
def resources_list_handler(&block)
|
||||
@handlers["resources/list"] = block
|
||||
end
|
||||
|
||||
def resources_read_handler(&block)
|
||||
@handlers["resources/read"] = block
|
||||
end
|
||||
|
||||
def tools_list_handler(&block)
|
||||
@handlers["tools/list"] = block
|
||||
end
|
||||
|
||||
def tools_call_handler(&block)
|
||||
@handlers["tools/call"] = block
|
||||
end
|
||||
|
||||
def prompts_list_handler(&block)
|
||||
@handlers["prompts/list"] = block
|
||||
end
|
||||
|
||||
def prompts_get_handler(&block)
|
||||
@handlers["prompts/get"] = block
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def handle_notification(request)
|
||||
nil
|
||||
end
|
||||
|
||||
def call_method(request)
|
||||
case request.method
|
||||
when "initialize"
|
||||
JsonRPC::Response.new(id: request.id, result: {
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
capabilities: capabilities,
|
||||
serverInfo: server_info,
|
||||
})
|
||||
when "ping"
|
||||
JsonRPC::Response.new(id: request.id, result: "pong")
|
||||
when "tools/list"
|
||||
list_tools(request)
|
||||
when "tools/call"
|
||||
call_tool(request)
|
||||
when "prompts/list"
|
||||
list_prompts(request)
|
||||
when "prompts/get"
|
||||
get_prompt(request)
|
||||
else
|
||||
raise JsonRPC::MethodNotFoundError.new(message: "Method not found #{request.method}")
|
||||
end
|
||||
end
|
||||
|
||||
def capabilities
|
||||
@capabilities ||= {
|
||||
experimental: nil,
|
||||
|
|
@ -76,6 +86,41 @@ module ModelContextProtocol
|
|||
}
|
||||
end
|
||||
|
||||
def handle_notification(request)
|
||||
nil
|
||||
end
|
||||
|
||||
def handle_method(request)
|
||||
request_handler = @handlers[request.method]
|
||||
raise JsonRPC::MethodNotFoundError.new(message: "Method not found #{request.method}") unless request_handler
|
||||
|
||||
result = request_handler.call(request)
|
||||
wrapped_result = case request.method
|
||||
when "tools/list"
|
||||
{ tools: result }
|
||||
when "prompts/list"
|
||||
{ prompts: result }
|
||||
when "resources/list"
|
||||
{ resources: result }
|
||||
else
|
||||
result
|
||||
end
|
||||
|
||||
JsonRPC::Response.new(id: request.id, result: wrapped_result)
|
||||
end
|
||||
|
||||
def init(request)
|
||||
{
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
capabilities: capabilities,
|
||||
serverInfo: server_info,
|
||||
}
|
||||
end
|
||||
|
||||
def ping(request)
|
||||
"pong"
|
||||
end
|
||||
|
||||
def call_tool(request)
|
||||
tool_name = request.params&.dig("name")
|
||||
tool = tools[tool_name]
|
||||
|
|
@ -89,15 +134,15 @@ module ModelContextProtocol
|
|||
raise JsonRPC::InternalError.new(message: e.message)
|
||||
end
|
||||
|
||||
JsonRPC::Response.new(id: request.id, result: result.to_h)
|
||||
result.to_h
|
||||
end
|
||||
|
||||
def list_tools(request)
|
||||
JsonRPC::Response.new(id: request.id, result: { tools: @tools.map { |_, tool| tool.to_h } })
|
||||
@tools.map { |_, tool| tool.to_h }
|
||||
end
|
||||
|
||||
def list_prompts(request)
|
||||
JsonRPC::Response.new(id: request.id, result: { prompts: @prompts.map { |_, prompt| prompt.to_h } })
|
||||
@prompts.map { |_, prompt| prompt.to_h }
|
||||
end
|
||||
|
||||
def get_prompt(request)
|
||||
|
|
@ -106,12 +151,28 @@ module ModelContextProtocol
|
|||
raise JsonRPC::MethodNotFoundError.new(message: "Prompt not found #{prompt_name}") unless prompt
|
||||
|
||||
begin
|
||||
result = prompt.template(request.params&.dig("arguments") || {})
|
||||
prompt_args = request.params&.dig("arguments")
|
||||
prompt.validate_arguments!(prompt_args)
|
||||
|
||||
result = prompt.template(prompt_args)
|
||||
rescue ArgumentError => e
|
||||
raise JsonRPC::InvalidParamsError.new(message: e.message)
|
||||
end
|
||||
|
||||
JsonRPC::Response.new(id: request.id, result: result.to_h)
|
||||
result.to_h
|
||||
end
|
||||
|
||||
def list_resources(request)
|
||||
@resources.map(&:to_h)
|
||||
end
|
||||
|
||||
def read_resource(request)
|
||||
resource_uri = request.params&.dig("uri")
|
||||
|
||||
resource = @resource_index[resource_uri]
|
||||
raise JsonRPC::MethodNotFoundError.new(message: "Resource not found #{resource_uri}") unless resource
|
||||
|
||||
resource.to_h
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -15,17 +15,52 @@ module ModelContextProtocol
|
|||
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:, description: nil, input_schema: nil, &block)
|
||||
@name = name
|
||||
@description = description
|
||||
@input_schema = input_schema
|
||||
@tool_block = block_given? ? block : -> { Response.new([{ type: "text", content: "OK" }]) }
|
||||
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)
|
||||
@tool_block.call(*args)
|
||||
raise NotImplementedError, "Subclasses must implement call"
|
||||
end
|
||||
|
||||
def to_h
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
VERSION = "0.1.1"
|
||||
VERSION = "0.2.2"
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue