add titles to tools, prompts and resources

This commit is contained in:
jdnvn 2025-08-17 22:58:03 -04:00
commit 8b4085468e
4 changed files with 94 additions and 40 deletions

View file

@ -111,6 +111,7 @@ module MCP
test ".define allows definition of simple prompts with a block" do
prompt = Prompt.define(
name: "mock_prompt",
title: "Mock Prompt",
description: "a mock prompt for testing",
arguments: [
Prompt::Argument.new(name: "test_argument", description: "Test argument", required: true),

View file

@ -226,7 +226,8 @@ module MCP
@server.resources = [
MCP::Resource.new(
uri: "https://test_resource.invalid",
name: "Test Resource",
name: "test-resource",
title: "Test Resource",
description: "A test resource",
mime_type: "text/plain",
),

View file

@ -7,19 +7,24 @@ module MCP
class ServerTest < ActiveSupport::TestCase
include InstrumentationTestHelper
setup do
@tool = Tool.define(name: "test_tool", description: "Test tool")
@tool = Tool.define(
name: "test_tool",
title: "Test tool",
description: "A test tool",
)
@tool_that_raises = Tool.define(
name: "tool_that_raises",
description: "Tool that raises",
title: "Tool that raises",
description: "A tool that raises",
input_schema: { type: "object", properties: { message: { type: "string" } }, required: ["message"] },
) { raise StandardError, "Tool error" }
@tool_with_no_args = Tool.define(
name: "tool_with_no_args",
title: "Tool with no args",
description: "This tool performs specific functionality...",
annotations: {
title: "Tool with no args",
read_only_hint: true,
},
) do
@ -28,6 +33,7 @@ module MCP
@prompt = Prompt.define(
name: "test_prompt",
title: "Test Prompt",
description: "Test prompt",
arguments: [
Prompt::Argument.new(name: "test_argument", description: "Test argument", required: true),
@ -43,14 +49,16 @@ module MCP
@resource = Resource.new(
uri: "https://test_resource.invalid",
name: "Test resource",
name: "test-resource",
title: "Test Resource",
description: "Test resource",
mime_type: "text/plain",
)
@resource_template = ResourceTemplate.new(
uri_template: "https://test_resource.invalid/{id}",
name: "Test resource",
name: "test-resource",
title: "Test Resource",
description: "Test resource",
mime_type: "text/plain",
)
@ -163,7 +171,8 @@ module MCP
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 "Test tool", result[:tools][0][:title]
assert_equal "A test tool", result[:tools][0][:description]
assert_equal({ type: "object" }, result[:tools][0][:inputSchema])
assert_instrumentation_data({ method: "tools/list" })
end
@ -179,7 +188,8 @@ module MCP
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 "Test tool", result[:tools][0][:title]
assert_equal "A test tool", result[:tools][0][:description]
end
test "#tools_list_handler sets the tools/list handler" do
@ -224,7 +234,8 @@ module MCP
test "#handle tools/call returns error if required tool arguments are missing" do
tool_with_required_argument = Tool.define(
name: "test_tool",
description: "Test tool",
title: "Test tool",
description: "A test tool",
input_schema: { properties: { message: { type: "string" } }, required: ["message"] },
) do |message: nil|
Tool::Response.new("success #{message}")
@ -532,7 +543,7 @@ module MCP
test "#resources_list_handler sets the resources/list handler" do
@server.resources_list_handler do
[{ uri: "https://test_resource.invalid", name: "Test resource", description: "Test resource" }]
[{ uri: "https://test_resource.invalid", name: "test-resource", title: "Test Resource", description: "Test resource" }]
end
request = {
@ -543,7 +554,7 @@ module MCP
response = @server.handle(request)
assert_equal(
{ resources: [{ uri: "https://test_resource.invalid", name: "Test resource", description: "Test resource" }] },
{ resources: [{ uri: "https://test_resource.invalid", name: "test-resource", title: "Test Resource", description: "Test resource" }] },
response[:result],
)
assert_instrumentation_data({ method: "resources/list" })

View file

@ -25,8 +25,12 @@ module MCP
end
test "#to_h returns a hash with name, description, and inputSchema" do
tool = Tool.define(name: "mock_tool", description: "a mock tool for testing")
assert_equal tool.to_h, { name: "mock_tool", description: "a mock tool for testing", inputSchema: { type: "object" } }
tool = Tool.define(
name: "mock_tool",
title: "Mock Tool",
description: "a mock tool for testing",
)
assert_equal tool.to_h, { name: "mock_tool", title: "Mock Tool", description: "a mock tool for testing", inputSchema: { type: "object" } }
end
test "#to_h includes annotations when present" do
@ -109,7 +113,11 @@ module MCP
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 = Tool.define(
name: "mock_tool",
title: "Mock Tool",
description: "a mock tool for testing",
) do |_|
Tool::Response.new([{ type: "text", content: "OK" }])
end
@ -121,9 +129,9 @@ module MCP
test ".define allows definition of tools with annotations" do
tool = Tool.define(
name: "mock_tool",
title: "Mock Tool",
description: "a mock tool for testing",
annotations: {
title: "Mock Tool",
read_only_hint: true,
},
) do |_|
@ -131,9 +139,10 @@ module MCP
end
assert_equal tool.name_value, "mock_tool"
assert_equal tool.title, "Mock Tool"
assert_equal tool.description, "a mock tool for testing"
assert_equal tool.input_schema, Tool::InputSchema.new
assert_equal tool.annotations_value.to_h, { title: "Mock Tool", readOnlyHint: true }
assert_equal tool.annotations_value.to_h, { readOnlyHint: true }
end
# Tests for Tool::Annotations class