d2-mcp
A small Go MCP server for validating and rendering D2 diagrams. Send complete source and rendering options; receive PNG, SVG, or text. The default server keeps no documents, artifacts, or HTTP sessions. It needs no D2 executable, browser, or PNG converter.
d2-mcp 2.0.0 uses D2 0.9.0, Go 1.27.2, and the official Go MCP SDK 1.8.0. D2's Dagre, ELK, and open-source TALA layouts are bundled. See inputs and limits.
Install
Install a 2.0.0 binary or build from source, then connect Claude, Codex, Pi, or VS Code. See what shipped in 2.0.0 and the 1.2.0 migration notes.
For hosted use without installing a binary, connect to https://d2.horv.co/mcp
using the remote HTTP client commands.
Clients and transports
Stdio remains the default. For Claude Desktop or another client using mcpServers:
{
"mcpServers": {
"d2": {
"command": "/absolute/path/d2-mcp",
"args": ["--image-type", "png"]
}
}
}
For a stateless Streamable HTTP endpoint:
./d2-mcp --transport http --host 127.0.0.1 --port 8080
Connect a Streamable HTTP MCP client to http://127.0.0.1:8080/mcp. For example, a VS Code .vscode/mcp.json entry:
{
"servers": {
"d2": {"type": "http", "url": "http://127.0.0.1:8080/mcp"}
}
}
HTTP uses JSON responses, no session ID, and no background event stream. Every call carries its source/options; subsequent calls may go to another replica. GET/DELETE on /mcp return 405. Current MCP 2026-07-28 discovery and legacy initialization are handled by the SDK; integration tests cover 2025-03-26, 2025-06-18, 2025-11-25, and 2026-07-28 over HTTP. Stdio and legacy SSE retain their supported initialization protocols.
Send exactly one JSON-RPC object per HTTP POST. Batches are rejected on every protocol version, including legacy SSE message posts, to prevent unbounded aggregation of tool results. Disconnects cancel current-protocol (2026-07-28) work cooperatively; the SDK retains older protocol calls until their application deadline.
Legacy SSE remains available at /sse with --transport sse (or --sse). Its SDK-managed connections are stateful and need a stable server connection. Prefer HTTP for new integrations. MCP_TRANSPORT, PORT, and legacy SSE_MODE/SSE_PORT are still accepted; explicit transport/port flags take precedence, and --sse selects SSE.
HTTP binds to loopback by default, rejects cross-origin browser requests, and retains the SDK's localhost/DNS-rebinding protection. There is no built-in authentication or multi-user authorization. If exposing it beyond a trusted local environment, supply authentication, TLS, request limits, and process resource limits in your deployment. The optional Cloudflare deployment supports private bearer access or explicit token-free public access with the runtime setting D2_MCP_PUBLIC=true and a shared request limit.
Optional diagram viewer
Add --ui to offer a bundled MCP Apps viewer to compatible
hosts. It previews inline diagrams, supports pan/zoom/fit and copying D2 source,
and offers PNG downloads when the host advertises them. Ask the agent to change
the source or theme; the viewer has no separate editable diagram state.
The flag defaults to off. Ordinary tool names, results, and stateless HTTP
behavior are unchanged; no Node runtime, website, or download store is needed.
./d2-mcp --transport http --host 127.0.0.1 --port 8080 --ui
Connect to /mcp using the client's custom MCP configuration. A remote host
needs your operator-provided HTTPS endpoint, not a marketplace listing.
Client availability and download support vary; see the guide's evidence and
limitations. The viewer is included in the 2.0.0 binaries.
Tools
compile-d2: validate syntax, semantics, layout, and server limits, with located diagnostics when available.
render-d2: render one diagram and return its artifact.
fetch_d2_cheat_sheet: retrieve the bundled reference and supported subset.
Both diagram tools require exactly one of code or file_path. file_path is a relative .d2 path inside an explicitly configured --input-dir; it refers to the server's filesystem. Agents without local files should send code. Input is never fetched from a URL.
Example tools/call parameters:
{
"name": "render-d2",
"arguments": {
"code": "direction: right\nclient -> api -> database\ndatabase.shape: cylinder",
"format": "png"
}
}