此示例创建一个教 Claude 使用视觉图表和类比来解释代码的技能。由于它使用默认前置元数据,Claude 可以在你询问某事如何工作时自动加载它,或者你可以使用 /explain-code 直接调用它。
1
创建技能目录
在你的个人技能文件夹中为技能创建一个目录。个人技能在所有项目中都可用。
Copy
mkdir -p ~/.claude/skills/explain-code
2
编写 SKILL.md
每个技能都需要一个 SKILL.md 文件,包含两部分:YAML 前置元数据(在 --- 标记之间)告诉 Claude 何时使用该技能,以及包含 Claude 在调用技能时遵循的说明的 markdown 内容。name 字段变成 /slash-command,description 帮助 Claude 决定何时自动加载它。创建 ~/.claude/skills/explain-code/SKILL.md:
Copy
---name: explain-codedescription: Explains code with visual diagrams and analogies. Use when explaining how code works, teaching about a codebase, or when the user asks "how does this work?"---When explaining code, always include:1. **Start with an analogy**: Compare the code to something from everyday life2. **Draw a diagram**: Use ASCII art to show the flow, structure, or relationships3. **Walk through the code**: Explain step-by-step what happens4. **Highlight a gotcha**: What's a common mistake or misconception?Keep explanations conversational. For complex concepts, use multiple analogies.
技能文件可以包含任何说明,但思考你想如何调用它们有助于指导包含的内容:参考内容添加 Claude 应用于你当前工作的知识。约定、模式、风格指南、领域知识。此内容内联运行,以便 Claude 可以将其与你的对话上下文一起使用。
Copy
---name: api-conventionsdescription: API design patterns for this codebase---When writing API endpoints:- Use RESTful naming conventions- Return consistent error formats- Include request validation
任务内容为 Claude 提供特定操作的分步说明,如部署、提交或代码生成。这些通常是你想使用 /skill-name 直接调用的操作,而不是让 Claude 决定何时运行它们。添加 disable-model-invocation: true 以防止 Claude 自动触发它。
Copy
---name: deploydescription: Deploy the application to productioncontext: forkdisable-model-invocation: true---Deploy the application:1. Run the test suite2. Build the application3. Push to the deployment target
你的 SKILL.md 可以包含任何内容,但思考你想如何调用技能(由你、由 Claude 或两者)以及你想在哪里运行它(内联或在子代理中)有助于指导包含的内容。对于复杂的技能,你也可以添加支持文件以保持主技能专注。
默认情况下,你和 Claude 都可以调用任何没有设置 disable-model-invocation: true 的技能。你可以键入 /skill-name 直接调用它,Claude 可以在与你的对话相关时自动加载它。两个前置元数据字段让你限制这一点:
disable-model-invocation: true:只有你可以调用该技能。用于有副作用或你想控制时间的工作流,如 /commit、/deploy 或 /send-slack-message。你不希望 Claude 因为你的代码看起来准备好了就决定部署。
user-invocable: false:只有 Claude 可以调用该技能。用于不可作为命令操作的背景知识。legacy-system-context 技能解释了旧系统的工作原理。Claude 在相关时应该知道这一点,但 /legacy-system-context 对用户来说不是一个有意义的操作。
此示例创建一个只有你可以触发的部署技能。disable-model-invocation: true 字段防止 Claude 自动运行它:
Copy
---name: deploydescription: Deploy the application to productiondisable-model-invocation: true---Deploy $ARGUMENTS to production:1. Run the test suite2. Build the application3. Push to the deployment target4. Verify the deployment succeeded
以下是两个字段如何影响调用和上下文加载:
前置元数据
你可以调用
Claude 可以调用
何时加载到上下文
(默认)
是
是
描述始终在上下文中,调用时加载完整技能
disable-model-invocation: true
是
否
描述不在上下文中,你调用时加载完整技能
user-invocable: false
否
是
描述始终在上下文中,调用时加载完整技能
在常规会话中,技能描述被加载到上下文中,以便 Claude 知道什么可用,但完整的技能内容仅在调用时加载。预加载技能的子代理的工作方式不同:完整的技能内容在启动时注入。
你和 Claude 都可以在调用技能时传递参数。参数可通过 $ARGUMENTS 占位符获得。此技能通过编号修复 GitHub 问题。$ARGUMENTS 占位符被替换为技能名称后面的任何内容:
Copy
---name: fix-issuedescription: Fix a GitHub issuedisable-model-invocation: true---Fix GitHub issue $ARGUMENTS following our coding standards.1. Read the issue description2. Understand the requirements3. Implement the fix4. Write tests5. Create a commit
---name: deep-researchdescription: Research a topic thoroughlycontext: forkagent: Explore---Research $ARGUMENTS thoroughly:1. Find relevant files using Glob and Grep2. Read and analyze the code3. Summarize findings with specific file references
技能可以捆绑并运行任何语言的脚本,给 Claude 超越单个提示中可能的功能。一个强大的模式是生成视觉输出:在浏览器中打开的交互式 HTML 文件,用于探索数据、调试或创建报告。此示例创建一个代码库浏览器:一个交互式树视图,你可以在其中展开和折叠目录、一目了然地查看文件大小,并按颜色识别文件类型。创建技能目录:
创建 ~/.claude/skills/codebase-visualizer/SKILL.md。描述告诉 Claude 何时激活此技能,说明告诉 Claude 运行捆绑的脚本:
Copy
---name: codebase-visualizerdescription: Generate an interactive collapsible tree visualization of your codebase. Use when exploring a new repo, understanding project structure, or identifying large files.allowed-tools: Bash(python:*)---# Codebase VisualizerGenerate an interactive HTML tree view that shows your project's file structure with collapsible directories.## UsageRun the visualization script from your project root:```bashpython ~/.claude/skills/codebase-visualizer/scripts/visualize.py .```This creates `codebase-map.html` in the current directory and opens it in your default browser.## What the visualization shows- **Collapsible directories**: Click folders to expand/collapse- **File sizes**: Displayed next to each file- **Colors**: Different colors for different file types- **Directory totals**: Shows aggregate size of each folder
创建 ~/.claude/skills/codebase-visualizer/scripts/visualize.py。此脚本扫描目录树并生成一个自包含的 HTML 文件,具有: