Internal Release 0.2.4

- remove activesupport as a runtime dependency
- use alternative to activesupport/inflector methods
- use `json_rpc_handler` gem
- add json_rpc_handler to gem dependencies
This commit is contained in:
Ates Goral 2025-03-18 15:01:25 -04:00 • committed by Topher Bullock
commit c5591be30b
5 changed files with 145 additions and 507 deletions

View file

@ -1,63 +0,0 @@
# frozen_string_literal: true
require "test_helper"
module ModelContextProtocol
module JsonRPC
class ErrorTest < ActiveSupport::TestCase
test "Error#initialize sets attributes" do
code = -32700
message = "Test error"
data = { foo: "bar" }
error = Error.new(code: code, message: message, data: data)
assert_equal code, error.code
assert_equal message, error.message
assert_equal data, error.data
end
test "Error#to_h returns hash representation" do
error = Error.new(code: -32700, message: "Test error", data: { foo: "bar" })
expected = {
code: -32700,
message: "Test error",
data: { foo: "bar" },
}
assert_equal expected, error.to_h
end
test "Error#to_h excludes nil data" do
error = Error.new(code: -32700, message: "Test error", data: nil)
expected = {
code: -32700,
message: "Test error",
}
assert_equal expected, error.to_h
end
test "InvalidRequestError sets correct code" do
error = InvalidRequestError.new(message: "Invalid request")
assert_equal Error::INVALID_REQUEST_CODE, error.code
assert_equal "Invalid request", error.message
end
test "ParseError sets correct code" do
error = ParseError.new(message: "Parse error")
assert_equal Error::PARSE_ERROR_CODE, error.code
assert_equal "Parse error", error.message
end
test "MethodNotFoundError sets correct code" do
error = MethodNotFoundError.new(message: "Method not found")
assert_equal Error::METHOD_NOT_FOUND_CODE, error.code
assert_equal "Method not found", error.message
end
test "InvalidParamsError sets correct code" do
error = InvalidParamsError.new(message: "Invalid params")
assert_equal Error::INVALID_PARAMS_CODE, error.code
assert_equal "Invalid params", error.message
end
end
end
end

View file

@ -1,106 +0,0 @@
# frozen_string_literal: true
require "test_helper"
module ModelContextProtocol
module JsonRPC
class RequestTest < ActiveSupport::TestCase
test "Request.parse parses valid JSON-RPC request" do
json = {
jsonrpc: "2.0",
method: "test_method",
params: { foo: "bar" },
id: 1,
}.to_json
request = Request.parse(json)
assert_equal "2.0", request.version
assert_equal "test_method", request.method
assert_equal({ "foo" => "bar" }, request.params)
assert_equal 1, request.id
end
test "Request.parse raises ParseError for invalid JSON" do
assert_raises(ParseError) do
Request.parse("invalid json")
end
end
test "Request#initialize sets attributes" do
request = Request.new(
version: "2.0",
method: "test_method",
params: { "foo" => "bar" },
id: 1,
)
assert_equal "2.0", request.version
assert_equal "test_method", request.method
assert_equal({ "foo" => "bar" }, request.params)
assert_equal 1, request.id
end
test "Request#notification? returns true when id is nil" do
request = Request.new(
version: "2.0",
method: "test_method",
)
assert request.notification?
end
test "Request#notification? returns false when id is present" do
request = Request.new(
version: "2.0",
method: "test_method",
id: 1,
)
refute request.notification?
end
test "Request#valid? returns true for valid request" do
request = Request.new(
version: "2.0",
method: "test_method",
)
assert request.valid?
end
test "Request#valid? returns false for invalid version" do
request = Request.new(
version: "1.0",
method: "test_method",
)
refute request.valid?
end
test "Request#validate! raises InvalidRequestError for invalid version" do
assert_raises(InvalidRequestError) do
Request.new(
version: "1.0",
method: "test_method",
).validate!
end
end
test "Request#validate! raises InvalidRequestError for rpc prefixed method" do
assert_raises(InvalidRequestError) do
Request.new(
version: "2.0",
method: "rpc.test",
).validate!
end
end
test "Request#method_not_found! raises MethodNotFoundError" do
request = Request.new(
version: "2.0",
method: "test_method",
)
assert_raises(MethodNotFoundError) do
request.method_not_found!
end
end
end
end
end

