Class: MCP::Skill::ResourceEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/mcp/skill/resource_entry.rb

Overview

One file of a skill as its manifest lists it: { uri, digest, size } (SEP-2640). The digest is sha256: followed by 64 lowercase hex characters over the file's raw bytes, size is the byte length. Only the shape is checked here; whether the URI lies under the skill's root is checked by MCP::Skill, which knows the root.

Constant Summary collapse

DIGEST_PATTERN =
/\Asha256:[0-9a-f]{64}\z/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, digest:, size:) ⇒ ResourceEntry

Returns a new instance of ResourceEntry.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mcp/skill/resource_entry.rb', line 76

def initialize(uri:, digest:, size:)
  unless uri.is_a?(String) && Uri.structural?(uri)
    raise ArgumentError, "The value of uri must be a skill:// URI with non-empty path segments."
  end

  unless digest.is_a?(String) && digest.encoding.ascii_compatible? && DIGEST_PATTERN.match?(digest)
    raise ArgumentError, "The value of digest must be sha256: followed by 64 lowercase hex characters."
  end

  unless size.is_a?(Integer) && size >= 0
    raise ArgumentError, "The value of size must be a non-negative Integer (got #{size.class})."
  end

  @uri = -uri
  @digest = -digest
  @size = size
  freeze
end

Instance Attribute Details

#digest ⇒ Object (readonly)

Returns the value of attribute digest.



74
75
76
# File 'lib/mcp/skill/resource_entry.rb', line 74

def digest
  @digest
end

#size ⇒ Object (readonly)

Returns the value of attribute size.



74
75
76
# File 'lib/mcp/skill/resource_entry.rb', line 74

def size
  @size
end

#uri ⇒ Object (readonly)

Returns the value of attribute uri.



74
75
76
# File 'lib/mcp/skill/resource_entry.rb', line 74

def uri
  @uri
end

Class Method Details

.from(value) ⇒ Object

Returns value when it is already a ResourceEntry, builds one from a Hash, and refuses anything else.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mcp/skill/resource_entry.rb', line 20

def from(value)
  return value if value.is_a?(ResourceEntry)

  unless value.is_a?(Hash)
    raise ArgumentError, "A skill resource entry must be an MCP::Skill::ResourceEntry or a Hash (got #{value.class})."
  end

  keywords = {}
  value.each do |key, member|
    unless key.is_a?(Symbol) || key.is_a?(String)
      raise ArgumentError, "A skill resource entry Hash key must be a Symbol or a String (got #{key.class})."
    end

    keyword = HASH_KEYWORDS[key.to_s]
    unless keyword
      raise ArgumentError, "A skill resource entry Hash may only hold uri, digest, and size (got #{key.inspect})."
    end
    raise ArgumentError, "A skill resource entry Hash gives #{keyword} twice." if keywords.key?(keyword)

    keywords[keyword] = member
  end

  new(**keywords)
end

.from_content(uri:, content:) ⇒ Object

Builds the entry for a file from the exact bytes the resource will serve, so an author never formats a digest by hand: pass File.binread output for a binary file and UTF-8 text for a text file.



58
59
60
61
62
63
64
# File 'lib/mcp/skill/resource_entry.rb', line 58

def from_content(uri:, content:)
  unless content.is_a?(String)
    raise ArgumentError, "The value of content must be a String (got #{content.class})."
  end

  new(uri: uri, digest: "sha256:#{Digest::SHA256.hexdigest(content.b)}", size: content.bytesize)
end

.from_wire(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds an entry from a server's wire Hash (String or Symbol keys), reading uri, digest, and size and ignoring any other member a server may add; from refuses unknown members for local definitions instead.

Raises:

  • (ArgumentError)


49
50
51
52
53
54
# File 'lib/mcp/skill/resource_entry.rb', line 49

def from_wire(value)
  return value if value.is_a?(ResourceEntry)
  raise ArgumentError, "A skill resource entry must be a Hash (got #{value.class})." unless value.is_a?(Hash)

  new(uri: wire_member(value, "uri"), digest: wire_member(value, "digest"), size: wire_member(value, "size"))
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



99
100
101
# File 'lib/mcp/skill/resource_entry.rb', line 99

def ==(other)
  other.is_a?(ResourceEntry) && to_h == other.to_h
end

#hash ⇒ Object



104
105
106
# File 'lib/mcp/skill/resource_entry.rb', line 104

def hash
  to_h.hash
end

#to_h ⇒ Object



95
96
97
# File 'lib/mcp/skill/resource_entry.rb', line 95

def to_h
  { uri: uri, digest: digest, size: size }
end