Class: ActionMCP::Resource

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • uri (String)

    The URI of the resource

  • name (String)

    Display name of the resource

  • title (String, nil) (defaults to: nil)

    Human-readable title

  • description (String, nil) (defaults to: nil)

    Description of the resource

  • mime_type (String, nil) (defaults to: nil)

    MIME type of the resource content

  • size (Integer, nil) (defaults to: nil)

    Size of the resource in bytes

  • annotations (Hash, nil) (defaults to: nil)

    Optional annotations

  • meta (Hash, #to_hash, #to_h, nil) (defaults to: nil)

    Optional extension metadata. Emitted on the wire as _meta.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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)
  raise ArgumentError, "uri must be a non-empty string" unless uri.is_a?(String) && uri.present?
  raise ArgumentError, "name must be a non-empty string" unless name.is_a?(String) && name.present?

  { title: title, description: description, mime_type: mime_type }.each do |field, value|
    raise ArgumentError, "#{field} must be a string or nil" unless value.nil? || value.is_a?(String)
  end
  unless size.nil? || (size.is_a?(Integer) && size >= 0)
    raise ArgumentError, "size must be a non-negative integer or nil"
  end

  @uri = uri
  @name = name
  @title = title
  @description = description
  @mime_type = mime_type
  @size = size
  @annotations = annotations.nil? ? nil : Content::Validation.copy_object!(annotations, "annotations")
  Content::Validation.validate_annotations!(@annotations)
  @meta = meta.nil? ? nil : Content::Validation.copy_object!(meta, "meta")
  to_h
  freeze
end

Instance Attribute Details

#annotationsObject (readonly)

Returns the value of attribute annotations.



7
8
9
# File 'lib/action_mcp/resource.rb', line 7

def annotations
  @annotations
end

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/action_mcp/resource.rb', line 7

def description
  @description
end

#metaObject (readonly)

Returns the value of attribute meta.



7
8
9
# File 'lib/action_mcp/resource.rb', line 7

def meta
  @meta
end

#mime_typeObject (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

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/action_mcp/resource.rb', line 7

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



7
8
9
# File 'lib/action_mcp/resource.rb', line 7

def size
  @size
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/action_mcp/resource.rb', line 7

def title
  @title
end

#uriObject (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?



61
62
63
64
65
66
# File 'lib/action_mcp/resource.rb', line 61

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 && meta == other.meta
end

#hashObject



69
70
71
# File 'lib/action_mcp/resource.rb', line 69

def hash
  [ uri, name, title, description, mime_type, size, annotations, meta ].hash
end

#to_hHash

Convert the resource to a hash with the keys expected by MCP. Note: The key for mime_type is converted to 'mimeType' as specified.

Returns:

  • (Hash)

    A hash representation of the resource.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/action_mcp/resource.rb', line 45

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] = meta if meta && !meta.empty?
  Content::Validation.validate_resource!(hash)
  hash
end

#to_jsonObject



57
58
59
# File 'lib/action_mcp/resource.rb', line 57

def to_json(*)
  MultiJson.dump(to_h, *)
end