Skip to content

Commit 04da1d8

Browse files
committed
Support structuredContent in tool response
1 parent 8f682c4 commit 04da1d8

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/mcp/tool/response.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,29 @@ class Tool
55
class Response
66
NOT_GIVEN = Object.new.freeze
77

8-
attr_reader :content
8+
attr_reader :content, :structured_content
99

10-
def initialize(content, deprecated_error = NOT_GIVEN, error: false)
10+
def initialize(content = nil, deprecated_error = NOT_GIVEN, error: false, structured_content: nil)
1111
if deprecated_error != NOT_GIVEN
1212
warn("Passing `error` with the 2nd argument of `Response.new` is deprecated. Use keyword argument like `Response.new(content, error: error)` instead.", uplevel: 1)
1313
error = deprecated_error
1414
end
1515

1616
@content = content
1717
@error = error
18+
19+
if structured_content
20+
@structured_content = structured_content
21+
@content ||= [{ type: "text", text: structured_content.to_json }]
22+
end
1823
end
1924

2025
def error?
2126
!!@error
2227
end
2328

2429
def to_h
25-
{ content:, isError: error? }.compact
30+
{ content:, isError: error?, structuredContent: @structured_content }.compact
2631
end
2732
end
2833
end

test/mcp/tool/response_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ class ResponseTest < ActiveSupport::TestCase
3838
refute response.error?
3939
end
4040

41+
test "#initialize with content and structuredContent" do
42+
content = [{
43+
type: "text",
44+
text: "{\"code\":401,\"message\":\"Unauthorized\"}",
45+
}]
46+
structured_content = { code: 401, message: "Unauthorized" }
47+
response = Response.new(content, structured_content: structured_content)
48+
49+
assert_equal content, response.content
50+
assert_equal structured_content, response.structured_content
51+
refute response.error?
52+
end
53+
4154
test "#error? for a standard response" do
4255
response = Response.new(nil, error: false)
4356
refute response.error?
@@ -72,6 +85,32 @@ class ResponseTest < ActiveSupport::TestCase
7285
assert_equal content, actual[:content]
7386
assert actual[:isError]
7487
end
88+
89+
test "#to_h for a standard response with content and structured content" do
90+
content = [{
91+
type: "text",
92+
text: "{\"code\":401,\"message\":\"Unauthorized\"}",
93+
}]
94+
structured_content = { code: 401, message: "Unauthorized" }
95+
response = Response.new(content, structured_content: structured_content)
96+
actual = response.to_h
97+
98+
assert_equal [:content, :isError, :structuredContent].sort, actual.keys.sort
99+
assert_equal content, actual[:content]
100+
assert_equal structured_content, actual[:structuredContent]
101+
refute actual[:isError]
102+
end
103+
104+
test "#to_h for a standard response with structured content only" do
105+
structured_content = { code: 401, message: "Unauthorized" }
106+
response = Response.new(structured_content: structured_content)
107+
actual = response.to_h
108+
109+
assert_equal [:content, :isError, :structuredContent].sort, actual.keys.sort
110+
assert_equal [{ type: "text", text: structured_content.to_json }], actual[:content]
111+
assert_equal structured_content, actual[:structuredContent]
112+
refute actual[:isError]
113+
end
75114
end
76115
end
77116
end

0 commit comments

Comments
 (0)