Internal Release 0.5.1
- Allow protocol version to be set at the server level through configuration instead of a class method, providing more flexibility in protocol version management [#43](https://github.com/Shopify/mcp-ruby/pull/43) @topherbullock - Fixed instrumentation handling to ensure accurate timing information and proper error reporting [#42](https://github.com/Shopify/mcp-ruby/pull/42) @tobiasbales - Improved error handling and reporting in the server - Fixed timing information collection in instrumentation - Added proper error types for different failure scenarios - Enhanced test coverage for error cases - [Allow protocol version to be set at the server level #43](https://github.com/Shopify/mcp-ruby/pull/43) @topherbullock - [Fix instrumentation handling #42](https://github.com/Shopify/mcp-ruby/pull/42) @tobiasbales
This commit is contained in:
parent
7175d49108
commit
d2b6a33788
7 changed files with 248 additions and 86 deletions
|
|
@ -9,6 +9,7 @@ require "model_context_protocol/resource"
|
|||
require "model_context_protocol/prompt"
|
||||
require "model_context_protocol/version"
|
||||
require "model_context_protocol/configuration"
|
||||
require "model_context_protocol/methods"
|
||||
|
||||
module ModelContextProtocol
|
||||
class << self
|
||||
|
|
|
|||
|
|
@ -2,11 +2,22 @@
|
|||
|
||||
module ModelContextProtocol
|
||||
class Configuration
|
||||
attr_writer :exception_reporter, :instrumentation_callback
|
||||
DEFAULT_PROTOCOL_VERSION = "2025-03-26"
|
||||
|
||||
def initialize(exception_reporter: nil, instrumentation_callback: nil)
|
||||
attr_writer :exception_reporter, :instrumentation_callback, :protocol_version
|
||||
|
||||
def initialize(exception_reporter: nil, instrumentation_callback: nil, protocol_version: nil)
|
||||
@exception_reporter = exception_reporter
|
||||
@instrumentation_callback = instrumentation_callback
|
||||
@protocol_version = protocol_version
|
||||
end
|
||||
|
||||
def protocol_version
|
||||
@protocol_version || DEFAULT_PROTOCOL_VERSION
|
||||
end
|
||||
|
||||
def protocol_version?
|
||||
!@protocol_version.nil?
|
||||
end
|
||||
|
||||
def exception_reporter
|
||||
|
|
@ -38,10 +49,16 @@ module ModelContextProtocol
|
|||
else
|
||||
@instrumentation_callback
|
||||
end
|
||||
protocol_version = if other.protocol_version?
|
||||
other.protocol_version
|
||||
else
|
||||
@protocol_version
|
||||
end
|
||||
|
||||
Configuration.new(
|
||||
exception_reporter:,
|
||||
instrumentation_callback:,
|
||||
protocol_version:,
|
||||
)
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -4,17 +4,19 @@ module ModelContextProtocol
|
|||
module Instrumentation
|
||||
def instrument_call(method, &block)
|
||||
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
@instrumentation_data = {}
|
||||
add_instrumentation_data(method:)
|
||||
begin
|
||||
@instrumentation_data = {}
|
||||
add_instrumentation_data(method:)
|
||||
|
||||
result = yield block
|
||||
result = yield block
|
||||
|
||||
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
add_instrumentation_data(duration: end_time - start_time)
|
||||
result
|
||||
ensure
|
||||
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
add_instrumentation_data(duration: end_time - start_time)
|
||||
|
||||
configuration.instrumentation_callback.call(@instrumentation_data)
|
||||
|
||||
result
|
||||
configuration.instrumentation_callback.call(@instrumentation_data)
|
||||
end
|
||||
end
|
||||
|
||||
def add_instrumentation_data(**kwargs)
|
||||
|
|
|
|||
14
lib/model_context_protocol/methods.rb
Normal file
14
lib/model_context_protocol/methods.rb
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
module Methods
|
||||
RESOURCES_LIST = "resources/list"
|
||||
RESOURCES_READ = "resources/read"
|
||||
TOOLS_LIST = "tools/list"
|
||||
TOOLS_CALL = "tools/call"
|
||||
PROMPTS_LIST = "prompts/list"
|
||||
PROMPTS_GET = "prompts/get"
|
||||
INITIALIZE = "initialize"
|
||||
PING = "ping"
|
||||
end
|
||||
end
|
||||
|
|
@ -108,7 +108,9 @@ module ModelContextProtocol
|
|||
missing = required_args - args.keys
|
||||
return if missing.empty?
|
||||
|
||||
raise ArgumentError, "Missing required arguments: #{missing.join(", ")}"
|
||||
raise ModelContextProtocol::Server::RequestHandlerError.new(
|
||||
"Missing required arguments: #{missing.join(", ")}", nil, error_type: :missing_required_arguments
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
|||
|
|
@ -2,18 +2,22 @@
|
|||
|
||||
require "json_rpc_handler"
|
||||
require_relative "instrumentation"
|
||||
require_relative "methods"
|
||||
|
||||
module ModelContextProtocol
|
||||
class Server
|
||||
class RequestHandlerError < StandardError
|
||||
def initialize(message, request)
|
||||
attr_reader :error_type
|
||||
attr_reader :original_error
|
||||
|
||||
def initialize(message, request, error_type: :internal_error, original_error: nil)
|
||||
super(message)
|
||||
@request = request
|
||||
@error_type = error_type
|
||||
@original_error = original_error
|
||||
end
|
||||
end
|
||||
|
||||
PROTOCOL_VERSION = "2025-03-26"
|
||||
|
||||
include Instrumentation
|
||||
|
||||
attr_accessor :name, :tools, :prompts, :resources, :context, :configuration
|
||||
|
|
@ -28,14 +32,14 @@ module ModelContextProtocol
|
|||
@context = context
|
||||
@configuration = ModelContextProtocol.configuration.merge(configuration)
|
||||
@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" => ->(_) { {} },
|
||||
Methods::RESOURCES_LIST => method(:list_resources),
|
||||
Methods::RESOURCES_READ => method(:read_resource),
|
||||
Methods::TOOLS_LIST => method(:list_tools),
|
||||
Methods::TOOLS_CALL => method(:call_tool),
|
||||
Methods::PROMPTS_LIST => method(:list_prompts),
|
||||
Methods::PROMPTS_GET => method(:get_prompt),
|
||||
Methods::INITIALIZE => method(:init),
|
||||
Methods::PING => ->(_) { {} },
|
||||
}
|
||||
end
|
||||
|
||||
|
|
@ -52,47 +56,61 @@ module ModelContextProtocol
|
|||
end
|
||||
|
||||
def resources_list_handler(&block)
|
||||
@handlers["resources/list"] = block
|
||||
@handlers[Methods::RESOURCES_LIST] = block
|
||||
end
|
||||
|
||||
def resources_read_handler(&block)
|
||||
@handlers["resources/read"] = block
|
||||
@handlers[Methods::RESOURCES_READ] = block
|
||||
end
|
||||
|
||||
def tools_list_handler(&block)
|
||||
@handlers["tools/list"] = block
|
||||
@handlers[Methods::TOOLS_LIST] = block
|
||||
end
|
||||
|
||||
def tools_call_handler(&block)
|
||||
@handlers["tools/call"] = block
|
||||
@handlers[Methods::TOOLS_CALL] = block
|
||||
end
|
||||
|
||||
def prompts_list_handler(&block)
|
||||
@handlers["prompts/list"] = block
|
||||
@handlers[Methods::PROMPTS_LIST] = block
|
||||
end
|
||||
|
||||
def prompts_get_handler(&block)
|
||||
@handlers["prompts/get"] = block
|
||||
@handlers[Methods::PROMPTS_GET] = block
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def handle_request(request, method)
|
||||
instrument_call(method) do
|
||||
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
|
||||
@handlers[method]
|
||||
end
|
||||
rescue => e
|
||||
report_exception(e, { request: request })
|
||||
raise RequestHandlerError.new("Internal error handling #{request[:method]} request", request)
|
||||
handler = @handlers[method]
|
||||
unless handler
|
||||
instrument_call("unsupported_method") {}
|
||||
return
|
||||
end
|
||||
|
||||
->(params) {
|
||||
instrument_call(method) do
|
||||
case method
|
||||
when Methods::TOOLS_LIST
|
||||
{ tools: @handlers[Methods::TOOLS_LIST].call(params) }
|
||||
when Methods::PROMPTS_LIST
|
||||
{ prompts: @handlers[Methods::PROMPTS_LIST].call(params) }
|
||||
when Methods::RESOURCES_LIST
|
||||
{ resources: @handlers[Methods::RESOURCES_LIST].call(params) }
|
||||
else
|
||||
@handlers[method].call(params)
|
||||
end
|
||||
rescue => e
|
||||
report_exception(e, { request: request })
|
||||
if e.is_a?(RequestHandlerError)
|
||||
add_instrumentation_data(error: e.error_type)
|
||||
raise e
|
||||
end
|
||||
|
||||
add_instrumentation_data(error: :internal_error)
|
||||
raise RequestHandlerError.new("Internal error handling #{method} request", request, original_error: e)
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
def capabilities
|
||||
|
|
@ -111,52 +129,49 @@ module ModelContextProtocol
|
|||
end
|
||||
|
||||
def init(request)
|
||||
add_instrumentation_data(method: "initialize")
|
||||
add_instrumentation_data(method: Methods::INITIALIZE)
|
||||
{
|
||||
protocolVersion: PROTOCOL_VERSION,
|
||||
protocolVersion: configuration.protocol_version,
|
||||
capabilities: capabilities,
|
||||
serverInfo: server_info,
|
||||
}
|
||||
end
|
||||
|
||||
def list_tools(request)
|
||||
add_instrumentation_data(method: "tools/list")
|
||||
add_instrumentation_data(method: Methods::TOOLS_LIST)
|
||||
@tools.map { |_, tool| tool.to_h }
|
||||
end
|
||||
|
||||
def call_tool(request)
|
||||
add_instrumentation_data(method: "tools/call")
|
||||
add_instrumentation_data(method: Methods::TOOLS_CALL)
|
||||
tool_name = request[:name]
|
||||
tool = tools[tool_name]
|
||||
unless tool
|
||||
add_instrumentation_data(error: :tool_not_found)
|
||||
raise "Tool not found #{tool_name}"
|
||||
raise RequestHandlerError.new("Tool not found #{tool_name}", request, error_type: :tool_not_found)
|
||||
end
|
||||
|
||||
add_instrumentation_data(tool_name:)
|
||||
|
||||
begin
|
||||
result = tool.call(**request[:arguments], context:)
|
||||
result.to_h
|
||||
tool.call(**request[:arguments], context:).to_h
|
||||
rescue => e
|
||||
report_exception(e, { tool_name: tool_name, arguments: request[:arguments] })
|
||||
add_instrumentation_data(error: :internal_error)
|
||||
raise RequestHandlerError.new("Internal error calling tool #{tool_name}", request)
|
||||
raise RequestHandlerError.new("Internal error calling tool #{tool_name}", request, original_error: e)
|
||||
end
|
||||
end
|
||||
|
||||
def list_prompts(request)
|
||||
add_instrumentation_data(method: "prompts/list")
|
||||
add_instrumentation_data(method: Methods::PROMPTS_LIST)
|
||||
@prompts.map { |_, prompt| prompt.to_h }
|
||||
end
|
||||
|
||||
def get_prompt(request)
|
||||
add_instrumentation_data(method: "prompts/get")
|
||||
add_instrumentation_data(method: Methods::PROMPTS_GET)
|
||||
prompt_name = request[:name]
|
||||
prompt = @prompts[prompt_name]
|
||||
unless prompt
|
||||
add_instrumentation_data(error: :prompt_not_found)
|
||||
raise "Prompt not found #{prompt_name}"
|
||||
raise RequestHandlerError.new("Prompt not found #{prompt_name}", request, error_type: :prompt_not_found)
|
||||
end
|
||||
|
||||
add_instrumentation_data(prompt_name:)
|
||||
|
|
@ -168,19 +183,19 @@ module ModelContextProtocol
|
|||
end
|
||||
|
||||
def list_resources(request)
|
||||
add_instrumentation_data(method: "resources/list")
|
||||
add_instrumentation_data(method: Methods::RESOURCES_LIST)
|
||||
|
||||
@resources.map(&:to_h)
|
||||
end
|
||||
|
||||
def read_resource(request)
|
||||
add_instrumentation_data(method: "resources/read")
|
||||
add_instrumentation_data(method: Methods::RESOURCES_READ)
|
||||
resource_uri = request[:uri]
|
||||
|
||||
resource = @resource_index[resource_uri]
|
||||
unless resource
|
||||
add_instrumentation_data(error: :resource_not_found)
|
||||
raise "Resource not found #{resource_uri}"
|
||||
raise RequestHandlerError.new("Resource not found #{resource_uri}", request, error_type: :resource_not_found)
|
||||
end
|
||||
|
||||
add_instrumentation_data(resource_uri:)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module ModelContextProtocol
|
||||
VERSION = "0.5.0"
|
||||
VERSION = "0.5.1"
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue