Class: ActionMCP::Resource
- Inherits:
-
Object
- Object
- ActionMCP::Resource
- Defined in:
- lib/action_mcp/resource.rb
Overview
Represents a resource with its metadata. Used by resources/list to describe concrete resources.
Instance Attribute Summary collapse
-
#annotations ⇒ Object
readonly
Returns the value of attribute annotations.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#mime_type ⇒ Object
readonly
Returns the value of attribute mime_type.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(uri:, name:, title: nil, description: nil, mime_type: nil, size: nil, annotations: nil, meta: nil) ⇒ Resource
constructor
A new instance of Resource.
-
#to_h ⇒ Hash
Convert the resource to a hash with the keys expected by MCP.
- #to_json ⇒ Object
Constructor Details
#initialize(uri:, name:, title: nil, description: nil, mime_type: nil, size: nil, annotations: nil, meta: nil) ⇒ Resource
Returns a new instance of Resource.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/action_mcp/resource.rb', line 17 def initialize(uri:, name:, title: nil, description: nil, mime_type: nil, size: nil, annotations: nil, meta: nil) @uri = uri @name = name @title = title @description = description @mime_type = mime_type @size = size @annotations = annotations @meta = if .nil? nil elsif .respond_to?(:to_hash) .to_hash elsif .respond_to?(:to_h) .to_h else raise ArgumentError, "meta must respond to :to_hash or :to_h, got: #{.class}" end freeze end |
Instance Attribute Details
#annotations ⇒ Object (readonly)
Returns the value of attribute annotations.
7 8 9 |
# File 'lib/action_mcp/resource.rb', line 7 def annotations @annotations end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
7 8 9 |
# File 'lib/action_mcp/resource.rb', line 7 def description @description end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
7 8 9 |
# File 'lib/action_mcp/resource.rb', line 7 def @meta end |
#mime_type ⇒ Object (readonly)
Returns the value of attribute mime_type.
7 8 9 |
# File 'lib/action_mcp/resource.rb', line 7 def mime_type @mime_type end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/action_mcp/resource.rb', line 7 def name @name end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
7 8 9 |
# File 'lib/action_mcp/resource.rb', line 7 def size @size end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/action_mcp/resource.rb', line 7 def title @title end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
7 8 9 |
# File 'lib/action_mcp/resource.rb', line 7 def uri @uri end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
57 58 59 60 61 62 |
# File 'lib/action_mcp/resource.rb', line 57 def ==(other) other.is_a?(Resource) && uri == other.uri && name == other.name && title == other.title && description == other.description && mime_type == other.mime_type && size == other.size && annotations == other.annotations && == other. end |
#hash ⇒ Object
65 66 67 |
# File 'lib/action_mcp/resource.rb', line 65 def hash [ uri, name, title, description, mime_type, size, annotations, ].hash end |
#to_h ⇒ Hash
Convert the resource to a hash with the keys expected by MCP. Note: The key for mime_type is converted to ‘mimeType’ as specified.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/action_mcp/resource.rb', line 42 def to_h hash = { uri: uri, name: name } hash[:title] = title if title hash[:description] = description if description hash[:mimeType] = mime_type if mime_type hash[:size] = size if size hash[:annotations] = annotations if annotations hash[:_meta] = if && !.empty? hash end |
#to_json ⇒ Object
53 54 55 |
# File 'lib/action_mcp/resource.rb', line 53 def to_json(*) MultiJson.dump(to_h, *) end |