@soft-boy/agentschema 0.3.10 → 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Eric Wang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,11 +1,18 @@
1
1
  # agentschema
2
2
 
3
+ [![npm](https://img.shields.io/npm/v/@soft-boy/agentschema)](https://www.npmjs.com/package/@soft-boy/agentschema)
4
+ [![downloads](https://img.shields.io/npm/dm/@soft-boy/agentschema)](https://www.npmjs.com/package/@soft-boy/agentschema)
5
+ [![node](https://img.shields.io/node/v/@soft-boy/agentschema)](https://nodejs.org)
6
+ [![license](https://img.shields.io/npm/l/@soft-boy/agentschema)](LICENSE)
7
+
3
8
  Define an AI-Agent as a folder of Markdown files.
4
9
 
5
10
  ```
6
11
  my-agent/
7
12
  ├── agent.md # the agent
8
13
  ├── env.md # the environment it runs in
14
+ ├── system_prompt/
15
+ │ └── rules.md # more of the system prompt
9
16
  ├── tools/
10
17
  │ ├── agent_toolset_20260401.md # the pre-built tools to enable
11
18
  │ └── get_weather.md # a custom tool
@@ -23,6 +30,26 @@ my-agent/
23
30
 
24
31
  **Status:** v0.2 draft. v0.2 breaks from the Claude Managed Agents schema: tools move out of `agent.md` and into `tools/`.
25
32
 
33
+ ## Table of contents
34
+
35
+ - [Philosophy](#philosophy)
36
+ - [Install](#install)
37
+ - [Authentication](#authentication)
38
+ - [Getting started](#getting-started)
39
+ - [Specification](#specification)
40
+ - [`agent.md`](#agentmd)
41
+ - [`system_prompt/`](#system_prompt)
42
+ - [`env.md`](#envmd)
43
+ - [`tools/`](#tools)
44
+ - [`skills/`](#skills)
45
+ - [`vaults/`](#vaults)
46
+ - [`channels/`](#channels)
47
+ - [`scheduled/`](#scheduled)
48
+ - [Usage](#usage)
49
+ - [Library](#library)
50
+ - [Development](#development)
51
+ - [License](#license)
52
+
26
53
  ## Philosophy
27
54
 
28
55
  1. **One folder, one agent.** A folder defines one agent, its environment, and what it needs to run. Sessions and each user's own credentials come from whatever runs the agent.
@@ -63,6 +90,10 @@ agentschema run my-agent
63
90
 
64
91
  The frontmatter uses the fields of a [Managed Agents agent](https://platform.claude.com/docs/en/managed-agents/agent-setup), and `name` and `model` are required. The body is the system prompt.
65
92
 
93
+ ### `system_prompt/`
94
+
95
+ A long system prompt can be split into files here, at any depth. They're added after `agent.md`'s body, in the order `tree` lists them, with a blank line between each one, so number them to keep them in order, like `01-role.md` and `02-rules.md`. Each file is taken as written. See [System prompt](docs/system_prompt.md).
96
+
66
97
  ### `env.md`
67
98
 
68
99
  The frontmatter uses the fields of a [Managed Agents environment](https://platform.claude.com/docs/en/managed-agents/environments). The body is a note for people, and isn't sent anywhere.
@@ -208,3 +239,7 @@ npm install
208
239
  npm test # unit tests; deploy runs against a fake API client
209
240
  npm link # makes `agentschema` available globally from this checkout
210
241
  ```
242
+
243
+ ## License
244
+
245
+ [MIT](LICENSE)
package/lib/channels.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { pathsUnder, textOf } from "./files.js";
2
2
  import { AGENT_FILE } from "./instance.js";
3
+ import { systemPromptOf } from "./system-prompt.js";
3
4
  import { FileError, parseMarkdown } from "./markdown.js";
4
5
  import { AGENTBOOK, MCP_TOOLSET } from "./tools.js";
5
6
 
@@ -152,7 +153,7 @@ export function readChannels(files) {
152
153
 
153
154
  // Errors and warnings for the channel files, in check's format. `agent` and `tools` are what the channel
154
155
  // is attached to, and `toolsets` is the catalog behind `type: agentbook` files.
155
- export function checkChannels(channels, { agent = { body: "" }, tools = [], toolsets } = {}, errors = [], warnings = []) {
156
+ export function checkChannels(channels, { agent = { body: "" }, systemPrompt = [], tools = [], toolsets } = {}, errors = [], warnings = []) {
156
157
  const unasked = toolsThatRunUnasked(tools, toolsets);
157
158
  const identities = new Map();
158
159
  // Where each place in a `send_to` came from: only one bot can post there, so it's clear which one does.
@@ -188,7 +189,7 @@ export function checkChannels(channels, { agent = { body: "" }, tools = [], tool
188
189
  else if (!SHARED_FIELDS.includes(key) && !Object.hasOwn(type.fields, key)) err(`unknown field \`${key}\``);
189
190
  }
190
191
 
191
- if (channelSystem(agent, channel).length > MAX_SYSTEM_PROMPT) {
192
+ if (channelSystem({ agent, systemPrompt }, channel).length > MAX_SYSTEM_PROMPT) {
192
193
  err(`with ${AGENT_FILE}'s system prompt, this comes to more than ${MAX_SYSTEM_PROMPT.toLocaleString("en-US")} characters; the limit is the API's`);
193
194
  }
194
195
 
@@ -331,4 +332,7 @@ export function channelSettings({ data }) {
331
332
 
332
333
  // The system prompt for a conversation on this channel: the agent's, then the channel's own instructions.
333
334
  // A session can override `system` for itself, so a channel doesn't need its own copy of the agent.
334
- export const channelSystem = (agent, channel) => [agent.body, channel.body].filter(Boolean).join("\n\n");
335
+ // `instance` is what checkFiles returns, so the agent's prompt includes system_prompt/. An agent alone,
336
+ // as before, still works, with only agent.md's body.
337
+ export const channelSystem = (instance, channel) =>
338
+ [instance.agent ? systemPromptOf(instance) : instance.body, channel.body].filter(Boolean).join("\n\n");
package/lib/check.js CHANGED
@@ -2,6 +2,7 @@ import { checkChannels, readChannels } from "./channels.js";
2
2
  import { textOf } from "./files.js";
3
3
  import { AGENT_FILE, ENV_FILE, readInstance } from "./instance.js";
4
4
  import { checkScheduled, readScheduled } from "./scheduled.js";
5
+ import { SYSTEM_PROMPT_DIR, readSystemPrompt, systemPromptOf } from "./system-prompt.js";
5
6
  import { MAX_UPLOAD_BYTES, SKILLS_DIR, readSkills } from "./skills.js";
6
7
  import { MCP_TOOLSET, TOOLS_DIR, checkTools, readTools } from "./tools.js";
7
8
  import { CREDENTIAL_TYPES, ENV_VAR, SECRET_FIELDS, TOKEN_ENDPOINT_AUTH, VAULT_FILE, get, readVaults } from "./vaults.js";
@@ -36,10 +37,12 @@ export function checkFiles(files, options = {}) {
36
37
  const { skills, errors: skillErrors } = readSkills(files);
37
38
  const { channels, errors: channelErrors } = readChannels(files);
38
39
  const { tasks: scheduled, errors: scheduledErrors } = readScheduled(files);
39
- errors.push(...toolErrors, ...vaultErrors, ...skillErrors, ...channelErrors, ...scheduledErrors);
40
+ const { parts: systemPrompt, errors: systemPromptErrors } = readSystemPrompt(files);
41
+ errors.push(...toolErrors, ...vaultErrors, ...skillErrors, ...channelErrors, ...scheduledErrors, ...systemPromptErrors);
40
42
  const warnings = [];
41
43
 
42
44
  if (agent) checkAgent(agent, errors, warnings);
45
+ if (agent) checkSystemPromptLength({ agent, systemPrompt }, errors);
43
46
  if (env) checkEnv(env, errors, warnings);
44
47
  checkTools(tools, errors, warnings, options);
45
48
  if (agent) checkMcpServers(agent, tools, errors);
@@ -49,19 +52,30 @@ export function checkFiles(files, options = {}) {
49
52
  if (declaredSkills + skills.length > 500) errors.push(`${declaredSkills + skills.length} skills; the limit is 500`);
50
53
  for (const vault of vaults) checkVault(vault, errors, warnings);
51
54
  checkEnvVarCredentials(vaults, env, warnings);
52
- checkChannels(channels, { agent: agent ?? undefined, tools, toolsets: options.toolsets }, errors, warnings);
55
+ checkChannels(channels, { agent: agent ?? undefined, systemPrompt, tools, toolsets: options.toolsets }, errors, warnings);
53
56
  checkScheduled(scheduled, { tools }, errors, warnings);
54
57
 
55
58
  const vaultFiles = vaults.flatMap((v) => [...(v.file ? [[`${v.path}/${VAULT_FILE}`, v.file]] : []), ...v.credentials.map((c) => [c.file, c])]);
56
59
  const skillTexts = skills.flatMap((s) => s.files.map((f) => [`${s.path}/${f.rel}`, { raw: textOf(f.bytes) }]));
57
- for (const [file, parsed] of [[AGENT_FILE, agent], [ENV_FILE, env], ...tools.map((t) => [t.file, t]), ...vaultFiles, ...skillTexts, ...channels.map((c) => [c.file, c]), ...scheduled.map((t) => [t.file, t])]) {
60
+ for (const [file, parsed] of [[AGENT_FILE, agent], [ENV_FILE, env], ...tools.map((t) => [t.file, t]), ...vaultFiles, ...skillTexts, ...channels.map((c) => [c.file, c]), ...scheduled.map((t) => [t.file, t]), ...systemPrompt.map((p) => [p.file, { raw: p.text }])]) {
58
61
  if (!parsed) continue;
59
62
  for (const [pattern, what] of SECRET_PATTERNS) {
60
63
  if (pattern.test(parsed.raw)) errors.push(`${file}: looks like it contains ${what}; keep secrets out of these files`);
61
64
  }
62
65
  }
63
66
 
64
- return { errors, warnings, instance: agent && env ? { agent, env, tools, vaults, skills, channels, scheduled } : null };
67
+ return { errors, warnings, instance: agent && env ? { agent, env, tools, vaults, skills, channels, scheduled, systemPrompt } : null };
68
+ }
69
+
70
+ const MAX_SYSTEM_PROMPT = 100_000;
71
+
72
+ // The system prompt is agent.md's body and system_prompt/'s files together, and the API's limit is on
73
+ // all of it.
74
+ function checkSystemPromptLength(instance, errors) {
75
+ const { length } = systemPromptOf(instance);
76
+ if (length <= MAX_SYSTEM_PROMPT) return;
77
+ const where = instance.systemPrompt.length > 0 ? `${AGENT_FILE} and ${SYSTEM_PROMPT_DIR}/` : AGENT_FILE;
78
+ errors.push(`${where}: the system prompt is ${length.toLocaleString("en-US")} characters; the limit is ${MAX_SYSTEM_PROMPT.toLocaleString("en-US")}`);
65
79
  }
66
80
 
67
81
  function checkAgent({ data, body }, errors, warnings) {
@@ -76,7 +90,6 @@ function checkAgent({ data, body }, errors, warnings) {
76
90
  }
77
91
 
78
92
  if ("system" in data) err("`system` is not allowed in frontmatter; the Markdown body is the system prompt");
79
- if (body.length > 100_000) err(`system prompt is ${body.length} characters; the limit is 100,000`);
80
93
 
81
94
  if (data.description !== undefined && data.description !== null) {
82
95
  if (typeof data.description !== "string") err("`description` must be a string");
package/lib/index.js CHANGED
@@ -7,6 +7,7 @@ export { AGENT_FILE, ENV_FILE, agentBody, envBody, readInstance } from "./instan
7
7
  export { FileError, editFrontmatter, parseMarkdown } from "./markdown.js";
8
8
  export { MODELS, NETWORKING, builtInFile, defaultAnswers, scaffold } from "./scaffold.js";
9
9
  export { SCHEDULED_DIR, cronProblem, isTimeZone, readScheduled, taskBody, taskKey, taskSettings } from "./scheduled.js";
10
+ export { SYSTEM_PROMPT_DIR, readSystemPrompt, systemPromptOf } from "./system-prompt.js";
10
11
  export { MAX_UPLOAD_BYTES, SKILLS_DIR, SKILL_FILE, readSkills, skillKey, skillRefs } from "./skills.js";
11
12
  export { agentMessageText, textStream } from "./stream.js";
12
13
  export {
package/lib/instance.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { textOf } from "./files.js";
2
2
  import { FileError, parseMarkdown } from "./markdown.js";
3
+ import { systemPromptOf } from "./system-prompt.js";
3
4
  import { compileTools } from "./tools.js";
4
5
 
5
6
  export const AGENT_FILE = "agent.md";
@@ -30,12 +31,15 @@ export function readInstance(files) {
30
31
 
31
32
  // The bodies sent to POST /v1/agents and POST /v1/environments.
32
33
  // The agent's tools come from tools/, compiled into the API's `tools` list. `skills` are the
33
- // references to the skills deployed from skills/, added to any in agent.md's frontmatter.
34
+ // references to the skills deployed from skills/, added to any in agent.md's frontmatter. The system
35
+ // prompt is agent.md's body, then system_prompt/'s files.
34
36
  // `options.toolsets` is the catalog behind `type: agentbook` tool files.
35
- export function agentBody({ agent, tools }, skills = [], options = {}) {
37
+ export function agentBody(instance, skills = [], options = {}) {
38
+ const { agent, tools } = instance;
36
39
  const body = { ...agent.data, tools: compileTools(tools, options) };
37
40
  if (skills.length > 0) body.skills = [...(agent.data.skills ?? []), ...skills];
38
- if (agent.body) body.system = agent.body;
41
+ const system = systemPromptOf(instance);
42
+ if (system) body.system = system;
39
43
  return body;
40
44
  }
41
45
 
@@ -0,0 +1,32 @@
1
+ import { pathsUnder, textOf } from "./files.js";
2
+
3
+ // A long system prompt can be split into files in system_prompt/, which go after agent.md's body. Every
4
+ // file in the folder counts, at any depth, in the order `tree` lists them, so the folder reads top to
5
+ // bottom the way the prompt does: each folder's entries sorted by name, character by character, with a
6
+ // folder's files right where the folder sorts. Each file is taken as written, with no frontmatter.
7
+ export const SYSTEM_PROMPT_DIR = "system_prompt";
8
+
9
+ // Text that came from something other than a text file: decoding bytes that aren't UTF-8 leaves
10
+ // replacement characters, and binary files have NULs.
11
+ const isBinary = (text) => text.includes("\uFFFD") || text.includes("\u0000");
12
+
13
+ // Reads system_prompt/. Returns { parts, errors }, where each part is { file, text } in tree order.
14
+ // No system_prompt/ folder means no parts.
15
+ export function readSystemPrompt(files) {
16
+ if (SYSTEM_PROMPT_DIR in files) return { parts: [], errors: [`${SYSTEM_PROMPT_DIR}: not a directory`] };
17
+ const parts = [];
18
+ const errors = [];
19
+ // pathsUnder already lists them in tree's order.
20
+ for (const file of pathsUnder(files, SYSTEM_PROMPT_DIR)) {
21
+ const text = textOf(files[file]);
22
+ if (isBinary(text)) errors.push(`${file}: isn't a text file, so it can't be part of the system prompt`);
23
+ else parts.push({ file, text });
24
+ }
25
+ return { parts, errors };
26
+ }
27
+
28
+ // The whole system prompt: agent.md's body, then each file in system_prompt/, with a blank line between
29
+ // them. Empty files add nothing.
30
+ export function systemPromptOf({ agent, systemPrompt = [] }) {
31
+ return [agent?.body ?? "", ...systemPrompt.map((part) => part.text.trim())].filter(Boolean).join("\n\n");
32
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soft-boy/agentschema",
3
- "version": "0.3.10",
3
+ "version": "0.3.12",
4
4
  "description": "Markdown based AI agent schema",
5
5
  "type": "module",
6
6
  "bin": {
package/src/folder.js CHANGED
@@ -5,10 +5,11 @@ import { editFrontmatter } from "../lib/markdown.js";
5
5
  import { AGENT_FILE, ENV_FILE } from "../lib/instance.js";
6
6
  import { SCHEDULED_DIR } from "../lib/scheduled.js";
7
7
  import { SKILLS_DIR } from "../lib/skills.js";
8
+ import { SYSTEM_PROMPT_DIR } from "../lib/system-prompt.js";
8
9
  import { TOOLS_DIR } from "../lib/tools.js";
9
10
  import { VAULTS_DIR } from "../lib/vaults.js";
10
11
 
11
- const FOLDERS = [TOOLS_DIR, SKILLS_DIR, VAULTS_DIR, CHANNELS_DIR, SCHEDULED_DIR];
12
+ const FOLDERS = [TOOLS_DIR, SKILLS_DIR, VAULTS_DIR, CHANNELS_DIR, SCHEDULED_DIR, SYSTEM_PROMPT_DIR];
12
13
  const TOP_LEVEL_FILES = [AGENT_FILE, ENV_FILE];
13
14
 
14
15
  // Why `dir` can't be read as an agent folder, or null when it can.
package/src/oauth.js CHANGED
@@ -18,8 +18,12 @@ export async function discover(serverUrl, { fetch = globalThis.fetch } = {}) {
18
18
  const url = new URL(serverUrl);
19
19
  const path = url.pathname.replace(/\/$/, "");
20
20
 
21
- // Protected resource metadata (RFC 9728): which authorization server the MCP server trusts.
21
+ // Protected resource metadata (RFC 9728): which authorization server the MCP server trusts. The
22
+ // server can say where that is when it turns away a request with no token, and some put it where
23
+ // the well-known addresses below wouldn't find it, so its word comes first.
24
+ const hinted = await resourceMetadataHint(serverUrl, fetch);
22
25
  const resourceMeta =
26
+ (hinted && (await getJSON(hinted, fetch))) ||
23
27
  (path && (await getJSON(`${url.origin}/.well-known/oauth-protected-resource${path}`, fetch))) ||
24
28
  (await getJSON(`${url.origin}/.well-known/oauth-protected-resource`, fetch));
25
29
  const issuer = resourceMeta?.authorization_servers?.[0] ?? url.origin;
@@ -53,6 +57,25 @@ export async function discover(serverUrl, { fetch = globalThis.fetch } = {}) {
53
57
  };
54
58
  }
55
59
 
60
+ // Where the MCP server's 401 says its protected resource metadata is: the resource_metadata
61
+ // parameter of its WWW-Authenticate header. Returns null when it doesn't say.
62
+ async function resourceMetadataHint(serverUrl, fetch) {
63
+ try {
64
+ const res = await fetch(serverUrl, {
65
+ method: "POST",
66
+ headers: { "content-type": "application/json", accept: "application/json, text/event-stream" },
67
+ body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "agentschema", version: "0" } } }),
68
+ signal: AbortSignal.timeout(10_000),
69
+ });
70
+ if (res.status !== 401) return null;
71
+ const match = /\bresource_metadata\s*=\s*(?:"([^"]*)"|([^\s,]+))/i.exec(res.headers.get("www-authenticate") ?? "");
72
+ const hint = match?.[1] ?? match?.[2];
73
+ return hint && URL.canParse(hint) ? hint : null;
74
+ } catch {
75
+ return null;
76
+ }
77
+ }
78
+
56
79
  async function getJSON(url, fetch) {
57
80
  try {
58
81
  const res = await fetch(url, { headers: { accept: "application/json" } });
package/src/session.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { join } from "node:path";
2
2
  import { AGENT_FILE, ENV_FILE, agentBody, envBody } from "../lib/instance.js";
3
+ import { SYSTEM_PROMPT_DIR } from "../lib/system-prompt.js";
3
4
  import { skillKey, skillRefs } from "../lib/skills.js";
4
5
  import { TOOLS_DIR } from "../lib/tools.js";
5
6
  import { credentialKey, vaultKey } from "../lib/vaults.js";
@@ -28,7 +29,7 @@ export async function openSession(dir, { client, sessionId }) {
28
29
  const { instance, errors } = check(dir);
29
30
  if (instance && errors.length === 0) {
30
31
  if (hashBody(agentBody(instance, skillRefs(instance.skills, lock))) !== agent.hash) {
31
- notes.push(`${AGENT_FILE} or ${TOOLS_DIR}/ has changed since the last deploy`);
32
+ notes.push(`${AGENT_FILE}, ${SYSTEM_PROMPT_DIR}/ or ${TOOLS_DIR}/ has changed since the last deploy`);
32
33
  }
33
34
  for (const skill of instance.skills) {
34
35
  if (hashBody(skillFingerprint(skill)) !== lock.resources[skillKey(skill)]?.hash) notes.push(`${skill.path} has changed since the last deploy`);