skills/d2/SKILL.md
Use when writing, generating, reviewing, or improving D2 diagram code. Trigger when the user asks to create architecture diagrams, flowcharts, sequence diagrams, ER diagrams, class diagrams, or any diagram using D2 syntax. Also trigger when working with .d2 files or when the d2_render MCP tool is available.
npx skillsauth add itsjool/d2-mcp d2Install this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
D2 is a declarative diagram scripting language that compiles to SVG. Text → diagrams. Docs: https://d2lang.com | Playground: https://play.d2lang.com
Choose the right diagram type before writing any code:
| User wants to show... | Use this D2 pattern |
|-----------------------|---------------------|
| System components and how they connect | Containers + connections (architecture) |
| Request/response flow, API interactions, message order | shape: sequence_diagram |
| A process, algorithm, or decision tree | Shapes + directed connections (flowchart) |
| Database tables and foreign keys | shape: sql_table (ER diagram) |
| Class hierarchies, object relationships | shape: class (UML class diagram) |
| System context: users, systems, boundaries (C4) | Containers + shape: c4-person + theme 303 |
| A state machine or lifecycle | Shapes + labeled connections (state diagram) |
| Progressive reveal / storytelling | steps: {} with animate_interval |
| Multiple views of the same system | layers: {} or scenarios: {} |
| A dashboard / grid of metrics | Container with grid-rows / grid-columns |
Default shape: rectangle. Use direction: right or direction: down to control flow.
Pick the diagram type (use the table above)
Draft the D2 code — start with core entities, add connections, add style last
Call d2_inspect, then draw an ASCII replica from its output:
d2_inspect(d2_code=...)
Use the inspect output to hand-draw a clean ASCII diagram using box-drawing characters (┌─┐│└┘├┤┬┴┼ ▶ ◀ ↓ ↑). You are generating this ASCII — not the D2 renderer. This avoids all of D2's known ASCII rendering bugs (cross-container connections, reverse edges, direction:right).
How to draw it:
┌─ Control Plane ──────┐→ for forward, ← for reverse, ↔ for bidirectional, -- for undirectedPut the result in a code fence and ask the user if the structure looks right:
Here's the structure:
┌─ SaaS Cloud ─────────────────────────────┐
│ ┌──────────────────┐ │
│ │ Control Plane │ │
│ │ ┌─────────────┐ │ │
│ │ │ API Gateway │ │ │
│ │ └──────┬──────┘ │ │
│ │ route │ │
│ │ ┌──────▼──────┐ │ │
│ │ │ gRPC Server │ │ │
│ │ └──────┬──────┘ │ │
│ └─────────┼────────┘ │
└────────────┼────────────────────────────-┘
│ mirror request →
┌─ Customer Edge ──────────────────────────┐
│ ┌──────────────────┐ │
│ │ Data Plane Agent│ │
│ │ ┌─────────────┐ │ │
│ │ │ gRPC Client │ │ │
│ │ └─────────────┘ │ │
│ └──────────────────┘ │
└──────────────────────────────────────────┘
Does this look right? I'll render the SVG if so.
Rules:
d2_inspect first — draw from its output, not from memoryd2_render(ascii=true) — D2's ASCII renderer has known bugsOnly if the user confirms the structure, render SVG: d2_render(d2_code=..., skip_fonts=true)
If invalid at either step, call d2_validate to get specific line/column errors
When saving: write the .d2 source first (instant), SVG render last (see Saving Rendered SVGs)
Iterate — adjust based on feedback, re-render ASCII to confirm, then SVG
Rule: skip_fonts defaults to true — never override it unless the user explicitly asks for embedded fonts. System fonts are indistinguishable from Source Sans Pro in practice, and skipping fonts reduces render time and SVG size by ~500KB.
# Bare identifier = rectangle by default
server
# With a display label (key vs label — connections use the KEY, not the label)
server: "API Server"
# Set shape type
db: {
shape: cylinder
}
# Multiple on one line
web; app; db
# Inline style shorthand
cache: { shape: queue; style.fill: "#fce8e6" }
Available shape types:
rectangle (default), square, circle, oval, diamond, hexagon, cloud,
cylinder, queue, package, parallelogram, document, page, step,
callout, stored_data, person, c4-person,
sql_table, class, sequence_diagram, image
A -> B # directed arrow
A <- B # reverse
A -- B # undirected line
A <-> B # bidirectional
A -> B: label # with label
# Chaining
A -> B -> C -> D
# Multiple connections (creates SEPARATE arrows — D2 never merges them)
A -> B: "first"
A -> B: "second" # distinct second arrow, not an override
# Reference a specific connection (0-indexed) to style it
(A -> B)[0].style.stroke: red
cloud: {
label: "AWS"
vpc: {
web: "Web Tier"
app: "App Tier"
web -> app
}
}
# Cross-container connections: use _ to refer to the parent scope
cloud: {
aws: {
db
db -> _.gcloud.backup # _ = parent scope (cloud)
}
gcloud: {
backup
}
}
explanation: |md
## Architecture Overview
This diagram shows the **three-tier** architecture.
- Web tier
- App tier
- Data tier
|
formula: |latex
\frac{\partial f}{\partial x} = 2x
|
my_shape: {
style: {
fill: "#4a90d9" # background color
stroke: "#2c5f8a" # border color
stroke-width: 2
stroke-dash: 5 # dashed border
border-radius: 8 # rounded corners
font-size: 14
font-color: white
opacity: 0.9
shadow: true
bold: true
italic: false
3d: true # rectangles/squares only
multiple: true # stacked visual — implies "many instances"
double-border: true # rectangles and ovals only
text-transform: uppercase
}
}
A -> B: {
style: {
stroke: red
stroke-width: 3
stroke-dash: 5
animated: true # flowing animation — use to show data/request flow
bold: true
font-color: "#666"
}
}
A -> B: {
source-arrowhead: {
shape: diamond
style.filled: true
}
target-arrowhead: {
shape: circle
}
}
Arrowhead shapes: triangle (default), arrow, diamond, circle, box,
cf-one, cf-many, cf-one-required, cf-many-required, cross
Use cf-* arrowheads on ER connections to show cardinality (crow's foot notation).
# Style all shapes
*.style.fill: "#f0f4ff"
*.style.stroke: "#3b5bdb"
*.style.border-radius: 6
# Style all connections
(* -> *)[*].style.stroke: "#888"
(* -> *)[*].style.animated: true
# Scoped glob — only shapes inside this container
cloud: {
*.style.fill: "#e8f5e9"
}
classes: {
important: {
style: { stroke: red; stroke-width: 3; bold: true }
}
faded: {
style: { opacity: 0.4 }
}
external: {
style: { stroke-dash: 5; fill: "#f8f8f8" }
}
}
# Apply to shapes
critical_db.class: important
legacy_service.class: faded
# Apply multiple (left-to-right, later class wins on conflicts)
service.class: [important; faded]
# Apply to connections
A -> B: { class: important }
vars: {
primary: "#3b5bdb"
secondary: "#74c0fc"
accent: "#f06595"
# In-file config (overridden by d2_render tool params)
d2-config: {
theme-id: 0
theme-overrides: {
B1: "#0057b8" # brand primary — maps to the main accent color
N7: "#1a1a2e" # darkest neutral
}
}
}
server: {
style.fill: ${primary}
style.stroke: ${secondary}
}
Color override codes: N1–N7 (neutrals), B1–B6 (brand), AA2–AA5 (accent A), AB4–AB5 (accent B)
*.style.border-radius: 6
*.style.font-size: 13
direction: right
internet: { shape: cloud; label: "Internet" }
frontend: {
label: "Frontend\n(React)"
icon: https://icons.terrastruct.com/dev/react.svg
style.fill: "#e8f4fd"
}
api: { label: "API Gateway"; style.fill: "#fff3cd" }
services: {
label: "Microservices"
style.fill: "#f8f9fa"
auth: "Auth Service"
orders: "Orders Service"
payments: "Payments Service"
}
db: { shape: cylinder; label: "PostgreSQL"; style.fill: "#d4edda" }
cache: { shape: queue; label: "Redis"; style.fill: "#fce8e6" }
internet -> frontend
frontend -> api: "HTTPS"
api -> services.auth: "JWT validate"
api -> services.orders
api -> services.payments
services.orders -> db
services.payments -> db
services.auth -> cache: "session"
direction: down
start: { shape: circle; style.fill: "#4caf50"; style.font-color: white }
end_ok: { shape: circle; style.fill: "#4caf50"; style.font-color: white; label: "Done" }
end_err: { shape: circle; style.fill: "#f44336"; style.font-color: white; label: "Failed" }
validate: { shape: diamond; label: "Valid?" }
process: "Process Request"
notify: "Send Notification"
error: "Return Error"
start -> process
process -> validate
validate -> notify: "Yes"
validate -> error: "No"
notify -> end_ok
error -> end_err
auth_flow: {
shape: sequence_diagram
# Declare actors in display order
client
gateway
auth
db
client -> gateway: "POST /login"
gateway -> auth: "validate(credentials)"
auth -> db: "SELECT user WHERE email=?"
db -> auth: "user record"
# Note on a specific actor (no connections = annotation)
auth."checks bcrypt hash"
auth -> gateway: "JWT token"
gateway -> client: "200 OK + token"
# Group / fragment
error_case: {
gateway -> client: "401 Unauthorized"
}
}
Key rules for sequence diagrams:
users: {
shape: sql_table
id: uuid {constraint: primary_key}
email: varchar(255) {constraint: [unique; not_null]}
name: varchar(100)
org_id: uuid {constraint: foreign_key}
created_at: timestamptz
}
organizations: {
shape: sql_table
id: uuid {constraint: primary_key}
name: varchar(255) {constraint: not_null}
}
posts: {
shape: sql_table
id: uuid {constraint: primary_key}
author_id: uuid {constraint: foreign_key}
title: varchar(255)
body: text
}
# FK connections — connect column to column
users.org_id -> organizations.id
posts.author_id -> users.id
SQL table notes:
stroke styles the table body; fill styles the header rowprimary_key, foreign_key, unique, not_null{constraint: [primary_key; not_null]}UserService: {
shape: class
# Fields: visibility + name: type
# Quote the key if params contain ":", quote the value if it contains "[]"
-db: Database
"-users": "User[]"
"#cache": Cache
# Methods: visibility + name(params): return
+getUser(): User
"+createUser(data: UserInput)": User
"-validateEmail(email: string)": bool
}
User: {
shape: class
+id: string
+email: string
+name: string
+createdAt: Date
}
# Relationships as connections with labels
UserService -> User: uses
UserRepo -> User: manages
Visibility: + public, - private, # protected
Quoting rules:
[] → quote the value: "-users": "User[]": → quote the key: "+method(id: string)": ReturnTypeUse theme 303 (C4) for C4 diagrams — it styles containers with the canonical C4 look.
# Render with: theme_id=303
direction: right
vars: {
d2-config: {
theme-id: 303
}
}
# Actors
user: {
shape: c4-person
label: "Customer"
style.fill: "#08427b"
style.font-color: white
}
# Systems
web_app: {
label: "Web Application\n[React SPA]"
style.fill: "#1168bd"
style.font-color: white
}
api: {
label: "API Server\n[Node.js / Express]"
style.fill: "#1168bd"
style.font-color: white
}
db: {
shape: cylinder
label: "Database\n[PostgreSQL]"
style.fill: "#1168bd"
style.font-color: white
}
external_payment: {
label: "Payment Gateway\n[Stripe]"
style.fill: "#999999"
style.font-color: white
}
user -> web_app: "Uses [HTTPS]"
web_app -> api: "API calls [JSON/HTTPS]"
api -> db: "Reads/writes [SQL]"
api -> external_payment: "Charges card [HTTPS]"
direction: right
# States
idle: { shape: rectangle; label: "Idle" }
loading: { shape: rectangle; label: "Loading"; style.fill: "#fff3cd" }
success: { shape: rectangle; label: "Success"; style.fill: "#d4edda" }
error: { shape: rectangle; label: "Error"; style.fill: "#f8d7da" }
# Terminal states with double-border
success: { style.double-border: true }
# Transitions
idle -> loading: "fetch()"
loading -> success: "onSuccess"
loading -> error: "onError"
error -> idle: "reset()"
success -> idle: "reset()"
dashboard: {
label: "System Health"
grid-rows: 2
grid-columns: 3
grid-gap: 16
cpu: { label: "CPU\n42%" }
memory: { label: "Memory\n71%" }
disk: { label: "Disk\n28%" }
latency: { label: "p99 Latency\n180ms"; style.fill: "#fff3cd" }
errors: { label: "Error Rate\n0.2%"; style.fill: "#d4edda" }
uptime: { label: "Uptime\n99.97%"; style.fill: "#d4edda" }
}
Use steps to build up a diagram progressively. Render with animate_interval to produce an animated SVG.
steps: {
s1: {
user: "User"
}
s2: {
user: "User"
web: "Web Server"
user -> web: "request"
}
s3: {
user: "User"
web: "Web Server"
db: "Database"
user -> web: "request"
web -> db: "query"
}
s4: {
user: "User"
web: "Web Server"
db: "Database"
cache: "Redis"
user -> web: "request"
web -> db: "query"
web -> cache: "cache hit"
}
}
Render: d2_render(d2_code=..., animate_interval=1500, target="*")
Each step inherits the previous. Use scenarios for variations on a base; use layers for fully independent views.
| Context | Recommended theme | ID | |---------|------------------|----| | Technical docs, general use | Neutral Default | 0 | | C4 architecture diagrams | C4 | 303 | | Dark mode documentation | Dark Mauve | 200 | | Vibrant / colorful | Flagship Terrastruct | 3 | | Paper / whiteboard aesthetic | Origami | 302 | | Terminal / monospace style | Terminal | 300 | | Accessible / colorblind-safe | Colorblind Clear | 8 | | Presentations (high contrast) | Neutral Default or Flagship | 0 or 3 |
D2 supports separate themes for light and dark browser mode in one SVG:
d2_render(d2_code=..., theme_id=0, dark_theme_id=200)
The browser automatically switches based on prefers-color-scheme. Use this when embedding SVGs in documentation sites.
| ID | Name | Character | |-----|---------------------------|------------------------------------| | 0 | Neutral Default | Clean, professional (good default) | | 1 | Neutral Grey | Muted, monochrome | | 3 | Flagship Terrastruct | Vibrant, colorful | | 4 | Cool Classics | Blues and greens | | 5 | Mixed Berry Blue | Purples and blues | | 6 | Grape Soda | Purple-dominant | | 7 | Aubergine | Deep purple tones | | 8 | Colorblind Clear | Accessible palette | | 100 | Vanilla Nitro Cola | Warm neutrals | | 101 | Orange Creamsicle | Orange accent | | 102 | Shirley Temple | Pink and red | | 103 | Earth Tones | Browns and tans | | 104 | Everglade Green | Forest greens | | 105 | Buttered Toast | Warm yellows | | 200 | Dark Mauve | Dark mode | | 201 | Dark Flagship Terrastruct | Dark mode, colorful | | 300 | Terminal | Monospace, dot-fill containers | | 301 | Terminal Grayscale | Terminal style, grayscale | | 302 | Origami | Paper aesthetic | | 303 | C4 | C4 architecture diagram style |
| Engine | Use when | Notes | |--------|----------|-------| | dagre | Always — default | Fast, handles nesting and cross-container connections well | | elk | User explicitly requests it | Extremely slow in WASM (can take minutes). Do NOT choose it yourself. |
Default rule: never set layout-engine at all. Dagre is the default. Only set ELK if the user asks for it.
Direction control:
direction: right # top-level: up, down, left, right
# Per-container direction (ELK only — do not use unless user requests ELK)
container: {
direction: right
a -> b -> c
}
# Layers: independent views (no inheritance between layers)
layers: {
overview: {
web -> app -> db
}
detailed: {
web: "Nginx" { shape: rectangle }
web -> app: "HTTP/1.1"
app -> db: "PostgreSQL wire protocol"
}
}
# Scenarios: variations on a base diagram (inherit from root)
web -> app -> db
scenarios: {
with_cache: {
app -> cache: "read-through"
}
with_cdn: {
cdn -> web
}
}
# Steps: sequential, each step inherits the previous (use for animated storytelling)
steps: {
s1: { user }
s2: { user -> web }
s3: { user -> web -> app }
s4: { user -> web -> app -> db }
}
Animate layers/scenarios/steps: d2_render(d2_code=..., animate_interval=1500, target="*")
Render one board: target="layers.production" or target="layers.production.*" for it and its children.
# Icon from URL — Terrastruct's free icon library
server: {
icon: https://icons.terrastruct.com/tech/server.svg
}
# Control icon position
server: {
icon: https://icons.terrastruct.com/tech/server.svg
icon.near: top-left # top-left, top-center, top-right, center-left, center-right, bottom-*
}
# Standalone image shape
github: {
shape: image
icon: https://icons.terrastruct.com/social/github.svg
}
Free icons: https://icons.terrastruct.com — includes AWS, GCP, Azure, dev tools, tech logos.
server: {
tooltip: "Handles all API requests. SLA: 99.9%"
link: https://docs.example.com/server
}
server -> db: {
tooltip: "Uses PgBouncer connection pooling"
}
Tooltips and links are embedded in the SVG and work when opened in a browser. To force the tooltip appendix to render even on shapes without tooltips, add forceAppendix: true to your render options (not yet exposed in the d2_render tool — use in-file config or omit).
dashboard: {
grid-rows: 2
grid-columns: 3
grid-gap: 20 # gap between all cells
vertical-gap: 10 # fine-tune vertical spacing
horizontal-gap: 15 # fine-tune horizontal spacing
widget_a: "Revenue"
widget_b: "Users"
widget_c: "Orders"
widget_d: "Latency"
widget_e: "Errors"
widget_f: "Uptime"
}
width/height on grid containers requires ELK layout# Spread file contents into current scope
...@shared_styles.d2
# Assign file to a key
network: @network_diagram.d2
# Import specific object from file
db_schema: @schema.users
Save in this order — write source first, SVG second:
Step 1 — Write the D2 source immediately (no render needed, instant):
Write the D2 code to diagrams/<stem>.d2 with the Write tool.
Step 2 — Reuse the SVG from the conversation render:
The SVG you already rendered with skip_fonts=true (default) is the save-worthy output. Write it to diagrams/<stem>.svg. No second render needed.
Embedded fonts: skip_fonts=true is the default and correct for saved files too — system fonts are indistinguishable from Source Sans Pro in practice. Only render with skip_fonts=false if the user explicitly asks for embedded fonts, and warn them it will add ~500KB and take significantly longer.
Filename convention:
diagrams/ relative to project root (create if missing)YYYY-MM-DD_HH-MM_<slug> — local time, 24-hour, 2–4 word slug2025-06-01_14-32_auth-flow-sequence, 2025-06-01_09-05_aws-three-tierAfter saving, tell the user the paths. SVGs open in any browser and are fully interactive (tooltips, links, animations).
For HTML embedding: add no_xml_tag=true to omit the <?xml?> declaration.
These are things D2 does that Mermaid cannot — reach for them when appropriate:
_ for parent scope: aws.db -> _.gcloud.replicastyle.animated: true creates flowing arrows in the SVG (great for showing data flow)steps/scenarios/layers + animate_interval produce a single SVG that cycles through statesd2_render(theme_id=0, dark_theme_id=200) — browser auto-switchesvars, reference with ${primary} throughout.class styles once, apply to many shapes like CSS classesicons.terrastruct.com directly into shapesstyle.animated: true on connections to show active data flow — it's more informative than color aloned2_validate before d2_render if you're unsure about syntax — it returns specific line/column errorsDo not use d2_render(ascii=true) for structural previews:
The D2 ASCII renderer (added in v0.7.1) has known bugs: direction: right with cross-container connections garbles output, and reverse/back-arrows (reply, response) produce tangled routing even with direction: down. Terrastruct has acknowledged the approach is architecturally flawed and a rewrite is in progress.
Use d2_inspect instead — it generates a reliable text summary from the compiled diagram object, with no rendering involved.
Repeated connections are not merged:
A -> B: "label 1"
A -> B: "label 2" # creates a SECOND distinct arrow, not an update
Use (A -> B)[0].label: "label 1" to reference existing connections.
_ is the parent scope, not the root:
outer: {
inner: {
x -> _.y # _ refers to outer, not root
}
y
}
Quoting class method params:
# WRONG — colon inside key breaks parsing
+createUser(data: UserInput): User
# CORRECT — quote the key when params contain ":"
"+createUser(data: UserInput)": User
ELK in WASM is extremely slow:
Never set layout-engine: elk in vars.d2-config or pass layout: "elk" to d2_render unless the user explicitly asks. It can take several minutes per render.
width/height on containers requires ELK:
grid-rows/grid-columns works with dagre, but explicit width/height on containers only takes effect with ELK layout.
Connections use keys, not labels:
server: "API Server" # label is "API Server", key is "server"
server -> db # correct — uses the key
"API Server" -> db # WRONG — this creates a new shape named "API Server"
skip_fonts=true by default in conversation:
Always pass skip_fonts=true when rendering for display in conversation. Only omit it (i.e. use skip_fonts=false) when saving the final SVG to disk. Each render without skip_fonts=true adds ~500KB of base64 font data to your context.
tools
Use when work should span one or more detached tasks but still behave like one job with a single owner context. TaskFlow is the durable flow substrate under authoring layers like Lobster, ACPX, plugins, or plain code. Keep conditional logic in the caller; use TaskFlow for flow identity, child-task linkage, waiting state, revision-checked mutations, and user-facing emergence.
tools
# Lobster Lobster executes multi-step workflows with approval checkpoints. Use it when: - User wants a repeatable automation (triage, monitor, sync) - Actions need human approval before executing (send, post, delete) - Multiple tool calls should run as one deterministic operation ## When to use Lobster | User intent | Use Lobster? | | ------------------------------------------------------ | --------------------------
tools
# Lobster Lobster executes multi-step workflows with approval checkpoints. Use it when: - User wants a repeatable automation (triage, monitor, sync) - Actions need human approval before executing (send, post, delete) - Multiple tool calls should run as one deterministic operation ## When to use Lobster | User intent | Use Lobster? | | ------------------------------------------------------ | --------------------------
tools
A CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.