Skip to main content
Glama
README.md
<div align="center">

# SkillForge MCP

**把你的 Agent Skills 目录,变成一个可以被任何 MCP 客户端调用的技能库。**

本地优先 · 渐进式披露 · 零配置启动 · 7 个 MCP 工具 · 双传输(stdio / Streamable HTTP)

[![CI](https://github.com/chengducloud/skillforge-mcp/actions/workflows/ci.yml/badge.svg)](https://github.com/chengducloud/skillforge-mcp/actions/workflows/ci.yml)
[![Node](https://img.shields.io/badge/node-%3E%3D20-3c873a)](https://nodejs.org)
[![MCP](https://img.shields.io/badge/MCP-2025--06--18-6b4fbb)](https://modelcontextprotocol.io)
[![License](https://img.shields.io/badge/license-MIT-blue)](LICENSE)
[![Tests](https://img.shields.io/badge/tests-28%20passing-brightgreen)](test/)

</div>

---

## 目录

- [这个项目解决什么问题](#这个项目解决什么问题)
- [核心概念:三级渐进式披露](#核心概念三级渐进式披露)
- [架构](#架构)
- [功能清单](#功能清单)
- [快速开始](#快速开始)
- [接入 MCP 客户端](#接入-mcp-客户端)
- [MCP 工具参考](#mcp-工具参考)
- [资源与提示词](#资源与提示词)
- [SKILL.md 规范](#skillmd-规范)
- [CLI 参考](#cli-参考)
- [配置](#配置)
- [HTTP 部署](#http-部署)
- [项目结构](#项目结构)
- [开发与测试](#开发与测试)
- [设计取舍](#设计取舍)
- [路线图](#路线图)
- [FAQ](#faq)
- [License](#license)

---

## 这个项目解决什么问题

Agent Skills(技能)已经成了给 LLM 注入专业流程的主流做法:一个文件夹、一份 `SKILL.md`、若干参考资料和脚本。但一旦技能数量超过五六个,两个问题立刻出现:

1. **上下文装不下。** 把所有 `SKILL.md` 全文塞进 system prompt,十个技能就能吃掉两三万 token,而且 90% 的内容和当前任务无关。
2. **模型不知道有哪些技能。** 没有目录,模型只能靠猜;有了目录但描述写得差,模型还是选错。

SkillForge MCP 就是这两件事的答案:它把本地技能目录**扫描成索引**,再通过 **MCP(Model Context Protocol)**只暴露「名字 + 一句话描述 + token 成本」的目录。模型先用目录选技能,再用 `skills_get` 加载被选中那一个的正文,需要细节时再用 `skills_read_resource` 读单个参考文件。

结果:**3 个技能总共 1649 token 的正文,目录只占约 300 token。** 规模越大,差距越明显。

它同时是一套**技能基础设施**:

- 一个**校验器**(`skills_validate` / `skillforge validate`),按 Agent Skills 的写作规则做 lint,把「描述没写触发条件」「正文太大」「链接指向不存在的文件」这类问题在提交前拦下来。
- 一个**脚手架**(`skillforge init`),生成符合规范、能直接通过校验的技能骨架。
- 一份**可 diff 的索引**(`skillforge index`),让技能库的变更进入 code review 流程。

> 一句话:**这是 Agent Skills 的 registry + linter + MCP gateway。**

## 核心概念:三级渐进式披露

这是整个项目唯一重要的设计。所有工具、所有返回结构都围绕它组织。

```text
第 0 级  skill://index          目录:名字 + 描述 + token 成本        ~40 token/技能
   │
   ├─ 模型决定「用哪个」
   ▼
第 1 级  skills_get             选中技能的 SKILL.md 正文 + 文件清单    ~400-800 token
   │
   ├─ 正文说「细节见 references/xxx.md」
   ▼
第 2 级  skills_read_resource   单个参考文件 / 脚本 / 模板            按需,通常 1-2 个
```

**为什么必须这样分层:**

- 目录必须**便宜**。`skills_list` 永远不返回正文——这不是省事,是硬约束。一旦目录带上正文,整个方案退化回「全量塞 prompt」。
- 正文应该是**路由器**,不是百科。`SKILL.md` 只写「什么时候用、按什么顺序做、去哪找细节」,长表格和示例沉到 `references/`。
- 细节必须**可寻址**。模型只有在正文明确指向某个文件时才去读,避免「一次性 dump 整棵参考树」这种把上下文烧光的反模式。

`skills_get` 的返回里带 `truncated` 和 `hint`,正文超限时明确告诉模型「被截断了,去读具体文件」——而不是悄悄给它半截指令。

## 架构

```text
┌──────────────────────────────────────────────────────────────────┐
│  MCP Client                                                      │
│  Claude Desktop · Cursor · VS Code · Zed · 自研 Agent            │
└───────────────┬──────────────────────────────────────────────────┘
                │  JSON-RPC over stdio 或 Streamable HTTP
┌───────────────▼──────────────────────────────────────────────────┐
│  Transport Layer                                                 │
│    transports/stdio.ts        stdout=协议帧, stderr=日志          │
│    transports/http.ts         stateless, 每请求一个 protocol 对象 │
└───────────────┬──────────────────────────────────────────────────┘
                │
┌───────────────▼──────────────────────────────────────────────────┐
│  Protocol Surface           server.ts → buildServer()            │
│    tools/       7 个工具,只读,返回 JSON 文本                    │
│    resources/   skill://index · skill://{name}/SKILL.md · files   │
│    prompts/     use-skill · author-skill                         │
└───────────────┬──────────────────────────────────────────────────┘
                │
┌───────────────▼──────────────────────────────────────────────────┐
│  Core                                                            │
│    registry.ts   扫描 · mtime 增量缓存 · 排序搜索 · 路径防护      │
│    frontmatter.ts  YAML 解析与容错归一化                          │
│    validator.ts   规则引擎 → error/warning/info                   │
│    watcher.ts     chokidar + 300ms 去抖                           │
└───────────────┬──────────────────────────────────────────────────┘
                │
┌───────────────▼──────────────────────────────────────────────────┐
│  Skills on disk                                                  │
│    skills/code-review/      SKILL.md + references/               │
│    skills/release-notes/    SKILL.md + references/ + scripts/    │
│    skills/api-design/       SKILL.md + references/               │
│    ~/.agent-skills/         你的个人技能库(可选)                │
└──────────────────────────────────────────────────────────────────┘
```

**关键实现点:**

| 位置 | 做法 | 原因 |
| --- | --- | --- |
| `logger.ts` | 一切日志走 stderr | stdio 传输下 stdout 是 JSON-RPC 通道,一个 `console.log` 就会污染协议流 |
| `registry.ts` | 以 `SKILL.md` 的 mtime 做增量 | 每次请求重新读全量文件会拖慢 HTTP 场景;mtime 命中直接复用对象 |
| `registry.ts` | 同名技能「后者覆盖前者」 | 允许「内置技能 + 个人覆盖」的常见组合 |
| `registry.ts` | `readResource` 做 realpath 边界检查 | 防止 `../` 穿越读到技能目录外的文件 |
| `http.ts` | stateless 模式 | 只读目录服务不需要 session,避免断线重连和内存泄漏 |
| `search` | 可解释的打分 | 分数和 `matchedOn` 一起返回,模型能判断「为什么推荐它」 |

## 功能清单

- **多根目录扫描**:`--root` 可重复,支持 `~` 展开,支持最多 3 层嵌套(`skills/category/name/`)。
- **增量索引**:mtime 未变则复用,扫描 3 个技能约 60ms。
- **可解释搜索**:name > tag > description > body 加权,返回 `score` / `matchedOn` / `snippet`。
- **路径防护**:`skills_read_resource` 拒绝任何逃逸技能目录的路径。
- **二进制友好**:读到图片/PDF 时返回元信息而非乱码。
- **多格式容错**:frontmatter 损坏不致命,退化为「整篇当正文」;缺 `name` 用目录名;缺 `description` 取第一段散文。
- **校验器**:17 条规则,分 error / warning / info,退出码可进 CI。
- **双传输**:stdio(桌面客户端)+ Streamable HTTP(容器/远程),同一份核心代码。
- **文件监听**:`--watch` 下编辑器保存后 300ms 内自动重建索引。
- **零运行时依赖负担**:5 个直接依赖,无数据库、无守护进程、无遥测。

## 快速开始

```bash
git clone https://github.com/chengducloud/skillforge-mcp.git
cd skillforge-mcp
npm install
npm run build

# 看一眼它索引到了什么
node dist/cli.js list
node dist/cli.js search "review a pull request"
node dist/cli.js validate
```

期望输出:

```text
NAME           VERSION  TOKENS  FILES  DESCRIPTION
-------------  -------  ------  -----  ------------------------------------------
api-design     1.0.0    696     1      Design or critique a REST/HTTP API — resour…
code-review    1.0.0    498     2      Review a diff or pull request for correctness…
release-notes  1.1.0    455     2      Turn merged pull requests or commit history i…

3 skill(s) across 2 root(s).
```

跑一次真实的 MCP 往返(会拉起子进程、列工具、读资源、取 prompt):

```bash
npm run example
```

## 接入 MCP 客户端

### Claude Desktop

`~/Library/Application Support/Claude/claude_desktop_config.json`(macOS)
`%APPDATA%\Claude\claude_desktop_config.json`(Windows)

```json
{
  "mcpServers": {
    "skillforge": {
      "command": "node",
      "args": ["/absolute/path/to/skillforge-mcp/dist/index.js"],
      "env": {
        "SKILLFORGE_ROOTS": "/absolute/path/to/skillforge-mcp/skills:/Users/me/.agent-skills",
        "SKILLFORGE_LOG_LEVEL": "warn"
      }
    }
  }
}
```

### Cursor

`.cursor/mcp.json`(项目级)或 `~/.cursor/mcp.json`(全局):

```json
{
  "mcpServers": {
    "skillforge": {
      "command": "npx",
      "args": ["-y", "skillforge-mcp"],
      "env": { "SKILLFORGE_ROOTS": "skills:~/.agent-skills" }
    }
  }
}
```

### VS Code

`.vscode/mcp.json`:

```json
{
  "servers": {
    "skillforge": {
      "type": "stdio",
      "command": "node",
      "args": ["${workspaceFolder}/dist/index.js"],
      "env": { "SKILLFORGE_ROOTS": "${workspaceFolder}/skills" }
    }
  }
}
```

### 远程 / 容器(HTTP)

```bash
docker build -t skillforge-mcp .
docker run --rm -p 7331:7331 -v "$PWD/skills:/app/skills:ro" skillforge-mcp
```

客户端配置为 `http://localhost:7331/mcp`(Streamable HTTP,stateless)。

> **Windows 提示**:`SKILLFORGE_ROOTS` 用系统路径分隔符(`;`),在 JSON 里写成 `C:\\\\skills;C:\\\\Users\\\\me\\\\.agent-skills`,或直接传 `--root` 参数避免转义。

## MCP 工具参考

7 个工具,全部只读。唯一的「写」是 `skills_reload`,它只重建内存索引,不碰磁盘。

### `skills_list`

目录。**不返回正文**,这是硬性设计约束。

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `tag` | string | 只返回带该标签的技能(大小写不敏感精确匹配) |
| `limit` | number | 默认 100 |
| `includeFiles` | boolean | 是否附带文件清单,默认 false |

返回:`total` / `returned` / `roots` / `skills[]`(name, description, version, tags, allowedTools, files, tokens, path)/ `hint`。

### `skills_search`

跨 name / description / tag / body 的加权检索。

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `query` | string(必填) | 自然语言或关键词 |
| `limit` | number | 默认 5 |

返回每条命中带 `score`、`matchedOn`(name/tag/description/body)、`snippet`。空结果时 `hint` 会建议改用 `skills_list`。

### `skills_get`

加载选中技能的正文(第 1 级)。

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `name` | string(必填) | 技能名或目录名 |
| `maxChars` | number | 截断长度,默认取 `SKILLFORGE_MAX_BODY_CHARS`(24000) |

返回正文 + `manifest`(references/scripts/assets 清单)+ `truncated` + 下一步提示。

### `skills_read_resource`

读技能内的单个文件(第 2 级)。

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `name` | string(必填) | 技能名 |
| `path` | string(必填) | 相对技能目录的路径,如 `references/severity.md` |
| `maxChars` | number | 默认 60000 |

路径逃逸会返回 `isError: true`,不会读到目录外的东西。

### `skills_validate`

| 参数 | 类型 | 说明 |
| --- | --- | --- |
| `name` | string | 省略则校验全部 |

返回 `ValidationReport`:`ok` / `errors` / `warnings` / `infos` / `issues[]`(level, code, message, file)。

### `skills_stats`

无参数。返回技能数、文件数、总字节、总 token、按类型分布、扫描时间、生效配置。

### `skills_reload`

无参数。重新扫描并返回变更摘要(added / updated / removed / unchanged)。

## 资源与提示词

**Resources**(可被客户端直接订阅/读取):

| URI | 内容 |
| --- | --- |
| `skill://index` | 完整目录 JSON(技能列表 + 统计) |
| `skill://{name}/SKILL.md` | 单技能正文 |
| `skill://{name}/files/{path}` | 技能内任意文件 |

另有 Resource Templates,客户端可以按模板构造 URI 做补全。

**Prompts**:

| 名称 | 参数 | 用途 |
| --- | --- | --- |
| `use-skill` | `name`(必填), `task` | 把指定技能的正文直接注入对话并说明任务,适合不支持自动选择技能的客户端 |
| `author-skill` | `topic`(必填), `tools` | 引导模型按规范新写一个技能,最后要求它自己调 `skills_validate` 修复 |

## SKILL.md 规范

```markdown
---
name: release-notes                  # 必填,kebab-case,<= 64 字符
description: Turn merged PRs into user-facing release notes. Use when the user asks for a changelog.   # 必填,<= 1024
version: 1.1.0                       # 建议 semver
license: MIT
tags: [release, changelog]           # 或 "release, changelog"
allowed-tools: Read Grep Bash        # 或 YAML 列表
metadata:                            # 任意扩展字段
  language: en
---

# Release notes

## When to use
...

## Workflow
1. ...
2. ...

## References
- `references/template.md` — 骨架与范例
```

**目录约定:**

```text
my-skill/
├── SKILL.md          # 必须
├── references/       # 深度材料:长表格、示例库、规范原文
├── scripts/          # 确定性脚本:collect.sh、render.py
├── assets/           # 模板、fixture
└── (任意其他文件)     # 会被索引,但按 other 分类
```

**校验规则(17 条):**

| code | 级别 | 触发条件 |
| --- | --- | --- |
| `frontmatter/missing` | error | 文件不以 `---` 开头 |
| `name/missing` | warning | 无 `name`,回退到目录名 |
| `name/invalid-format` | error | 非 kebab-case |
| `name/too-long` | error | 超过 64 字符 |
| `name/dir-mismatch` | warning | `name` 与目录名不一致 |
| `name/case` | warning | `name` 含大写 |
| `description/missing` | error | 缺 `description` |
| `description/too-short` | warning | 少于 20 字符 |
| `description/too-long` | error | 超过 1024 字符 |
| `description/no-trigger` | info | 没提「什么时候用」 |
| `version/invalid` | warning | `version` 不是 semver |
| `body/empty` | error | 正文为空 |
| `body/too-large` | warning | 正文超过约 5000 token |
| `body/no-title` | info | 正文没有一级标题 |
| `metadata/no-version` | info | 未声明版本,默认 `0.0.0` |
| `link/broken` | warning | 相对链接指向不存在的文件 |
| `content/todo` | info | 残留 TODO / FIXME |

`skillforge validate --strict` 会把 warning 也当成失败,适合放进 pre-commit。

更多写法建议见 [docs/SKILL-FORMAT.md](docs/SKILL-FORMAT.md)。

## CLI 参考

```bash
skillforge serve [--http] [-p 7331] [--root <dir...>] [--watch]   # 启动服务(默认 stdio)
skillforge list [--json]                                         # 目录(--json 输出完整索引)
skillforge search "query" [-n 5]                                 # 搜索
skillforge show <name>                                           # 打印 SKILL.md
skillforge validate [dir] [--strict]                             # 校验
skillforge init <name> [--dir skills]                            # 脚手架
skillforge index [-o index.json]                                 # 导出索引
skillforge config                                                # 查看生效配置
skillforge info                                                  # 版本与环境
```

`skillforge init` 生成的骨架开箱通过校验:

```text
Created /path/skills/my-skill/SKILL.md
Next: skillforge validate /path/skills/my-skill
```

## 配置

优先级:**CLI 参数 > 环境变量 > `skillforge.config.json` > 默认值**。

| 环境变量 | 默认 | 说明 |
| --- | --- | --- |
| `SKILLFORGE_ROOTS` | `skills` + `~/.agent-skills` | 多个根目录,用路径分隔符分隔(`:` / `;`) |
| `SKILLFORGE_MAX_BODY_CHARS` | `24000` | `skills_get` 正文截断长度 |
| `SKILLFORGE_HTTP_PORT` | `7331` | HTTP 端口 |
| `SKILLFORGE_HTTP_HOST` | `127.0.0.1` | HTTP 绑定地址 |
| `SKILLFORGE_LOG_LEVEL` | `info` | silent / error / warn / info / debug |
| `SKILLFORGE_WATCH` | `false` | 是否监听文件变化 |

```jsonc
// skillforge.config.json
{
  "roots": ["skills", "~/.agent-skills"],
  "maxBodyChars": 24000,
  "maxDepth": 3,
  "ignore": ["node_modules", ".git", "dist"],
  "http": { "port": 7331, "host": "127.0.0.1", "path": "/mcp" },
  "watch": false,
  "logLevel": "info"
}
```

## HTTP 部署

`serve --http` 使用 **stateless Streamable HTTP**:

- `POST /mcp` — JSON-RPC 入口,每个请求独立,无 session
- `GET /health` — 健康检查(含技能数与根目录,会做一次轻量重扫)
- `GET /` — 服务元信息
- `GET /mcp`、`DELETE /mcp` — 返回 405(stateless 模式不提供 SSE 流和会话)

```bash
node dist/cli.js serve --http --port 7331 --root ./skills --watch
curl -s http://127.0.0.1:7331/health
curl -s -X POST http://127.0.0.1:7331/mcp \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"skills_stats","arguments":{}}}'
```

**生产注意事项**:默认允许跨域、无鉴权,只适合内网或本机。对外暴露时至少加一层反向代理鉴权,并把 CORS 收紧到具体来源。

## 项目结构

```text
skillforge-mcp/
├── src/
│   ├── index.ts              默认入口(stdio server)
│   ├── cli.ts                CLI(serve/list/search/show/validate/init/index/config)
│   ├── server.ts             buildServer + createSkillForge
│   ├── config.ts             配置加载与优先级合并
│   ├── version.ts            身份常量 + server instructions
│   ├── core/
│   │   ├── types.ts          领域类型与默认配置
│   │   ├── frontmatter.ts    YAML 解析、归一化、容错
│   │   ├── registry.ts       扫描 / 缓存 / 搜索 / 资源读取
│   │   ├── validator.ts      规则引擎
│   │   └── watcher.ts        chokidar 监听 + 去抖
│   ├── tools/                definitions / handlers / index
│   ├── resources/            skill:// 资源
│   ├── prompts/              use-skill / author-skill
│   ├── transports/           stdio / http
│   ├── utils/                logger / text
│   └── examples/client.ts    端到端 MCP 客户端示例
├── skills/                   3 个可直接用的示例技能
├── test/                     28 个用例(node:test + tsx)
├── docs/                     ARCHITECTURE / SKILL-FORMAT
├── Dockerfile
└── .github/workflows/ci.yml
```

## 开发与测试

```bash
npm run dev          # tsx watch,改代码自动重启 CLI
npm run typecheck    # tsc --noEmit
npm test             # node --test,28 个用例
npm run build        # 输出 dist + .d.ts
npm run example      # 真实 MCP 往返
```

测试覆盖三层:

- `test/registry.test.ts` — 扫描、增量缓存、删除检测、搜索排序、路径穿越防护、空结果
- `test/validator.test.ts` — 每条规则的命中与级别
- `test/tools.test.ts` — 工具契约(描述长度、schema 形状)、参数校验、错误返回、截断行为

`core/` 完全不依赖 MCP SDK,所以这些测试不需要起服务,跑完约 2.7s。

## 设计取舍

**为什么不用 `McpServer.registerTool()` 而是低层 `Server` + `setRequestHandler`?**
低层 API 的 handler 签名稳定,且能精确控制 `isError`、`structuredContent` 的有无。对一个「返回结构本身就是产品」的服务,显式比便捷更重要。

**为什么 token 数是估算值?**
`chars/4`(CJK 按 `1.6`)不需要引入 tokenizer 依赖,误差在 10% 以内。目录用途是**排序和预算提示**,不需要精确到个位;真要精确,接 `tiktoken` 反而会给每个用户装 100MB 的 WASM。

**为什么搜索不用向量检索?**
技能库规模通常是 5-50 个,词法加权(name/tag/description/body)足够,且**可解释**——`matchedOn` 让模型知道为什么被推荐。引入 embedding 意味着外部服务、冷启动、密钥和不可解释的分数。规模上百时再加混合检索。

**为什么 JSON 文本而不是 `structuredContent`?**
`structuredContent` 需要同时声明 `outputSchema`,否则严格客户端会拒收;而 schema 一改就是协议层面的破坏性变更。JSON 文本在所有客户端上都稳定可解析。

**为什么同名技能允许覆盖?**
实际使用中「内置一份通用技能 + 个人目录里放自己的版本」非常常见。约定后者胜出,比强迫改名更符合直觉。

## 路线图

- [ ] `skills_install`:从 git 仓库 / 本地 zip 安装技能到根目录(带校验 + 冲突检测)
- [ ] `skills_diff`:比较两个技能版本的指令差异
- [ ] 混合检索:词法 + 可选 embedding(`SKILLFORGE_EMBEDDINGS=1` 开启)
- [ ] 索引持久化:大库(>200 技能)时落盘 `.skillforge-cache.json`
- [ ] OpenTelemetry tracing:哪个技能被选中、加载了多少 token
- [ ] Web UI:技能库浏览与编辑(只读优先)

## FAQ

**Q:技能放在哪?**
任意目录。默认读 `./skills` 和 `~/.agent-skills`,用 `--root` 或 `SKILLFORGE_ROOTS` 指向你的位置。

**Q:支持 Claude Code 的 `.claude/skills` 吗?**
支持,直接把根目录指过去即可:`--root ~/.claude/skills`。

**Q:会不会不小心改我的技能文件?**
不会。7 个工具里没有一个写磁盘。`skills_reload` 只重建内存索引。

**Q:技能很多会不会拖慢启动?**
3 个技能约 60ms。扫描是文件系统遍历 + 一次读文件,没有解析开销;后续重扫走 mtime 缓存。200 个技能在普通 SSD 上仍是百毫秒级。

**Q:能在 CI 里用吗?**
可以:`skillforge validate --strict` 在有问题时退出码为 1。

## License

MIT © SkillForge contributors

TDQS

A4.1/5.0

Scored across 7 tools

Disambiguation5/5

Each tool maps to a distinct step in a clear progressive-disclosure pipeline: enumerate (skills_list), find (skills_search), load (skills_get), read a bundled file (skills_read_resource), then maintain (skills_validate, skills_stats, skills_reload). The list-vs-search and get-vs-read_resource boundaries are explicitly spelled out with guidance on when to use each. No two tools appear interchangeable.

Naming Consistency5/5

Uniform snake_case with a single shared `skills_` namespace prefix followed by a verb or noun (list, search, get, read_resource, validate, stats, reload). Predictable and readable throughout, with no camelCase or style mixing.

Tool Count5/5

Seven tools is well within the sweet spot for a skill-registry server and each one earns its place: discovery, retrieval, resource access, validation, introspection, and index refresh. Nothing looks redundant or padded.

Completeness4/5

The read/consume lifecycle is fully covered — enumerate, search, load, drill into resources, lint, inspect stats, and reindex — with no dead ends for an agent following a skill workflow. The only gap is authoring: there is no create/edit/delete-skill tool, so writing a SKILL.md must happen outside the server (validation is offered, but not mutation).

Maintenance

ActivityNo data
ResponsivenessNo recent community issues