Internal Release v0.1.1
- implement `Prompt` - adjust Tool approach - update readme for tool calling - document usage of `Prompt`
This commit is contained in:
commit
a4df3b8688
10 changed files with 1530 additions and 0 deletions
33
lib/model_context_protocol/content.rb
Normal file
33
lib/model_context_protocol/content.rb
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
# typed: true
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
module Content
|
||||
class Text
|
||||
attr_reader :text, :annotations
|
||||
|
||||
def initialize(text, annotations: nil)
|
||||
@text = text
|
||||
@annotations = annotations
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ text:, annotations: }.compact
|
||||
end
|
||||
end
|
||||
|
||||
class Image
|
||||
attr_reader :data, :mime_type, :annotations
|
||||
|
||||
def initialize(data, mime_type, annotations: nil)
|
||||
@data = data
|
||||
@mime_type = mime_type
|
||||
@annotations = annotations
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ data:, mime_type:, annotations: }.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
lib/model_context_protocol/json_rpc.rb
Normal file
11
lib/model_context_protocol/json_rpc.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "model_context_protocol/json_rpc/error"
|
||||
require "model_context_protocol/json_rpc/request"
|
||||
require "model_context_protocol/json_rpc/response"
|
||||
|
||||
module ModelContextProtocol
|
||||
module JsonRPC
|
||||
VERSION = "2.0"
|
||||
end
|
||||
end
|
||||
58
lib/model_context_protocol/json_rpc/error.rb
Normal file
58
lib/model_context_protocol/json_rpc/error.rb
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
module JsonRPC
|
||||
class Error < StandardError
|
||||
PARSE_ERROR_CODE = -32700
|
||||
INVALID_REQUEST_CODE = -32600
|
||||
METHOD_NOT_FOUND_CODE = -32601
|
||||
INVALID_PARAMS_CODE = -32602
|
||||
INTERNAL_ERROR_CODE = -32603
|
||||
|
||||
attr_reader :code, :message, :data
|
||||
|
||||
def initialize(code:, message:, data: nil)
|
||||
super(message)
|
||||
@code = code
|
||||
@message = message
|
||||
@data = data
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ code:, message:, data: }.compact
|
||||
end
|
||||
end
|
||||
|
||||
class InvalidRequestError < Error
|
||||
def initialize(message:)
|
||||
super(code: INVALID_REQUEST_CODE, message: message)
|
||||
end
|
||||
end
|
||||
|
||||
class ParseError < Error
|
||||
def initialize(message:)
|
||||
super(code: PARSE_ERROR_CODE, message: message)
|
||||
end
|
||||
end
|
||||
|
||||
class MethodNotFoundError < Error
|
||||
def initialize(message:)
|
||||
super(code: METHOD_NOT_FOUND_CODE, message: message)
|
||||
end
|
||||
end
|
||||
|
||||
class InvalidParamsError < Error
|
||||
def initialize(message:)
|
||||
super(code: INVALID_PARAMS_CODE, message: message)
|
||||
end
|
||||
end
|
||||
|
||||
class InternalError < Error
|
||||
def initialize(message:)
|
||||
super(code: INTERNAL_ERROR_CODE, message: message)
|
||||
end
|
||||
end
|
||||
|
||||
class InvalidResponse < StandardError; end
|
||||
end
|
||||
end
|
||||
50
lib/model_context_protocol/json_rpc/request.rb
Normal file
50
lib/model_context_protocol/json_rpc/request.rb
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "json"
|
||||
|
||||
module ModelContextProtocol
|
||||
module JsonRPC
|
||||
class Request
|
||||
class << self
|
||||
def parse(json)
|
||||
parsed = JSON.parse(json)
|
||||
from_h(parsed)
|
||||
rescue JSON::ParserError => e
|
||||
raise ParseError.new(message: e.message)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def from_h(hash)
|
||||
new(version: hash["jsonrpc"], method: hash["method"], params: hash["params"], id: hash["id"])
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :version, :method, :params, :id
|
||||
|
||||
def initialize(version:, method:, params: nil, id: nil)
|
||||
@version = version
|
||||
@method = method
|
||||
@params = params
|
||||
@id = id
|
||||
end
|
||||
|
||||
def notification?
|
||||
id.nil?
|
||||
end
|
||||
|
||||
def valid?
|
||||
@version == JsonRPC::VERSION && !method.start_with?("rpc.")
|
||||
end
|
||||
|
||||
def method_not_found!
|
||||
raise MethodNotFoundError.new(message: "Method not found #{method}")
|
||||
end
|
||||
|
||||
def validate!
|
||||
raise InvalidRequestError.new(message: "Method cannot start with 'rpc.'") if method.start_with?("rpc.")
|
||||
raise InvalidRequestError.new(message: "Invalid JSON-RPC version") unless valid?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
29
lib/model_context_protocol/json_rpc/response.rb
Normal file
29
lib/model_context_protocol/json_rpc/response.rb
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
module JsonRPC
|
||||
class Response
|
||||
attr_reader :version, :result, :error, :id
|
||||
|
||||
def initialize(result: nil, error: nil, id: nil)
|
||||
@version = VERSION
|
||||
@result = result
|
||||
@error = error
|
||||
@id = id
|
||||
|
||||
validate!
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ id:, result:, jsonrpc: @version, error: @error&.to_h }.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate!
|
||||
raise InvalidResponse, "Result and error cannot both be present" if @result && @error
|
||||
raise InvalidResponse, "Either result or error must be present" if @result.nil? && @error.nil?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
79
lib/model_context_protocol/prompt.rb
Normal file
79
lib/model_context_protocol/prompt.rb
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
class Prompt
|
||||
class Argument
|
||||
attr_reader :name, :description, :required, :arguments
|
||||
|
||||
def initialize(name:, description: nil, required: false)
|
||||
@name = name
|
||||
@description = description
|
||||
@required = required
|
||||
@arguments = arguments
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ name:, description:, required: }.compact
|
||||
end
|
||||
end
|
||||
|
||||
class Message
|
||||
attr_reader :role, :content
|
||||
|
||||
def initialize(role:, content:)
|
||||
@role = role
|
||||
@content = content
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ role:, content: content.to_h }.compact
|
||||
end
|
||||
end
|
||||
|
||||
class Result
|
||||
attr_reader :description, :messages
|
||||
|
||||
def initialize(description: nil, messages: [])
|
||||
@description = description
|
||||
@messages = messages
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ description:, messages: messages.map(&:to_h) }.compact
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :name, :description, :arguments
|
||||
|
||||
def initialize(name:, description: nil, arguments: [], &block)
|
||||
@name = name
|
||||
@description = description
|
||||
@arguments = arguments
|
||||
@template_block = block
|
||||
end
|
||||
|
||||
def template(args)
|
||||
validate_args!(args)
|
||||
result = @template_block.call(args)
|
||||
result
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ name:, description:, arguments: arguments.map(&:to_h) }.compact
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
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
|
||||
58
lib/model_context_protocol/resource.rb
Normal file
58
lib/model_context_protocol/resource.rb
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
module Resource
|
||||
class Embedded
|
||||
attr_reader :resource, :annotations
|
||||
|
||||
def initialize(resource:, annotations: nil)
|
||||
@resource = resource
|
||||
@annotations = annotations
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ resource: resource.to_h, annotations: }.compact
|
||||
end
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
117
lib/model_context_protocol/server.rb
Normal file
117
lib/model_context_protocol/server.rb
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
class Server
|
||||
PROTOCOL_VERSION = "2024-11-05"
|
||||
|
||||
attr_accessor :name, :tools, :prompts
|
||||
|
||||
def initialize(name: "model_context_protocol", tools: [], prompts: [])
|
||||
@name = name
|
||||
@tools = tools.to_h { |t| [t.name, t] }
|
||||
@prompts = prompts.to_h { |p| [p.name, p] }
|
||||
end
|
||||
|
||||
def handle(request)
|
||||
response = begin
|
||||
parsed_request = JsonRPC::Request.parse(request)
|
||||
parsed_request.validate!
|
||||
|
||||
if parsed_request.notification?
|
||||
handle_notification(parsed_request)
|
||||
else
|
||||
call_method(parsed_request)
|
||||
end
|
||||
rescue JsonRPC::Error => e
|
||||
JsonRPC::Response.new(id: parsed_request&.id, error: e)
|
||||
end
|
||||
|
||||
response
|
||||
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,
|
||||
logging: nil,
|
||||
prompts: nil,
|
||||
resources: nil,
|
||||
tools: {},
|
||||
model_config: nil,
|
||||
}.compact
|
||||
end
|
||||
|
||||
def server_info
|
||||
@server_info ||= {
|
||||
name:,
|
||||
version: ModelContextProtocol::VERSION,
|
||||
}
|
||||
end
|
||||
|
||||
def call_tool(request)
|
||||
tool_name = request.params&.dig("name")
|
||||
tool = tools[tool_name]
|
||||
raise JsonRPC::MethodNotFoundError.new(message: "Tool not found #{tool_name}") unless tool
|
||||
|
||||
tool_args = request.params&.dig("arguments")
|
||||
|
||||
result = begin
|
||||
tool.call(**tool_args)
|
||||
rescue => e
|
||||
raise JsonRPC::InternalError.new(message: e.message)
|
||||
end
|
||||
|
||||
JsonRPC::Response.new(id: request.id, result: result.to_h)
|
||||
end
|
||||
|
||||
def list_tools(request)
|
||||
JsonRPC::Response.new(id: request.id, result: { tools: @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 } })
|
||||
end
|
||||
|
||||
def get_prompt(request)
|
||||
prompt_name = request.params&.dig("name")
|
||||
prompt = @prompts[prompt_name]
|
||||
raise JsonRPC::MethodNotFoundError.new(message: "Prompt not found #{prompt_name}") unless prompt
|
||||
|
||||
begin
|
||||
result = prompt.template(request.params&.dig("arguments") || {})
|
||||
rescue ArgumentError => e
|
||||
raise JsonRPC::InvalidParamsError.new(message: e.message)
|
||||
end
|
||||
|
||||
JsonRPC::Response.new(id: request.id, result: result.to_h)
|
||||
end
|
||||
end
|
||||
end
|
||||
35
lib/model_context_protocol/tool.rb
Normal file
35
lib/model_context_protocol/tool.rb
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# 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
|
||||
|
||||
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" }]) }
|
||||
end
|
||||
|
||||
def call(*args)
|
||||
@tool_block.call(*args)
|
||||
end
|
||||
|
||||
def to_h
|
||||
{ name:, description:, inputSchema: input_schema }
|
||||
end
|
||||
end
|
||||
end
|
||||
5
lib/model_context_protocol/version.rb
Normal file
5
lib/model_context_protocol/version.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
VERSION = "0.1.1"
|
||||
end
|
||||
Loading…
Reference in a new issue