
如果 Copilot 能自動套用你們團隊的最佳實踐,而不需要你每次都解釋,會怎樣?
在本章中,你將學習 Agent 技能:Copilot 會在與你的任務相關時自動載入的指令資料夾。Agent 會改變 Copilot 思考的方式,而技能則教 Copilot 完成任務的具體方法。你將建立一個安全性稽核技能,讓 Copilot 在你詢問安全性時自動套用,打造團隊標準的審查準則,確保程式碼品質一致,並學習技能如何在 Copilot CLI、VS Code 及 GitHub Copilot 雲端 agent 中運作。
完成本章後,你將能夠:
⏱️ 預估時間:約 55 分鐘(閱讀 20 分鐘 + 實作 35 分鐘)
一般的電鑽很有用,但專用的配件讓它更強大。

技能的運作方式也是如此。就像根據不同工作更換鑽頭一樣,你可以為 Copilot 加入不同任務的技能:
| 技能配件 | 用途 |
|---|---|
commit |
產生一致的提交訊息 |
security-audit |
檢查 OWASP 漏洞 |
generate-tests |
建立完整的 pytest 測試 |
code-checklist |
套用團隊程式碼品質標準 |
技能是專用配件,能擴展 Copilot 的能力

了解什麼是技能、為什麼重要,以及它們與 Agent 和 MCP 的差異。
copilot
> /skills list
這會顯示 Copilot 能找到的所有技能,包括 CLI 內建的內建技能,以及來自你專案和個人資料夾的技能。
💡 內建技能:Copilot CLI 隨附預先安裝的技能。例如,
customizing-copilot-cloud-agents-environment技能提供自訂 Copilot 雲端 agent 環境的指南。你不需要建立或安裝即可使用這些技能。執行/skills list來查看有哪些可用技能。
查看實際的技能檔案: 參考我們提供的 code-checklist SKILL.md 來了解格式。它只是 YAML frontmatter 加上 markdown 指令。
Agent 技能是包含指令、腳本和資源的資料夾,當與你的任務相關時,Copilot 會自動載入。Copilot 會讀取你的提示詞,檢查是否有技能符合,並自動套用相關指令。
copilot
> Check books.py against our quality checklist
# Copilot 偵測到這符合你的 "code-checklist" 技能
# 並自動套用 Python 品質檢查清單
> Generate tests for the BookCollection class
# Copilot 載入你的 "pytest-gen" 技能
# 並套用你偏好的測試結構
> What are the code quality issues in this file?
# Copilot 載入你的 "code-checklist" 技能
# 並依據團隊標準檢查
💡 關鍵洞見:技能會根據你的提示詞是否符合技能描述自動觸發。只要自然提問,Copilot 就會在背後自動套用相關技能。你也可以直接呼叫技能,接下來會學到怎麼做。
🧰 現成範本:參考 .github/skills 資料夾,裡面有可直接複製貼上的簡易技能範例。
雖然自動觸發是技能的主要運作方式,你也可以用技能名稱作為 slash 指令直接呼叫技能:
> /generate-tests Create tests for the user authentication module
> /code-checklist Check books.py for code quality issues
> /security-audit Check the API endpoints for vulnerabilities
這讓你在需要確保使用特定技能時有明確的控制權。
你可以在同一則訊息中呼叫多個技能,而且技能的 slash 指令可以出現在提示詞的任何地方——不一定要在開頭。這在你想一次完成兩種不同檢查時很方便:
> Check @samples/book-app-project/book_app.py with /code-checklist and also run /generate-tests for it
> Review the auth module /security-audit then /code-checklist the result
Copilot 會在同一回應中套用每個指定的技能,省去你多次分開提問的麻煩。
💡 小技巧:把技能 slash 指令放在句子中你覺得最自然的位置。可以放在開頭、中間或結尾。
📝 技能與 Agent 呼叫方式差異:不要混淆技能呼叫與 Agent 呼叫:
- 技能:
/skill-name <prompt>,例如/code-checklist Check this file- Agent:
/agent(從清單選擇)或copilot --agent <name>(命令列)如果你同時有同名的技能和 Agent(例如 “code-reviewer”),輸入
/code-reviewer會呼叫技能,而不是 Agent。
你可以直接問 Copilot:
> What skills did you use for that response?
> What skills do you have available for security reviews?
技能只是 GitHub Copilot 擴充模型的一部分。以下是它們與 Agent 和 MCP 伺服器的比較。
不用急著了解 MCP。我們會在第 06 章介紹。這裡先讓你了解技能在整體架構中的定位。

| 功能 | 作用 | 適用時機 |
|---|---|---|
| Agent | 改變 AI 的思考方式 | 需要跨多種任務的專業知識 |
| 技能 | 提供特定任務指令 | 具體、可重複、步驟明確的任務 |
| MCP | 連接外部服務 | 需要從 API 取得即時資料 |
Agent 適合廣泛專業知識,技能適合特定任務指令,MCP 則用於外部資料。Agent 在對話中可以同時使用一個或多個技能。例如,當你請 Agent 檢查程式碼時,它可能會自動套用 security-audit 技能和 code-checklist 技能。
📚 深入了解:參考官方文件 About Agent Skills 取得技能格式與最佳實踐完整說明。
在深入技能建立方法前,先看看為什麼值得學習技能。當你看到一致性的提升後,”怎麼做” 就會更有意義。
每次程式碼審查,你可能都會漏掉某些項目:
copilot
> Review this code for issues
# 一般性審查——可能漏掉你們團隊特別關注的問題
或者你每次都要寫一長串提示:
> Review this code checking for bare except clauses, missing type hints,
> mutable default arguments, missing context managers for file I/O,
> functions over 50 lines, print statements in production code...
耗時:30 秒以上。一致性:取決於記憶力。
安裝 code-checklist 技能後,只要自然提問即可:
copilot
> Check the book collection code for quality issues
幕後發生的事:
code-checklist 技能符合
只要自然提問,Copilot 會自動對應並套用正確技能。
輸出範例:
## Code Checklist: books.py
### Code Quality
- [PASS] All functions have type hints
- [PASS] No bare except clauses
- [PASS] No mutable default arguments
- [PASS] Context managers used for file I/O
- [PASS] Functions are under 50 lines
- [PASS] Variable and function names follow PEP 8
### Input Validation
- [FAIL] User input is not validated - add_book() accepts any year value
- [FAIL] Edge cases not fully handled - empty strings accepted for title/author
- [PASS] Error messages are clear and helpful
### Testing
- [FAIL] No corresponding pytest tests found
### Summary
3 items need attention before merge
差異:你們團隊的標準每次都會自動套用,無需手動輸入。
假設你們團隊有 10 點 PR 檢查清單。沒有技能時,每個開發者都要記住這 10 點,總有人會漏掉。使用 pr-review 技能後,全團隊都能獲得一致的審查:
copilot
> Can you review this PR?
Copilot 會自動載入你們團隊的 pr-review 技能並檢查所有 10 點:
PR Review: feature/user-auth
## Security ✅
- No hardcoded secrets
- Input validation present
- No bare except clauses
## Code Quality ⚠️
- [WARN] print statement on line 45 - remove before merge
- [WARN] TODO on line 78 missing issue reference
- [WARN] Missing type hints on public functions
## Testing ✅
- New tests added
- Edge cases covered
## Documentation ❌
- [FAIL] Breaking change not documented in CHANGELOG
- [FAIL] API changes need OpenAPI spec update
威力:每位團隊成員都能自動套用相同標準。新進人員無需死背檢查清單,技能會自動處理。

