Skip to content

Commit 1c49840

Browse files
committed
Nicer error API
Both the combination of positional argument and naming does not feel like Ruby. This change makes `is_error` argument a keyword argument called `error`. Thanks to this, the usage is more self-explanatory: ```ruby response = Response.new([], error: true) ```
1 parent 3325862 commit 1c49840

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

lib/mcp/tool/response.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
module MCP
44
class Tool
55
class Response
6-
attr_reader :content, :is_error
6+
attr_reader :content, :error
77

8-
def initialize(content, is_error = false)
8+
def initialize(content, error: false)
99
@content = content
10-
@is_error = is_error
10+
@error = error
1111
end
1212

1313
def to_h
14-
{ content:, isError: is_error }.compact
14+
{ content:, isError: error? }.compact
1515
end
16+
17+
alias_method :error?, :error
1618
end
1719
end
1820
end

test/mcp/tool/response_test.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 and error flag" do
9+
content = [{
10+
type: "text",
11+
text: "Unauthorized",
12+
}]
13+
response = Response.new(content, error: true)
14+
15+
assert_equal content, response.content
16+
assert response.error?
17+
18+
response = Response.new(content, error: false)
19+
assert_equal content, response.content
20+
refute response.error?
21+
22+
response = Response.new(content)
23+
assert_equal content, response.content
24+
refute response.error?
25+
end
26+
27+
test "#to_h" do
28+
content = [{
29+
type: "text",
30+
text: "Unauthorized",
31+
}]
32+
response = Response.new(content)
33+
actual = response.to_h
34+
35+
assert_equal [:content, :isError].sort, actual.keys.sort
36+
assert_equal content, actual[:content]
37+
refute actual[:isError]
38+
39+
response = Response.new(content, error: true)
40+
actual = response.to_h
41+
assert_equal [:content, :isError].sort, actual.keys.sort
42+
assert_equal content, actual[:content]
43+
assert actual[:isError]
44+
end
45+
46+
test "#error?" do
47+
response = Response.new(nil, error: true)
48+
assert response.error?
49+
50+
response = Response.new(nil, error: false)
51+
refute response.error?
52+
end
53+
end
54+
end
55+
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)