Advanced
โฑ 8 min read
๐ examples/32_agent_files.mjs
Lesson 42: Agent definition files: one YAML, a live tool-using agent, a live subagent
*.agent.yaml definitions: load, freeze, resolve the toolset, run live, delegate live
1. Why This Feature Exists
A non-coder writes one agents/<name>.agent.yaml. This program uses that file for real โ every model call below goes to the provider in secret.env:
2. What the Example Demonstrates
A โ THE FILE
the YAML, on screen, plus the one script it declares. Nothing in the file builds a client, names a secret or defines a server: a definition selects what the host already provides.
B โ LOAD
loadAgentFile validates every field, resolves the script on disk (contained, hashed) and returns a deep-frozen spec.C โ THE TOOLBOX
createAgentToolbox supplies a real workspace and a real host tool; resolveToolset turns use: [files], host:lookup_policy and the declared script into the exact tool list.D โ LIVE SESSION:
applyAgentSpec wires a real session. The modLIVE SESSION:
applyAgentSpec wires a real session. The model reads the note, runs the declared script tool, consults the host tool, and answers with the exact word count. The tool events below are the model's own.E โ LIVE PROFILE: the SAME spec becomes a subagent profile via `
LIVE PROFILE: the SAME spec becomes a subagent profile via
toTaskAgent. A parent session delegates to it, and the child runs the same tools under the same approval policy. Public API: merci-sdk/agents โ loadAgentFile, createAgentToolbox, resolveToolset, applyAgentSpec, toTaskAgent; merci-sdk โ the client, EVENTS, log. Run: node examples/32_agent_files.mjs Needs: PROVIDER plus its credentials in secret.env. @version 0.27.0imported from 'merci-sdk'
EVENTS, initializeClient, loadCredentials, log
3. How the Core Call Works
The entry point, condensed. The full runnable file is linked below.
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { EVENTS, initializeClient, loadCredentials, log } from 'merci-sdk';
import {
applyAgentSpec,
createAgentToolbox,
loadAgentFile,
resolveToolset,
toTaskAgent
} from 'merci-sdk/agents';
import { reportUsage, resolveExample, runMain, trackUsage, warnEmptyAnswer } from './_shared/example-utils.mjs';
import { SCRATCH_DIR } from './_shared/real-tools.mjs';
const WORK_DIR = join(SCRATCH_DIR, '48-agent-files');
const AGENTS_DIR = join(WORK_DIR, 'agents');
const SCRIPTS_DIR = join(AGENTS_DIR, 'scripts');
const WORKSPACE_DIR = join(WORK_DIR, 'workspace');
const NOTE_PATH = join(WORKSPACE_DIR, 'staging-note.md');
/** 40 words, so the house rule below has something real to judge. */
const NOTE = [
'The staging cluster failed twice this week.',
'Both failures began with a configuration drift in the ingress rules.',
'Roll back the last change, pin the controller version, and add a check that compares the live rules with the reviewed manifest.'
].join(' ');
/** The declared script tool: one file, one JSON argv, one JSON answer. */
const WORD_COUNT_SCRIPT = `import { readFileSync } from 'node:fs';
const { path } = JSON.parse(process.argv[2] ?? '{}');
const text = readFileSync(path, 'utf8');
const words = text.trim().split(/\\s+/).filter(Boolean).length;
process.stdout.write(JSON.stringify({ path, words }));
`;
/** The definition a non-coder edits. Groups for beginners, a selector for power users. */
const AGENT_YAML = `apiVersion: merci.dev/v1alpha1
kind: Agent
metadata:
name: reviewer
description: Reviews a short engineering note โ reads it, counts its words, and checks the house style rule.
spec:4. Behavior the Code Fixes
*.agent.yaml definitions: load, freeze, resolve the toolset, run live, delegate live
5. Run the Example
Execute the runnable example from the repository root:
node examples/32_agent_files.mjs
What it does:
*.agent.yaml definitions: load, freeze, resolve the toolset, run live, delegate live