分享一个生成 drawio UML 图的 ClaudeCode Skill (基于 draw.io 官方的提示词)

2026-04-11 14:191阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐
问题描述:

drawio 官方 MCP (如果喜欢用 MCP 的,无需看下去,建议使用 drawio 官方 MCP)

最近 draw.io 官方推特宣布推出了“官方 MCP Server”
CleanShot 2026-02-11 at 06.05.28@2x1338×1288 171 KB

推文地址: https://x.com/drawio/status/2020918870375370825
NPM 中的 MCP 地址: https://www.npmjs.com/package/@drawio/mcp
GitHub 地址: GitHub - jgraph/drawio-mcp

Skills (基于 drawio 官方的替代 MCP 的方案提示词)

需要有 python 环境

因为 MCP 每个请求都会携带完整的工具描述,对比 Skills 的渐进式披露 (即只带 Metadata/Frontmatter) 消耗的 Token 量要大得多,所以我更倾向使用 Skills

drawio 官方推上说提供了「提示词(指令)」来代替 MCP,因此我基于 drawio 官方的提示词,实现了一个 drawio-diagram Skill

drawio 官方 GitHub 说明: GitHub - jgraph/drawio-mcp
drawio 官方具体提示词地址: drawio-mcp/src/claude-project-instructions.txt at main · jgraph/drawio-mcp · GitHub

效果

  1. 输入提示词: 画一个 TCP 连接流程时序图 (默认生成中文说明,用其他语言是: 用英语画一个 TCP 连接流程时序图)

    也可以使用命令: /drawio-diagram 画一个 TCP 连接流程时序图 (默认中文说明)
    /drawio-diagram en 画一个 TCP 连接流程时序图 (指定 en 英语说明)

    CleanShot 2026-02-11 at 06.46.15@2x1920×1185 168 KB

  2. 生成一个备份 HTML 文件到当前 ClaudeCode 工作目录

    文件名称格式: {意图}-{类型}-{时间戳}.html

  3. 会自动打开浏览器,在 https://app.diagrams.net 中进行渲染出 UML 图
    CleanShot 2026-02-11 at 06.37.30@2x1920×1174 161 KB

安装

skill 文件目录 ($HOME/.claude/skills/drawio-diagram)

$ tree $HOME/.claude/skills/drawio-diagram . ├── SKILL.md └── references └── claude-project-instructions.md

SKILL.md

代码

--- name: drawio-diagram description: Use when the user requests any visual diagram using draw.io - flowcharts, architecture diagrams, UML, ERD, sequence diagrams, org charts, mind maps, wireframes, network topology, or other diagramming needs argument-hint: "[language: zh-CN (default) | en | ja | ...]" allowed-tools: - Write - Read - Bash(python3:*) - Bash(python:*) metadata: author: jgraph source: https://github.com/jgraph/drawio-mcp --- # Draw.io Diagram Generation Generate draw.io diagrams by creating compressed URLs via Python and opening them in the browser. ## Configuration ### Working Directory Current working directory: !`pwd` ### Language - **Default**: `zh-CN` (Chinese) - **Resolution order** (highest priority first): 1. `$0` argument from `/drawio-diagram [language]` invocation 2. Language preference explicitly mentioned in the user's request (e.g., "用英文画", "draw in Japanese") 3. Default: `zh-CN` - Diagram labels, notes, and descriptions should use the resolved language - Technical terms (protocol names, opcodes, HTTP headers, status codes, framework names, etc.) MUST remain in English - Examples: - `zh-CN`: "握手阶段" (not "Handshake Phase"), but keep "HTTP GET", "101 Switching Protocols" - `en`: "Handshake Phase", "HTTP GET", "101 Switching Protocols" (all English) ## Reference Read [claude-project-instructions.md](references/claude-project-instructions.md) (relative to this skill file) for the complete diagram generation instructions, including: - Supported diagram types - Format selection guide (Mermaid / CSV / XML) - Python URL generation code - Format examples (Mermaid, XML, CSV syntax) ## Agent CLI Adaptation The reference instructions mention "HTML artifact" — in Agent CLI environments there are no artifacts. Instead: 1. **Modify the Python script**: At the end of the script (after generating the URL), add `import webbrowser; webbrowser.open(url)` to directly open the draw.io URL in the default browser — no intermediate HTML page needed 2. **Save HTML backup**: Save the HTML to `{intent}-{type}-{timestamp}.html` in the current working directory (see Configuration > Working Directory above). Naming convention: - `{intent}`: 1-5 word kebab-case subject (e.g., `mqtt-conn`, `user-auth-flow`) - `{type}`: Abbreviated diagram type (e.g., `seq`, `flow`, `erd`, `arch`, `class`, `mind`) - `{timestamp}`: Format `YYYYMMDDHHMMSS` - Example: `mqtt-conn-seq-20260211143052.html` - **Retry / fix of a previous diagram** (e.g., user reports an error, asks for style tweaks, or requests content changes to the same diagram): Reuse the exact same filename to overwrite it - **Different diagram**: Generate a new filename ### URL Safety **NEVER** manually type, copy, or reproduce generated URLs in text responses. The URL contains compressed base64 data — even a single character change breaks it. Always let the Python script generate the URL and embed it directly in the HTML file.

