From ea4828987c0d70470f19df0f321593728449b79e Mon Sep 17 00:00:00 2001 From: Kevin Fischer Date: Tue, 13 May 2025 23:51:02 +0900 Subject: [PATCH] Allow specifying a server version --- lib/model_context_protocol/server.rb | 8 ++++++-- test/model_context_protocol/server_test.rb | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/model_context_protocol/server.rb b/lib/model_context_protocol/server.rb index e707566..f6de3ee 100644 --- a/lib/model_context_protocol/server.rb +++ b/lib/model_context_protocol/server.rb @@ -6,6 +6,8 @@ require_relative "methods" module ModelContextProtocol class Server + DEFAULT_VERSION = "0.1.0" + class RequestHandlerError < StandardError attr_reader :error_type attr_reader :original_error @@ -20,10 +22,11 @@ module ModelContextProtocol include Instrumentation - attr_accessor :name, :tools, :prompts, :resources, :server_context, :configuration, :capabilities + attr_accessor :name, :version, :tools, :prompts, :resources, :server_context, :configuration, :capabilities def initialize( name: "model_context_protocol", + version: DEFAULT_VERSION, tools: [], prompts: [], resources: [], @@ -33,6 +36,7 @@ module ModelContextProtocol capabilities: { prompts: {}, resources: {}, tools: {} } ) @name = name + @version = version @tools = tools.to_h { |t| [t.name_value, t] } @prompts = prompts.to_h { |p| [p.name_value, p] } @resources = resources @@ -153,7 +157,7 @@ module ModelContextProtocol def server_info @server_info ||= { name:, - version: ModelContextProtocol::VERSION, + version:, } end diff --git a/test/model_context_protocol/server_test.rb b/test/model_context_protocol/server_test.rb index 2a35667..5890d6b 100644 --- a/test/model_context_protocol/server_test.rb +++ b/test/model_context_protocol/server_test.rb @@ -51,6 +51,7 @@ module ModelContextProtocol @server = Server.new( name: @server_name, + version: "1.2.3", tools: [@tool, @tool_that_raises], prompts: [@prompt], resources: [@resource], @@ -120,7 +121,7 @@ module ModelContextProtocol }, "serverInfo": { "name": @server_name, - "version": ModelContextProtocol::VERSION, + "version": "1.2.3", }, }, } @@ -648,6 +649,18 @@ module ModelContextProtocol assert_equal Configuration::DEFAULT_PROTOCOL_VERSION, response[:result][:protocolVersion] end + test "server uses default version when not configured" do + server = Server.new(name: "test_server") + request = { + jsonrpc: "2.0", + method: "initialize", + id: 1, + } + + response = server.handle(request) + assert_equal Server::DEFAULT_VERSION, response[:result][:serverInfo][:version] + end + test "#define_tool adds a tool to the server" do @server.define_tool( name: "defined_tool",