Internal Release v0.1.1
- implement `Prompt` - adjust Tool approach - update readme for tool calling - document usage of `Prompt`
This commit is contained in:
commit
a4df3b8688
6 changed files with 1530 additions and 0 deletions
63
test/model_context_protocol/json_rpc/error_test.rb
Normal file
63
test/model_context_protocol/json_rpc/error_test.rb
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
# 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
|
||||
106
test/model_context_protocol/json_rpc/request_test.rb
Normal file
106
test/model_context_protocol/json_rpc/request_test.rb
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
# 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
|
||||
70
test/model_context_protocol/json_rpc/response_test.rb
Normal file
70
test/model_context_protocol/json_rpc/response_test.rb
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
# 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
|
||||
51
test/model_context_protocol/prompt_test.rb
Normal file
51
test/model_context_protocol/prompt_test.rb
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
module ModelContextProtocol
|
||||
class PromptTest < ActiveSupport::TestCase
|
||||
test "#template validates arguments" do
|
||||
prompt = Prompt.new(
|
||||
name: "test_prompt",
|
||||
description: "Test prompt",
|
||||
arguments: [
|
||||
Prompt::Argument.new(name: "test_argument", description: "Test argument", required: true),
|
||||
],
|
||||
)
|
||||
assert_raises(ArgumentError) do
|
||||
prompt.template({})
|
||||
end
|
||||
end
|
||||
|
||||
test "#template returns a Result with description and messages when arguments are valid" do
|
||||
prompt = Prompt.new(
|
||||
name: "test_prompt",
|
||||
description: "Test prompt",
|
||||
arguments: [
|
||||
Prompt::Argument.new(name: "test_argument", description: "Test argument", required: true),
|
||||
],
|
||||
) do |_|
|
||||
Prompt::Result.new(
|
||||
description: "Hello, world!",
|
||||
messages: [
|
||||
Prompt::Message.new(role: "user", content: Content::Text.new("Hello, world!")),
|
||||
Prompt::Message.new(role: "assistant", content: Content::Text.new("Hello, friend!")),
|
||||
],
|
||||
)
|
||||
end
|
||||
|
||||
expected = {
|
||||
description: "Hello, world!",
|
||||
messages: [
|
||||
{ role: "user", content: { text: "Hello, world!" } },
|
||||
{ role: "assistant", content: { text: "Hello, friend!" } },
|
||||
],
|
||||
}
|
||||
|
||||
result = prompt.template({ "test_argument" => "Hello, friend!" })
|
||||
|
||||
assert_equal expected, result.to_h
|
||||
end
|
||||
end
|
||||
end
|
||||
225
test/model_context_protocol/server_test.rb
Normal file
225
test/model_context_protocol/server_test.rb
Normal file
|
|
@ -0,0 +1,225 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
module ModelContextProtocol
|
||||
class ServerTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@tool = Tool.new(name: "test_tool", description: "Test tool", input_schema: {})
|
||||
|
||||
@prompt = Prompt.new(
|
||||
name: "test_prompt",
|
||||
description: "Test prompt",
|
||||
arguments: [
|
||||
Prompt::Argument.new(name: "test_argument", description: "Test argument", required: true),
|
||||
],
|
||||
) do |_|
|
||||
Prompt::Result.new(
|
||||
description: "Hello, world!",
|
||||
messages: [
|
||||
Prompt::Message.new(role: "user", content: Content::Text.new("Hello, world!")),
|
||||
],
|
||||
)
|
||||
end
|
||||
|
||||
@server_name = "test_server"
|
||||
@server = Server.new(name: @server_name, tools: [@tool], prompts: [@prompt])
|
||||
end
|
||||
|
||||
test "ping request returns pong" do
|
||||
request = {
|
||||
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
|
||||
end
|
||||
|
||||
test "initialize request returns protocol info, server info, and capabilities" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "initialize",
|
||||
id: 1,
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
refute_nil response
|
||||
|
||||
result = response.result
|
||||
|
||||
assert_equal Server::PROTOCOL_VERSION, result[:protocolVersion]
|
||||
assert_kind_of Hash, result[:capabilities]
|
||||
assert_equal @server_name, result[:serverInfo][:name]
|
||||
assert_equal ModelContextProtocol::VERSION, result[:serverInfo][:version]
|
||||
end
|
||||
|
||||
test "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])
|
||||
end
|
||||
|
||||
test "returns nil for notification requests" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "some_notification",
|
||||
}.to_json
|
||||
|
||||
assert_nil @server.handle(request)
|
||||
end
|
||||
|
||||
test "tools/call executes tool and returns result" do
|
||||
tool_name = "test_tool"
|
||||
tool_args = { "arg" => "value" }
|
||||
tool_response = Tool::Response.new([{ "result" => "success" }])
|
||||
|
||||
@tool.expects(:call).with(**tool_args).returns(tool_response)
|
||||
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "tools/call",
|
||||
params: {
|
||||
name: tool_name,
|
||||
arguments: tool_args,
|
||||
},
|
||||
id: 1,
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
assert_equal tool_response.to_h, response.result
|
||||
end
|
||||
|
||||
test "tools/call returns error if the tool raises an error" do
|
||||
@tool.expects(:call).raises(StandardError.new("Tool error"))
|
||||
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "tools/call",
|
||||
params: {
|
||||
name: "test_tool",
|
||||
arguments: {},
|
||||
},
|
||||
id: 1,
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
|
||||
assert_instance_of(JsonRPC::InternalError, response.error)
|
||||
assert_equal "Tool error", response.error&.message
|
||||
end
|
||||
|
||||
test "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 "tools/call returns error for unknown tool" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "tools/call",
|
||||
params: {
|
||||
name: "unknown_tool",
|
||||
arguments: {},
|
||||
},
|
||||
id: 1,
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
assert_instance_of(JsonRPC::Response, response)
|
||||
assert_instance_of(JsonRPC::MethodNotFoundError, response.error)
|
||||
end
|
||||
|
||||
test "prompts/list returns list of prompts" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "prompts/list",
|
||||
id: 1,
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
assert_equal({ prompts: [@prompt.to_h] }, response.result)
|
||||
end
|
||||
|
||||
test "prompts/get returns templated prompt" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "prompts/get",
|
||||
id: 1,
|
||||
params: {
|
||||
name: "test_prompt",
|
||||
arguments: { "test_argument" => "Hello, friend!" },
|
||||
},
|
||||
}.to_json
|
||||
|
||||
expected_result = {
|
||||
description: "Hello, world!",
|
||||
messages: [
|
||||
{ role: "user", content: { text: "Hello, world!" } },
|
||||
],
|
||||
}
|
||||
|
||||
response = @server.handle(request)
|
||||
|
||||
assert_equal(expected_result, response.result)
|
||||
end
|
||||
|
||||
test "prompts/get returns error if prompt is not found" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "prompts/get",
|
||||
id: 1,
|
||||
params: {
|
||||
name: "unknown_prompt",
|
||||
arguments: {},
|
||||
},
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
assert_instance_of(JsonRPC::Response, response)
|
||||
assert_instance_of(JsonRPC::MethodNotFoundError, response.error)
|
||||
end
|
||||
|
||||
test "prompts/get returns error if prompt arguments are invalid" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "prompts/get",
|
||||
id: 1,
|
||||
params: {
|
||||
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
|
||||
end
|
||||
|
||||
test "unknown method returns method not found error" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "unknown_method",
|
||||
id: 1,
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
assert_instance_of(JsonRPC::Response, response)
|
||||
assert_instance_of(JsonRPC::MethodNotFoundError, response.error)
|
||||
end
|
||||
end
|
||||
end
|
||||
22
test/model_context_protocol/tool_test.rb
Normal file
22
test/model_context_protocol/tool_test.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
module ModelContextProtocol
|
||||
class ToolTest < ActiveSupport::TestCase
|
||||
test "#to_h returns a hash with name, description, and inputSchema" do
|
||||
tool = Tool.new(name: "mock_tool", description: "a mock tool for testing")
|
||||
assert_equal tool.to_h, { name: "mock_tool", description: "a mock tool for testing", inputSchema: nil }
|
||||
end
|
||||
|
||||
test "#call invokes the tool block and returns the response" do
|
||||
tool = Tool.new(name: "mock_tool", description: "a mock tool for testing") do |_|
|
||||
Tool::Response.new([{ type: "text", content: "OK" }])
|
||||
end
|
||||
|
||||
response = tool.call
|
||||
assert_equal response.content, [{ type: "text", content: "OK" }]
|
||||
assert_equal response.is_error, false
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue