Skip to content

Commit 002df2c

Browse files
authored
Merge pull request #107 from pusewicz/response-keyword-boolean
Response error keyword argument
2 parents f63f360 + 6689db2 commit 002df2c

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

lib/mcp/tool/response.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@
33
module MCP
44
class Tool
55
class Response
6-
attr_reader :content, :is_error
6+
NOT_GIVEN = Object.new.freeze
7+
8+
attr_reader :content
9+
10+
def initialize(content, deprecated_error = NOT_GIVEN, error: false)
11+
if deprecated_error != NOT_GIVEN
12+
warn("Passing `error` with the 2nd argument of `Response.new` is deprecated. Use keyword argument like `Response.new(content, error: error)` instead.", uplevel: 1)
13+
error = deprecated_error
14+
end
715

8-
def initialize(content, is_error = false)
916
@content = content
10-
@is_error = is_error
17+
@error = error
18+
end
19+
20+
def error?
21+
!!@error
1122
end
1223

1324
def to_h
14-
{ content:, isError: is_error }.compact
25+
{ content:, isError: error? }.compact
1526
end
1627
end
1728
end

test/mcp/tool/response_test.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
module MCP
6+
class Tool
7+
class ResponseTest < ActiveSupport::TestCase
8+
test "#initialize with content" do
9+
content = [{
10+
type: "text",
11+
text: "Unauthorized",
12+
}]
13+
response = Response.new(content)
14+
15+
assert_equal content, response.content
16+
refute response.error?
17+
end
18+
19+
test "#initialize with content and error set to true" do
20+
content = [{
21+
type: "text",
22+
text: "Unauthorized",
23+
}]
24+
response = Response.new(content, error: true)
25+
26+
assert_equal content, response.content
27+
assert response.error?
28+
end
29+
30+
test "#initialize with content and error explicitly set to false" do
31+
content = [{
32+
type: "text",
33+
text: "Unauthorized",
34+
}]
35+
response = Response.new(content, error: false)
36+
37+
assert_equal content, response.content
38+
refute response.error?
39+
end
40+
41+
test "#error? for a standard response" do
42+
response = Response.new(nil, error: false)
43+
refute response.error?
44+
end
45+
46+
test "#error? for an error response" do
47+
response = Response.new(nil, error: true)
48+
assert response.error?
49+
end
50+
51+
test "#to_h for a standard response" do
52+
content = [{
53+
type: "text",
54+
text: "Unauthorized",
55+
}]
56+
response = Response.new(content)
57+
actual = response.to_h
58+
59+
assert_equal [:content, :isError].sort, actual.keys.sort
60+
assert_equal content, actual[:content]
61+
refute actual[:isError]
62+
end
63+
64+
test "#to_h for an error response" do
65+
content = [{
66+
type: "text",
67+
text: "Unauthorized",
68+
}]
69+
response = Response.new(content, error: true)
70+
actual = response.to_h
71+
assert_equal [:content, :isError].sort, actual.keys.sort
72+
assert_equal content, actual[:content]
73+
assert actual[:isError]
74+
end
75+
end
76+
end
77+
end

test/mcp/tool_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def call(message:, server_context: nil)
4545
tool = TestTool
4646
response = tool.call(message: "test")
4747
assert_equal response.content, [{ type: "text", content: "OK" }]
48-
assert_equal response.is_error, false
48+
refute response.error?
4949
end
5050

5151
test "allows declarative definition of tools as classes" do
@@ -250,7 +250,7 @@ def call(message:, server_context: nil)
250250
tool = TypedTestTool
251251
response = tool.call(message: "test")
252252
assert_equal response.content, [{ type: "text", content: "OK" }]
253-
assert_equal response.is_error, false
253+
refute response.error?
254254
end
255255

256256
class TestToolWithoutServerContext < Tool

0 commit comments

Comments
 (0)