從 SKILL.md 檔案打造你自己的技能。
技能儲存在 .github/skills/(專案專屬)或 ~/.copilot/skills/(使用者層級)。
Copilot 會自動掃描這些位置尋找技能:
| 位置 | 範圍 |
|---|---|
.github/skills/ |
專案專屬(透過 git 與團隊共用) |
~/.copilot/skills/ |
使用者專屬(你個人的技能) |
每個技能都放在自己的資料夾,裡面有一個 SKILL.md 檔案。你也可以選擇加入腳本、範例或其他資源:
.github/skills/
└── my-skill/
├── SKILL.md # 必要:技能定義與指令
├── examples/ # 選用:Copilot 可參考的範例檔案
│ └── sample.py
└── scripts/ # 選用:技能可用的腳本
└── validate.sh
💡 小技巧:資料夾名稱應與 SKILL.md frontmatter 的
name欄位一致(小寫並用連字號)。
技能採用簡單的 markdown 格式,搭配 YAML frontmatter:
---
name: code-checklist
description: Comprehensive code quality checklist with security, performance, and maintainability checks
license: MIT
---
# Code Checklist
When checking code, look for:
## Security
- SQL injection vulnerabilities
- XSS vulnerabilities
- Authentication/authorization issues
- Sensitive data exposure
## Performance
- N+1 query problems (running one query per item instead of one query for all items)
- Unnecessary loops or computations
- Memory leaks
- Blocking operations
## Maintainability
- Function length (flag functions > 50 lines)
- Code duplication
- Missing error handling
- Unclear naming
## Output Format
Provide issues as a numbered list with severity:
- [CRITICAL] - Must fix before merge
- [HIGH] - Should fix before merge
- [MEDIUM] - Should address soon
- [LOW] - Nice to have
YAML 屬性說明:
| 屬性 | 必填 | 說明 |
|---|---|---|
name |
是 | 唯一識別名稱(小寫、空格用連字號) |
description |
是 | 技能的用途,以及 Copilot 何時應該使用 |
license |
否 | 技能適用的授權條款 |
argument-hint |
否 | 顯示給使用者的簡短提示,說明技能預期的參數(例如:”file path or code snippet”) |
💡 什麼是
argument-hint? 當使用者直接呼叫技能(如/security-audit)時,argument-hint會作為佔位提示,告訴使用者接下來要輸入什麼——就像迷你說明。例如設為argument-hint: "file path to review",就會提示使用者在技能名稱後輸入檔案路徑。
📖 官方文件:About Agent Skills
我們來建立一個檢查 OWASP Top 10 漏洞的安全性稽核技能:
# 建立技能資料夾
mkdir -p .github/skills/security-audit
# 建立 SKILL.md 檔案
cat > .github/skills/security-audit/SKILL.md << 'EOF'
---
name: security-audit
description: Security-focused code review checking OWASP (Open Web Application Security Project) Top 10 vulnerabilities
---
# Security Audit
Perform a security audit checking for:
## Injection Vulnerabilities
- SQL injection (string concatenation in queries)
- Command injection (unsanitized shell commands)
- LDAP injection
- XPath injection
## Authentication Issues
- Hardcoded credentials
- Weak password requirements
- Missing rate limiting
- Session management flaws
## Sensitive Data
- Plaintext passwords
- API keys in code
- Logging sensitive information
- Missing encryption
## Access Control
- Missing authorization checks
- Insecure direct object references
- Path traversal vulnerabilities
## Output
For each issue found, provide:
1. File and line number
2. Vulnerability type
3. Severity (CRITICAL/HIGH/MEDIUM/LOW)
4. Recommended fix
EOF
# 測試你的技能(技能會根據提示自動載入)
copilot
> @samples/book-app-project/ Check this code for security vulnerabilities
# Copilot 偵測到 "security vulnerabilities" 符合你的技能
# 並自動套用 OWASP 檢查清單
預期輸出(實際結果會有所不同):
Security Audit: book-app-project
[HIGH] Hardcoded file path (book_app.py, line 12)
File path is hardcoded rather than configurable
Fix: Use environment variable or config file
[MEDIUM] No input validation (book_app.py, line 34)
User input passed directly to function without sanitization
Fix: Add input validation before processing
✅ No SQL injection found
✅ No hardcoded credentials found
SKILL.md 中的 description 欄位非常重要!Copilot 會根據它決定是否載入你的技能:
---
name: security-audit
description: Use for security reviews, vulnerability scanning,
checking for SQL injection, XSS, authentication issues,
OWASP Top 10 vulnerabilities, and security best practices
---
💡 小技巧:包含你平常提問時會用到的關鍵字。如果你會說 “security review”,就把 “security review” 寫進 description。
技能與 Agent 可以協同運作。Agent 提供專業知識,技能則給予具體指令:
# 以 code-reviewer agent 開始
copilot --agent code-reviewer
> Check the book app for quality issues
# code-reviewer agent 的專業知識會結合
# 你的 code-checklist 技能檢查清單
探索已安裝技能、尋找社群技能,並分享你自己的技能。

