Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module ModelContextProtocol
def index
server = ModelContextProtocol::Server.new(
name: "my_server",
version: "1.0.0",
tools: [SomeTool, AnotherTool],
prompts: [MyPrompt],
server_context: { user_id: current_user.id },
Expand Down
1 change: 1 addition & 0 deletions examples/stdio_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def template(args, server_context:)
# Set up the server
server = MCP::Server.new(
name: "example_server",
version: "1.0.0",
tools: [ExampleTool],
prompts: [ExamplePrompt],
resources: [
Expand Down
8 changes: 6 additions & 2 deletions lib/model_context_protocol/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

module ModelContextProtocol
class Server
DEFAULT_VERSION = "0.1.0"

class RequestHandlerError < StandardError
attr_reader :error_type
attr_reader :original_error
Expand All @@ -20,10 +22,11 @@ def initialize(message, request, error_type: :internal_error, original_error: ni

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: [],
Expand All @@ -33,6 +36,7 @@ def initialize(
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
Expand Down Expand Up @@ -153,7 +157,7 @@ def handle_request(request, method)
def server_info
@server_info ||= {
name:,
version: ModelContextProtocol::VERSION,
version:,
}
end

Expand Down
15 changes: 14 additions & 1 deletion test/model_context_protocol/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ServerTest < ActiveSupport::TestCase

@server = Server.new(
name: @server_name,
version: "1.2.3",
tools: [@tool, @tool_that_raises],
prompts: [@prompt],
resources: [@resource],
Expand Down Expand Up @@ -120,7 +121,7 @@ class ServerTest < ActiveSupport::TestCase
},
"serverInfo": {
"name": @server_name,
"version": ModelContextProtocol::VERSION,
"version": "1.2.3",
},
},
}
Expand Down Expand Up @@ -648,6 +649,18 @@ class ServerTest < ActiveSupport::TestCase
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",
Expand Down