references/claude-project-instructions.md

复制于 drawio 官方提示词
与 drawio 官方提示词差别是 45 行的 true 改为 True (ClaudeCode 说 drawio 官方提示词使用了错误的语法)

代码

# Draw.io Diagram Generation When the user requests any visual diagram, use draw.io to create it. ## Supported Diagrams Draw.io supports virtually any diagram type: - **Standard**: Flowcharts, org charts, mind maps, timelines, Venn diagrams - **Software**: UML (class, sequence, activity, use case), ERD, architecture diagrams - **Cloud/Infrastructure**: AWS, Azure, GCP, Kubernetes, network topology - **Engineering**: Electrical circuits, digital logic, P&ID, floor plans - **Business**: BPMN, value streams, customer journeys, SWOT - **UI/UX**: Wireframes, mockups, sitemaps - **And more**: Infographics, data flows, decision trees, etc. ## Format Selection Choose the optimal format for the task: | Format | Best For | |--------|----------| | **Mermaid** | Flowcharts, sequences, ERD, Gantt, state diagrams, class diagrams | | **CSV** | Hierarchical data (org charts), bulk import from spreadsheets | | **XML** | Complex layouts, precise positioning, custom styling, icons, shapes | ## URL Generation Execute this Python code to generate the draw.io URL and output it as an HTML artifact: ```python import json, zlib, base64 from urllib.parse import quote # Set these variables: diagram_type = "mermaid" # "mermaid", "xml", or "csv" diagram_code = """graph TD A[Start] --> B[End]""" # Generate compressed URL encoded = quote(diagram_code, safe='') c = zlib.compressobj(9, zlib.DEFLATED, -15) raw_deflate = c.compress(encoded.encode('utf-8')) + c.flush() data = base64.b64encode(raw_deflate).decode() payload = json.dumps({"type": diagram_type, "compressed": True, "data": data}) url = f"https://app.diagrams.net/?pv=0&grid=0#create={quote(payload, safe='')}" # Output as HTML page print(f"""<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; background: #f8f9fa; }} .card {{ text-align: center; background: white; border-radius: 12px; padding: 40px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }} .card h2 {{ margin: 0 0 8px; color: #1a1a1a; }} .card p {{ margin: 0 0 24px; color: #666; }} .btn {{ display: inline-block; padding: 14px 32px; background: #4285f4; color: white; text-decoration: none; border-radius: 8px; font-size: 16px; font-weight: 500; transition: background 0.2s; }} .btn:hover {{ background: #3367d6; }} </style> </head> <body> <div class="card"> <h2>Diagram Ready</h2> <p>Click below to open your diagram in draw.io</p> <a class="btn" href="{url}" target="_blank" rel="noopener noreferrer"> Open in draw.io </a> </div> </body> </html>""") ``` ## Format Examples ### Mermaid ``` graph TD A[Start] --> B{Decision} B -->|Yes| C[Action] B -->|No| D[End] ``` ### XML (draw.io native) ```xml <mxGraphModel> <root> <mxCell id="0"/> <mxCell id="1" parent="0"/> <mxCell id="2" value="Box" style="rounded=1;fillColor=#d5e8d4;" vertex="1" parent="1"> <mxGeometry x="100" y="100" width="120" height="60" as="geometry"/> </mxCell> </root> </mxGraphModel> ``` ### CSV (hierarchical data) ``` # label: %name% # style: rounded=1;whiteSpace=wrap;html=1; # connect: {"from":"manager","to":"name","invert":true} # layout: auto name,manager CEO, CTO,CEO CFO,CEO ``` ## Instructions 1. When a diagram is requested, determine the best format 2. Generate the diagram code 3. Execute the Python code to create the URL 4. **Create an HTML artifact** from the script output – this is the clickable link for the user ## CRITICAL: URL Output Rules **NEVER type, retype, or reproduce the generated URL in your chat response.** The URL contains compressed base64 data. Retyping it WILL corrupt it – even a single changed character breaks the link completely. Instead, follow this process: 1. Execute the Python script 2. The script outputs a complete HTML page with the correct link embedded 3. Present the HTML output as an artifact (the link inside is guaranteed correct because it was generated by the script, not by you) 4. In your chat message, simply tell the user to click the button in the artifact **DO NOT** copy the URL from the script output into your response text. The artifact IS the delivery mechanism for the link.

原理

drawio 官方的说明: GitHub - jgraph/drawio-mcp
CleanShot 2026-02-11 at 16.31.08@2x1522×936 173 KB

网友解答:
--【壹】--:

后排支持大佬!


--【贰】--:

大佬强的


--【叁】--:

这个确实很实用,之前画图很头疼,这回方便了,感谢大佬。


--【肆】--:

谢谢佬,刚好毕设可以用上


--【伍】--:

前排支持!


--【陆】--:

他实现的逻辑 其实内部还都是代码吧?在 iflow 里测试了一下。好像生成的主要是 mermaid 的代码?
CleanShot 2026-02-11 at 15.08.58666×350 34.7 KB
可以真正的画图吗?


--【柒】--:

不是,是 .claude/skills/drawio-diagram/references 目录下


--【捌】--:

挺好,谢谢分享


--【玖】--:

感谢佬友分享


--【拾】--:

感谢佬友分享


--【拾壹】--:

官方有skill了

github.com/jgraph/drawio-mcp

skill-cli/SKILL.md

main

--- name: drawio description: Generate draw.io diagrams as .drawio files, optionally export to PNG/SVG/PDF with embedded XML allowed-tools: Bash, Write --- # Draw.io Diagram Skill Generate draw.io diagrams as native `.drawio` files. Optionally export to PNG, SVG, or PDF with the diagram XML embedded (so the exported file remains editable in draw.io). ## How to create a diagram 1. **Generate draw.io XML** in mxGraphModel format for the requested diagram 2. **Write the XML** to a `.drawio` file in the current working directory using the Write tool 3. **If the user requested an export format** (png, svg, pdf), export using the draw.io CLI with `--embed-diagram`, then delete the source `.drawio` file 4. **Open the result** — the exported file if exported, or the `.drawio` file otherwise ## Choosing the output format Check the user's request for a format preference. Examples: 此文件已被截断。 显示原始文件


--【拾贰】--:

这个真不错,刚好开始学Skills,就拿佬友的来练手了


--【拾叁】--:

问题 1: 原理

原理是生成代码,但不是只是生成 Mermaid,还有 CSV, XML,这些是 drawio 都能支持并渲染出来

你可以理解为, mermaid/csv 为中间代码,会被 drawio 渲染引擎转换为 drawio 原生的 xml 代码,然后渲染

drawio 官方在 GitHub 上说了原理

看 How it Works
CleanShot 2026-02-11 at 16.31.08@2x1522×936 173 KB

问题 2: 可以真正的画图吗?

可以呀,最后自动打开浏览器跳转到 https://app.diagrams.net/?pv=0&grid=0#create=<encoded_payload>,那么就会被 drawio 渲染器,将生成的 Mermaid/CSV 渲染成 drawio 原生 xml,并加载到页面上,所以你可以在画布上随便修改,添加,这就是画图呀。


--【拾肆】--:

感谢大佬!


--【拾伍】--:

references/claude-project-instructions.md

这个文件是放到项目.claude/CLAUDE.md中吗


--【拾陆】--:

可以试试


--【拾柒】--:

感谢分享啊