Class: ActionMCP::ResourceResponse
- Inherits:
-
BaseResponse
- Object
- BaseResponse
- ActionMCP::ResourceResponse
- Defined in:
- lib/action_mcp/resource_response.rb
Overview
Manages resource resolution results and errors for ResourceTemplate operations. Unlike ToolResponse, ResourceResponse only uses JSON-RPC protocol errors per MCP spec.
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
Attributes inherited from BaseResponse
Instance Method Summary collapse
-
#add_content(content) ⇒ Object
Add a resource content item to the response.
-
#add_contents(contents_array) ⇒ Object
Add multiple content items.
-
#build_success_hash ⇒ Object
Implementation of build_success_hash for ResourceResponse.
-
#compare_with_same_class(other) ⇒ Object
Implementation of compare_with_same_class for ResourceResponse.
-
#hash_components ⇒ Object
Implementation of hash_components for ResourceResponse.
-
#initialize ⇒ ResourceResponse
constructor
A new instance of ResourceResponse.
-
#inspect ⇒ Object
Pretty print for better debugging.
- #mark_as_callback_aborted!(uri) ⇒ Object
- #mark_as_not_found!(uri) ⇒ Object
- #mark_as_parameter_validation_failed!(missing_params, uri) ⇒ Object
- #mark_as_resolution_failed!(uri, reason = nil) ⇒ Object
-
#mark_as_template_not_found!(uri) ⇒ Object
Mark as error with ResourceTemplate-specific error types.
-
#to_h(_options = nil) ⇒ Object
Override to_h to use -32002 for resource not found (consistent with send_resource_read).
Methods inherited from BaseResponse
#==, #eql?, #error?, #hash, #mark_as_error!, #success?, #to_json
Constructor Details
#initialize ⇒ ResourceResponse
Returns a new instance of ResourceResponse.
11 12 13 14 |
# File 'lib/action_mcp/resource_response.rb', line 11 def initialize super @contents = [] end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
7 8 9 |
# File 'lib/action_mcp/resource_response.rb', line 7 def contents @contents end |
Instance Method Details
#add_content(content) ⇒ Object
Add a resource content item to the response
17 18 19 20 |
# File 'lib/action_mcp/resource_response.rb', line 17 def add_content(content) @contents << content content # Return the content for chaining end |
#add_contents(contents_array) ⇒ Object
Add multiple content items
23 24 25 26 |
# File 'lib/action_mcp/resource_response.rb', line 23 def add_contents(contents_array) @contents.concat(contents_array) self end |
#build_success_hash ⇒ Object
Implementation of build_success_hash for ResourceResponse
95 96 97 98 99 |
# File 'lib/action_mcp/resource_response.rb', line 95 def build_success_hash { contents: @contents.map(&:to_h) } end |
#compare_with_same_class(other) ⇒ Object
Implementation of compare_with_same_class for ResourceResponse
102 103 104 |
# File 'lib/action_mcp/resource_response.rb', line 102 def compare_with_same_class(other) contents == other.contents && is_error == other.is_error end |
#hash_components ⇒ Object
Implementation of hash_components for ResourceResponse
107 108 109 |
# File 'lib/action_mcp/resource_response.rb', line 107 def hash_components [ contents, is_error ] end |
#inspect ⇒ Object
Pretty print for better debugging
112 113 114 115 116 117 118 |
# File 'lib/action_mcp/resource_response.rb', line 112 def inspect if is_error "#<#{self.class.name} error: #{@error_message}>" else "#<#{self.class.name} contents: #{contents.size} items>" end end |
#mark_as_callback_aborted!(uri) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/action_mcp/resource_response.rb', line 64 def mark_as_callback_aborted!(uri) mark_as_error!( :internal_error, message: "Resource resolution was aborted by callback chain", data: { uri: uri, error_type: "CALLBACK_ABORTED" } ) end |
#mark_as_not_found!(uri) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/action_mcp/resource_response.rb', line 75 def mark_as_not_found!(uri) @is_error = true @symbol = nil @error_message = "Resource not found" @error_data = { uri: uri } self end |
#mark_as_parameter_validation_failed!(missing_params, uri) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/action_mcp/resource_response.rb', line 37 def mark_as_parameter_validation_failed!(missing_params, uri) mark_as_error!( :invalid_params, message: "Required parameters missing: #{missing_params.join(', ')}", data: { uri: uri, missing_parameters: missing_params, error_type: "PARAMETER_VALIDATION_FAILED" } ) end |
#mark_as_resolution_failed!(uri, reason = nil) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/action_mcp/resource_response.rb', line 49 def mark_as_resolution_failed!(uri, reason = nil) = "Resource resolution failed for URI: #{uri}" += " - #{reason}" if reason mark_as_error!( :internal_error, message: , data: { uri: uri, reason: reason, error_type: "RESOLUTION_FAILED" } ) end |
#mark_as_template_not_found!(uri) ⇒ Object
Mark as error with ResourceTemplate-specific error types
29 30 31 32 33 34 35 |
# File 'lib/action_mcp/resource_response.rb', line 29 def mark_as_template_not_found!(uri) mark_as_error!( :invalid_params, message: "Resource template not found for URI: #{uri}", data: { uri: uri, error_type: "TEMPLATE_NOT_FOUND" } ) end |
#to_h(_options = nil) ⇒ Object
Override to_h to use -32002 for resource not found (consistent with send_resource_read)
84 85 86 87 88 89 90 91 92 |
# File 'lib/action_mcp/resource_response.rb', line 84 def to_h( = nil) if @is_error && @symbol.nil? { code: -32_002, message: @error_message, data: @error_data } elsif @is_error JSON_RPC::JsonRpcError.new(@symbol, message: @error_message, data: @error_data).to_h else build_success_hash end end |