Internal Release 0.2.4
- remove activesupport as a runtime dependency - use alternative to activesupport/inflector methods - use `json_rpc_handler` gem - add json_rpc_handler to gem dependencies
This commit is contained in:
parent
6004f42573
commit
c5591be30b
8 changed files with 145 additions and 507 deletions
|
|
@ -1,58 +0,0 @@
|
|||
# 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
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
# 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
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
# 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
|
||||
|
|
@ -60,7 +60,7 @@ module ModelContextProtocol
|
|||
end
|
||||
|
||||
def name_value
|
||||
@name_value || name.demodulize.underscore
|
||||
@name_value || StringUtils.handle_from_class_name(name)
|
||||
end
|
||||
|
||||
def description(value)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "json_rpc_handler"
|
||||
|
||||
module ModelContextProtocol
|
||||
class Server
|
||||
PROTOCOL_VERSION = "2024-11-05"
|
||||
|
|
@ -26,20 +28,20 @@ module ModelContextProtocol
|
|||
end
|
||||
|
||||
def handle(request)
|
||||
response = begin
|
||||
parsed_request = JsonRPC::Request.parse(request)
|
||||
parsed_request.validate!
|
||||
|
||||
if parsed_request.notification?
|
||||
handle_notification(parsed_request)
|
||||
JsonRpcHandler.handle(request) do |method|
|
||||
handler = case method
|
||||
when "tools/list"
|
||||
->(params) { { tools: @handlers["tools/list"].call(params) } }
|
||||
when "prompts/list"
|
||||
->(params) { { prompts: @handlers["prompts/list"].call(params) } }
|
||||
when "resources/list"
|
||||
->(params) { { resources: @handlers["resources/list"].call(params) } }
|
||||
else
|
||||
handle_method(parsed_request)
|
||||
@handlers[method]
|
||||
end
|
||||
rescue JsonRPC::Error => e
|
||||
JsonRPC::Response.new(id: parsed_request&.id, error: e)
|
||||
end
|
||||
|
||||
response
|
||||
handler
|
||||
end
|
||||
end
|
||||
|
||||
def resources_list_handler(&block)
|
||||
|
|
@ -86,29 +88,6 @@ 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,
|
||||
|
|
@ -121,43 +100,33 @@ module ModelContextProtocol
|
|||
"pong"
|
||||
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
|
||||
|
||||
result.to_h
|
||||
end
|
||||
|
||||
def list_tools(request)
|
||||
@tools.map { |_, tool| tool.to_h }
|
||||
end
|
||||
|
||||
def call_tool(request)
|
||||
tool_name = request[:name]
|
||||
tool = tools[tool_name]
|
||||
raise "Tool not found #{tool_name}" unless tool
|
||||
|
||||
result = tool.call(**request[:arguments])
|
||||
result.to_h
|
||||
end
|
||||
|
||||
def list_prompts(request)
|
||||
@prompts.map { |_, prompt| prompt.to_h }
|
||||
end
|
||||
|
||||
def get_prompt(request)
|
||||
prompt_name = request.params&.dig("name")
|
||||
prompt_name = request[:name]
|
||||
prompt = @prompts[prompt_name]
|
||||
raise JsonRPC::MethodNotFoundError.new(message: "Prompt not found #{prompt_name}") unless prompt
|
||||
|
||||
begin
|
||||
prompt_args = request.params&.dig("arguments")
|
||||
prompt.validate_arguments!(prompt_args)
|
||||
raise "Prompt not found #{prompt_name}" unless prompt
|
||||
|
||||
result = prompt.template(prompt_args)
|
||||
rescue ArgumentError => e
|
||||
raise JsonRPC::InvalidParamsError.new(message: e.message)
|
||||
end
|
||||
prompt_args = request[:arguments]
|
||||
prompt.validate_arguments!(prompt_args)
|
||||
|
||||
result = prompt.template(prompt_args)
|
||||
|
||||
result.to_h
|
||||
end
|
||||
|
|
@ -167,10 +136,10 @@ module ModelContextProtocol
|
|||
end
|
||||
|
||||
def read_resource(request)
|
||||
resource_uri = request.params&.dig("uri")
|
||||
resource_uri = request[:uri]
|
||||
|
||||
resource = @resource_index[resource_uri]
|
||||
raise JsonRPC::MethodNotFoundError.new(message: "Resource not found #{resource_uri}") unless resource
|
||||
raise "Resource not found #{resource_uri}" unless resource
|
||||
|
||||
resource.to_h
|
||||
end
|
||||
|
|
|
|||
26
lib/model_context_protocol/string_utils.rb
Normal file
26
lib/model_context_protocol/string_utils.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
module StringUtils
|
||||
extend self
|
||||
|
||||
def handle_from_class_name(class_name)
|
||||
underscore(demodulize(class_name))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def demodulize(path)
|
||||
path.to_s.split("::").last || path.to_s
|
||||
end
|
||||
|
||||
def underscore(camel_cased_word)
|
||||
camel_cased_word.dup
|
||||
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
||||
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
|
||||
.tr("-", "_")
|
||||
.downcase
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -31,7 +31,7 @@ module ModelContextProtocol
|
|||
end
|
||||
|
||||
def name_value
|
||||
@name_value || name.demodulize.underscore
|
||||
@name_value || StringUtils.handle_from_class_name(name)
|
||||
end
|
||||
|
||||
def description(value)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
VERSION = "0.2.2"
|
||||
VERSION = "0.2.4"
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue