Class: ActionMCP::ToolResponse

Inherits:
BaseResponse show all
Defined in:
lib/action_mcp/tool_response.rb

Overview

Manages the collection of content objects for tool results

Instance Attribute Summary collapse

Attributes inherited from BaseResponse

#is_error

Instance Method Summary collapse

Methods inherited from BaseResponse

#==, #eql?, #hash, #mark_as_error!, #to_json

Constructor Details

#initializeToolResponse

Returns a new instance of ToolResponse.



10
11
12
13
14
15
# File 'lib/action_mcp/tool_response.rb', line 10

def initialize
  super
  @contents = []
  @structured_content = nil
  @tool_execution_error = false  # Track if this is a tool execution error
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



6
7
8
# File 'lib/action_mcp/tool_response.rb', line 6

def contents
  @contents
end

#structured_contentObject (readonly)

Returns the value of attribute structured_content.



6
7
8
# File 'lib/action_mcp/tool_response.rb', line 6

def structured_content
  @structured_content
end

#tool_execution_errorObject (readonly)

Returns the value of attribute tool_execution_error.



6
7
8
# File 'lib/action_mcp/tool_response.rb', line 6

def tool_execution_error
  @tool_execution_error
end

Instance Method Details

#add(content) ⇒ Object

Add content to the response



18
19
20
21
22
# File 'lib/action_mcp/tool_response.rb', line 18

def add(content)
  serialize_content(content)
  @contents << content
  content # Return the content for chaining
end

#as_json(options = nil) ⇒ Object



60
61
62
# File 'lib/action_mcp/tool_response.rb', line 60

def as_json(options = nil)
  to_h(options)
end

#build_success_hashObject

Implementation of build_success_hash for ToolResponse



65
66
67
68
69
70
71
72
# File 'lib/action_mcp/tool_response.rb', line 65

def build_success_hash
  result = {
    content: serialized_contents
  }
  result[:structuredContent] = @structured_content if @structured_content
  Content::Validation.validate_tool_result!(result)
  result
end

#compare_with_same_class(other) ⇒ Object

Implementation of compare_with_same_class for ToolResponse



75
76
77
78
79
# File 'lib/action_mcp/tool_response.rb', line 75

def compare_with_same_class(other)
  contents == other.contents && is_error == other.is_error &&
    structured_content == other.structured_content &&
    tool_execution_error == other.tool_execution_error
end

#error?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/action_mcp/tool_response.rb', line 38

def error?
  super || @tool_execution_error
end

#hash_componentsObject

Implementation of hash_components for ToolResponse



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

def hash_components
  [ contents, is_error, structured_content, tool_execution_error ]
end

#inspectObject

Pretty print for better debugging



87
88
89
90
91
92
# File 'lib/action_mcp/tool_response.rb', line 87

def inspect
  parts = [ "content: #{contents.inspect}" ]
  parts << "structuredContent: #{structured_content.inspect}" if structured_content
  parts << "isError: #{is_error}"
  "#<#{self.class.name} #{parts.join(', ')}>"
end

#report_tool_error(message) ⇒ Object

Report a tool execution error (as opposed to protocol error) This follows MCP spec for tool execution errors



33
34
35
36
# File 'lib/action_mcp/tool_response.rb', line 33

def report_tool_error(message)
  @tool_execution_error = true
  add(Content::Text.new(message.to_s))
end

#set_structured_content(content) ⇒ Object

Set structured content for the response

Raises:

  • (ArgumentError)


25
26
27
28
29
# File 'lib/action_mcp/tool_response.rb', line 25

def set_structured_content(content)
  raise ArgumentError, "structuredContent must be a JSON object" unless content.is_a?(Hash)

  @structured_content = Content::Validation.copy_object!(content, "structuredContent")
end

#success?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/action_mcp/tool_response.rb', line 42

def success?
  !error?
end

#to_h(_options = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/action_mcp/tool_response.rb', line 46

def to_h(_options = nil)
  if @tool_execution_error
    result = {
      isError: true,
      content: serialized_contents
    }
    result[:structuredContent] = @structured_content if @structured_content
    Content::Validation.validate_tool_result!(result)
    result
  else
    super
  end
end