Class: ActionMCP::BaseResponse
- Inherits:
-
Object
- Object
- ActionMCP::BaseResponse
- Includes:
- Enumerable
- Defined in:
- lib/action_mcp/base_response.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#is_error ⇒ Object
readonly
Returns the value of attribute is_error.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compare with hash for easier testing.
-
#build_success_hash ⇒ Object
Method to be implemented by subclasses.
-
#compare_with_same_class(other) ⇒ Object
Method to be implemented by subclasses for comparison.
-
#eql?(other) ⇒ Boolean
Implement eql? for hash key comparison.
- #error? ⇒ Boolean
-
#hash ⇒ Object
Implement hash method for hash key usage.
-
#hash_components ⇒ Object
Method to be implemented by subclasses for hash generation.
-
#initialize ⇒ BaseResponse
constructor
A new instance of BaseResponse.
-
#mark_as_error!(symbol = :invalid_request, message: nil, data: nil) ⇒ Object
Mark response as error.
- #success? ⇒ Boolean
-
#to_h(_options = nil) ⇒ Object
(also: #as_json)
Convert to hash format expected by MCP protocol.
-
#to_json(options = nil) ⇒ Object
Handle to_json directly.
Constructor Details
#initialize ⇒ BaseResponse
Returns a new instance of BaseResponse.
8 9 10 |
# File 'lib/action_mcp/base_response.rb', line 8 def initialize @is_error = false end |
Instance Attribute Details
#is_error ⇒ Object (readonly)
Returns the value of attribute is_error.
6 7 8 |
# File 'lib/action_mcp/base_response.rb', line 6 def is_error @is_error end |
Instance Method Details
#==(other) ⇒ Object
Compare with hash for easier testing
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/action_mcp/base_response.rb', line 44 def ==(other) case other when Hash # Convert both to normalized format for comparison hash_self = to_h.deep_transform_keys { |key| key.to_s.underscore } hash_other = other.deep_transform_keys { |key| key.to_s.underscore } hash_self == hash_other when self.class compare_with_same_class(other) else super end end |
#build_success_hash ⇒ Object
Method to be implemented by subclasses
31 32 33 |
# File 'lib/action_mcp/base_response.rb', line 31 def build_success_hash raise NotImplementedError, "Subclasses must implement #build_success_hash" end |
#compare_with_same_class(other) ⇒ Object
Method to be implemented by subclasses for comparison
59 60 61 |
# File 'lib/action_mcp/base_response.rb', line 59 def compare_with_same_class(other) raise NotImplementedError, "Subclasses must implement #compare_with_same_class" end |
#eql?(other) ⇒ Boolean
Implement eql? for hash key comparison
64 65 66 |
# File 'lib/action_mcp/base_response.rb', line 64 def eql?(other) self == other end |
#error? ⇒ Boolean
82 83 84 |
# File 'lib/action_mcp/base_response.rb', line 82 def error? is_error end |
#hash ⇒ Object
Implement hash method for hash key usage
74 75 76 |
# File 'lib/action_mcp/base_response.rb', line 74 def hash hash_components.hash end |
#hash_components ⇒ Object
Method to be implemented by subclasses for hash generation
69 70 71 |
# File 'lib/action_mcp/base_response.rb', line 69 def hash_components raise NotImplementedError, "Subclasses must implement #hash_components" end |
#mark_as_error!(symbol = :invalid_request, message: nil, data: nil) ⇒ Object
Mark response as error
13 14 15 16 17 18 19 |
# File 'lib/action_mcp/base_response.rb', line 13 def mark_as_error!(symbol = :invalid_request, message: nil, data: nil) @is_error = true @symbol = symbol = @error_data = data self end |
#success? ⇒ Boolean
78 79 80 |
# File 'lib/action_mcp/base_response.rb', line 78 def success? !is_error end |
#to_h(_options = nil) ⇒ Object Also known as: as_json
Convert to hash format expected by MCP protocol
22 23 24 25 26 27 28 |
# File 'lib/action_mcp/base_response.rb', line 22 def to_h( = nil) if @is_error JSON_RPC::JsonRpcError.new(@symbol, message: , data: @error_data).to_h else build_success_hash end end |
#to_json(options = nil) ⇒ Object
Handle to_json directly
39 40 41 |
# File 'lib/action_mcp/base_response.rb', line 39 def to_json( = nil) to_h.to_json() end |