drawio by jgraph/drawio-mcp
npx skills add https://github.com/jgraph/drawio-mcp --skill drawio生成 draw.io 图表作为原生的 .drawio 文件。可选择导出为 PNG、SVG 或 PDF 格式,并嵌入图表 XML(这样导出的文件在 draw.io 中仍可编辑)。
.drawio 文件--embed-diagram 参数导出,然后删除源 .drawio 文件。如果未找到 CLI,则保留 .drawio 文件并告知用户可以安装 draw.io 桌面应用以启用导出功能,或者直接打开 .drawio 文件.drawio 文件。如果打开命令失败,则打印文件路径以便用户手动打开广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
检查用户的请求以了解格式偏好。例如:
/drawio create a flowchart → flowchart.drawio/drawio png flowchart for login → login-flow.drawio.png/drawio svg: ER diagram → er-diagram.drawio.svg/drawio pdf architecture overview → architecture-overview.drawio.pdf如果未提及格式,只需写入 .drawio 文件并在 draw.io 中打开。用户随时可以要求稍后导出。
| 格式 | 嵌入 XML | 备注 |
|---|---|---|
png | 是 (-e) | 随处可查看,可在 draw.io 中编辑 |
svg | 是 (-e) | 可缩放,可在 draw.io 中编辑 |
pdf | 是 (-e) | 可打印,可在 draw.io 中编辑 |
jpg | 否 | 有损,不支持嵌入 XML |
PNG、SVG 和 PDF 都支持 --embed-diagram——导出的文件包含完整的图表 XML,因此在 draw.io 中打开可以恢复为可编辑的图表。
draw.io 桌面应用包含一个用于导出的命令行界面。
首先,检测环境,然后相应地定位 CLI:
当 /proc/version 包含 microsoft 或 WSL 时,检测到 WSL2:
grep -qi microsoft /proc/version 2>/dev/null && echo "WSL2"
在 WSL2 上,通过 /mnt/c/... 使用 Windows draw.io 桌面可执行文件:
DRAWIO_CMD=`/mnt/c/Program Files/draw.io/draw.io.exe`
需要使用反引号引用来处理 bash 中 Program Files 的空格。
如果 draw.io 安装在非默认位置,请检查常见的替代路径:
# 默认安装路径
`/mnt/c/Program Files/draw.io/draw.io.exe`
# 按用户安装(如果上述路径不存在)
`/mnt/c/Users/$WIN_USER/AppData/Local/Programs/draw.io/draw.io.exe`
/Applications/draw.io.app/Contents/MacOS/draw.io
drawio # 通常通过 snap/apt/flatpak 在 PATH 上
"C:\Program Files\draw.io\draw.io.exe"
在回退到特定平台路径之前,使用 which drawio(或在 Windows 上使用 where drawio)检查它是否在 PATH 上。
drawio -x -f <format> -e -b 10 -o <output> <input.drawio>
WSL2 示例:
`/mnt/c/Program Files/draw.io/draw.io.exe` -x -f png -e -b 10 -o diagram.drawio.png diagram.drawio
关键标志:
-x / --export: 导出模式-f / --format: 输出格式 (png, svg, pdf, jpg)-e / --embed-diagram: 在输出中嵌入图表 XML(仅限 PNG、SVG、PDF)-o / --output: 输出文件路径-b / --border: 图表周围的边框宽度(默认:0)-t / --transparent: 透明背景(仅限 PNG)-s / --scale: 缩放图表大小--width / --height: 适应指定尺寸(保持宽高比)-a / --all-pages: 导出所有页面(仅限 PDF)-p / --page-index: 选择特定页面(从 1 开始)| 环境 | 命令 |
|---|---|
| macOS | open <file> |
| Linux (原生) | xdg-open <file> |
| WSL2 | cmd.exe /c start "" "$(wslpath -w <file>)" |
| Windows | start <file> |
WSL2 注意事项:
wslpath -w <file> 将 WSL2 路径(例如 /home/user/diagram.drawio)转换为 Windows 路径(例如 C:\Users\...)。这是必需的,因为 cmd.exe 无法解析 /mnt/c/... 风格的路径。start 后面的空字符串 "" 是必需的,以防止 start 将文件名解释为窗口标题。WSL2 示例:
cmd.exe /c start "" "$(wslpath -w diagram.drawio)"
login-flow、database-schema)name.drawio.png、name.drawio.svg、name.drawio.pdf——这表示文件包含嵌入的图表 XML.drawio 文件——导出的文件包含完整的图表.drawio 文件是原生的 mxGraphModel XML。始终直接生成 XML——Mermaid 和 CSV 格式需要服务器端转换,无法保存为原生文件。
每个图表必须具有以下结构:
<mxGraphModel adaptiveColors="auto">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<!-- 图表单元格放在这里,parent="1" -->
</root>
</mxGraphModel>
id="0" 是根层id="1" 是默认的父层parent="1",除非使用多个层有关常见样式、样式属性、边路由详细信息(包括路径点)以及容器/分组示例,请参阅 references/xml-reference.md。
关键:每个边 mxCell 必须包含一个 <mxGeometry relative="1" as="geometry" /> 子元素,即使没有路径点。自闭合的边单元格(例如 <mxCell ... edge="1" ... />)是无效的,并且无法正确渲染。始终使用展开形式:
<mxCell id="e1" edge="1" parent="1" source="a" target="b" style="...">
<mxGeometry relative="1" as="geometry" />
</mxCell>
edgeStyle=orthogonalEdgeStyle 表示直角连接器(最常见)value 属性即可。完整的边路由和容器指南,请参阅 references/xml-reference.md。
对于嵌套元素,使用父子包含关系(parent="containerId")——不要只是堆叠形状。子元素在容器内使用相对坐标。有关容器类型、规则和示例,请参阅 references/xml-reference.md。
draw.io 支持自动深色模式渲染。颜色的行为取决于属性:
strokeColor、fillColor、fontColor 默认为 "default",在浅色主题下呈现为黑色,在深色主题下呈现为白色。当未设置显式颜色时,颜色会自动适应。fillColor=#DAE8FC)指定浅色模式下的颜色。深色模式下的颜色是通过反转 RGB 值(以 93% 的比例混合到反色)并将色调旋转 180°(通过 mxUtils.getInverseColor)自动计算的。light-dark() 函数——要显式指定两种颜色,请在样式字符串中使用 light-dark(lightColor,darkColor),例如 fontColor=light-dark(#7EA6E0,#FF0000)。第一个参数用于浅色模式,第二个用于深色模式。要启用深色模式颜色适应,mxGraphModel 元素必须包含 adaptiveColors="auto"。
生成图表时,通常不需要指定深色模式颜色——自动反转处理大多数情况。仅当自动反转的颜色不令人满意时,才使用 light-dark()。
完整的 draw.io 样式参考:https://www.drawio.com/doc/faq/drawio-style-reference.html
XML 模式定义 (XSD):https://www.drawio.com/assets/mxfile.xsd
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 未找到 draw.io CLI | 桌面应用未安装或不在 PATH 上 | 保留 .drawio 文件并告知用户安装 draw.io 桌面应用,或手动打开文件 |
| 导出产生空/损坏的文件 | XML 无效(例如,注释中有双连字符,未转义的特殊字符) | 在写入前验证 XML 格式是否正确;请参阅下面的 XML 格式正确性部分 |
| 图表打开但显示空白 | 缺少根单元格 id="0" 和 id="1" | 确保基本的 mxGraphModel 结构完整 |
| 边未渲染 | 边 mxCell 是自闭合的(没有子 mxGeometry 元素) | 每条边都必须有一个 <mxGeometry relative="1" as="geometry" /> 作为子元素 |
| 导出后文件无法打开 | 文件路径不正确或缺少文件关联 | 打印绝对文件路径,以便用户手动打开 |
--)。 根据 XML 规范,-- 在 <!-- --> 内是非法的,会导致解析错误。使用单连字符或重新措辞。&、<、>、"mxCell 使用唯一的 id 值每周安装次数
352
仓库
GitHub 星标
1.5K
首次出现
2026年2月22日
安全审计
安装于
opencode343
gemini-cli342
codex342
amp341
kimi-cli341
cursor341
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).
.drawio file in the current working directory using the Write tool--embed-diagram, then delete the source .drawio file. If the CLI is not found, keep the .drawio file and tell the user they can install the draw.io desktop app to enable export, or open the .drawio file directly.drawio file otherwise. If the open command fails, print the file path so the user can open it manuallyCheck the user's request for a format preference. Examples:
/drawio create a flowchart → flowchart.drawio/drawio png flowchart for login → login-flow.drawio.png/drawio svg: ER diagram → er-diagram.drawio.svg/drawio pdf architecture overview → architecture-overview.drawio.pdfIf no format is mentioned, just write the .drawio file and open it in draw.io. The user can always ask to export later.
| Format | Embed XML | Notes |
|---|---|---|
png | Yes (-e) | Viewable everywhere, editable in draw.io |
svg | Yes (-e) | Scalable, editable in draw.io |
pdf | Yes (-e) | Printable, editable in draw.io |
jpg |
PNG, SVG, and PDF all support --embed-diagram — the exported file contains the full diagram XML, so opening it in draw.io recovers the editable diagram.
The draw.io desktop app includes a command-line interface for exporting.
First, detect the environment, then locate the CLI accordingly:
WSL2 is detected when /proc/version contains microsoft or WSL:
grep -qi microsoft /proc/version 2>/dev/null && echo "WSL2"
On WSL2, use the Windows draw.io Desktop executable via /mnt/c/...:
DRAWIO_CMD=`/mnt/c/Program Files/draw.io/draw.io.exe`
The backtick quoting is required to handle the space in Program Files in bash.
If draw.io is installed in a non-default location, check common alternatives:
# Default install path
`/mnt/c/Program Files/draw.io/draw.io.exe`
# Per-user install (if the above does not exist)
`/mnt/c/Users/$WIN_USER/AppData/Local/Programs/draw.io/draw.io.exe`
/Applications/draw.io.app/Contents/MacOS/draw.io
drawio # typically on PATH via snap/apt/flatpak
"C:\Program Files\draw.io\draw.io.exe"
Use which drawio (or where drawio on Windows) to check if it's on PATH before falling back to the platform-specific path.
drawio -x -f <format> -e -b 10 -o <output> <input.drawio>
WSL2 example:
`/mnt/c/Program Files/draw.io/draw.io.exe` -x -f png -e -b 10 -o diagram.drawio.png diagram.drawio
Key flags:
-x / --export: export mode-f / --format: output format (png, svg, pdf, jpg)-e / --embed-diagram: embed diagram XML in the output (PNG, SVG, PDF only)-o / --output: output file path-b / --border: border width around diagram (default: 0)-t / : transparent background (PNG only)| Environment | Command |
|---|---|
| macOS | open <file> |
| Linux (native) | xdg-open <file> |
| WSL2 | cmd.exe /c start "" "$(wslpath -w <file>)" |
| Windows | start <file> |
WSL2 notes:
wslpath -w <file> converts a WSL2 path (e.g. /home/user/diagram.drawio) to a Windows path (e.g. C:\Users\...). This is required because cmd.exe cannot resolve /mnt/c/... style paths."" after start is required to prevent start from interpreting the filename as a window title.WSL2 example:
cmd.exe /c start "" "$(wslpath -w diagram.drawio)"
login-flow, database-schema)name.drawio.png, name.drawio.svg, name.drawio.pdf — this signals the file contains embedded diagram XML.drawio file — the exported file contains the full diagramA .drawio file is native mxGraphModel XML. Always generate XML directly — Mermaid and CSV formats require server-side conversion and cannot be saved as native files.
Every diagram must have this structure:
<mxGraphModel adaptiveColors="auto">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<!-- Diagram cells go here with parent="1" -->
</root>
</mxGraphModel>
id="0" is the root layerid="1" is the default parent layerparent="1" unless using multiple layersConsult references/xml-reference.md for common styles, style properties, edge routing details (including waypoints), and container/group examples.
CRITICAL: Every edgemxCell must contain a <mxGeometry relative="1" as="geometry" /> child element, even when there are no waypoints. Self-closing edge cells (e.g. <mxCell ... edge="1" ... />) are invalid and will not render correctly. Always use the expanded form:
<mxCell id="e1" edge="1" parent="1" source="a" target="b" style="...">
<mxGeometry relative="1" as="geometry" />
</mxCell>
edgeStyle=orthogonalEdgeStyle for right-angle connectors (most common)value attribute directly.See references/xml-reference.md for full edge routing and container guidance.
Use parent-child containment (parent="containerId") for nested elements — do not just stack shapes. Children use relative coordinates within the container. See references/xml-reference.md for container types, rules, and examples.
draw.io supports automatic dark mode rendering. How colors behave depends on the property:
strokeColor, fillColor, fontColor default to "default", which renders as black in light theme and white in dark theme. When no explicit color is set, colors adapt automatically.fillColor=#DAE8FC) specify the light-mode color. The dark-mode color is computed automatically by inverting the RGB values (blending toward the inverse at 93%) and rotating the hue by 180° (via mxUtils.getInverseColor).light-dark() function — To specify both colors explicitly, use light-dark(lightColor,darkColor) in the style string, e.g. fontColor=light-dark(#7EA6E0,#FF0000). The first argument is used in light mode, the second in dark mode.To enable dark mode color adaptation, the mxGraphModel element must include adaptiveColors="auto".
When generating diagrams, you generally do not need to specify dark-mode colors — the automatic inversion handles most cases. Use light-dark() only when the automatic inverse color is unsatisfactory.
For the complete draw.io style reference: https://www.drawio.com/doc/faq/drawio-style-reference.html
For the XML Schema Definition (XSD): https://www.drawio.com/assets/mxfile.xsd
| Problem | Cause | Solution |
|---|---|---|
| draw.io CLI not found | Desktop app not installed or not on PATH | Keep the .drawio file and tell the user to install the draw.io desktop app, or open the file manually |
| Export produces empty/corrupt file | Invalid XML (e.g. double hyphens in comments, unescaped special characters) | Validate XML well-formedness before writing; see the XML well-formedness section below |
| Diagram opens but looks blank | Missing root cells id="0" and id="1" | Ensure the basic mxGraphModel structure is complete |
| Edges not rendering | Edge mxCell is self-closing (no child mxGeometry element) | Every edge must have <mxGeometry relative="1" as="geometry" /> as a child element |
--) inside XML comments. -- is illegal inside <!-- --> per the XML spec and causes parse errors. Use single hyphens or rephrase.&, <, >, "id values for each mxCellWeekly Installs
352
Repository
GitHub Stars
1.5K
First Seen
Feb 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode343
gemini-cli342
codex342
amp341
kimi-cli341
cursor341
xdrop 文件传输脚本:Bun 环境下安全上传下载工具,支持加密分享
20,700 周安装
| No |
| Lossy, no embedded XML support |
--transparent-s / --scale: scale the diagram size--width / --height: fit into specified dimensions (preserves aspect ratio)-a / --all-pages: export all pages (PDF only)-p / --page-index: select a specific page (1-based)| File won't open after export | Incorrect file path or missing file association | Print the absolute file path so the user can open it manually |