Class: ActionMCP::ToolResponse
- Inherits:
-
BaseResponse
- Object
- BaseResponse
- ActionMCP::ToolResponse
- Defined in:
- lib/action_mcp/tool_response.rb
Overview
Manages the collection of content objects for tool results
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#structured_content ⇒ Object
readonly
Returns the value of attribute structured_content.
-
#tool_execution_error ⇒ Object
readonly
Returns the value of attribute tool_execution_error.
Attributes inherited from BaseResponse
Instance Method Summary collapse
-
#add(content) ⇒ Object
Add content to the response.
-
#build_success_hash ⇒ Object
Implementation of build_success_hash for ToolResponse.
-
#compare_with_same_class(other) ⇒ Object
Implementation of compare_with_same_class for ToolResponse.
-
#hash_components ⇒ Object
Implementation of hash_components for ToolResponse.
-
#initialize ⇒ ToolResponse
constructor
A new instance of ToolResponse.
-
#inspect ⇒ Object
Pretty print for better debugging.
-
#report_tool_error(message) ⇒ Object
Report a tool execution error (as opposed to protocol error) This follows MCP spec for tool execution errors.
-
#set_structured_content(content) ⇒ Object
Set structured content for the response.
- #to_h(_options = nil) ⇒ Object
Methods inherited from BaseResponse
#==, #eql?, #error?, #hash, #mark_as_error!, #success?, #to_json
Constructor Details
#initialize ⇒ ToolResponse
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
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
6 7 8 |
# File 'lib/action_mcp/tool_response.rb', line 6 def contents @contents end |
#structured_content ⇒ Object (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_error ⇒ Object (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_hash ⇒ Object
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_components ⇒ Object
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 |
#inspect ⇒ Object
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() @tool_execution_error = true add(Content::Text.new()) 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( = 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 |