ruby-sdk/lib/mcp/tool/response.rb
Piotr Usewicz 6689db2ca0
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)
```
2025-08-18 16:50:20 +02:00

29 lines
671 B
Ruby

# frozen_string_literal: true
module MCP
class Tool
class Response
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
@content = content
@error = error
end
def error?
!!@error
end
def to_h
{ content:, isError: error? }.compact
end
end
end
end