View file

@ -1,70 +0,0 @@
# frozen_string_literal: true
require "test_helper"
module ModelContextProtocol
module JsonRPC
class ResponseTest < ActiveSupport::TestCase
test "Response#initialize sets attributes with result" do
result = { foo: "bar" }
id = 1
response = Response.new(result: result, id: id)
assert_equal "2.0", response.version
assert_equal result, response.result
assert_nil response.error
assert_equal id, response.id
end
test "Response#initialize sets attributes with error" do
error = Error.new(code: -32700, message: "Test error")
id = 1
response = Response.new(error: error, id: id)
assert_equal "2.0", response.version
assert_nil response.result
assert_equal error, response.error
assert_equal id, response.id
end
test "Response#to_h returns hash representation with result" do
result = { foo: "bar" }
id = 1
response = Response.new(result: result, id: id)
expected = {
jsonrpc: "2.0",
result: result,
id: id,
}
assert_equal expected, response.to_h
end
test "Response#to_h returns hash representation with error" do
error = Error.new(code: -32700, message: "Test error")
id = 1
response = Response.new(error: error, id: id)
expected = {
jsonrpc: "2.0",
error: error.to_h,
id: id,
}
assert_equal expected, response.to_h
end
test "Response#initialize raises InvalidResponse when both result and error present" do
assert_raises(InvalidResponse) do
Response.new(
result: { foo: "bar" },
error: Error.new(code: -32700, message: "Test error"),
)
end
end
test "Response#initialize raises InvalidResponse when neither result nor error present" do
assert_raises(InvalidResponse) do
Response.new
end
end
end
end
end

View file

