原文标题:Using scripts in skills
从一次性命令(uvx/npx/pipx)到自包含脚本(PEP 723、Deno、Bun、Ruby),再到「为 agent 而设计脚本」的十条工程规范:禁交互、--help、结构化输出、幂等、dry-run、退出码、输出体积控制。
第 1 节
原文标题:One-off commands
当已有包正好能做这件事时,直接在 SKILL.md 里引用它即可,不必自己写 scripts/。很多生态提供了运行时自动解析依赖的工具:
三条经验:锁版本(npx eslint@9.0.0)让行为随时间稳定;声明前置条件(「需要 Node.js 18+」)或写进 compatibility 字段;命令变复杂就下沉为脚本。
第 2 节
原文标题:Referencing scripts from SKILL.md
用相对于技能目录根的相对路径引用随包文件,agent 会自动解析,不需要绝对路径。两个动作缺一不可:
把「有哪些工具」与「什么时候用」放在一起,能让 agent 从「一次性写代码」转向「调用既有能力」——这也是打包脚本的主要收益。
## Available scripts
- scripts/validate.sh — Validates configuration files
- scripts/process.py — Processes input data
## Workflow
1. Run the validation script:
bash scripts/validate.sh "$INPUT_FILE"
2. Process the results:
python3 scripts/process.py --input results.json💡 「有哪些脚本」+「什么时候怎么用」两部分都要写。
第 3 节
原文标题:Self-contained scripts
需要可复用逻辑时,把脚本放进 scripts/ 并在脚本里内联声明依赖,这样 agent 用一条命令就能跑,不需要额外的清单文件或安装步骤:
# /// 标记内的 TOML 块声明依赖,用 uv run scripts/extract.py 执行(uv 会建隔离环境、装依赖、运行);pipx run 也支持;用 PEP 508 规范锁版本("beautifulsoup4>=4.12,<5"),requires-python 约束 Python 版本,uv lock --script 生成锁文件;npm: / jsr: 导入说明符,脚本天然自包含;版本遵循 semver(@1.0.0 精确、@^1.0.0 兼容),依赖全局缓存,--reload 强制重取;带原生插件(node-gyp)的包可能不可用,自带预编译二进制的包最稳;node_modules 时运行时自动安装缺失包,版本直接写在 import 路径里;不需要 package.json,TypeScript 原生可用;注意如果目录树上层存在 node_modules,自动安装会被禁用并回退到 Node 解析;bundler/inline 在脚本内声明 gem(Ruby 2.6+ 自带 bundler);显式锁版本(gem 'nokogiri', '~> 1.16'),因为没有锁文件;工作目录里已有的 Gemfile 或 BUNDLE_GEMFILE 环境变量可能造成干扰。# /// script
# requires-python = ">=3.11"
# dependencies = [
# "beautifulsoup4>=4.12,<5",
# "requests",
# ]
# ///
import requests
from bs4 import BeautifulSoup
# ... script body ...
# Run with: uv run scripts/extract.py💡 用 uv run(或 pipx run)执行,隔离环境 + 自动装依赖。
// Deno — scripts/extract.ts
import { parse } from "npm:node-html-parser@1.0.0";
import { walk } from "jsr:@std/fs@^1.0.0/walk";
// Bun — scripts/extract.ts
import { parse } from "node-html-parser@1.0.0";💡 Deno 用 npm:/jsr: 说明符,Bun 在没有 node_modules 时自动安装并全局缓存。
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "nokogiri", "~> 1.16"
end
# ... script body ...💡 Ruby 2.6+ 自带 bundler;注意没有锁文件,版本要显式锁定。
第 4 节
原文标题:Avoid interactive prompts
这是 agent 执行环境的硬性要求:agent 运行在非交互 shell 里,无法回应 TTY 提示、密码对话或确认菜单。一个阻塞在交互输入上的脚本会无限挂起,把整轮任务卡死。
所有输入都必须通过命令行 flag、环境变量或 stdin 传入。缺失必需参数时,脚本要立刻报错退出并说明用法,而不是「提示输入」。文档的对照示例非常直观:坏写法会打印一个等待输入的提示符然后停住;好写法直接报 Error: --env is required. Options: development, staging, production. 并给出 Usage 行。
# Bad: hangs waiting for input
$ python scripts/deploy.py
Target environment: _
# Good: clear error with guidance
$ python scripts/deploy.py
Error: --env is required. Options: development, staging, production.
Usage: python scripts/deploy.py --env staging --tag v1.2.3💡 非交互环境里,等待输入 = 永久挂起;直接报错 + 给用法才是正确姿势。
第 5 节
原文标题:Document usage with --help
--help 的输出是 agent 了解脚本接口的主要途径。内容应包含:简短描述、可用参数、用法示例。
同时要保持简洁:这段输出会进入 agent 的上下文窗口,和它手上所有其他内容争抢注意力。一个信息密度高、10-20 行的 help 通常比 60 行的完整手册更有效。
Usage: scripts/process.py [OPTIONS] INPUT_FILE
Process input data and produce a summary report.
Options:
--format FORMAT Output format: json, csv, table (default: json)
--output FILE Write output to FILE instead of stdout
--verbose Print progress to stderr
Examples:
scripts/process.py data.csv
scripts/process.py --format csv --output report.csv data.csv💡 简洁、包含默认值与示例——这段文本会占用 agent 的上下文。
第 6 节
原文标题:Write helpful error messages
agent 拿到错误后,错误信息的措辞直接决定它的下一次尝试。含糊的 Error: invalid input 会白白浪费一轮。好的错误信息要回答三件事:哪里错了、期望什么、接下来该试什么。
文档示例:Error: --format must be one of: json, csv, table. Received: "xml"——同时给出了允许集合、实际收到的值,agent 下一步几乎不需要猜测。这与最佳实践里的「plan-validate-execute」是同一思路:错误信息即自我纠正的接口。
Error: --format must be one of: json, csv, table.
Received: "xml"💡 列出允许集合与实际取值,agent 无需再猜。
第 7 节
原文标题:Use structured output
优先输出结构化格式(JSON、CSV、TSV)而不是自由文本:结构化格式既能被 agent 消费,也能被 jq、cut、awk 等标准工具处理,从而可以进入管道组合。用空白对齐的人类可读表格很难被程序解析,属于反面做法。
数据与诊断分离:结构化数据走 stdout,进度信息、警告和其他诊断走 stderr。这样 agent 能拿到干净可解析的输出,同时在需要时仍可访问诊断信息。
# Whitespace-aligned — hard to parse programmatically
NAME STATUS CREATED
my-service running 2025-01-15
# Delimited — unambiguous field boundaries
{"name": "my-service", "status": "running", "created": "2025-01-15"}💡 能用 jq/cut/awk 处理的输出,才能进入管道组合。
第 8 节
原文标题:Further considerations
文档给出的补充清单,几乎每一条都来自「agent 会重试、会误用、会读不懂输出」这个前提:
--dry-run,让 agent 先预览将要发生什么;--help 中说明各码含义;--confirm、--force),按风险等级决定;--offset 之类的参数按需取更多;如果不能分页,则要求调用方传 --output(文件路径或 - 显式选择 stdout)。