Merge branch 'main' into auto-determine-capabilities

This commit is contained in:
Topher Bullock 2025-05-20 17:01:45 -04:00 • committed by GitHub
commit 00e1ed8fec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 25 additions and 3 deletions

View file

@ -50,6 +50,7 @@ module ModelContextProtocol
def index def index
server = ModelContextProtocol::Server.new( server = ModelContextProtocol::Server.new(
name: "my_server", name: "my_server",
version: "1.0.0",
tools: [SomeTool, AnotherTool], tools: [SomeTool, AnotherTool],
prompts: [MyPrompt], prompts: [MyPrompt],
server_context: { user_id: current_user.id }, server_context: { user_id: current_user.id },

View file

@ -54,6 +54,7 @@ end
# Set up the server # Set up the server
server = MCP::Server.new( server = MCP::Server.new(
name: "example_server", name: "example_server",
version: "1.0.0",
tools: [ExampleTool], tools: [ExampleTool],
prompts: [ExamplePrompt], prompts: [ExamplePrompt],
resources: [ resources: [

View file

@ -6,6 +6,8 @@ require_relative "methods"
module ModelContextProtocol module ModelContextProtocol
class Server class Server
DEFAULT_VERSION = "0.1.0"
class RequestHandlerError < StandardError class RequestHandlerError < StandardError
attr_reader :error_type attr_reader :error_type
attr_reader :original_error attr_reader :original_error
@ -20,11 +22,13 @@ module ModelContextProtocol
include Instrumentation include Instrumentation
attr_writer :capabilities attr_writer :capabilities
attr_accessor :name, :tools, :prompts, :resources, :server_context, :configuration attr_accessor :name, :version, :tools, :prompts, :resources, :server_context, :configuration
def initialize( def initialize(
name: "model_context_protocol", name: "model_context_protocol",
version: DEFAULT_VERSION,
tools: [], tools: [],
prompts: [], prompts: [],
resources: [], resources: [],
@ -34,6 +38,7 @@ module ModelContextProtocol
capabilities: nil capabilities: nil
) )
@name = name @name = name
@version = version
@tools = tools.to_h { |t| [t.name_value, t] } @tools = tools.to_h { |t| [t.name_value, t] }
@prompts = prompts.to_h { |p| [p.name_value, p] } @prompts = prompts.to_h { |p| [p.name_value, p] }
@resources = resources @resources = resources
@ -169,7 +174,7 @@ module ModelContextProtocol
def server_info def server_info
@server_info ||= { @server_info ||= {
name:, name:,
version: ModelContextProtocol::VERSION, version:,
} }
end end

View file

@ -9,6 +9,8 @@ module ModelContextProtocol
def initialize(server) def initialize(server)
@server = server @server = server
@open = false @open = false
$stdin.set_encoding("UTF-8")
$stdout.set_encoding("UTF-8")
super super
end end

View file

@ -51,6 +51,7 @@ module ModelContextProtocol
@server = Server.new( @server = Server.new(
name: @server_name, name: @server_name,
version: "1.2.3",
tools: [@tool, @tool_that_raises], tools: [@tool, @tool_that_raises],
prompts: [@prompt], prompts: [@prompt],
resources: [@resource], resources: [@resource],
@ -120,7 +121,7 @@ module ModelContextProtocol
}, },
"serverInfo": { "serverInfo": {
"name": @server_name, "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] assert_equal Configuration::DEFAULT_PROTOCOL_VERSION, response[:result][:protocolVersion]
end 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 test "#define_tool adds a tool to the server" do
@server.define_tool( @server.define_tool(
name: "defined_tool", name: "defined_tool",