
如果 Copilot 能自動套用您團隊的最佳實踐,而您無需每次都重新說明,那會如何?
在本章中,您將學習代理程式技能(Agent Skills):這些資料夾包含指令,Copilot 會在與您任務相關時自動載入。代理程式改變的是 Copilot 如何思考,而技能則教導 Copilot 以特定方式完成任務。您將建立一個安全性稽核技能,讓 Copilot 在您詢問安全性問題時自動套用;建立符合團隊標準的審查標準,確保一致的程式碼品質;並了解技能如何在 Copilot CLI、VS Code 和 Copilot 程式碼代理程式之間運作。
完成本章後,您將能夠:
⏱️ 預估時間:約 55 分鐘(閱讀 20 分鐘 + 實作 35 分鐘)
通用電鑽雖然好用,但專用配件才能讓它如虎添翼。

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

了解技能是什麼、為何重要,以及它們與代理程式和 MCP 有何不同。
copilot
> /skills list
這會顯示 Copilot 在您的專案和個人資料夾中找到的所有技能。
查看真實的技能檔案: 看看我們提供的 code-checklist SKILL.md,了解其格式。它只是 YAML 前置資料加上 Markdown 指令。
代理程式技能是包含指令、腳本和資源的資料夾,Copilot 在與您的任務相關時自動載入。Copilot 讀取您的提示詞,檢查是否有符合的技能,然後自動套用相關指令。
copilot
> Check books.py against our quality checklist
# Copilot detects this matches your "code-checklist" skill
# and automatically applies its Python quality checklist
> Generate tests for the BookCollection class
# Copilot loads your "pytest-gen" skill
# and applies your preferred test structure
> What are the code quality issues in this file?
# Copilot loads your "code-checklist" skill
# and checks against your team's standards
💡 核心洞察:技能根據您的提示詞與技能描述的匹配程度自動觸發。只要自然地提問,Copilot 就會在幕後套用相關技能。您也可以直接呼叫技能,這將在下一節介紹。
🧰 隨取即用的範本:請查看 .github/skills 資料夾,取得可直接複製使用的技能。
雖然自動觸發是技能的主要運作方式,您也可以使用技能名稱作為斜線指令來直接呼叫技能:
> /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
當您想確保使用特定技能時,這能給您明確的控制權。
📝 技能 vs. 代理程式的呼叫方式:請勿混淆技能與代理程式的呼叫方式:
- 技能:
/技能名稱 <提示詞>,例如/code-checklist Check this file- 代理程式:
/agent(從清單選擇)或copilot --agent <名稱>(命令列)如果您同時有同名的技能和代理程式(例如「code-reviewer」),輸入
/code-reviewer會呼叫技能,而非代理程式。
您可以直接詢問 Copilot:
> What skills did you use for that response?
> What skills do you have available for security reviews?
技能只是 GitHub Copilot 擴充模型的一個環節。以下是它們與代理程式和 MCP 伺服器的比較。
暫時不用擔心 MCP。我們將在第 06 章中介紹。這裡提及是為了讓您了解技能在整體架構中的定位。

| 功能 | 作用 | 使用時機 |
|---|---|---|
| 代理程式 | 改變 AI 的思考方式 | 需要跨多項任務的專業知識 |
| 技能 | 提供任務特定的指令 | 具有詳細步驟的特定、可重複任務 |
| MCP | 連接外部服務 | 需要 API 的即時資料 |
代理程式用於廣泛的專業知識,技能用於特定任務指令,MCP 用於外部資料。在對話過程中,代理程式可以使用一個或多個技能。例如,當您要求代理程式檢查程式碼時,它可能會自動同時套用 security-audit 技能和 code-checklist 技能。
📚 深入了解:請參閱官方的 About Agent Skills 文件,取得技能格式和最佳實踐的完整參考。
在深入了解如何建立技能之前,讓我們先看看為何值得學習技能。一旦您看到一致性帶來的好處,「如何做」就會更容易理解。
每次程式碼審查,您可能都會忘記某些事項:
copilot
> Review this code for issues
# Generic review - might miss your team's specific concerns
或者每次都要輸入一長串提示詞:
> 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 # Required: Skill definition and instructions
├── examples/ # Optional: Example files Copilot can reference
│ └── sample.py
└── scripts/ # Optional: Scripts the skill can use
└── validate.sh
💡 提示:資料夾名稱應與 SKILL.md 前置資料中的
name相符(小寫加連字號)。
技能使用帶有 YAML 前置資料的簡單 Markdown 格式:
---
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 |
否 | 適用於此技能的授權條款 |
📖 官方文件:About Agent Skills
讓我們建立一個用於檢查 OWASP Top 10 漏洞的安全性稽核技能:
# Create skill directory
mkdir -p .github/skills/security-audit
# Create the SKILL.md file
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
# Test your skill (skills load automatically based on your prompt)
copilot
> @samples/book-app-project/ Check this code for security vulnerabilities
# Copilot detects "security vulnerabilities" matches your skill
# and automatically applies its OWASP checklist
預期輸出(您的結果可能有所不同):
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」。
技能和代理程式可以協同運作。代理程式提供專業知識,技能提供具體指令:
# Start with a code-reviewer agent
copilot --agent code-reviewer
> Check the book app for quality issues
# code-reviewer agent's expertise combines
# with your code-checklist skill's checklist
探索已安裝的技能,尋找社群技能,並分享您自己的技能。

/skills 指令管理技能使用 /skills 指令來管理您已安裝的技能:
| 指令 | 功能說明 |
|---|---|
/skills list |
顯示所有已安裝的技能 |
/skills info <name> |
取得特定技能的詳細資訊 |
/skills add <name> |
啟用技能(來自程式庫或市集) |
/skills remove <name> |
停用或解除安裝技能 |
/skills reload |
編輯 SKILL.md 檔案後重新載入技能 |
💡 記住:您無需為每個提示詞「啟用」技能。技能一經安裝,當您的提示詞符合其描述時便會自動觸發。這些指令用於管理哪些技能可用,而非用於使用技能。
copilot
> /skills list
Available 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
...
> /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
/skills reload建立或編輯技能的 SKILL.md 檔案後,執行 /skills reload 以套用變更,無需重新啟動 Copilot:
# Edit your skill file
# Then in Copilot:
> /skills reload
Skills reloaded successfully.
💡 補充知識:使用
/compact壓縮對話歷程後,技能仍然有效,無需重新載入。
💡 什麼是外掛? 外掛是可安裝的套件,可將技能、代理程式和 MCP 伺服器設定打包在一起。可以把它們想像成 Copilot CLI 的「應用程式商店」擴充功能。
/plugin 指令讓您能夠瀏覽和安裝這些套件:
copilot
> /plugin list
# Shows installed plugins
> /plugin marketplace
# Browse available plugins
> /plugin install <plugin-name>
# Install a plugin from the marketplace
外掛可以將多種功能捆綁在一起——單一外掛可能包含相互配合的相關技能、代理程式和 MCP 伺服器設定。
現成的技能也可從社群程式庫取得:
如果您在 GitHub 程式庫中找到技能,請將其資料夾複製到您的技能目錄:
# Clone the awesome-copilot repository
git clone https://github.com/github/awesome-copilot.git /tmp/awesome-copilot
# Copy a specific skill to your project
cp -r /tmp/awesome-copilot/skills/code-checklist .github/skills/
# Or for personal use across all projects
cp -r /tmp/awesome-copilot/skills/code-checklist ~/.copilot/skills/
⚠️ 安裝前請先審查:在將技能複製到您的專案之前,務必先閱讀其
SKILL.md。技能控制 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: Team-standard PR review checklist
---
# PR Review
Review code changes against team standards:
## Security Checklist
- [ ] No hardcoded secrets or API keys
- [ ] Input validation on all user data
- [ ] No bare except clauses
- [ ] No sensitive data in logs
## Code Quality
- [ ] Functions under 50 lines
- [ ] No print statements in production code
- [ ] Type hints on public functions
- [ ] Context managers for file I/O
- [ ] No TODOs without issue references
## Testing
- [ ] New code has tests
- [ ] Edge cases covered
- [ ] No skipped tests without explanation
## Documentation
- [ ] API changes documented
- [ ] Breaking changes noted
- [ ] README updated if needed
## Output Format
Provide results as:
- ✅ PASS: Items that look good
- ⚠️ WARN: Items that could be improved
- ❌ FAIL: Items that must be fixed before merge
EOF
quick-review 技能:
透過提問來測試:「Do a quick review of books.py」
技能比較:計時手動輸入詳細安全性審查提示詞所需的時間。然後只需問「Check for security issues in this file」,讓您的 security-audit 技能自動載入。技能節省了多少時間?
自我檢核:當您能解釋為何 description 欄位很重要時(它是 Copilot 決定是否載入您的技能的依據),代表您已理解技能的概念。
上述範例建立了 pytest-gen 和 pr-review 技能。現在練習建立一種完全不同類型的技能:用於從資料產生格式化輸出的技能。
/skills list。您也可以使用 ls .github/skills/ 查看專案技能,或使用 ls ~/.copilot/skills/ 查看個人技能。.github/skills/book-summary/SKILL.md 建立一個 book-summary 技能,用於產生書籍集合的格式化 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 技能,當您詢問書籍集合時,Copilot 會自動套用它。
commit-message 技能,以一致的格式產生慣例提交訊息copilot-skill 主題標籤分享/技能名稱 斜線指令直接呼叫技能.github/skills/ 用於專案/團隊共用,~/.copilot/skills/ 用於個人使用📋 快速參考:請參閱 GitHub Copilot CLI 指令參考,取得完整的指令和快捷鍵清單。
技能透過自動載入指令來擴展 Copilot 的功能。但如何連接到外部服務呢?這正是 MCP 的用武之地。
在第 06 章:MCP 伺服器中,您將學習:
| ← 返回第 04 章 | 繼續前往第 06 章 → |