Build, validate, save and load Agent Skills as Python values, with their files and directories, then give them to a local agent or serve them over MCP.
An Agent Skill is a directory an agent can read
when a task calls for it: a SKILL.md with YAML frontmatter (a name and a description
of when to use the skill) and markdown instructions, plus the files those instructions
refer to, such as a policy or a script.
pyfect.ai.skill makes a skill a value. skill.make(frontmatter=..., instructions=..., files=...) gives a Skill, checked when it is made: the frontmatter must follow the
specification (name is 1 to 64 lowercase letters, digits and single hyphens,
description 1 to 1024 characters), and a file's path must stay inside the skill and must
not be SKILL.md, which the skill writes itself. A bad skill raises SchemaError, so a
skill in your code is either valid or never made. pyright checks the shape too: the
frontmatter's Frontmatter type needs name and description, and any other key holds
JSON.
Files and directories#
skill.to_files(s) gives the skill as files: the generated SKILL.md, then the supporting
files. Its type says SKILL.md is always there and always text. Each frontmatter key and
value is written as JSON, which YAML reads back unchanged, so a description such as
yes: it's #1 survives the trip.
skill.from_files(files) goes the other way, for skills you did not write, such as a
directory read from disk. It takes text or bytes, and fails with IllegalArgumentError
when SKILL.md is missing, is not UTF-8, does not begin with YAML frontmatter, or the
skill is not valid. A hand-written SKILL.md with a BOM or Windows line endings reads
fine.
A Skill is also a schema class, so S.to_codec_json(skill.Skill) stores it as JSON. A
file's bytes are written as {"base64": ...}, so text and binary files come back as
what they were.
import tempfile
from pathlib import Path
import pyfect as fx
from pyfect import schema as S
from pyfect import stream
from pyfect.ai import language_model, prompt, skill
from pyfect.ai.language_model import LanguageModel, ProviderOptions
from pyfect.errors import IllegalArgumentError
# A skill is data: frontmatter, instructions and the files they refer to.
refunds = skill.make(
frontmatter={"name": "refunds", "description": "Process customer refunds"},
instructions="Read references/policy.md before issuing a refund.\n",
files={
"references/policy.md": "Refunds over $100 need approval.\n",
"assets/logo.png": bytes([0x89, 0x50, 0x4E, 0x47]),
},
)
# As files, it is a SKILL.md and its supporting files, ready to write to a directory.
files = skill.to_files(refunds)
print(files["SKILL.md"])
with tempfile.TemporaryDirectory() as directory:
root = Path(directory) / "refunds"
for path, content in files.items():
(root / path).parent.mkdir(parents=True, exist_ok=True)
if isinstance(content, str):
(root / path).write_text(content)
else:
(root / path).write_bytes(content)
# Reading a directory back: every file as bytes, SKILL.md included.
on_disk = {str(p.relative_to(root)): p.read_bytes() for p in root.rglob("*") if p.is_file()}
loaded = fx.run_blocking(skill.from_files(on_disk).or_die())
print(loaded.frontmatter, sorted(loaded.files))
# A bad name fails with IllegalArgumentError, as untrusted input should.
bad = skill.from_files({"SKILL.md": "---\nname: Refunds!\ndescription: x\n---\n"})
rejected = bad.map(lambda _: "loaded").pipe(
fx.catch_tag(IllegalArgumentError, lambda error: fx.succeed(type(error).__name__))
)
print(fx.run_blocking(rejected))
# Its JSON form keeps text and bytes apart.
encoded = S.encode_sync(S.to_codec_json(skill.Skill))(refunds)
assert isinstance(encoded, dict)
print(encoded["files"])
# A local agent: the skill's instructions go in the system prompt.
@fx.fn
def agent():
conversation = prompt.make("Refund order 1042, $80.").set_system(refunds.instructions)
reply = yield from language_model.generate_text(conversation)
print(reply.text)
# A scripted model that answers with the system prompt it was given.
def answer(options: ProviderOptions):
[system, *_] = options.prompt.content
text = f"system prompt: {system.content!r}"
usage = {"inputTokens": {"total": 9}, "outputTokens": {"total": 4}}
return fx.succeed(
[{"type": "text", "text": text}, {"type": "finish", "reason": "stop", "usage": usage}]
)
Scripted = fx.layer.effect(
LanguageModel, language_model.make(generate_text=answer, stream_text=lambda _: stream.empty)
)
fx.run_main(agent().pipe(fx.provide(Scripted)))Using a skill#
In a local agent, the skill is the instructions you give the model: put
s.instructions in the system prompt, as above, or list each skill's name and
description there and add a skill's instructions once the model picks it. A tool that
reads s.files[path] lets the model open the files the instructions refer to.
To share a skill with other agents, serve it over MCP:
mcp_server.skill("skill://billing/refunds/SKILL.md", refunds) registers it, as it
registers a map of files. The URI's last path segment must be the skill's name.
Reference: skill,
mcp_server.