@ -39,13 +39,13 @@ module ModelContextProtocol
jsonrpc: "2.0",
method: "ping",
id: 1,
}.to_json
}
response = @server.handle(request)
refute_nil response
assert_equal "pong", response.result
assert_equal 1, response.id
assert_equal "pong", response[:result]
assert_equal 1, response[:id]
end
test "#handle initialize request returns protocol info, server info, and capabilities" do
@ -53,13 +53,12 @@ module ModelContextProtocol
jsonrpc: "2.0",
method: "initialize",
id: 1,
}.to_json
}
response = @server.handle(request)
refute_nil response
result = response.result
result = response[:result]
assert_equal Server::PROTOCOL_VERSION, result[:protocolVersion]
assert_kind_of Hash, result[:capabilities]
assert_equal @server_name, result[:serverInfo][:name]
@ -70,29 +69,24 @@ module ModelContextProtocol
request = {
jsonrpc: "2.0",
method: "some_notification",
}.to_json
}
assert_nil @server.handle(request)
end
test "#handle returns error response for invalid requests" do
response = @server.handle("invalid json")
assert_instance_of JsonRPC::Response, response
assert_instance_of(JsonRPC::ParseError, response.error)
end
test "#handle tools/list returns available tools" do
request = {
jsonrpc: "2.0",
method: "tools/list",
id: 1,
}.to_json
}
response = @server.handle(request)
assert_kind_of Array, response.result[:tools]
assert_equal "test_tool", response.result[:tools][0][:name]
assert_equal "Test tool", response.result[:tools][0][:description]
assert_equal({}, response.result[:tools][0][:inputSchema])
result = response[:result]
assert_kind_of Array, result[:tools]
assert_equal "test_tool", result[:tools][0][:name]
assert_equal "Test tool", result[:tools][0][:description]
assert_equal({}, result[:tools][0][:inputSchema])
end
test "#tools_list_handler sets the tools/list handler" do
@ -104,10 +98,11 @@ module ModelContextProtocol
jsonrpc: "2.0",
method: "tools/list",
id: 1,
}.to_json
}
response = @server.handle(request)
assert_equal({ tools: [{ name: "hammer", description: "Hammer time!" }] }, response.result)
result = response[:result]
assert_equal({ tools: [{ name: "hammer", description: "Hammer time!" }] }, result)
end
test "#handle tools/call executes tool and returns result" do
@ -125,10 +120,10 @@ module ModelContextProtocol
arguments: tool_args,
},
id: 1,
}.to_json
}
response = @server.handle(request)
assert_equal tool_response.to_h, response.result
assert_equal tool_response.to_h, response[:result]
end
test "#handle tools/call returns error if the tool raises an error" do
@ -142,12 +137,12 @@ module ModelContextProtocol
arguments: {},
},
id: 1,
}.to_json
}
response = @server.handle(request)
assert_instance_of(JsonRPC::InternalError, response.error)
assert_equal "Tool error", response.error&.message
assert_equal "Internal error", response[:error][:message]
assert_equal "Tool error", response[:error][:data]
end
test "#handle tools/call returns error for unknown tool" do
@ -159,16 +154,16 @@ module ModelContextProtocol
arguments: {},
},
id: 1,
}.to_json
}
response = @server.handle(request)
assert_instance_of(JsonRPC::Response, response)
assert_instance_of(JsonRPC::MethodNotFoundError, response.error)
assert_equal "Internal error", response[:error][:message]
assert_equal "Tool not found unknown_tool", response[:error][:data]
end
test "#tools_call_handler sets the tools/call handler" do
@server.tools_call_handler do |request|
tool_name = request.params&.dig("name")
tool_name = request[:name]
return Tool::Response.new("#{tool_name} called successfully").to_h
end
@ -177,10 +172,10 @@ module ModelContextProtocol
method: "tools/call",
params: { name: "my_tool", arguments: {} },
id: 1,
}.to_json
}
response = @server.handle(request)
assert_equal({ content: "my_tool called successfully", is_error: false }, response.result)
assert_equal({ content: "my_tool called successfully", is_error: false }, response[:result])
end
test "#handle prompts/list returns list of prompts" do
@ -188,10 +183,10 @@ module ModelContextProtocol
jsonrpc: "2.0",
method: "prompts/list",
id: 1,
}.to_json
}
response = @server.handle(request)
assert_equal({ prompts: [@prompt.to_h] }, response.result)
assert_equal({ prompts: [@prompt.to_h] }, response[:result])
end
test "#prompts_list_handler sets the prompts/list handler" do
@ -203,10 +198,10 @@ module ModelContextProtocol
jsonrpc: "2.0",
method: "prompts/list",
id: 1,
}.to_json
}
response = @server.handle(request)
assert_equal({ prompts: [{ name: "foo_prompt", description: "Foo prompt" }] }, response.result)
assert_equal({ prompts: [{ name: "foo_prompt", description: "Foo prompt" }] }, response[:result])
end
test "#handle prompts/get returns templated prompt" do
@ -218,7 +213,7 @@ module ModelContextProtocol
name: "test_prompt",
arguments: { "test_argument" => "Hello, friend!" },
},
}.to_json
}
expected_result = {
description: "Hello, world!",
@ -228,8 +223,7 @@ module ModelContextProtocol
}
response = @server.handle(request)
assert_equal(expected_result, response.result)
assert_equal(expected_result, response[:result])
end
test "#handle prompts/get returns error if prompt is not found" do
@ -241,11 +235,10 @@ module ModelContextProtocol
name: "unknown_prompt",
arguments: {},
},
}.to_json
}
response = @server.handle(request)
assert_instance_of(JsonRPC::Response, response)
assert_instance_of(JsonRPC::MethodNotFoundError, response.error)
assert_equal("Prompt not found unknown_prompt", response[:error][:data])
end
test "#handle prompts/get returns error if prompt arguments are invalid" do
@ -257,21 +250,19 @@ module ModelContextProtocol
name: "test_prompt",
arguments: { "unknown_argument" => "Hello, friend!" },
},
}.to_json
}
response = @server.handle(request)
assert_instance_of(JsonRPC::Response, response)
assert_instance_of(JsonRPC::InvalidParamsError, response.error)
assert_equal "Missing required arguments: test_argument", response.error.message
assert_equal "Missing required arguments: test_argument", response[:error][:data]
end
test "#prompts_get_handler sets the prompts/get handler" do
@server.prompts_get_handler do |request|
prompt_name = request.params&.dig("name")
prompt_name = request[:name]
return Prompt::Result.new(
description: prompt_name,
messages: [
Prompt::Message.new(role: "user", content: Content::Text.new(request.params&.dig("arguments", "foo"))),
Prompt::Message.new(role: "user", content: Content::Text.new(request[:arguments][:foo])),
],
).to_h
end
@ -281,12 +272,12 @@ module ModelContextProtocol
method: "prompts/get",
id: 1,
params: { name: "foo_bar_prompt", arguments: { "foo" => "bar" } },
}.to_json
}
response = @server.handle(request)
assert_equal(
{ description: "foo_bar_prompt", messages: [{ role: "user", content: { text: "bar" } }] },
response.result,
response[:result],
)
end
@ -295,10 +286,10 @@ module ModelContextProtocol
jsonrpc: "2.0",
method: "resources/list",
id: 1,
}.to_json
}
response = @server.handle(request)
assert_equal({ resources: [@resource.to_h] }, response.result)
assert_equal({ resources: [@resource.to_h] }, response[:result])
end
test "#resources_list_handler sets the resources/list handler" do
@ -310,12 +301,12 @@ module ModelContextProtocol
jsonrpc: "2.0",
method: "resources/list",
id: 1,
}.to_json
}
response = @server.handle(request)
assert_equal(
{ resources: [{ uri: "test_resource", name: "Test resource", description: "Test resource" }] },
response.result,
response[:result],
)
end
@ -327,10 +318,10 @@ module ModelContextProtocol
params: {
uri: @resource.uri,
},
}.to_json
}
response = @server.handle(request)
assert_equal(@resource.to_h, response.result)
assert_equal(@resource.to_h, response[:result])
end
test "#handle resources/read returns error if resource is not found" do
@ -341,16 +332,15 @@ module ModelContextProtocol
params: {
uri: "unknown_resource",
},
}.to_json
}
response = @server.handle(request)
assert_instance_of(JsonRPC::Response, response)
assert_equal "Resource not found unknown_resource", response.error.message
assert_equal "Resource not found unknown_resource", response[:error][:data]
end
test "#resources_read_handler sets the resources/read handler" do
@server.resources_read_handler do |request|
uri = request.params&.dig("uri")
uri = request[:uri]
Resource.new(
uri: uri,
@ -368,7 +358,7 @@ module ModelContextProtocol
params: {
uri: "test_resource",
},
}.to_json
}
response = @server.handle(request)
assert_equal(
@ -379,7 +369,7 @@ module ModelContextProtocol
mimeType: "text/plain",
contents: [{ text: "Lorem ipsum dolor sit amet" }],
},
response.result,
response[:result],
)
end
@ -388,11 +378,12 @@ module ModelContextProtocol
jsonrpc: "2.0",
id: 1,
method: "unknown_method",
}.to_json
}
response = @server.handle(request)
assert_instance_of(JsonRPC::Response, response)
assert_instance_of(JsonRPC::MethodNotFoundError, response.error)
assert_equal "Method not found", response[:error][:message]
assert_equal "unknown_method", response[:error][:data]
end
end
end

View file

@ -0,0 +1,22 @@
# frozen_string_literal: true
require "test_helper"
module ModelContextProtocol
class StringUtilsTest < Minitest::Test
def test_handle_from_class_name_returns_the_class_name_without_the_module_for_a_class_without_a_module
assert_equal("test", StringUtils.handle_from_class_name("Test"))
assert_equal("test_class", StringUtils.handle_from_class_name("TestClass"))
end
def test_handle_from_class_name_returns_the_class_name_without_the_module_for_a_class_with_a_single_parent_module
assert_equal("test", StringUtils.handle_from_class_name("Module::Test"))
assert_equal("test_class", StringUtils.handle_from_class_name("Module::TestClass"))
end
def test_handle_from_class_name_returns_the_class_name_without_the_module_for_a_class_with_multiple_parent_modules
assert_equal("test", StringUtils.handle_from_class_name("Module::Submodule::Test"))
assert_equal("test_class", StringUtils.handle_from_class_name("Module::Submodule::TestClass"))
end
end
end