Skip to content

cli: add a built-in MCP server for D2 - #3058

Merged
alixander merged 10 commits into
masterfrom
feat/d2-mcp
Nov 26, 2026
Merged

alixander merged 10 commits into
masterfrom
feat/d2-mcp

Conversation

@alixander

@alixander alixander commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Human


AI

Closes #2518.

Adds d2 mcp, a local stdio server with three tools. guide provides working D2 examples and a reference generated from the running compiler. check reports source diagnostics and can return formatted D2. render returns a PNG by default, or SVG or text, and supports named boards.

Start it with d2 mcp for inline source or d2 mcp --root . to allow .d2 source files, imports, and local images beneath the current project. Diagram paths and their symlink targets must have a .d2 extension. Private network addresses remain blocked. Each call has a configurable deadline. A render that completes successfully still returns its diagram. Up to two checks or renders can run concurrently. The optional d2nomcp build tag excludes the SDK.

The viewer displayed the diagram in the official MCP Apps basic-host v2.0.4. Zoom, Fit, and PNG fallback worked in Chromium. MCP Inspector 2.16.0 completed the handshake and received the diagram, but its default content security policy blocks embedded images. The viewer requests no network domains.

Built on macOS arm64 with Go 1.27.4, -trimpath, and -ldflags "-s -w". The stripped binary grew from 39,966,642 bytes on master at 032ef6b7 to 41,335,698 bytes (+1,369,056 bytes, within the 2,000,000-byte budget). The Intel macOS release archive measured 18,993,738 bytes locally against the 19,000,000-byte ceiling. Native and WebAssembly builds use the same bundled TTF files and produce byte-identical SVG font data URIs. All three tools share the SDK’s pointer-shaped handler code while retaining their own schemas. The official Go MCP SDK is pinned to v1.8.0.

Verification

  • ./make.sh fmt, ./make.sh lint, ./make.sh test pass.

  • go test -race ./d2cli/ -run TestMCP passes, including eight concurrent renders.

  • go build -tags d2nomcp ./... passes. The resulting CLI reports the expected usage error for mcp.

  • All specification cases T1–T42 exist and pass. Coverage includes filesystem boundaries, image policy, exact PNG limits, UTF-8 byte positions, tool discovery, and both protocol generations.

  • The Appendix D shell transcript works. The 2025-06-18 handshake and 2026-07-28 discovery path are also covered by CLI tests.

  • Every stdout line in the stdio transcript is a JSON protocol message, including when the client closes stdin immediately.

  • Stripped binary sizes and their difference are recorded above.

  • CLI help, man page, and changelog are updated.

  • No existing test output changed.

  • The SDK version is recorded above.

  • Compiler-backed tests exercise both forms of all 17 rows in the guide’s “Mistakes to avoid” table. PNG tests verify that a missing icon produces a placeholder and a warning with the viewer enabled or disabled.

  • Both protocol versions completed discovery, viewer reads, recovery from an unavailable tool, and PNG, SVG and text rendering with the stripped binary. The fixture imported a UTF-16LE diagram.

  • Tall diagrams fit the host's available height in Chromium and WebKit. The browser tests exercise Zoom and Fit after the MCP Apps handshake.

  • Rendering tests cover selected-board layout validation, optional preview failures, preview size limits and expanded text labels. For three representative diagrams, all nine native CLI outputs across PNG, SVG and text matched master byte for byte.

  • govulncheck ./d2cli reported no known vulnerabilities.

  • Exercised LSP and MCP over stdio from one binary. LSP found and renamed the declaration, edge endpoint and near target. MCP then checked and rendered that edited diagram. The d2nomcp binary still served LSP and reported the expected MCP exclusion.

  • Default, monospace and sketch font renders matched byte for byte before and after the font packaging change across SVG, PNG and text.

  • sent from alixander's Codex

…ings

Exercise each common mistake and correction in the embedded guide against the compiler.

- sent from alixander's Codex
- sent from alixander's Codex
- sent from alixander's Codex
Preserve the requested drawing when optional viewer work fails, honor cancellation during SVG and text rendering, and bound expanded previews and text rows. Normalize file encoding for byte diagnostics and check the viewer in Chromium and WebKit.

- sent from alixander's Codex
Using pointers lets the Go compiler emit one pointer-shaped instantiation for all three SDK handlers. Each tool keeps its own concrete input schema and validation.

- sent from alixander's Codex
The combined LSP and MCP release exceeded the Intel Mac archive size budget. Reuse the existing WebAssembly initializer to embed the TTF bytes directly and preserve each SVG font data URI byte for byte.

- sent from alixander's Codex
@alixander
alixander merged commit 89a3c1f into master Nov 26, 2026
20 checks passed
@alixander
alixander deleted the feat/d2-mcp branch November 26, 2026 17:03
alixander added a commit that referenced this pull request Nov 26, 2026
Master adds the CLI's MCP and LSP servers (#3058, #3059), uvx
distribution (#3061) and a setup-d2 doc (#3062), and makes native builds
embed the TTFs the way master's WebAssembly initializer did (9064887).

d2fonts_embed.go and d2fonts_woff.go are master's, limited to native
builds, so the native CLI's font bytes match master's exactly.
WebAssembly builds keep this branch's d2fonts_embed_wasm.go, which loads
the same TTFs from ttf_stream.br. go.sum is master's, tidied.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does Terrastruct consider building an MCP server for D2?

1 participant