Skip to content

Commit c5f2f98

Browse files
authored
Merge pull request #150 from koic/set_default_content_to_an_empty_array_instead_of_nil
Set default `content` to an empty array instead of `nil`
2 parents 73cec83 + 0034952 commit c5f2f98

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/mcp/tool/response.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def initialize(content = nil, deprecated_error = NOT_GIVEN, error: false, struct
1313
error = deprecated_error
1414
end
1515

16-
@content = content
16+
@content = content || []
1717
@error = error
1818
@structured_content = structured_content
1919
end

test/mcp/tool/response_test.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,24 @@ class ResponseTest < ActiveSupport::TestCase
101101
refute actual[:isError]
102102
end
103103

104+
test "#to_h for a standard response with nil content and structured content" do
105+
structured_content = { code: 401, message: "Unauthorized" }
106+
response = Response.new(nil, structured_content: structured_content)
107+
actual = response.to_h
108+
109+
assert_equal [:content, :isError, :structuredContent], actual.keys
110+
assert_empty actual[:content]
111+
assert_equal structured_content, actual[:structuredContent]
112+
refute actual[:isError]
113+
end
114+
104115
test "#to_h for a standard response with structured content only" do
105116
structured_content = { code: 401, message: "Unauthorized" }
106117
response = Response.new(structured_content: structured_content)
107118
actual = response.to_h
108119

109-
assert_equal [:isError, :structuredContent], actual.keys
110-
assert_nil actual[:content]
120+
assert_equal [:content, :isError, :structuredContent], actual.keys
121+
assert_empty actual[:content]
111122
assert_equal structured_content, actual[:structuredContent]
112123
refute actual[:isError]
113124
end

0 commit comments

Comments
 (0)