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.
- #as_json(options = nil) ⇒ Object
-
#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.
- #error? ⇒ Boolean
-
#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.
- #success? ⇒ Boolean
- #to_h(_options = nil) ⇒ Object
Methods inherited from BaseResponse
#==, #eql?, #hash, #mark_as_error!, #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 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( = nil) to_h() end |
#build_success_hash ⇒ Object
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
38 39 40 |
# File 'lib/action_mcp/tool_response.rb', line 38 def error? super || @tool_execution_error end |
#hash_components ⇒ Object
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 |
#inspect ⇒ Object
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() @tool_execution_error = true add(Content::Text.new(.to_s)) end |
#set_structured_content(content) ⇒ Object
Set structured content for the response
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
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( = 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 |