Class: ActionMCP::BaseResponse

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/action_mcp/base_response.rb

Direct Known Subclasses

PromptResponse, ResourceResponse, ToolResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseResponse

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_errorObject (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_hashObject

Method to be implemented by subclasses

Raises:

  • (NotImplementedError)


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

Raises:

  • (NotImplementedError)


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

Returns:

  • (Boolean)


64
65
66
# File 'lib/action_mcp/base_response.rb', line 64

def eql?(other)
  self == other
end

#error?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/action_mcp/base_response.rb', line 82

def error?
  is_error
end

#hashObject

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_componentsObject

Method to be implemented by subclasses for hash generation

Raises:

  • (NotImplementedError)


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_message = message
  @error_data = data
  self
end

#success?Boolean

Returns:

  • (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(_options = nil)
  if @is_error
    JSON_RPC::JsonRpcError.new(@symbol, message: @error_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(options = nil)
  to_h.to_json(options)
end