Skip to content

Commit 7bef102

Browse files
authored
Merge pull request #212 from koic/omit_icons_from_response_when_empty_or_nil
Omit `icons` from responses when empty or nil
2 parents 27e5dc9 + bd416c7 commit 7bef102

File tree

10 files changed

+163
-5
lines changed

10 files changed

+163
-5
lines changed

lib/mcp/prompt.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def to_h
2020
name: name_value,
2121
title: title_value,
2222
description: description_value,
23-
icons: icons&.map(&:to_h),
23+
icons: icons_value&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
2424
arguments: arguments_value&.map(&:to_h),
2525
_meta: meta_value,
2626
}.compact

lib/mcp/resource.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def to_h
1919
name: name,
2020
title: title,
2121
description: description,
22-
icons: icons.map(&:to_h),
22+
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
2323
mimeType: mime_type,
2424
}.compact
2525
end

lib/mcp/resource_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def to_h
1919
name: name,
2020
title: title,
2121
description: description,
22-
icons: icons.map(&:to_h),
22+
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
2323
mimeType: mime_type,
2424
}.compact
2525
end

lib/mcp/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def default_capabilities
290290
def server_info
291291
@server_info ||= {
292292
description: description,
293-
icons: icons,
293+
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
294294
name: name,
295295
title: title,
296296
version: version,

lib/mcp/tool.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def to_h
2121
name: name_value,
2222
title: title_value,
2323
description: description_value,
24-
icons: icons&.map(&:to_h),
24+
icons: icons_value&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
2525
inputSchema: input_schema_value.to_h,
2626
outputSchema: @output_schema_value&.to_h,
2727
annotations: annotations_value&.to_h,

test/mcp/prompt_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,24 @@ class NoArgumentsPrompt < Prompt
195195

196196
assert_equal expected, prompt.to_h
197197
end
198+
199+
test "#to_h does not have `:icons` key when icons is empty" do
200+
prompt = Prompt.define(
201+
name: "prompt_without_icons",
202+
description: "a prompt without icons",
203+
)
204+
205+
refute prompt.to_h.key?(:icons)
206+
end
207+
208+
test "#to_h does not have `:icons` key when icons is nil" do
209+
prompt = Prompt.define(
210+
name: "prompt_without_icons",
211+
description: "a prompt without icons",
212+
icons: nil,
213+
)
214+
215+
refute prompt.to_h.key?(:icons)
216+
end
198217
end
199218
end

test/mcp/resource_template_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
module MCP
6+
class ResourceTemplateTest < ActiveSupport::TestCase
7+
test "#to_h does not have `:icons` key when icons is empty" do
8+
resource_template = ResourceTemplate.new(
9+
uri_template: "file:///{path}",
10+
name: "resource_template_without_icons",
11+
description: "a resource template without icons",
12+
)
13+
14+
refute resource_template.to_h.key?(:icons)
15+
end
16+
17+
test "#to_h does not have `:icons` key when icons is nil" do
18+
resource_template = ResourceTemplate.new(
19+
uri_template: "file:///{path}",
20+
name: "resource_template_without_icons",
21+
description: "a resource template without icons",
22+
icons: nil,
23+
)
24+
25+
refute resource_template.to_h.key?(:icons)
26+
end
27+
28+
test "#to_h includes icons when present" do
29+
resource_template = ResourceTemplate.new(
30+
uri_template: "file:///{path}",
31+
name: "resource_template_with_icons",
32+
description: "a resource template with icons",
33+
icons: [Icon.new(mime_type: "image/png", sizes: ["48x48"], src: "https://example.com", theme: "light")],
34+
)
35+
expected_icons = [{ mimeType: "image/png", sizes: ["48x48"], src: "https://example.com", theme: "light" }]
36+
37+
assert_equal expected_icons, resource_template.to_h[:icons]
38+
end
39+
end
40+
end

test/mcp/resource_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
module MCP
6+
class ResourceTest < ActiveSupport::TestCase
7+
test "#to_h does not have `:icons` key when icons is empty" do
8+
resource = Resource.new(
9+
uri: "file:///test.txt",
10+
name: "resource_without_icons",
11+
description: "a resource without icons",
12+
)
13+
14+
refute resource.to_h.key?(:icons)
15+
end
16+
17+
test "#to_h does not have `:icons` key when icons is nil" do
18+
resource = Resource.new(
19+
uri: "file:///test.txt",
20+
name: "resource_without_icons",
21+
description: "a resource without icons",
22+
icons: nil,
23+
)
24+
25+
refute resource.to_h.key?(:icons)
26+
end
27+
28+
test "#to_h includes icons when present" do
29+
resource = Resource.new(
30+
uri: "file:///test.txt",
31+
name: "resource_with_icons",
32+
description: "a resource with icons",
33+
icons: [Icon.new(mime_type: "image/png", sizes: ["48x48"], src: "https://example.com", theme: "light")],
34+
)
35+
expected_icons = [{ mimeType: "image/png", sizes: ["48x48"], src: "https://example.com", theme: "light" }]
36+
37+
assert_equal expected_icons, resource.to_h[:icons]
38+
end
39+
end
40+
end

test/mcp/server_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,46 @@ class Example < Tool
895895
refute response[:result][:serverInfo].key?(:website_url)
896896
end
897897

898+
test "server response does not include icons when icons is empty" do
899+
server = Server.new(name: "test_server")
900+
request = {
901+
jsonrpc: "2.0",
902+
method: "initialize",
903+
id: 1,
904+
}
905+
response = server.handle(request)
906+
907+
refute response[:result][:serverInfo].key?(:icons)
908+
end
909+
910+
test "server response does not include icons when icons is nil" do
911+
server = Server.new(name: "test_server", icons: nil)
912+
request = {
913+
jsonrpc: "2.0",
914+
method: "initialize",
915+
id: 1,
916+
}
917+
response = server.handle(request)
918+
919+
refute response[:result][:serverInfo].key?(:icons)
920+
end
921+
922+
test "server response includes icons when icons is present" do
923+
server = Server.new(
924+
name: "test_server",
925+
icons: [Icon.new(mime_type: "image/png", sizes: ["48x48"], src: "https://example.com", theme: "light")],
926+
)
927+
request = {
928+
jsonrpc: "2.0",
929+
method: "initialize",
930+
id: 1,
931+
}
932+
response = server.handle(request)
933+
expected_icons = [{ mimeType: "image/png", sizes: ["48x48"], src: "https://example.com", theme: "light" }]
934+
935+
assert_equal expected_icons, response[:result][:serverInfo][:icons]
936+
end
937+
898938
test "server uses default version when not configured" do
899939
server = Server.new(name: "test_server")
900940
request = {

test/mcp/tool_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ def call(message:, server_context: nil)
5353
refute tool.to_h.key?(:title)
5454
end
5555

56+
test "#to_h does not have `:icons` key when icons is empty" do
57+
tool = Tool.define(
58+
name: "tool_without_icons",
59+
description: "a tool without icons",
60+
)
61+
62+
refute tool.to_h.key?(:icons)
63+
end
64+
65+
test "#to_h does not have `:icons` key when icons is nil" do
66+
tool = Tool.define(
67+
name: "tool_without_icons",
68+
description: "a tool without icons",
69+
icons: nil,
70+
)
71+
72+
refute tool.to_h.key?(:icons)
73+
end
74+
5675
test "#to_h includes annotations when present" do
5776
tool = TestTool
5877
expected_annotations = {

0 commit comments

Comments
 (0)