Skip to main content
Glama
README.md
# FastMCP Skills over MCP 範例

用 [FastMCP](https://github.com/PrefectHQ/fastmcp) 4.2 從 MCP Server 發布 Agent Skills([SEP-2640](https://modelcontextprotocol.io/seps/2640-skills-extension)、`io.modelcontextprotocol/skills`),並附一個 Host 端載入器,示範按需讀檔與逐檔驗證。

- **Tools** 定義可呼叫的操作:`format_release_notes`。
- **Skills** 說明何時、如何使用這些操作:`release-notes` 技能指示模型呼叫上面的工具。

> FastMCP 對 Skills 的支援為實驗性,Skills 擴充規格為 Beta(基線 MCP 2026-07-28)。本範例於 2026-11-30 以 FastMCP 4.2.6、Python 3.12+ 實測。升級前請先核對發布說明。

## 結構

```text
skills/                                 # 每個含 SKILL.md 的資料夾就是一個技能
├── code-review/
│   ├── SKILL.md                        → skill://code-review/SKILL.md
│   └── references/checklist.md         # 附屬檔,按需讀取
└── ops/release-notes/                  # ops/ 是命名空間,不是技能
    ├── SKILL.md                        → skill://ops/release-notes/SKILL.md
    └── templates/release-notes.md
src/fastmcp_skills_example/
├── server.py   # FastMCP Server:SkillsDirectoryProvider + 一個工具
├── host.py     # Host 端載入器:固定條目、按需讀檔、驗證 size/digest/frontmatter
└── demo.py     # 端到端示範
tests/test_skills.py                    # 整合驗收
```

技能資料夾名稱必須等於 `SKILL.md` frontmatter 的 `name`,否則 Server 啟動時就會報錯。

## 快速開始

需要 [uv](https://docs.astral.sh/uv/)。

```bash
uv sync
uv run skills-demo          # 以 in-memory 方式啟動 Server 並跑完整個 Host 流程
uv run pytest               # 8 個整合測試
```

以 Streamable HTTP 啟動,再從另一個終端連線:

```bash
uv run skills-server --http --port 8000
uv run skills-demo --url http://127.0.0.1:8000/mcp
```

不加 `--http` 時走 stdio,可直接接到支援 MCP 的 Host。編輯技能時可加 `--reload`,每次請求都重讀資料夾。`SKILLS_DIR` 環境變數可改用其他技能目錄。

## Server 端

核心只有一行 Provider:

```python
from fastmcp import FastMCP
from fastmcp.skills.provider import SkillsDirectoryProvider

mcp = FastMCP("Team Skills", providers=[SkillsDirectoryProvider("./skills")])
```

Provider 會自動註冊 Skills 擴充,所以:

- 協商到擴充的 Client 可用 `skills/list`、`skills/get`;所有 Client 都能用一般的 `resources/read` 讀檔。
- 每個條目帶完整 manifest:每個檔案的 URI、原始位元組數 `size` 與 SHA-256 `digest`。
- 預設啟動時讀一次資料夾,manifest 與供應的位元組來自同一份快照。

注意事項:

- 不要使用 FastMCP 4.1 的 `experimental_skills=True`,該 API 已棄用,預計在 4.3 移除。
- FastMCP 4.2 未實作 `directoryRead`,Host 應從持有的 manifest 推導檔案樹。
- 回應預設為 `cacheScope: "private"`、`ttlMs: 0`,是保守設定。

實際的 `skills/list` 回應(節錄):

```json
{
  "resultType": "complete",
  "ttlMs": 0,
  "cacheScope": "private",
  "skills": [{
    "uri": "skill://code-review/SKILL.md",
    "frontmatter": { "name": "code-review", "description": "Review a code change ...", "metadata": { "author": "fastmcp-skills-example", "version": "1.0" } },
    "resources": [
      { "uri": "skill://code-review/SKILL.md", "digest": "sha256:1367f2…", "size": 658 },
      { "uri": "skill://code-review/references/checklist.md", "digest": "sha256:765543…", "size": 439 }
    ]
  }]
}
```

直接以 HTTP 呼叫時,除了 body 的 `_meta`,還須帶 SEP-2243 標頭 `MCP-Protocol-Version`、`Mcp-Method`(`resources/read` 另須 `Mcp-Name`)。缺少時 Server 回 HTTP 400。使用 SDK 的 Transport 會自動加上。

## Host 端

Client 須主動宣告擴充:

```python
from fastmcp import Client
from fastmcp.skills import SkillsClientExtension

async with Client(url, extensions=[SkillsClientExtension()]) as client:
    entries = await client.list_skills()          # 只取 metadata,不讀檔
    entry = await client.get_skill("skill://ops/release-notes/SKILL.md")
```

FastMCP 內建的 `client.read_skill(uri)` 會一次下載並驗證整包技能,適合匯入。要把技能載入模型 context 時,`host.py` 的 `SkillLoader` 改為按需讀取:

1. 固定(pin)載入時的條目。`dynamic` 條目依本範例政策直接拒絕。
2. `load_main()` 讀 `SKILL.md`,比對位元組數、SHA-256,並逐欄比對完整 frontmatter。
3. `load_file("templates/release-notes.md")` 以技能根目錄解析相對路徑,只允許 manifest 已列出的 URI,同樣逐一驗證。跳出技能根目錄的路徑一律拒絕。
4. `approval_key` 綁定來源 Server、技能 URI 與完整的 (URI, digest) 集合。集合改變時,舊的持久核准不再適用。
5. `for_model()` 進入模型 context 前標上來源 Server 與 URI。

驗證失敗時丟出 `SkillVerificationError`(`reason` 為 `unlisted`、`size`、`digest`、`frontmatter` 或 `dynamic`)。Host 應丟棄該內容,以 `skills/get` 刷新條目,再依核准規則重新載入。

本範例未涵蓋的 Host 責任:載入同意的 UI、`allowed-tools` 的權限處理(遠端技能預設忽略)、執行本機程式或跨 Server 讀取的明確核准、累計容量上限(512 檔、16 MiB)及磁碟快取。

## 整合驗收(`tests/test_skills.py`)

| 測試 | 驗證內容 |
| --- | --- |
| `test_server_declares_skills_extension` | 協商到 2026-07-28,Server 宣告 `io.modelcontextprotocol/skills` |
| `test_list_returns_complete_entries_matching_disk` | 條目完整,`size`/`digest` 與磁碟原始位元組一致,URI 末段等於 `name` |
| `test_get_unknown_skill_is_invalid_params` | 未知技能回 `-32602` |
| `test_loader_verifies_main_and_supporting_files` | 主檔與附屬檔皆依 manifest 驗證 |
| `test_loader_refuses_unlisted_and_escaping_paths` | 未列出或跳出根目錄的路徑被拒 |
| `test_frontmatter_mismatch_is_rejected` | 條目多一個 frontmatter 欄位即拒載 |
| `test_updated_file_breaks_pinned_entry_and_approval` | 發布更新後舊條目驗證失敗,刷新後可載入,核准鍵改變 |
| `test_skill_tool_is_callable` | 技能指示的工具可呼叫 |

## 參考

- [Skills 擴充 Beta 規格](https://skills.extensions.modelcontextprotocol.io/specification/beta/skills)
- [SEP-2640](https://modelcontextprotocol.io/seps/2640-skills-extension)
- [Agent Skills 格式](https://agentskills.io/specification)
- [FastMCP 4.2.0 Skills 文件](https://github.com/PrefectHQ/fastmcp/blob/v4.2.0/docs/servers/skills.mdx)

## 授權

MIT