Skip to content

Response keyword boolean #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/mcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require_relative "mcp/tool"
require_relative "mcp/tool/input_schema"
require_relative "mcp/tool/response"
require_relative "mcp/tool/error_response"
require_relative "mcp/tool/annotations"
require_relative "mcp/transport"
require_relative "mcp/version"
Expand Down
9 changes: 9 additions & 0 deletions lib/mcp/tool/error_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module MCP
class Tool
class ErrorResponse
def initialize(content) = super(content, error: true)
end
end
end
10 changes: 6 additions & 4 deletions lib/mcp/tool/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
module MCP
class Tool
class Response
attr_reader :content, :is_error
attr_reader :content, :error

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

def to_h
{ content:, isError: is_error }.compact
{ content:, isError: error? }.compact
end

alias_method :error?, :error
end
end
end
38 changes: 38 additions & 0 deletions test/mcp/tool/error_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "test_helper"

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

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

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

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

test "#error?" do
response = ErrorResponse.new(nil)
assert response.error?
end
end
end
end
55 changes: 55 additions & 0 deletions test/mcp/tool/response_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require "test_helper"

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

assert_equal content, response.content
assert response.error?

response = Response.new(content, error: false)
assert_equal content, response.content
refute response.error?

response = Response.new(content)
assert_equal content, response.content
refute response.error?
end

test "#to_h" 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]

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]
end

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

response = Response.new(nil, error: false)
refute response.error?
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