Skip to content

Commit e89af18

Browse files
Merge pull request #7 from kfischer-okarin/specify-server-version
Allow specifying the server version
2 parents bdbf015 + 7b39005 commit e89af18

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module ModelContextProtocol
5050
def index
5151
server = ModelContextProtocol::Server.new(
5252
name: "my_server",
53+
version: "1.0.0",
5354
tools: [SomeTool, AnotherTool],
5455
prompts: [MyPrompt],
5556
server_context: { user_id: current_user.id },

examples/stdio_server.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def template(args, server_context:)
5454
# Set up the server
5555
server = MCP::Server.new(
5656
name: "example_server",
57+
version: "1.0.0",
5758
tools: [ExampleTool],
5859
prompts: [ExamplePrompt],
5960
resources: [

lib/model_context_protocol/server.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
module ModelContextProtocol
88
class Server
9+
DEFAULT_VERSION = "0.1.0"
10+
911
class RequestHandlerError < StandardError
1012
attr_reader :error_type
1113
attr_reader :original_error
@@ -20,10 +22,11 @@ def initialize(message, request, error_type: :internal_error, original_error: ni
2022

2123
include Instrumentation
2224

23-
attr_accessor :name, :tools, :prompts, :resources, :server_context, :configuration, :capabilities
25+
attr_accessor :name, :version, :tools, :prompts, :resources, :server_context, :configuration, :capabilities
2426

2527
def initialize(
2628
name: "model_context_protocol",
29+
version: DEFAULT_VERSION,
2730
tools: [],
2831
prompts: [],
2932
resources: [],
@@ -33,6 +36,7 @@ def initialize(
3336
capabilities: { prompts: {}, resources: {}, tools: {} }
3437
)
3538
@name = name
39+
@version = version
3640
@tools = tools.to_h { |t| [t.name_value, t] }
3741
@prompts = prompts.to_h { |p| [p.name_value, p] }
3842
@resources = resources
@@ -153,7 +157,7 @@ def handle_request(request, method)
153157
def server_info
154158
@server_info ||= {
155159
name:,
156-
version: ModelContextProtocol::VERSION,
160+
version:,
157161
}
158162
end
159163

test/model_context_protocol/server_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class ServerTest < ActiveSupport::TestCase
5151

5252
@server = Server.new(
5353
name: @server_name,
54+
version: "1.2.3",
5455
tools: [@tool, @tool_that_raises],
5556
prompts: [@prompt],
5657
resources: [@resource],
@@ -120,7 +121,7 @@ class ServerTest < ActiveSupport::TestCase
120121
},
121122
"serverInfo": {
122123
"name": @server_name,
123-
"version": ModelContextProtocol::VERSION,
124+
"version": "1.2.3",
124125
},
125126
},
126127
}
@@ -648,6 +649,18 @@ class ServerTest < ActiveSupport::TestCase
648649
assert_equal Configuration::DEFAULT_PROTOCOL_VERSION, response[:result][:protocolVersion]
649650
end
650651

652+
test "server uses default version when not configured" do
653+
server = Server.new(name: "test_server")
654+
request = {
655+
jsonrpc: "2.0",
656+
method: "initialize",
657+
id: 1,
658+
}
659+
660+
response = server.handle(request)
661+
assert_equal Server::DEFAULT_VERSION, response[:result][:serverInfo][:version]
662+
end
663+
651664
test "#define_tool adds a tool to the server" do
652665
@server.define_tool(
653666
name: "defined_tool",

0 commit comments

Comments
 (0)