Internal Release 0.2.0
- fix `lib/mcp-ruby` auto-import path - implement server resources - add basic listing of resources to Server - allow setting handlers on Server - wrap handler method results - make tool definition more declarative - make prompt definition more declarative - bump dependency versions
This commit is contained in:
parent
a4df3b8688
commit
6004f42573
3 changed files with 708 additions and 164 deletions
|
|
@ -5,27 +5,13 @@ 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
|
||||
class TestPrompt < Prompt
|
||||
description "Test prompt"
|
||||
arguments [
|
||||
Prompt::Argument.new(name: "test_argument", description: "Test argument", required: true),
|
||||
]
|
||||
|
||||
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 |_|
|
||||
def template(args)
|
||||
Prompt::Result.new(
|
||||
description: "Hello, world!",
|
||||
messages: [
|
||||
|
|
@ -34,6 +20,108 @@ module ModelContextProtocol
|
|||
],
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
test "#template returns a Result with description and messages" do
|
||||
prompt = TestPrompt.new
|
||||
|
||||
expected_template_result = {
|
||||
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_template_result, result.to_h
|
||||
end
|
||||
|
||||
test "allows declarative definition of prompts as classes" do
|
||||
class MockPrompt < Prompt
|
||||
prompt_name "my_mock_prompt"
|
||||
description "a mock prompt for testing"
|
||||
arguments [
|
||||
Prompt::Argument.new(name: "test_argument", description: "Test argument", required: true),
|
||||
]
|
||||
|
||||
def template(args)
|
||||
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(args["test_argument"])),
|
||||
],
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
prompt = MockPrompt.new
|
||||
|
||||
assert_equal "my_mock_prompt", prompt.name
|
||||
assert_equal "a mock prompt for testing", prompt.description
|
||||
assert_equal "test_argument", prompt.arguments.first.name
|
||||
assert_equal "Test argument", prompt.arguments.first.description
|
||||
assert_equal true, prompt.arguments.first.required
|
||||
|
||||
expected_template_result = {
|
||||
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_template_result, result.to_h
|
||||
end
|
||||
|
||||
test "defaults to class name as prompt name" do
|
||||
class DefaultNamePrompt < Prompt
|
||||
description "a mock prompt for testing"
|
||||
arguments [
|
||||
Prompt::Argument.new(name: "test_argument", description: "Test argument", required: true),
|
||||
]
|
||||
|
||||
def template(args)
|
||||
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(args["test_argument"])),
|
||||
],
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
prompt = DefaultNamePrompt.new
|
||||
|
||||
assert_equal "default_name_prompt", prompt.name
|
||||
assert_equal "a mock prompt for testing", prompt.description
|
||||
assert_equal "test_argument", prompt.arguments.first.name
|
||||
end
|
||||
|
||||
test ".define allows definition of simple prompts with a block" do
|
||||
prompt = Prompt.define(
|
||||
name: "mock_prompt",
|
||||
description: "a mock prompt for testing",
|
||||
arguments: [
|
||||
Prompt::Argument.new(name: "test_argument", description: "Test argument", required: true),
|
||||
],
|
||||
) do |args|
|
||||
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(args["test_argument"])),
|
||||
],
|
||||
)
|
||||
end
|
||||
|
||||
assert_equal "mock_prompt", prompt.name
|
||||
assert_equal "a mock prompt for testing", prompt.description
|
||||
assert_equal "test_argument", prompt.arguments.first.name
|
||||
|
||||
expected = {
|
||||
description: "Hello, world!",
|
||||
|
|
@ -44,7 +132,6 @@ module ModelContextProtocol
|
|||
}
|
||||
|
||||
result = prompt.template({ "test_argument" => "Hello, friend!" })
|
||||
|
||||
assert_equal expected, result.to_h
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ module ModelContextProtocol
|
|||
setup do
|
||||
@tool = Tool.new(name: "test_tool", description: "Test tool", input_schema: {})
|
||||
|
||||
@prompt = Prompt.new(
|
||||
@prompt = Prompt.define(
|
||||
name: "test_prompt",
|
||||
description: "Test prompt",
|
||||
arguments: [
|
||||
|
|
@ -22,11 +22,19 @@ module ModelContextProtocol
|
|||
)
|
||||
end
|
||||
|
||||
@resource = Resource.new(
|
||||
uri: "test_resource",
|
||||
name: "Test resource",
|
||||
description: "Test resource",
|
||||
mime_type: "text/plain",
|
||||
contents: [Content::Text.new("Hello, world!")],
|
||||
)
|
||||
|
||||
@server_name = "test_server"
|
||||
@server = Server.new(name: @server_name, tools: [@tool], prompts: [@prompt])
|
||||
@server = Server.new(name: @server_name, tools: [@tool], prompts: [@prompt], resources: [@resource])
|
||||
end
|
||||
|
||||
test "ping request returns pong" do
|
||||
test "#handle ping request returns pong" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "ping",
|
||||
|
|
@ -40,7 +48,7 @@ module ModelContextProtocol
|
|||
assert_equal 1, response.id
|
||||
end
|
||||
|
||||
test "initialize request returns protocol info, server info, and capabilities" do
|
||||
test "#handle initialize request returns protocol info, server info, and capabilities" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "initialize",
|
||||
|
|
@ -58,7 +66,22 @@ module ModelContextProtocol
|
|||
assert_equal ModelContextProtocol::VERSION, result[:serverInfo][:version]
|
||||
end
|
||||
|
||||
test "tools/list returns available tools" do
|
||||
test "#handle returns nil for notification requests" do
|
||||
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",
|
||||
|
|
@ -72,16 +95,22 @@ module ModelContextProtocol
|
|||
assert_equal({}, response.result[:tools][0][:inputSchema])
|
||||
end
|
||||
|
||||
test "returns nil for notification requests" do
|
||||
test "#tools_list_handler sets the tools/list handler" do
|
||||
@server.tools_list_handler do
|
||||
[{ name: "hammer", description: "Hammer time!" }]
|
||||
end
|
||||
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "some_notification",
|
||||
method: "tools/list",
|
||||
id: 1,
|
||||
}.to_json
|
||||
|
||||
assert_nil @server.handle(request)
|
||||
response = @server.handle(request)
|
||||
assert_equal({ tools: [{ name: "hammer", description: "Hammer time!" }] }, response.result)
|
||||
end
|
||||
|
||||
test "tools/call executes tool and returns result" do
|
||||
test "#handle tools/call executes tool and returns result" do
|
||||
tool_name = "test_tool"
|
||||
tool_args = { "arg" => "value" }
|
||||
tool_response = Tool::Response.new([{ "result" => "success" }])
|
||||
|
|
@ -102,7 +131,7 @@ module ModelContextProtocol
|
|||
assert_equal tool_response.to_h, response.result
|
||||
end
|
||||
|
||||
test "tools/call returns error if the tool raises an error" do
|
||||
test "#handle tools/call returns error if the tool raises an error" do
|
||||
@tool.expects(:call).raises(StandardError.new("Tool error"))
|
||||
|
||||
request = {
|
||||
|
|
@ -121,13 +150,7 @@ module ModelContextProtocol
|
|||
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
|
||||
test "#handle tools/call returns error for unknown tool" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "tools/call",
|
||||
|
|
@ -143,7 +166,24 @@ module ModelContextProtocol
|
|||
assert_instance_of(JsonRPC::MethodNotFoundError, response.error)
|
||||
end
|
||||
|
||||
test "prompts/list returns list of prompts" do
|
||||
test "#tools_call_handler sets the tools/call handler" do
|
||||
@server.tools_call_handler do |request|
|
||||
tool_name = request.params&.dig("name")
|
||||
return Tool::Response.new("#{tool_name} called successfully").to_h
|
||||
end
|
||||
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
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)
|
||||
end
|
||||
|
||||
test "#handle prompts/list returns list of prompts" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "prompts/list",
|
||||
|
|
@ -154,7 +194,22 @@ module ModelContextProtocol
|
|||
assert_equal({ prompts: [@prompt.to_h] }, response.result)
|
||||
end
|
||||
|
||||
test "prompts/get returns templated prompt" do
|
||||
test "#prompts_list_handler sets the prompts/list handler" do
|
||||
@server.prompts_list_handler do
|
||||
[{ name: "foo_prompt", description: "Foo prompt" }]
|
||||
end
|
||||
|
||||
request = {
|
||||
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)
|
||||
end
|
||||
|
||||
test "#handle prompts/get returns templated prompt" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "prompts/get",
|
||||
|
|
@ -177,7 +232,7 @@ module ModelContextProtocol
|
|||
assert_equal(expected_result, response.result)
|
||||
end
|
||||
|
||||
test "prompts/get returns error if prompt is not found" do
|
||||
test "#handle prompts/get returns error if prompt is not found" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "prompts/get",
|
||||
|
|
@ -193,7 +248,7 @@ module ModelContextProtocol
|
|||
assert_instance_of(JsonRPC::MethodNotFoundError, response.error)
|
||||
end
|
||||
|
||||
test "prompts/get returns error if prompt arguments are invalid" do
|
||||
test "#handle prompts/get returns error if prompt arguments are invalid" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "prompts/get",
|
||||
|
|
@ -210,11 +265,129 @@ module ModelContextProtocol
|
|||
assert_equal "Missing required arguments: test_argument", response.error.message
|
||||
end
|
||||
|
||||
test "unknown method returns method not found error" do
|
||||
test "#prompts_get_handler sets the prompts/get handler" do
|
||||
@server.prompts_get_handler do |request|
|
||||
prompt_name = request.params&.dig("name")
|
||||
return Prompt::Result.new(
|
||||
description: prompt_name,
|
||||
messages: [
|
||||
Prompt::Message.new(role: "user", content: Content::Text.new(request.params&.dig("arguments", "foo"))),
|
||||
],
|
||||
).to_h
|
||||
end
|
||||
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "unknown_method",
|
||||
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,
|
||||
)
|
||||
end
|
||||
|
||||
test "#handle resources/list returns a list of resources" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "resources/list",
|
||||
id: 1,
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
assert_equal({ resources: [@resource.to_h] }, response.result)
|
||||
end
|
||||
|
||||
test "#resources_list_handler sets the resources/list handler" do
|
||||
@server.resources_list_handler do
|
||||
[{ uri: "test_resource", name: "Test resource", description: "Test resource" }]
|
||||
end
|
||||
|
||||
request = {
|
||||
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,
|
||||
)
|
||||
end
|
||||
|
||||
test "#handle resources/read returns a resource" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "resources/read",
|
||||
id: 1,
|
||||
params: {
|
||||
uri: @resource.uri,
|
||||
},
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
assert_equal(@resource.to_h, response.result)
|
||||
end
|
||||
|
||||
test "#handle resources/read returns error if resource is not found" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "resources/read",
|
||||
id: 1,
|
||||
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
|
||||
end
|
||||
|
||||
test "#resources_read_handler sets the resources/read handler" do
|
||||
@server.resources_read_handler do |request|
|
||||
uri = request.params&.dig("uri")
|
||||
|
||||
Resource.new(
|
||||
uri: uri,
|
||||
name: "Test resource",
|
||||
description: "Test resource",
|
||||
mime_type: "text/plain",
|
||||
contents: [Content::Text.new("Lorem ipsum dolor sit amet")],
|
||||
).to_h
|
||||
end
|
||||
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
method: "resources/read",
|
||||
id: 1,
|
||||
params: {
|
||||
uri: "test_resource",
|
||||
},
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
assert_equal(
|
||||
{
|
||||
uri: "test_resource",
|
||||
name: "Test resource",
|
||||
description: "Test resource",
|
||||
mimeType: "text/plain",
|
||||
contents: [{ text: "Lorem ipsum dolor sit amet" }],
|
||||
},
|
||||
response.result,
|
||||
)
|
||||
end
|
||||
|
||||
test "#handle unknown method returns method not found error" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
id: 1,
|
||||
method: "unknown_method",
|
||||
}.to_json
|
||||
|
||||
response = @server.handle(request)
|
||||
|
|
|
|||
|
|
@ -4,19 +4,62 @@ require "test_helper"
|
|||
|
||||
module ModelContextProtocol
|
||||
class ToolTest < ActiveSupport::TestCase
|
||||
class TestTool < Tool
|
||||
tool_name "test_tool"
|
||||
description "a test tool for testing"
|
||||
input_schema [{ type: "text", name: "message" }]
|
||||
|
||||
def call(message)
|
||||
Tool::Response.new([{ type: "text", content: "OK" }])
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
tool = TestTool.new
|
||||
response = tool.call("test")
|
||||
assert_equal response.content, [{ type: "text", content: "OK" }]
|
||||
assert_equal response.is_error, false
|
||||
end
|
||||
|
||||
test "allows declarative definition of tools as classes" do
|
||||
class MockTool < Tool
|
||||
tool_name "my_mock_tool"
|
||||
description "a mock tool for testing"
|
||||
input_schema [{ type: "text", name: "message" }]
|
||||
end
|
||||
|
||||
tool = MockTool.new
|
||||
assert_equal tool.name, "my_mock_tool"
|
||||
assert_equal tool.description, "a mock tool for testing"
|
||||
assert_equal tool.input_schema, [{ type: "text", name: "message" }]
|
||||
end
|
||||
|
||||
test "defaults to class name as tool name" do
|
||||
class DefaultNameTool < Tool
|
||||
description "a mock tool for testing"
|
||||
input_schema [{ type: "text", name: "message" }]
|
||||
end
|
||||
|
||||
tool = DefaultNameTool.new
|
||||
|
||||
assert_equal tool.name, "default_name_tool"
|
||||
assert_equal tool.description, "a mock tool for testing"
|
||||
assert_equal tool.input_schema, [{ type: "text", name: "message" }]
|
||||
end
|
||||
|
||||
test ".define allows definition of simple tools with a block" do
|
||||
tool = Tool.define(name: "mock_tool", description: "a mock tool for testing") do |_|
|
||||
Tool::Response.new([{ type: "text", content: "OK" }])
|
||||
end
|
||||
|
||||
assert_equal tool.name, "mock_tool"
|
||||
assert_equal tool.description, "a mock tool for testing"
|
||||
assert_equal tool.input_schema, nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue