File tree Expand file tree Collapse file tree 3 files changed +63
-6
lines changed
Expand file tree Collapse file tree 3 files changed +63
-6
lines changed Original file line number Diff line number Diff line change 33module 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
1820end
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments