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`.



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 meta.nil?
      nil
    elsif meta.respond_to?(:to_hash)
      meta.to_hash
    elsif meta.respond_to?(:to_h)
      meta.to_h
    else
      raise ArgumentError, "meta must respond to :to_hash or :to_h, got: #{meta.class}"
    end
  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?



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

#hashObject



65
66
67
# File 'lib/action_mcp/resource.rb', line 65

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.



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] = meta if meta && !meta.empty?
  hash
end

#to_jsonObject



53
54
55
# File 'lib/action_mcp/resource.rb', line 53

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