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.
Methods inherited from BaseResponse
#==, #eql?, #error?, #hash, #mark_as_error!, #success?, #to_h, #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
85 86 87 88 89 |
# File 'lib/action_mcp/resource_response.rb', line 85 def build_success_hash { contents: @contents.map(&:to_h) } end |
#compare_with_same_class(other) ⇒ Object
Implementation of compare_with_same_class for ResourceResponse
92 93 94 |
# File 'lib/action_mcp/resource_response.rb', line 92 def compare_with_same_class(other) contents == other.contents && is_error == other.is_error end |
#hash_components ⇒ Object
Implementation of hash_components for ResourceResponse
97 98 99 |
# File 'lib/action_mcp/resource_response.rb', line 97 def hash_components [ contents, is_error ] end |
#inspect ⇒ Object
Pretty print for better debugging
102 103 104 105 106 107 108 |
# File 'lib/action_mcp/resource_response.rb', line 102 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 82 |
# File 'lib/action_mcp/resource_response.rb', line 75 def mark_as_not_found!(uri) # Use method_not_found for resource not found (closest standard JSON-RPC error) mark_as_error!( :method_not_found, # -32601 - closest standard error for "not found" message: "Resource not found", data: { uri: uri } ) 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 |