copilot skill 指令與 /skills 管理技能Copilot CLI 提供兩種技能管理方式。你可以在啟動 Copilot 前直接在終端機操作,也可以在 Copilot 互動會話中操作。
copilot skill(終端機指令)copilot skill 子指令讓你直接在終端機管理技能,無需開啟互動式 Copilot 會話。這對於腳本自動化、快速檢查或事前新增技能很方便。
# 查看所有已安裝技能
copilot skill list
# 從本地檔案、URL 或目錄新增技能
copilot skill add .github/skills/my-skill/SKILL.md
copilot skill add https://example.com/skills/security-audit/SKILL.md
# 依名稱移除技能
copilot skill remove security-audit
/skills(Copilot 會話中)進入互動式 Copilot 會話後,使用 /skills(或快捷 /skill)即可管理技能,無需離開會話:
| 指令 | 作用 |
|---|---|
/skills list |
顯示所有已安裝技能 |
/skills info <name> |
查看特定技能詳細資訊 |
/skills add <name> |
啟用技能(來自儲存庫或市集) |
/skills remove <name> |
停用或解除安裝技能 |
/skills reload |
編輯 SKILL.md 後重新載入技能 |
💡
/skill快捷指令:你可以輸入/skill取代/skills——兩者等效。例如/skill list與/skills list效果相同。
💡 請記住:你不需要每次提示都「啟用」技能。安裝後,只要提示詞符合描述,技能就會自動觸發。這些指令是用來管理可用技能,而不是用來使用技能。
# 在終端機(無需互動會話):
copilot skill list
Project skills:
- security-audit: Security-focused code review checking OWASP Top 10
- generate-tests: Generate comprehensive unit tests with edge cases
- code-checklist: Team code quality checklist (disabled)
...
# 或在 Copilot 會話中:
copilot
> /skills list
Project skills:
- security-audit: Security-focused code review checking OWASP Top 10
- generate-tests: Generate comprehensive unit tests with edge cases
- code-checklist: Team code quality checklist (disabled)
...
> /skills info security-audit
Skill: security-audit
Source: Project
Location: .github/skills/security-audit/SKILL.md
Description: Security-focused code review checking OWASP Top 10 vulnerabilities
💡 已停用技能:標記為
(disabled)的技能已安裝但目前未啟用。它們不會被提示詞觸發,直到重新啟用。這可能是因為 SKILL.md 設定有誤,或技能被明確停用。你可以透過/skills啟用/停用技能。
/skills reload建立或編輯技能的 SKILL.md 檔案後,執行 /skills reload 即可在不重啟 Copilot 的情況下套用變更:
# 編輯你的技能檔案
# 然後在 Copilot 中:
> /skills reload
Skills reloaded successfully.
💡 小知識:即使你用
/compact指令壓縮對話歷史,技能依然有效。壓縮後無需重新載入。
💡 什麼是插件? 插件是可安裝的套件,可以同時包含技能、Agent 及 MCP 伺服器設定。你可以把它們想像成 Copilot CLI 的「應用程式商店」擴充套件。
用 /plugin 指令可以瀏覽並安裝這些套件:
copilot
> /plugin list
# 顯示已安裝的插件
> /plugin marketplace
# 瀏覽可用插件
> /plugin install <plugin-name>
# 從市集安裝插件
要讓本地插件目錄保持最新,請執行:
copilot plugin marketplace update
插件可以同時包含多種功能。一個插件可能包含相關的技能、Agent 和 MCP 伺服器設定,彼此協同運作。
也有許多現成技能可從社群儲存庫取得:
從 GitHub 儲存庫安裝技能最簡單的方法是使用 gh skill install 指令(需 GitHub CLI v2.90.0+):
# 瀏覽並互動式選擇 awesome-copilot 的技能
gh skill install github/awesome-copilot
# 或直接安裝特定技能
gh skill install github/awesome-copilot ai-ready
# 安裝到所有專案皆可用(使用者層級)
gh skill install github/awesome-copilot ai-ready --scope user
⚠️ 安裝前請審查:安裝技能前請務必閱讀其
SKILL.md。技能會控制 Copilot 的行為,惡意技能可能會指示 Copilot 執行有害指令或意外修改程式碼。

