Skip to content

Commit e26451d

Browse files
committed
Introduce ErrorResponse
Rather than passing a boolean flag to the response, introduce an `ErrorResponse` class.
1 parent 1c49840 commit e26451d

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

lib/mcp.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
require_relative "mcp/tool"
2020
require_relative "mcp/tool/input_schema"
2121
require_relative "mcp/tool/response"
22+
require_relative "mcp/tool/error_response"
2223
require_relative "mcp/tool/annotations"
2324
require_relative "mcp/transport"
2425
require_relative "mcp/version"

lib/mcp/tool/error_response.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module MCP
4+
class Tool
5+
class ErrorResponse
6+
def initialize(content) = super(content, error: true)
7+
end
8+
end
9+
end

test/mcp/tool/error_response.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
module MCP
6+
class Tool
7+
class ErrorResponseTest < ActiveSupport::TestCase
8+
test "#initialize with content" do
9+
content = [{
10+
type: "text",
11+
text: "Unauthorized",
12+
}]
13+
response = ErrorResponse.new(content)
14+
15+
assert_equal content, response.content
16+
assert response.error?
17+
end
18+
19+
test "#to_h" do
20+
content = [{
21+
type: "text",
22+
text: "Unauthorized",
23+
}]
24+
response = ErrorResponse.new(content)
25+
actual = response.to_h
26+
27+
assert_equal [:content, :isError].sort, actual.keys.sort
28+
assert_equal content, actual[:content]
29+
assert actual[:isError]
30+
end
31+
32+
test "#error?" do
33+
response = ErrorResponse.new(nil)
34+
assert response.error?
35+
end
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)