mcp-cli-scripts by jezweb/claude-skills
npx skills add https://github.com/jezweb/claude-skills --skill mcp-cli-scripts状态:生产就绪 最后更新:2026-01-09 依赖项:tsx(开发依赖) 当前版本:tsx@4.21.0
构建 MCP 服务器时,也应创建配套的 CLI 脚本,以提供相同(通常更扩展)的功能,供在终端环境中的 Claude Code 使用。
| 方面 | 远程 MCP (Claude.ai) | CLI 脚本 (Claude Code) |
|---|---|---|
| 上下文 | 结果流经模型上下文窗口 | 结果保留在本地,仅共享相关部分 |
| 文件系统 | 无访问权限 | 完整的读写访问权限 |
| 批量操作 | 一次一个调用 | 可以处理输入文件 |
| 缓存 | 无状态 | 可以在本地缓存结果 |
| 输出 | JSON 格式给模型 | JSON、CSV、表格、文件或标准输出 |
| 链式调用 | 模型编排 | 脚本可以直接管道传递/链式调用 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
mcp-{name}/
├── src/
│ └── index.ts # MCP 服务器(用于 Claude.ai,远程客户端)
├── scripts/
│ ├── {tool-name}.ts # 每个工具一个脚本
│ ├── {another-tool}.ts
│ └── _shared.ts # 共享的身份验证/配置助手(可选)
├── SCRIPTS.md # 记录 Claude Code 可用的脚本
├── package.json
└── README.md
每个脚本做好一件事,对应一个 MCP 工具,但具有扩展功能。
脚本默认将 JSON 输出到标准输出,便于解析。Claude Code 可以读取并使用结果。
// 良好 - 结构化输出
console.log(JSON.stringify({ success: true, data: result }, null, 2));
// 避免 - 非结构化文本(除非请求了 --format text)
console.log("Found 5 results:");
CLI 脚本可以提供对远程 MCP 无意义的功能:
// 输入/输出文件
--input data.csv // 从文件批量处理
--output results.json // 将结果保存到文件
--append // 追加到现有文件
// 缓存
--cache // 使用本地缓存
--cache-ttl 3600 // 缓存 1 小时
--no-cache // 强制重新请求
// 输出格式
--format json|csv|table // 不同的输出格式
--quiet // 抑制非必要输出
--verbose // 额外的调试信息
// 批量操作
--batch // 处理多个项目
--concurrency 5 // 并行处理限制
在所有脚本中使用一致的模式:
# 标准模式
--input <file> # 从文件读取输入
--output <file> # 将输出写入文件
--format <type> # 输出格式
--profile <name> # 身份验证配置文件(用于多账户)
--verbose # 调试输出
--help # 显示用法
脚本应可直接执行:
#!/usr/bin/env npx tsx
/**
* 此脚本功能的简要描述
*
* 用法:
* npx tsx scripts/tool-name.ts <required-arg>
* npx tsx scripts/tool-name.ts --option value
*
* 示例:
* npx tsx scripts/tool-name.ts 12345
* npx tsx scripts/tool-name.ts --input batch.csv --output results.json
*/
✅ 使用 #!/usr/bin/env npx tsx shebang(而非 node 或 ts-node) ✅ 默认将 JSON 输出到标准输出 ✅ 在所有脚本中使用一致的参数模式 ✅ 在 SCRIPTS.md 中记录脚本 ✅ 使用结构化 JSON 处理错误:{ success: false, error: "..." }
❌ 使用 console.log() 输出描述性文本(使用结构化 JSON) ❌ 每个脚本使用不同的参数模式 ❌ 忘记在 SCRIPTS.md 中记录脚本 ❌ 在 shebang 中使用 node 或 ts-node(tsx 处理 ESM+TypeScript)
在以下情况使用 CLI 脚本:
在以下情况使用 MCP 工具:
如果希望在 MCP 和脚本之间共享逻辑,请提取到核心模块:
src/
├── core/
│ ├── lookup.ts # 纯函数,无 I/O 假设
│ └── index.ts # 导出所有核心函数
├── mcp/
│ └── index.ts # MCP 处理程序,从核心导入
└── cli/
└── lookup.ts # CLI 包装器,从核心导入
不过,保持它们分离也是可以的——脚本可能会演变为具有 MCP 无法支持的功能,这没关系。
script-template.ts :完整的 TypeScript 脚本模板,包含参数解析、JSON 输出和文件 I/O 模式。
# 复制到你的项目
cp ~/.claude/skills/mcp-cli-scripts/templates/script-template.ts scripts/new-tool.ts
SCRIPTS-TEMPLATE.md :用于在 MCP 服务器仓库中记录可用脚本的模板。
# 复制到你的项目
cp ~/.claude/skills/mcp-cli-scripts/templates/SCRIPTS-TEMPLATE.md SCRIPTS.md
mcp-cli-scripts.md :脚本文件的修正规则。复制到项目的 .claude/rules/ 目录:
cp ~/.claude/skills/mcp-cli-scripts/rules/mcp-cli-scripts.md .claude/rules/
必需项:
添加到 package.json:
{
"devDependencies": {
"tsx": "^4.21.0"
}
}
{
"devDependencies": {
"tsx": "^4.21.0"
}
}
scripts/ 目录npx tsx scripts/tool-name.ts --help每周安装次数
313
代码仓库
GitHub 星标数
650
首次出现
2026年1月20日
安全审计
安装于
claude-code261
gemini-cli208
opencode205
cursor198
antigravity189
codex179
Status : Production Ready Last Updated : 2026-01-09 Dependencies : tsx (dev dependency) Current Versions : tsx@4.21.0
When building MCP servers, also create companion CLI scripts that provide the same (and often extended) functionality for use with Claude Code in terminal environments.
| Aspect | Remote MCP (Claude.ai) | CLI Scripts (Claude Code) |
|---|---|---|
| Context | Results flow through model context window | Results stay local, only relevant parts shared |
| File System | No access | Full read/write access |
| Batch Operations | One call at a time | Can process files of inputs |
| Caching | Stateless | Can cache results locally |
| Output | JSON to model | JSON, CSV, table, file, or stdout |
| Chaining | Model orchestrates | Scripts can pipe/chain directly |
mcp-{name}/
├── src/
│ └── index.ts # MCP server (for Claude.ai, remote clients)
├── scripts/
│ ├── {tool-name}.ts # One script per tool
│ ├── {another-tool}.ts
│ └── _shared.ts # Shared auth/config helpers (optional)
├── SCRIPTS.md # Documents available scripts for Claude Code
├── package.json
└── README.md
Each script does one thing well, matching an MCP tool but with extended capabilities.
Scripts output JSON to stdout for easy parsing. Claude Code can read and use the results.
// Good - structured output
console.log(JSON.stringify({ success: true, data: result }, null, 2));
// Avoid - unstructured text (unless --format text requested)
console.log("Found 5 results:");
CLI scripts can offer features that don't make sense for remote MCP:
// Input/Output files
--input data.csv // Batch process from file
--output results.json // Save results to file
--append // Append to existing file
// Caching
--cache // Use local cache
--cache-ttl 3600 // Cache for 1 hour
--no-cache // Force fresh request
// Output formats
--format json|csv|table // Different output formats
--quiet // Suppress non-essential output
--verbose // Extra debugging info
// Batch operations
--batch // Process multiple items
--concurrency 5 // Parallel processing limit
Use consistent patterns across all scripts:
# Standard patterns
--input <file> # Read input from file
--output <file> # Write output to file
--format <type> # Output format
--profile <name> # Auth profile (for multi-account)
--verbose # Debug output
--help # Show usage
Scripts should be directly executable:
#!/usr/bin/env npx tsx
/**
* Brief description of what this script does
*
* Usage:
* npx tsx scripts/tool-name.ts <required-arg>
* npx tsx scripts/tool-name.ts --option value
*
* Examples:
* npx tsx scripts/tool-name.ts 12345
* npx tsx scripts/tool-name.ts --input batch.csv --output results.json
*/
✅ Use #!/usr/bin/env npx tsx shebang (not node or ts-node) ✅ Output JSON to stdout by default ✅ Use consistent argument patterns across all scripts ✅ Document scripts in SCRIPTS.md ✅ Handle errors with structured JSON: { success: false, error: "..." }
❌ Use console.log() for prose output (use structured JSON) ❌ Use different argument patterns per script ❌ Forget to document the script in SCRIPTS.md ❌ Use node or ts-node in shebang (tsx handles ESM+TypeScript)
Use CLI scripts when:
Use MCP tools when:
If you want to share logic between MCP and scripts, extract to a core module:
src/
├── core/
│ ├── lookup.ts # Pure function, no I/O assumptions
│ └── index.ts # Export all core functions
├── mcp/
│ └── index.ts # MCP handlers, import from core
└── cli/
└── lookup.ts # CLI wrapper, import from core
However, keeping them separate is also fine - the scripts may evolve to have capabilities the MCP can't support, and that's okay.
script-template.ts : Complete TypeScript script template with argument parsing, JSON output, and file I/O patterns.
# Copy to your project
cp ~/.claude/skills/mcp-cli-scripts/templates/script-template.ts scripts/new-tool.ts
SCRIPTS-TEMPLATE.md : Template for documenting available scripts in an MCP server repo.
# Copy to your project
cp ~/.claude/skills/mcp-cli-scripts/templates/SCRIPTS-TEMPLATE.md SCRIPTS.md
mcp-cli-scripts.md : Correction rules for script files. Copy to .claude/rules/ in projects:
cp ~/.claude/skills/mcp-cli-scripts/rules/mcp-cli-scripts.md .claude/rules/
Required :
Add to package.json:
{
"devDependencies": {
"tsx": "^4.21.0"
}
}
{
"devDependencies": {
"tsx": "^4.21.0"
}
}
scripts/ directory in MCP server projectnpx tsx scripts/tool-name.ts --helpWeekly Installs
313
Repository
GitHub Stars
650
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code261
gemini-cli208
opencode205
cursor198
antigravity189
codex179
Skills CLI 使用指南:AI Agent 技能包管理器安装与管理教程
19,000 周安装