動手建立並測試你自己的技能,應用所學。
這裡有兩個不同模式的技能範例。請依照「建立你的第一個技能」中的 mkdir + cat 步驟操作,或直接複製貼上到正確位置。更多範例可參考 .github/skills。
這個技能可確保你的程式碼庫 pytest 結構一致:
mkdir -p .github/skills/pytest-gen
cat > .github/skills/pytest-gen/SKILL.md << 'EOF'
---
name: pytest-gen
description: Generate comprehensive pytest tests with fixtures and edge cases
---
# pytest Test Generation
Generate pytest tests that include:
## Test Structure
- Use pytest conventions (test_ prefix)
- One assertion per test when possible
- Clear test names describing expected behavior
- Use fixtures for setup/teardown
## Coverage
- Happy path scenarios
- Edge cases: None, empty strings, empty lists
- Boundary values
- Error scenarios with pytest.raises()
## Fixtures
- Use @pytest.fixture for reusable test data
- Use tmpdir/tmp_path for file operations
- Mock external dependencies with pytest-mock
## Output
Provide complete, runnable test file with proper imports.
EOF
這個技能可強制全團隊遵循一致的 PR 審查標準:
mkdir -p .github/skills/pr-review
cat > .github/skills/pr-review/SKILL.md << 'EOF'
---
name: pr-review
description: 團隊標準 PR 審查清單
---
# PR 審查
依據團隊標準審查程式碼變更:
## 安全性檢查清單
- [ ] 無硬編碼的秘密或 API 金鑰
- [ ] 所有使用者資料皆有輸入驗證
- [ ] 無裸露的 except 子句
- [ ] 日誌中無敏感資料
## 程式碼品質
- [ ] 函式少於 50 行
- [ ] 生產環境程式碼中無 print 陳述式
- [ ] 公開函式皆有型別註記
- [ ] 檔案 I/O 使用 Context Manager
- [ ] 無未附議題參考的 TODO
## 測試
- [ ] 新增程式碼皆有測試
- [ ] 邊界情境皆有涵蓋
- [ ] 無未說明原因的略過測試
## 文件
- [ ] API 變更有文件說明
- [ ] 重大變更有註記
- [ ] 如有需要已更新 README
## 輸出格式
請依下列格式提供結果:
- ✅ 通過:看起來沒問題的項目
- ⚠️ 警告:可改進的項目
- ❌ 未通過:合併前必須修正的項目
EOF
quick-review skill,進行三點檢查:
測試方式:詢問「Do a quick review of books.py」
Skill 比較:計時你手動撰寫詳細安全審查提示所需時間。然後只需詢問「Check for security issues in this file」,讓你的 security-audit skill 自動載入。這樣節省了多少時間?
自我檢查:當你能解釋為什麼 description 欄位很重要時(它決定 Copilot 是否載入你的 skill),就代表你已經理解 skills 的運作。
上面的範例建立了 pytest-gen 和 pr-review 這兩個 skill。現在請練習創建一個完全不同類型的 skill:用來從資料產生格式化輸出的 skill。
/skills list。你也可以用 ls .github/skills/ 查看專案 skills,或用 ls ~/.copilot/skills/ 查看個人 skills。.github/skills/book-summary/SKILL.md 建立一個 book-summary skill,能產生書籍收藏的格式化 markdown 摘要@samples/book-app-project/data.json Summarize the books in this collection/skills list 檢查/book-summary Summarize the books in this collection 直接呼叫成功標準:你有一個運作中的 book-summary skill,當你詢問書籍收藏時 Copilot 能自動套用。
commit-message skill,能產生格式一致的 conventional commit 訊息copilot-skill 標籤分享/skill-name slash 指令直接呼叫 skill.github/skills/ 適合專案/團隊共用,~/.copilot/skills/ 適合個人使用copilot skill,或在會話內用 /skills(快捷指令:/skill)📋 快速參考:完整指令與快捷鍵請見 GitHub Copilot CLI command reference。
Skills 讓 Copilot 能自動載入指令擴充功能。但如果要連接外部服務呢?這時就需要 MCP。
在 第 06 章:MCP 伺服器,你將學到:
| ← 回到第 04 章 | 繼續前往第 06 章 → |