Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/mcp/tool/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@
module MCP
class Tool
class Response
attr_reader :content, :is_error
NOT_GIVEN = Object.new.freeze

attr_reader :content

def initialize(content, deprecated_error = NOT_GIVEN, error: false)
if deprecated_error != NOT_GIVEN
warn("Passing `error` with the 2nd argument of `Response.new` is deprecated. Use keyword argument like `Response.new(content, error: error)` instead.", uplevel: 1)
error = deprecated_error
end

def initialize(content, is_error = false)
@content = content
@is_error = is_error
@error = error
end

def error?
!!@error
end

def to_h
{ content:, isError: is_error }.compact
{ content:, isError: error? }.compact
end
end
end
Expand Down
77 changes: 77 additions & 0 deletions test/mcp/tool/response_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require "test_helper"

module MCP
class Tool
class ResponseTest < ActiveSupport::TestCase
test "#initialize with content" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content)

assert_equal content, response.content
refute response.error?
end

test "#initialize with content and error set to true" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content, error: true)

assert_equal content, response.content
assert response.error?
end

test "#initialize with content and error explicitly set to false" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content, error: false)

assert_equal content, response.content
refute response.error?
end

test "#error? for a standard response" do
response = Response.new(nil, error: false)
refute response.error?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you split it into three tests, each independently executable per assertion group?

end

test "#error? for an error response" do
response = Response.new(nil, error: true)
assert response.error?
end

test "#to_h for a standard response" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content)
actual = response.to_h

assert_equal [:content, :isError].sort, actual.keys.sort
assert_equal content, actual[:content]
refute actual[:isError]
end

test "#to_h for an error response" do
content = [{
type: "text",
text: "Unauthorized",
}]
response = Response.new(content, error: true)
actual = response.to_h
assert_equal [:content, :isError].sort, actual.keys.sort
assert_equal content, actual[:content]
assert actual[:isError]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you split it into two tests, each independently executable per assertion group?

end
end
end
end
4 changes: 2 additions & 2 deletions test/mcp/tool_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def call(message:, server_context: nil)
tool = TestTool
response = tool.call(message: "test")
assert_equal response.content, [{ type: "text", content: "OK" }]
assert_equal response.is_error, false
refute response.error?
end

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

class TestToolWithoutServerContext < Tool
Expand Down