Internal Release 0.5.0
Bump protocol version to 2025-03-26 Provide fallback value for tool_name Make configuration per server Add support for tool annotations document tool annotations
This commit is contained in:
parent
2eca3681df
commit
7175d49108
4 changed files with 353 additions and 42 deletions
|
|
@ -6,12 +6,11 @@ module ModelContextProtocol
|
|||
class InstrumentationTest < ActiveSupport::TestCase
|
||||
class Subject
|
||||
include Instrumentation
|
||||
attr_reader :instrumentation_data_received
|
||||
attr_reader :instrumentation_data_received, :configuration
|
||||
|
||||
def initialize
|
||||
ModelContextProtocol.configure do |config|
|
||||
config.instrumentation_callback = ->(data) { @instrumentation_data_received = data }
|
||||
end
|
||||
@configuration = ModelContextProtocol::Configuration.new
|
||||
@configuration.instrumentation_callback = ->(data) { @instrumentation_data_received = data }
|
||||
end
|
||||
|
||||
def instrumented_method
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ module ModelContextProtocol
|
|||
expected_template_result = {
|
||||
description: "Hello, world!",
|
||||
messages: [
|
||||
{ role: "user", content: { text: "Hello, world!" } },
|
||||
{ role: "assistant", content: { text: "Hello, friend!" } },
|
||||
{ role: "user", content: { text: "Hello, world!", type: "text" } },
|
||||
{ role: "assistant", content: { text: "Hello, friend!", type: "text" } },
|
||||
],
|
||||
}
|
||||
|
||||
|
|
@ -72,8 +72,8 @@ module ModelContextProtocol
|
|||
expected_template_result = {
|
||||
description: "Hello, world!",
|
||||
messages: [
|
||||
{ role: "user", content: { text: "Hello, world!" } },
|
||||
{ role: "assistant", content: { text: "Hello, friend!" } },
|
||||
{ role: "user", content: { text: "Hello, world!", type: "text" } },
|
||||
{ role: "assistant", content: { text: "Hello, friend!", type: "text" } },
|
||||
],
|
||||
}
|
||||
|
||||
|
|
@ -134,8 +134,8 @@ module ModelContextProtocol
|
|||
expected = {
|
||||
description: "Hello, world!",
|
||||
messages: [
|
||||
{ role: "user", content: { text: "Hello, world!" } },
|
||||
{ role: "assistant", content: { text: "Hello, friend! user: 123" } },
|
||||
{ role: "user", content: { text: "Hello, world!", type: "text" } },
|
||||
{ role: "assistant", content: { text: "Hello, friend! user: 123", type: "text" } },
|
||||
],
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,19 +33,19 @@ module ModelContextProtocol
|
|||
)
|
||||
|
||||
@server_name = "test_server"
|
||||
ModelContextProtocol.configure do |config|
|
||||
config.instrumentation_callback = instrumentation_helper.callback
|
||||
end
|
||||
configuration = ModelContextProtocol::Configuration.new
|
||||
configuration.instrumentation_callback = instrumentation_helper.callback
|
||||
|
||||
@server = Server.new(
|
||||
name: @server_name,
|
||||
tools: [@tool],
|
||||
prompts: [@prompt],
|
||||
resources: [@resource],
|
||||
configuration:,
|
||||
)
|
||||
end
|
||||
|
||||
# https://spec.modelcontextprotocol.io/specification/2024-11-05/basic/utilities/ping/#behavior-requirements
|
||||
# https://spec.modelcontextprotocol.io/specification/2025-03-26/basic/utilities/ping/#behavior-requirements
|
||||
test "#handle ping request returns empty response" do
|
||||
request = {
|
||||
jsonrpc: "2.0",
|
||||
|
|
@ -98,7 +98,7 @@ module ModelContextProtocol
|
|||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"result": {
|
||||
"protocolVersion": "2024-11-05",
|
||||
"protocolVersion": "2025-03-26",
|
||||
"capabilities": {
|
||||
"prompts": {},
|
||||
"resources": {},
|
||||
|
|
@ -217,7 +217,7 @@ module ModelContextProtocol
|
|||
exception = StandardError.new("Tool error")
|
||||
@tool.expects(:call).raises(exception)
|
||||
|
||||
ModelContextProtocol.configuration.exception_reporter.expects(:call).with(
|
||||
@server.configuration.exception_reporter.expects(:call).with(
|
||||
exception,
|
||||
{
|
||||
tool_name: "test_tool",
|
||||
|
|
@ -347,14 +347,14 @@ module ModelContextProtocol
|
|||
id: 1,
|
||||
params: {
|
||||
name: "test_prompt",
|
||||
arguments: { "test_argument" => "Hello, friend!" },
|
||||
arguments: { test_argument: "Hello, friend!" },
|
||||
},
|
||||
}
|
||||
|
||||
expected_result = {
|
||||
description: "Hello, world!",
|
||||
messages: [
|
||||
{ role: "user", content: { text: "Hello, world!" } },
|
||||
{ role: "user", content: { text: "Hello, world!", type: "text" } },
|
||||
],
|
||||
}
|
||||
|
||||
|
|
@ -511,7 +511,7 @@ module ModelContextProtocol
|
|||
name: "Test resource",
|
||||
description: "Test resource",
|
||||
mimeType: "text/plain",
|
||||
contents: [{ text: "Lorem ipsum dolor sit amet" }],
|
||||
contents: [{ text: "Lorem ipsum dolor sit amet", type: "text" }],
|
||||
},
|
||||
response[:result],
|
||||
)
|
||||
|
|
@ -531,5 +531,28 @@ module ModelContextProtocol
|
|||
assert_equal "unknown_method", response[:error][:data]
|
||||
assert_instrumentation_data({ method: "unknown_method" })
|
||||
end
|
||||
|
||||
test "the global configuration is used if no configuration is passed to the server" do
|
||||
server = Server.new(name: "test_server")
|
||||
assert_equal ModelContextProtocol.configuration.instrumentation_callback,
|
||||
server.configuration.instrumentation_callback
|
||||
assert_equal ModelContextProtocol.configuration.exception_reporter,
|
||||
server.configuration.exception_reporter
|
||||
end
|
||||
|
||||
test "the server configuration takes precedence over the global configuration" do
|
||||
configuration = ModelContextProtocol::Configuration.new
|
||||
local_callback = ->(data) { puts "Local callback #{data.inspect}" }
|
||||
local_exception_reporter = ->(exception, context) {
|
||||
puts "Local exception reporter #{exception.inspect} #{context.inspect}"
|
||||
}
|
||||
configuration.instrumentation_callback = local_callback
|
||||
configuration.exception_reporter = local_exception_reporter
|
||||
|
||||
server = Server.new(name: "test_server", configuration:)
|
||||
|
||||
assert_equal local_callback, server.configuration.instrumentation_callback
|
||||
assert_equal local_exception_reporter, server.configuration.exception_reporter
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -8,6 +8,13 @@ module ModelContextProtocol
|
|||
tool_name "test_tool"
|
||||
description "a test tool for testing"
|
||||
input_schema [{ type: "text", name: "message" }]
|
||||
annotations(
|
||||
title: "Test Tool",
|
||||
read_only_hint: true,
|
||||
destructive_hint: false,
|
||||
idempotent_hint: true,
|
||||
open_world_hint: false,
|
||||
)
|
||||
|
||||
class << self
|
||||
def call(message)
|
||||
|
|
@ -21,6 +28,18 @@ module ModelContextProtocol
|
|||
assert_equal tool.to_h, { name: "mock_tool", description: "a mock tool for testing", inputSchema: nil }
|
||||
end
|
||||
|
||||
test "#to_h includes annotations when present" do
|
||||
tool = TestTool
|
||||
expected_annotations = {
|
||||
title: "Test Tool",
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: false,
|
||||
}
|
||||
assert_equal tool.to_h[:annotations], expected_annotations
|
||||
end
|
||||
|
||||
test "#call invokes the tool block and returns the response" do
|
||||
tool = TestTool
|
||||
response = tool.call("test")
|
||||
|
|
@ -49,6 +68,7 @@ module ModelContextProtocol
|
|||
|
||||
tool = DefaultNameTool
|
||||
|
||||
assert_equal tool.tool_name, "default_name_tool"
|
||||
assert_equal tool.name_value, "default_name_tool"
|
||||
assert_equal tool.description, "a mock tool for testing"
|
||||
assert_equal tool.input_schema, [{ type: "text", name: "message" }]
|
||||
|
|
@ -63,5 +83,118 @@ module ModelContextProtocol
|
|||
assert_equal tool.description, "a mock tool for testing"
|
||||
assert_equal tool.input_schema, nil
|
||||
end
|
||||
|
||||
test ".define allows definition of tools with annotations" do
|
||||
tool = Tool.define(
|
||||
name: "mock_tool",
|
||||
description: "a mock tool for testing",
|
||||
annotations: {
|
||||
title: "Mock Tool",
|
||||
read_only_hint: true,
|
||||
},
|
||||
) do |_|
|
||||
Tool::Response.new([{ type: "text", content: "OK" }])
|
||||
end
|
||||
|
||||
assert_equal tool.name_value, "mock_tool"
|
||||
assert_equal tool.description, "a mock tool for testing"
|
||||
assert_equal tool.input_schema, nil
|
||||
assert_equal tool.annotations_value.to_h, { title: "Mock Tool", readOnlyHint: true }
|
||||
end
|
||||
|
||||
# Tests for Tool::Annotations class
|
||||
test "Tool::Annotations initializes with all properties" do
|
||||
annotations = Tool::Annotations.new(
|
||||
title: "Test Tool",
|
||||
read_only_hint: true,
|
||||
destructive_hint: false,
|
||||
idempotent_hint: true,
|
||||
open_world_hint: false,
|
||||
)
|
||||
|
||||
assert_equal annotations.title, "Test Tool"
|
||||
assert_equal annotations.read_only_hint, true
|
||||
assert_equal annotations.destructive_hint, false
|
||||
assert_equal annotations.idempotent_hint, true
|
||||
assert_equal annotations.open_world_hint, false
|
||||
end
|
||||
|
||||
test "Tool::Annotations initializes with partial properties" do
|
||||
annotations = Tool::Annotations.new(
|
||||
title: "Test Tool",
|
||||
read_only_hint: true,
|
||||
)
|
||||
|
||||
assert_equal annotations.title, "Test Tool"
|
||||
assert_equal annotations.read_only_hint, true
|
||||
assert_nil annotations.destructive_hint
|
||||
assert_nil annotations.idempotent_hint
|
||||
assert_nil annotations.open_world_hint
|
||||
end
|
||||
|
||||
test "Tool::Annotations#to_h omits nil values" do
|
||||
annotations = Tool::Annotations.new(
|
||||
title: "Test Tool",
|
||||
read_only_hint: true,
|
||||
)
|
||||
|
||||
expected = {
|
||||
title: "Test Tool",
|
||||
readOnlyHint: true,
|
||||
}
|
||||
assert_equal annotations.to_h, expected
|
||||
end
|
||||
|
||||
test "Tool::Annotations#to_h handles all properties" do
|
||||
annotations = Tool::Annotations.new(
|
||||
title: "Test Tool",
|
||||
read_only_hint: true,
|
||||
destructive_hint: false,
|
||||
idempotent_hint: true,
|
||||
open_world_hint: false,
|
||||
)
|
||||
|
||||
expected = {
|
||||
title: "Test Tool",
|
||||
readOnlyHint: true,
|
||||
destructiveHint: false,
|
||||
idempotentHint: true,
|
||||
openWorldHint: false,
|
||||
}
|
||||
assert_equal annotations.to_h, expected
|
||||
end
|
||||
|
||||
test "Tool::Annotations#to_h returns empty hash when all values are nil" do
|
||||
annotations = Tool::Annotations.new
|
||||
assert_empty annotations.to_h
|
||||
end
|
||||
|
||||
test "Tool class method annotations can be set and retrieved" do
|
||||
class AnnotationsTestTool < Tool
|
||||
tool_name "annotations_test"
|
||||
annotations(
|
||||
title: "Annotations Test",
|
||||
read_only_hint: true,
|
||||
)
|
||||
end
|
||||
|
||||
tool = AnnotationsTestTool
|
||||
assert_instance_of Tool::Annotations, tool.annotations_value
|
||||
assert_equal tool.annotations_value.title, "Annotations Test"
|
||||
assert_equal tool.annotations_value.read_only_hint, true
|
||||
end
|
||||
|
||||
test "Tool class method annotations can be updated" do
|
||||
class UpdatableAnnotationsTool < Tool
|
||||
tool_name "updatable_annotations"
|
||||
end
|
||||
|
||||
tool = UpdatableAnnotationsTool
|
||||
tool.annotations(title: "Initial")
|
||||
assert_equal tool.annotations_value.title, "Initial"
|
||||
|
||||
tool.annotations(title: "Updated")
|
||||
assert_equal tool.annotations_value.title, "Updated"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue