add titles to tools, prompts and resources

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

View file

@ -373,6 +373,7 @@ This gem provides a `MCP::Tool` class that can be used to create tools in two wa
```ruby ```ruby
class MyTool < MCP::Tool class MyTool < MCP::Tool
title "My Tool"
description "This tool performs specific functionality..." description "This tool performs specific functionality..."
input_schema( input_schema(
properties: { properties: {
@ -381,7 +382,6 @@ class MyTool < MCP::Tool
required: ["message"] required: ["message"]
) )
annotations( annotations(
title: "My Tool",
read_only_hint: true, read_only_hint: true,
destructive_hint: false, destructive_hint: false,
idempotent_hint: true, idempotent_hint: true,
@ -401,9 +401,9 @@ tool = MyTool
```ruby ```ruby
tool = MCP::Tool.define( tool = MCP::Tool.define(
name: "my_tool", name: "my_tool",
title: "My Tool",
description: "This tool performs specific functionality...", description: "This tool performs specific functionality...",
annotations: { annotations: {
title: "My Tool",
read_only_hint: true read_only_hint: true
} }
) do |args, server_context| ) do |args, server_context|
@ -437,6 +437,7 @@ The `MCP::Prompt` class provides two ways to create prompts:
```ruby ```ruby
class MyPrompt < MCP::Prompt class MyPrompt < MCP::Prompt
prompt_name "my_prompt" # Optional - defaults to underscored class name prompt_name "my_prompt" # Optional - defaults to underscored class name
title "My Prompt"
description "This prompt performs specific functionality..." description "This prompt performs specific functionality..."
arguments [ arguments [
MCP::Prompt::Argument.new( MCP::Prompt::Argument.new(
@ -473,6 +474,7 @@ prompt = MyPrompt
```ruby ```ruby
prompt = MCP::Prompt.define( prompt = MCP::Prompt.define(
name: "my_prompt", name: "my_prompt",
title: "My Prompt",
description: "This prompt performs specific functionality...", description: "This prompt performs specific functionality...",
arguments: [ arguments: [
MCP::Prompt::Argument.new( MCP::Prompt::Argument.new(
@ -558,7 +560,8 @@ The `MCP::Resource` class provides a way to register resources with the server.
```ruby ```ruby
resource = MCP::Resource.new( resource = MCP::Resource.new(
uri: "https://example.com/my_resource", uri: "https://example.com/my_resource",
name: "My Resource", name: "my-resource",
title: "My Resource",
description: "Lorem ipsum dolor sit amet", description: "Lorem ipsum dolor sit amet",
mime_type: "text/html", mime_type: "text/html",
) )

View file

@ -62,7 +62,8 @@ server = MCP::Server.new(
resources: [ resources: [
MCP::Resource.new( MCP::Resource.new(
uri: "https://test_resource.invalid", uri: "https://test_resource.invalid",
name: "Test resource", name: "test-resource",
title: "Test Resource",
description: "Test resource that echoes back the uri as its content", description: "Test resource that echoes back the uri as its content",
mime_type: "text/plain", mime_type: "text/plain",
), ),

View file

@ -59,7 +59,8 @@ server = MCP::Server.new(
resources: [ resources: [
MCP::Resource.new( MCP::Resource.new(
uri: "https://test_resource.invalid", uri: "https://test_resource.invalid",
name: "Test resource", name: "test-resource",
title: "Test Resource",
description: "Test resource that echoes back the uri as its content", description: "Test resource that echoes back the uri as its content",
mime_type: "text/plain", mime_type: "text/plain",
), ),

View file

@ -6,6 +6,7 @@ module MCP
class << self class << self
NOT_SET = Object.new NOT_SET = Object.new
attr_reader :title_value
attr_reader :description_value attr_reader :description_value
attr_reader :arguments_value attr_reader :arguments_value
@ -14,12 +15,13 @@ module MCP
end end
def to_h def to_h
{ name: name_value, description: description_value, arguments: arguments_value.map(&:to_h) }.compact { name: name_value, title: title_value, description: description_value, arguments: arguments_value.map(&:to_h) }.compact
end end
def inherited(subclass) def inherited(subclass)
super super
subclass.instance_variable_set(:@name_value, nil) subclass.instance_variable_set(:@name_value, nil)
subclass.instance_variable_set(:@title_value, nil)
subclass.instance_variable_set(:@description_value, nil) subclass.instance_variable_set(:@description_value, nil)
subclass.instance_variable_set(:@arguments_value, nil) subclass.instance_variable_set(:@arguments_value, nil)
end end
@ -36,6 +38,14 @@ module MCP
@name_value || StringUtils.handle_from_class_name(name) @name_value || StringUtils.handle_from_class_name(name)
end end
def title(value = NOT_SET)
if value == NOT_SET
@title_value
else
@title_value = value
end
end
def description(value = NOT_SET) def description(value = NOT_SET)
if value == NOT_SET if value == NOT_SET
@description_value @description_value
@ -52,9 +62,10 @@ module MCP
end end
end end
def define(name: nil, description: nil, arguments: [], &block) def define(name: nil, title: nil, description: nil, arguments: [], &block)
Class.new(self) do Class.new(self) do
prompt_name name prompt_name name
title title
description description description description
arguments arguments arguments arguments
define_singleton_method(:template) do |args, server_context: nil| define_singleton_method(:template) do |args, server_context: nil|

View file

@ -3,21 +3,23 @@
module MCP module MCP
class Resource class Resource
attr_reader :uri, :name, :description, :mime_type attr_reader :uri, :name, :title, :description, :mime_type
def initialize(uri:, name:, description: nil, mime_type: nil) def initialize(uri:, name:, title: nil, description: nil, mime_type: nil)
@uri = uri @uri = uri
@name = name @name = name
@title = title
@description = description @description = description
@mime_type = mime_type @mime_type = mime_type
end end
def to_h def to_h
{ {
uri: @uri, uri: uri,
name: @name, name: name,
description: @description, title: title,
mimeType: @mime_type, description: description,
mimeType: mime_type,
}.compact }.compact
end end
end end

View file

@ -3,21 +3,23 @@
module MCP module MCP
class ResourceTemplate class ResourceTemplate
attr_reader :uri_template, :name, :description, :mime_type attr_reader :uri_template, :name, :title, :description, :mime_type
def initialize(uri_template:, name:, description: nil, mime_type: nil) def initialize(uri_template:, name:, title: nil, description: nil, mime_type: nil)
@uri_template = uri_template @uri_template = uri_template
@name = name @name = name
@title = title
@description = description @description = description
@mime_type = mime_type @mime_type = mime_type
end end
def to_h def to_h
{ {
uriTemplate: @uri_template, uriTemplate: uri_template,
name: @name, name: name,
description: @description, title: title,
mimeType: @mime_type, description: description,
mimeType: mime_type,
}.compact }.compact
end end
end end

View file

@ -96,13 +96,13 @@ module MCP
end end
end end
def define_tool(name: nil, description: nil, input_schema: nil, annotations: nil, &block) def define_tool(name: nil, title: nil, description: nil, input_schema: nil, annotations: nil, &block)
tool = Tool.define(name:, description:, input_schema:, annotations:, &block) tool = Tool.define(name:, title:, description:, input_schema:, annotations:, &block)
@tools[tool.name_value] = tool @tools[tool.name_value] = tool
end end
def define_prompt(name: nil, description: nil, arguments: [], &block) def define_prompt(name: nil, title: nil, description: nil, arguments: [], &block)
prompt = Prompt.define(name:, description:, arguments:, &block) prompt = Prompt.define(name:, title:, description:, arguments:, &block)
@prompts[prompt.name_value] = prompt @prompts[prompt.name_value] = prompt
end end

View file

@ -5,6 +5,7 @@ module MCP
class << self class << self
NOT_SET = Object.new NOT_SET = Object.new
attr_reader :title_value
attr_reader :description_value attr_reader :description_value
attr_reader :annotations_value attr_reader :annotations_value
@ -15,6 +16,7 @@ module MCP
def to_h def to_h
result = { result = {
name: name_value, name: name_value,
title: title_value,
description: description_value, description: description_value,
inputSchema: input_schema_value.to_h, inputSchema: input_schema_value.to_h,
} }
@ -25,6 +27,7 @@ module MCP
def inherited(subclass) def inherited(subclass)
super super
subclass.instance_variable_set(:@name_value, nil) subclass.instance_variable_set(:@name_value, nil)
subclass.instance_variable_set(:@title_value, nil)
subclass.instance_variable_set(:@description_value, nil) subclass.instance_variable_set(:@description_value, nil)
subclass.instance_variable_set(:@input_schema_value, nil) subclass.instance_variable_set(:@input_schema_value, nil)
subclass.instance_variable_set(:@annotations_value, nil) subclass.instance_variable_set(:@annotations_value, nil)
@ -46,6 +49,14 @@ module MCP
@input_schema_value || InputSchema.new @input_schema_value || InputSchema.new
end end
def title(value = NOT_SET)
if value == NOT_SET
@title_value
else
@title_value = value
end
end
def description(value = NOT_SET) def description(value = NOT_SET)
if value == NOT_SET if value == NOT_SET
@description_value @description_value
@ -74,9 +85,10 @@ module MCP
end end
end end
def define(name: nil, description: nil, input_schema: nil, annotations: nil, &block) def define(name: nil, title: nil, description: nil, input_schema: nil, annotations: nil, &block)
Class.new(self) do Class.new(self) do
tool_name name tool_name name
title title
description description description description
input_schema input_schema input_schema input_schema
self.annotations(annotations) if annotations self.annotations(annotations) if annotations

View file

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

View file

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

View file

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

View file

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