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?, #error?, #hash, #mark_as_error!, #success?, #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
# File 'lib/action_mcp/tool_response.rb', line 18

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

#build_success_hashObject

Implementation of build_success_hash for ToolResponse



49
50
51
52
53
54
55
# File 'lib/action_mcp/tool_response.rb', line 49

def build_success_hash
  result = {
    content: @contents.map(&:to_h)
  }
  result[:structuredContent] = @structured_content if @structured_content
  result
end

#compare_with_same_class(other) ⇒ Object

Implementation of compare_with_same_class for ToolResponse



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

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

#hash_componentsObject

Implementation of hash_components for ToolResponse



65
66
67
# File 'lib/action_mcp/tool_response.rb', line 65

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

#inspectObject

Pretty print for better debugging



70
71
72
73
74
75
# File 'lib/action_mcp/tool_response.rb', line 70

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



30
31
32
33
# File 'lib/action_mcp/tool_response.rb', line 30

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

#set_structured_content(content) ⇒ Object

Set structured content for the response



24
25
26
# File 'lib/action_mcp/tool_response.rb', line 24

def set_structured_content(content)
  @structured_content = content
end

#to_h(_options = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/action_mcp/tool_response.rb', line 35

def to_h(_options = nil)
  if @tool_execution_error
    result = {
      isError: true,
      content: @contents.map(&:to_h)
    }
    result[:structuredContent] = @structured_content if @structured_content
    result
  else
    super
  end
end