Class: MCP::Skill
- Inherits:
-
Object
- Object
- MCP::Skill
- Defined in:
- lib/mcp/skill.rb,
lib/mcp/skill/directory.rb,
lib/mcp/skill/frontmatter.rb,
lib/mcp/skill/resource_entry.rb
Overview
A skill as the Skills extension describes it (SEP-2640, io.modelcontextprotocol/skills): a skill://
URI naming its SKILL.md, the frontmatter of that file kept verbatim, either the list of its files
with their digests and sizes or the String "dynamic" for content generated per request, and the _meta
of the entry when it has one, passed through under the JSON rules of the frontmatter with no key acted on
(keys of the extension itself use the io.modelcontextprotocol.skills/ prefix).
Every check runs at construction, as MCP::Icon does, so a manifest a host may reject fails on
the server at definition time: the URI grammar, the frontmatter (name and description, name equal to
the last segment of the skill path, JSON-representable values), and the resource entries (under
the skill root, unique, the skill's own SKILL.md among them). A skill defined locally is also held to
the unreserved URI character set, the Agent Skills rules for name and description, and the SEP limits
(512 entries, 16 MiB); entries a client receives from a server are checked structurally only, because
a host MUST accept skills up to the limits and MAY accept larger ones, and the Agent Skills guidance for
clients is to load a skill whose name breaks a rule anyway. The files of a nested skill, its SKILL.md
included, are supporting files of the enclosing skill and appear in both manifests, as the specification
requires of a complete manifest.
Instances are frozen value objects. Equality is identity: the client binds verified state to the exact entry object it checked.
https://github.com/modelcontextprotocol/ext-skills/blob/main/specification/beta/skills.mdx
Defined Under Namespace
Modules: Json, Uri Classes: FileError, FileRecord, ResourceEntry
Constant Summary collapse
- EXTENSION_ID =
"io.modelcontextprotocol/skills"- MAX_RESOURCES =
SEP-2640 limits per skill: servers SHOULD NOT serve a skill beyond them, hosts MUST accept up to them.
512- MAX_TOTAL_SIZE =
16 * 1024 * 1024
- NAME_PATTERN =
The Agent Skills rules SEP-2640 requires of the last segment of a skill path, its
name: lowercase letters, digits, and single hyphens that neither lead nor trail, 64 characters at most.descriptionholds at most 1024 characters. /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/.freeze
- MAX_NAME_LENGTH =
64- MAX_DESCRIPTION_LENGTH =
1024- DYNAMIC =
The
resourcesvalue of a skill whose files are generated per request and carry no digests. "dynamic"
Instance Attribute Summary collapse
-
#frontmatter ⇒ Object
readonly
Returns the value of attribute frontmatter.
-
#meta ⇒ Object
readonly
The
_metaof the entry as a frozen Hash with String keys, or nil when the entry carries none. -
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
-
#skill_path ⇒ Object
readonly
Returns the value of attribute skill_path.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
-
.capability ⇒ Object
The
capabilities.extensionsfragment advertising the extension, for a hand-builtMCP::Server::Capabilities; a server configured withskills:adds it itself. -
.file_map(skills) ⇒ Object
private
The file records of directory-backed skills keyed by URI, for the server's default resource reader.
-
.file_roots(skills) ⇒ Object
private
The roots (
skill://<path>) of the directory-backed skills amongskills, for the server to tell a read of an unserved file under one of them from an ordinary unknown resource. -
.from_directory(root) ⇒ Object
Scans
rootand returns the skills found under it (an Array ofMCP::Skill, ordered by URI); every problem is anArgumentErrornaming the path. -
.from_entry(entry) ⇒ Object
private
Builds a skill from an entry a server returned (String or Symbol keys), under the structural rules only.
-
.read_file(skills, uri) ⇒ Object
Serves one file of a directory-backed skill for a custom
resources_read_handler, which replaces the default reader and with it the serving of skill files: returns thecontentsArray holding one item, or nil whenuriis not a file of any ofskills. -
.read_record(record, request) ⇒ Object
private
The default reader's path for a file record found in the server's map.
-
.verify_frontmatter(skill, text) ⇒ Object
Compares the frontmatter of a fetched
SKILL.md(its text) withskill's entry field by field, the host-side MUST of SEP-2640, whichverify_resourcedoes not cover. -
.verify_resource(skill, uri, bytes) ⇒ Object
Checks fetched bytes against the entry for
uriinskill's manifest (size, then SHA-256), for hosts that read skill files throughMCP::Client#read_resourcerather than#read_skill_resource. -
.with_file_records(uri:, frontmatter:, resources:, file_records:, meta: nil) ⇒ Object
private
Builds a locally defined skill that also carries the records
from_directoryserves files from.
Instance Method Summary collapse
- #dynamic? ⇒ Boolean
-
#initialize(uri:, frontmatter:, resources:, meta: nil) ⇒ Skill
constructor
A new instance of Skill.
- #to_h ⇒ Object
-
#total_size ⇒ Object
The sum of the listed sizes, or nil for a dynamic skill; the hook for a host that declines large skills.
Constructor Details
#initialize(uri:, frontmatter:, resources:, meta: nil) ⇒ Skill
Returns a new instance of Skill.
397 398 399 |
# File 'lib/mcp/skill.rb', line 397 def initialize(uri:, frontmatter:, resources:, meta: nil) initialize_members(uri, frontmatter, resources, meta: , local: true, file_records: nil) end |
Instance Attribute Details
#frontmatter ⇒ Object (readonly)
Returns the value of attribute frontmatter.
392 393 394 |
# File 'lib/mcp/skill.rb', line 392 def frontmatter @frontmatter end |
#meta ⇒ Object (readonly)
The _meta of the entry as a frozen Hash with String keys, or nil when the entry carries none.
395 396 397 |
# File 'lib/mcp/skill.rb', line 395 def @meta end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
392 393 394 |
# File 'lib/mcp/skill.rb', line 392 def name @name end |
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
392 393 394 |
# File 'lib/mcp/skill.rb', line 392 def resources @resources end |
#skill_path ⇒ Object (readonly)
Returns the value of attribute skill_path.
392 393 394 |
# File 'lib/mcp/skill.rb', line 392 def skill_path @skill_path end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
392 393 394 |
# File 'lib/mcp/skill.rb', line 392 def uri @uri end |
Class Method Details
.capability ⇒ Object
The capabilities.extensions fragment advertising the extension, for a hand-built
MCP::Server::Capabilities; a server configured with skills: adds it itself.
249 250 251 |
# File 'lib/mcp/skill.rb', line 249 def capability { EXTENSION_ID => {} } end |
.file_map(skills) ⇒ 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.
The file records of directory-backed skills keyed by URI, for the server's default resource reader. A nested skill and its enclosing skill share the records of the nested files, so the same record under the same URI is fine; two different records for one URI are not.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/mcp/skill.rb', line 354 def file_map(skills) map = {} skills.each do |skill| records = skill.__send__(:file_records) next unless records records.each do |uri, record| existing = map[uri] next if existing == record raise ArgumentError, "The skill file #{uri} is served by more than one skill from different files." if existing map[uri] = record end end map.freeze end |
.file_roots(skills) ⇒ 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.
The roots (skill://<path>) of the directory-backed skills among skills, for the server to tell
a read of an unserved file under one of them from an ordinary unknown resource.
375 376 377 |
# File 'lib/mcp/skill.rb', line 375 def file_roots(skills) skills.select { |skill| skill.__send__(:file_records) }.map { |skill| Uri.root(skill.skill_path) }.uniq.freeze end |
.from_directory(root) ⇒ Object
Scans root and returns the skills found under it (an Array of MCP::Skill, ordered by URI); every
problem is an ArgumentError naming the path. root is resolved once with File.realpath, so
a symbolic link to the directory is fine; a SKILL.md directly under root, or no skill at all, is
refused, because root is the container of skill directories, not a skill.
The tree is trusted as the operator's: it must be writable only by the operator while skills are built and served. The checks on each file narrow what a concurrent change to the tree can do; they do not confine a writer who can change it.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mcp/skill/directory.rb', line 25 def from_directory(root) root = root.to_path if root.respond_to?(:to_path) raise ArgumentError, "The skills root must be a path String (got #{root.class})." unless root.is_a?(String) real_root = begin File.realpath(root) rescue SystemCallError => e raise ArgumentError, "The skills root #{root} cannot be resolved (#{e.class})." end raise ArgumentError, "The skills root #{real_root} is not a directory." unless File.directory?(real_root) if File.exist?(File.join(real_root, Uri::MANIFEST)) raise ArgumentError, "The skills root #{real_root} holds a SKILL.md itself; pass the directory that contains the skill directories." end skills = [] scan_directory(real_root, [], skills) raise ArgumentError, "No directory holding a SKILL.md was found under #{real_root}." if skills.empty? skills.sort_by(&:uri) end |
.from_entry(entry) ⇒ 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 a skill from an entry a server returned (String or Symbol keys), under the structural rules only.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/mcp/skill.rb', line 256 def from_entry(entry) raise ArgumentError, "A skill entry must be a Hash (got #{entry.class})." unless entry.is_a?(Hash) skill = allocate skill.__send__( :initialize_members, read_key(entry, "uri"), read_key(entry, "frontmatter"), read_key(entry, "resources"), meta: read_key(entry, "_meta"), local: false, file_records: nil, ) skill end |
.read_file(skills, uri) ⇒ Object
Serves one file of a directory-backed skill for a custom resources_read_handler, which replaces
the default reader and with it the serving of skill files: returns the contents Array holding one item,
or nil when uri is not a file of any of skills. A file that changed since registration raises
the same -32603 the default reader answers. The files of a nested skill are files of the enclosing skill
as well, so a caller given the enclosing skill reads them whether or not the nested one is among skills.
51 52 53 54 55 56 57 58 |
# File 'lib/mcp/skill/directory.rb', line 51 def read_file(skills, uri) record = file_map(skills)[uri] return unless record [read_record_item(record)] rescue FileError => e raise file_changed_error(nil, e) end |
.read_record(record, request) ⇒ 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.
The default reader's path for a file record found in the server's map.
63 64 65 66 67 |
# File 'lib/mcp/skill/directory.rb', line 63 def read_record(record, request) [read_record_item(record)] rescue FileError => e raise file_changed_error(request, e) end |
.verify_frontmatter(skill, text) ⇒ Object
Compares the frontmatter of a fetched SKILL.md (its text) with skill's entry field by field,
the host-side MUST of SEP-2640, which verify_resource does not cover. Raises
MCP::Client::SkillVerificationError with reason :parse or :frontmatter.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/mcp/skill.rb', line 326 def verify_frontmatter(skill, text) raise ArgumentError, "The value of skill must be an MCP::Skill (got #{skill.class})." unless skill.is_a?(Skill) raise ArgumentError, "The value of text must be a String (got #{text.class})." unless text.is_a?(String) actual = begin Frontmatter.load(text) rescue Frontmatter::Error => e raise Client::SkillVerificationError.new( "The frontmatter of #{skill.uri.dump} cannot be read", uri: skill.uri, skill_uri: skill.uri, reason: e.reason, ) end unless Json.equal?(skill.frontmatter, actual) raise Client::SkillVerificationError.new( "The frontmatter of #{skill.uri.dump} differs from its entry", uri: skill.uri, skill_uri: skill.uri, reason: :frontmatter, ) end true end |
.verify_resource(skill, uri, bytes) ⇒ Object
Checks fetched bytes against the entry for uri in skill's manifest (size, then SHA-256), for hosts
that read skill files through MCP::Client#read_resource rather than #read_skill_resource. Pass
exactly the bytes received: text.b for a text item, the decoded blob otherwise. Raises
MCP::Client::SkillReadRefusedError when the skill is dynamic or does not list uri, and
MCP::Client::SkillVerificationError on a mismatch. Does not compare frontmatter: a SKILL.md
also needs verify_frontmatter.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/mcp/skill.rb', line 287 def verify_resource(skill, uri, bytes) check_skill_and_uri!(skill, uri) raise ArgumentError, "The value of bytes must be a String (got #{bytes.class})." unless bytes.is_a?(String) if skill.dynamic? raise Client::SkillReadRefusedError.new( "Skill #{skill.uri.dump} is dynamic and lists no digests to verify #{uri.dump} against", reason: :dynamic, uri: uri, skill_uri: skill.uri, ) end entry = skill.resources.find { |candidate| candidate.uri == uri } unless entry raise Client::SkillReadRefusedError.new( "Resource #{uri.dump} is not listed by #{skill.uri.dump}", reason: :not_in_manifest, uri: uri, skill_uri: skill.uri, ) end unless bytes.bytesize == entry.size raise Client::SkillVerificationError.new( "Resource #{uri.dump} does not have the size listed by #{skill.uri.dump}", uri: uri, skill_uri: skill.uri, reason: :size, ) end unless "sha256:#{Digest::SHA256.hexdigest(bytes)}" == entry.digest raise Client::SkillVerificationError.new( "Resource #{uri.dump} does not have the digest listed by #{skill.uri.dump}", uri: uri, skill_uri: skill.uri, reason: :digest, ) end true end |
.with_file_records(uri:, frontmatter:, resources:, file_records:, meta: nil) ⇒ 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 a locally defined skill that also carries the records from_directory serves files from.
275 276 277 278 279 |
# File 'lib/mcp/skill.rb', line 275 def with_file_records(uri:, frontmatter:, resources:, file_records:, meta: nil) skill = allocate skill.__send__(:initialize_members, uri, frontmatter, resources, meta: , local: true, file_records: file_records) skill end |
Instance Method Details
#dynamic? ⇒ Boolean
401 402 403 |
# File 'lib/mcp/skill.rb', line 401 def dynamic? @resources == DYNAMIC end |
#to_h ⇒ Object
410 411 412 413 414 |
# File 'lib/mcp/skill.rb', line 410 def to_h entry = { uri: uri, frontmatter: frontmatter, resources: dynamic? ? DYNAMIC : resources.map(&:to_h) } entry[:_meta] = if entry end |
#total_size ⇒ Object
The sum of the listed sizes, or nil for a dynamic skill; the hook for a host that declines large skills.
406 407 408 |
# File 'lib/mcp/skill.rb', line 406 def total_size @total_size end |