🎯 重點摘要: 本文完整解析 Claude Code 的四大擴展機制:
① MCP:連接外部資料源與服務的開放標準,從 .mcp.json 到自寫 Server
② Skill:用/指令觸發的可重用 prompt 集合
③ Hook:在特定事件點自動執行的 shell 指令,實現零手動自動化
④ Plugin:把 Skill、MCP、Hook 打包成一個可安裝的完整套件
Claude Code 是 Anthropic 推出的 agentic 編碼助手。除了基礎的對話編程之外,它還提供了四大強大的擴展機制:MCP、Skill、Hook 和 Plugin,讓你可以將外部資料源、自動化工作流程、可重用的指令集合整合進來。
本文將帶你一步步從零開始設定 MCP Server、撰寫 .mcp.json 配置文件、建立自定義 Skill,並理解它們彼此的關係。
📌 本文對應 Claude Code 2.1.x 版本。MCP 規格基於 Model Context Protocol 官方標準。
MCP 入門 — 什麼是 Model Context Protocol?
MCP (Model Context Protocol) 是一個開放標準,讓 Claude Code 能夠連接到外部資料來源與服務。透過 MCP,你可以:
- 從 Google Drive 讀取設計文件
- 查詢 SQLite、PostgreSQL 資料庫
- 更新 Jira 票券或 GitHub Issues
- 控制瀏覽器自動化測試
- 存取本地檔案系統
MCP Server 提供三種主要能力:
| 能力類型 | 說明 | Claude Code 中的使用方式 |
|---|---|---|
| Resources | 類似檔案的可讀取資料(API 回應、檔案內容) | Claude 會自動讀取資源作為 context |
| Tools | LLM 可以呼叫的函數(需使用者批准) | 顯示在 Claude Code 的工具列表中 |
| Prompts | 預寫的 prompt 模板 | 透過特定指令觸發 |
如何在 Claude Code 中安裝 MCP Server
Claude Code 支援兩種 MCP Server 連接方式:
1. 使用 .mcp.json 配置本地 Server (推薦)
在你的專案根目錄建立 .mcp.json 文件:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/Users/username/project"]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxxxxxxxxxxxxxxxxxxx"
}
},
"sqlite": {
"command": "uv",
"args": ["--directory", "/ABSOLUTE/PATH/sqlite-mcp", "run", "sqlite.py"],
"env": {
"SQLITE_PATH": "/path/to/database.db"
}
}
}
}
| 欄位 | 說明 | 必填 |
|---|---|---|
command |
執行 MCP Server 的指令(如 npx、uv、python) | ✅ |
args |
指令的參數列表 | ✅ |
env |
環境變數(如 API Token) | ❌ |
type |
傳輸類型:"stdio" (預設) 或 "http" (遠端 Server) |
❌ |
url |
當 type: "http" 時,指定 Server 的 URL |
❌ |
2. 透過 CLI 動態添加 MCP Server
在 Claude Code session 中,你可以使用下列指令管理 MCP Server:
| 命令 | 說明 | 範例 |
|---|---|---|
claude mcp |
列出所有 MCP Server 連線狀態 | claude mcp |
claude mcp reconnect <name> |
重新連線指定 Server | claude mcp reconnect github |
claude mcp disable <name> |
禁用指定 Server | claude mcp disable sqlite |
claude mcp disable all |
禁用所有 Server | claude mcp disable all |
claude mcp login <name> |
執行 Server 的 OAuth 流程 | claude mcp login sentry |
claude mcp logout <name> |
清除 Server 的 OAuth 憑證 | claude mcp logout sentry |
3. 添加到全局設定 (個人專用)
如果你希望 MCP Server 對所有專案都生效,將其配置添加到 全局設定檔:
# macOS / Linux
~/.claude/claude_desktop_config.json
# Windows
%USERPROFILE%\.claude\claude_desktop_config.json
⚠️ 安全提醒: .mcp.json 位於專案根目錄會被 Git 追踪,請勿在其中存放秘密金鑰。對於敏感憑證,推薦使用 claude mcp login 進行 OAuth 授權,或將 .mcp.json 加入 .gitignore。
實用 MCP Server 推薦列表
| Server 名稱 | 用途 | 安裝指令 |
|---|---|---|
| @modelcontextprotocol/server-filesystem | 本地檔案系統存取 | command: npx -y @modelcontextprotocol/server-filesystem <path> |
| @modelcontextprotocol/server-memory | 記憶持久化 (Graph) | command: npx -y @modelcontextprotocol/server-memory |
| @modelcontextprotocol/server-github | GitHub API 操作 | command: npx -y @modelcontextprotocol/server-github |
| github.com/modelcontextprotocol/servers/tree/main/src/sqlite | SQLite 資料庫查詢 | command: npx -y @modelcontextprotocol/server-sqlite |
| @modelcontextprotocol/server-brave-search | Brave 搜尋 | command: npx -y @modelcontextprotocol/server-brave-search |
| @modelcontextprotocol/server-fetch | HTTP 網頁抓取 | command: npx -y @modelcontextprotocol/server-fetch |
實戰範例:將 SQLite MCP Server 接入 Claude Code
假設你有一個 SQLite 資料庫 /Users/username/app.db,想讓 Claude Code 直接查詢它的內容:
步驟 1 — 建立 .mcp.json
{
"mcpServers": {
"sqlite": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-sqlite", "/Users/username/app.db"]
}
}
}
步驟 2 — 啟動 Claude Code
cd /path/to/project
claude
Claude Code 會自動掃描 .mcp.json,並詢問是否信任載入這些 Server。選擇信任之後,你就可以直接對 Claude 說:
查詢 users 資料表的所有欄位
Claude Code 會透過 MCP Server 執行 SQL 並回傳結果,全部不用手動打 SQL!
如何撰寫自定義 MCP Server
如果現有的 Server 不符合你的需求,你可以自己編寫一個。MCP 提供 Python、TypeScript/JavaScript、Java、C# 等多語言 SDK。
Python 範例:建立一個天氣查詢 MCP Server
步驟 1 — 初始化專案
pip install mcp[cli]
# 或者使用 uv (推薦)
uv init weather-server
uv add "mcp[cli]" httpx
步驟 2 — 撰寫 server.py
#!/usr/bin/env python3
"""天氣查詢 MCP Server"""
import json
import httpx
from mcp.server.fastapi import serve_app
from mcp.types import Tool
app = serve_app()
@app.list_tools()
async def handle_list_tools():
return [
Tool(
name="get_weather",
description="查詢指定城市的天氣",
inputSchema={
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名稱"}
},
"required": ["city"]
}
)
]
@app.call_tool()
async def handle_execute_tool(name: str, arguments: dict):
if name == "get_weather":
city = arguments.get("city", "Taipei")
# 這裡呼叫實際的天氣 API
result = await fetch_weather(city)
return result
return {"content": [], "isError": True}
if __name__ == "__main__":
app.run()
步驟 3 — 將其加入 .mcp.json
{
"mcpServers": {
"weather": {
"command": "python3",
"args": ["/ABSOLUTE/PATH/TO/weather-server/server.py"],
"env": {
"WEATHER_API_KEY": "your-api-key-here"
}
}
}
}
📚 學習資源: MCP 官方教學:Building Your First MCP Server。Context7 整理了豐富的 MCP Server 程式碼範例,搜索「MCP server」即可找到來自真實專案的配置參考。
Skill — 可重用的 Claude Code 指令
Skill 是 Claude Code 的可重用指令集合。當你在 session 中輸入 /skill-name,Claude Code 會載入對應的 prompt 並執行。
Skill 檔案結構
Skill 放在專案的 .claude/skills/ 目錄下,每個 Skill 一個資料夾,資料夾內放一個 SKILL.md:
.claude/
├── skills/
│ ├── code-review/
│ │ └── SKILL.md # /code-review
│ ├── bug-fix/
│ │ └── SKILL.md # /bug-fix
│ └── generate-tests/
│ └── SKILL.md # /generate-tests
└── settings.json # Hooks 配置 (見下方 Hook 章節)
建立自定義 Skill
範例:建立一個「code-review」Skill
在 .claude/skills/code-review/SKILL.md 中撰寫:
---
name: code-review
description: 對專案的變更檔案進行程式碼品質審查。使用者輸入 /code-review 或要求 code review 時使用。
---
# /code-review — 程式碼品質審查
## 目的
對專案中的 changed files 進行程式碼品質審查,包含:
- Bug 檢測
- 性能瓶頸
- 安全漏洞
- 可讀性問題
## 操作指令
請執行以下步驟:
1. 執行 git diff main --name-only 列出所有修改過的檔案
2. 逐一檢視每個檔案,執行 git diff main -- <file>
3. 對發現的問題進行分類:
- **Critical**: 可能導致崩潰或安全漏洞
- **Warning**: 效能問題或最佳化建議
- **Info**: 可讀性改善
4. 撰寫 review 報告,格式如下:
```
📊 Code Review 摘要
- 發現問題數:X 個
- Critical: Y 個 | Warning: Z 個 | Info: W 個
🔍 詳細問題:
[檔案名]
- L<line>: <問題描述>
建議: <修改建議>
```
5. 若使用者提供 `/code-review high`,會使用更嚴格的審查標準
6. 若使用者提供 `/code-review --fix`,在報告後自動修復可修復的問題
## 注意事項
- 只審查 Git 有變更的檔案,不要審查 node_modules 或建構產物
存檔後重新啟動 Claude Code(或執行 /exit 再進來),輸入 /code-review 就會看到 Skill 生效了。
💡 小提醒: Skill 沒出現的話,先確認資料夾名稱與 frontmatter 的name一致,再跑/doctor檢查配置。
全域 Skill (跨專案共用)
把 Skill 放到家目錄下的 ~/.claude/skills/,所有專案都能使用,適合放個人工作流:
~/.claude/
└── skills/
├── daily-report/
│ └── SKILL.md # /daily-report
└── commit-helper/
└── SKILL.md # /commit-helper
Hook — 不用開口的自動化
Hook 讓你在 Claude Code 的特定事件點自動執行 shell 指令。跟 Skill 最大的差別是:Skill 要你下指令才動,Hook 是不管你想不想都會執行。常見用途:
- 編輯檔案後自動跑 formatter / linter
- 阻擋危險指令 (如 force push、刪除資料表)
- 任務完成時送桌面通知
常用的事件點
| 事件 | 觸發時機 | 典型用途 |
|---|---|---|
| PreToolUse | 工具執行前 | 攔截危險指令、權限控管 |
| PostToolUse | 工具執行後 | 自動 format、lint 剛改過的檔案 |
| Stop | Claude 完成回應時 | 送通知、觸發後續檢查 |
| SessionStart | Session 啟動時 | 載入環境資訊、專案狀態 |
配置方式
Hooks 寫在 .claude/settings.json (專案) 或 ~/.claude/settings.json (全域),也可以在 session 中輸入 /hooks 用選單設定。例如「每次編輯檔案後自動跑 Prettier + 任務完成時跳通知」:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}
]
}
],
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"Claude Code 任務完成!\" with title \"Claude Code\"'"
}
]
}
]
}
}
⚠️ 注意: PostToolUse 的 Hook 會從 stdin 收到一段 JSON,包含剛剛操作的工具名稱與檔案路徑,所以範例中用jq取出.tool_input.file_path。
Plugin — 把所有能力打包發佈
Plugin 是最高層的封裝單位:一個 Plugin 可以同時包含 Skills、MCP Servers、Hooks、Subagents 與 Slash Commands,透過 marketplace 安裝:
/plugin marketplace add <repo-url>
/plugin install <plugin-name>
對團隊來說,這招非常省事:把共用的 code review 流程 (Skill)、內部 API 的 MCP Server、強制執行的 Hooks 打包成一個 Plugin,新人裝完就等於完成了整套環境建置。
MCP、Skill、Hook、Plugin 怎麼選?
| 機制 | 觸發方式 | 適合場景 | 配置位置 |
|---|---|---|---|
| MCP | 模型依需求呼叫工具 | 連接外部系統與資料源 | .mcp.json / claude mcp add |
| Skill | 使用者輸入 /指令 |
固定 SOP、可重用的 prompt 工作流 | .claude/skills/ |
| Hook | 事件點自動觸發 | 強制自動化 (format、阻擋、通知) | .claude/settings.json |
| Plugin | /plugin 安裝 |
打包以上所有能力,團隊共用 | marketplace repo |
選擇心法一句話:要連外部服務用 MCP;要把重複下過的指令變成一鍵流程用 Skill;要「一定會執行」的自動化用 Hook;要分享整包給團隊就用 Plugin。
總結
Claude Code 的強大之處不只在對話編程,而是 MCP、Skill、Hook、Plugin 這四層擴展機制:MCP 打通外部資料源,Skill 沉澱你的工作流,Hook 保證自動化必然執行,Plugin 負責打包分享。四個湊在一起,Claude Code 就從一個聊天助手變成真正客製化的工程平台。
🎯 快速上手: 在專案根目錄建立 .mcp.json 接上第一個 Server → 把常做的流程寫成 Skill → 用 Hook 讓 format 自動化 → 有需要再用 Plugin 打包分享給團隊。記住:先從痛點最大的那一個開始,不要一次全上。