Skip to main content
Glama

Render D2 Diagram

d2_render
Read-onlyIdempotent

Render D2 diagram source code to SVG or ASCII art directly via WASM, eliminating the need for a local d2 binary. Produce diagrams from code quickly for embedding, preview, or terminal display.

Instructions

Render D2 diagram source code to SVG or ASCII art using the D2 WASM engine.

D2 is a diagram scripting language. This tool compiles and renders D2 code without requiring the d2 binary — it uses the @terrastruct/d2 WASM package directly.

Args:

  • d2_code (string): D2 source code to render

  • theme_id (number): Theme ID (default: 0). See d2_list_themes for options. Key themes: 0=Neutral Default, 3=Flagship Terrastruct, 300=Terminal, 200=Dark Mauve

  • dark_theme_id (number): Dark mode theme ID (optional)

  • layout ('dagre' | 'elk'): Layout engine (default: 'dagre'). WARNING: elk is extremely slow in WASM — do NOT use unless explicitly requested. Dagre handles all diagram types well.

  • sketch (boolean): Hand-drawn style (default: false)

  • pad (number): Padding pixels (default: 100)

  • center (boolean): Center in viewbox (default: false)

  • scale (number): Scale factor, e.g. 0.5 halves size. Default fits SVG to screen. Set 1 to disable.

  • animate_interval (number): Animate multi-board diagrams (layers/scenarios/steps) at this ms interval. Requires target to be set. E.g. animate_interval=1000, target='*'

  • target (string): Which board to render. '*' = all boards (needs animate_interval > 0). 'layers.x' = specific layer. Default = root board only.

  • ascii (boolean): Output ASCII art instead of SVG (default: false)

  • ascii_mode ('standard' | 'extended'): ASCII char set. 'extended' uses Unicode (default).

  • no_xml_tag (boolean): Omit declaration for direct HTML embedding (default: false)

  • skip_fonts (boolean): Strip embedded font data from SVG (DEFAULT: true). Only set false if user explicitly requests embedded fonts.

Returns: SVG markup string (or ASCII art if ascii=true). SVG output starts with (unless no_xml_tag=true)

Examples:

  • Simple: d2_code="a -> b: connects"

  • Architecture: d2_code="server -> db: query\nserver -> cache: read"

  • Styled: d2_code="x: { style.fill: '#4a90d9' }\nx -> y", theme_id=3

  • ASCII preview: d2_code="a -> b -> c", ascii=true

  • Animated steps: d2_code="steps: { s1: {a} s2: {a -> b} }", animate_interval=1000, target="*"

  • With embedded fonts: skip_fonts=false (only when user explicitly requests it)

  • HTML embed: no_xml_tag=true

Error Handling:

  • Returns error with syntax details if D2 code is invalid

  • Use d2_validate first to check syntax before rendering

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
padNoPadding in pixels around the diagram (default: 100)
asciiNoRender as ASCII/Unicode art instead of SVG (default: false). Useful for terminal display, text-only contexts, and fast structural previews before committing to a full SVG render.
scaleNoScale factor for the output SVG. E.g. 0.5 halves the size. By default D2 renders SVGs that fit to screen. Set to 1 to disable fit-to-screen. Not applicable to ascii output.
centerNoCenter the SVG in its viewbox (default: false)
layoutNoLayout engine: 'dagre' (default, recommended for all diagrams — fast and handles nested containers well). 'elk' is extremely slow in WASM (can take minutes) — do NOT use it unless the user explicitly requests it.
sketchNoRender in hand-drawn/sketch style (default: false)
targetNoWhich board to render. Defaults to root board only. Use '*' to render all layers/scenarios/steps (requires animate_interval > 0 for multi-board). Use 'layers.x.*' to render layer 'x' and all its children. Use 'layers.x' to render only layer 'x'.
d2_codeYesThe D2 diagram source code to render
theme_idNoTheme ID (default: 0 = Neutral Default). Use d2_list_themes to see all options. Popular: 300 (Terminal), 200 (Dark Mauve), 3 (Flagship Terrastruct)
ascii_modeNoASCII rendering character set. 'standard' uses basic ASCII chars, 'extended' uses Unicode box-drawing chars (default: 'extended'). Only applies when ascii=true.
no_xml_tagNoOmit the <?xml ...?> declaration from SVG output (default: false). Useful when embedding SVG directly in HTML.
skip_fontsNoStrip embedded font data from SVG output (default: TRUE). Removes ~500KB of base64 WOFF data — SVG falls back to system fonts which look identical in practice. Only set to false if the user explicitly requests embedded fonts. No effect on ascii output.
dark_theme_idNoTheme ID to use when the viewer's browser is in dark mode. If unset, theme_id is used for both modes.
force_appendixNoAlways include the SVG appendix for tooltips and links, even if none are present in the diagram (default: false). Useful when you plan to add tooltips/links via post-processing.
animate_intervalNoIf set, packages multiple boards (layers/scenarios/steps) into one animated SVG, cycling through boards at this interval in milliseconds. Requires target to be set (e.g. target='*' for all boards). Use with diagrams that define layers/scenarios/steps.

Schema Changelog

Changes observed during successful MCP inspections. Dates show when Glama detected each change.

  1. First observedv1.0.0

TDQS

A4.6/5.0
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare this a safe, idempotent, read-only operation, but the description adds substantial behavioral context beyond them: the elk performance hazard ('extremely slow in WASM'), the exact output contract (SVG markup starting with <?xml ...?> unless no_xml_tag=true, or ASCII art), the ~500KB font-data implication of skip_fonts, and the multi-board animation semantics of animate_interval/target. It also discloses error behavior ('Returns error with syntax details if D2 code is invalid'). This is rich, decision-relevant behavior disclosure well above the annotation baseline.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with clear sections (overview, Args, Returns, Examples, Error Handling) and front-loads the core purpose in the first sentence. Given 15 parameters and complex multi-board/ASCII/font behaviors, the length is largely justified. However, the Args section re-states each parameter that the 100%-covered schema already documents, creating redundancy that costs agent context; a tighter version could reference the schema and add only the warnings, examples, and return contract.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with no output schema and high complexity (15 params, interactive behavior between animate_interval and target, two output modalities), the description is complete: it specifies the return type and format, the XML declaration condition, error behavior, and the validation workflow. The examples cover the major invocation patterns (simple, styled, ASCII, animated, HTML-embed, embedded-fonts), so an agent has everything needed to call the tool correctly without inspecting further.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so the baseline is 3, and the description does add genuine meaning beyond the schema: concrete D2 syntax examples ('a -> b: connects', styled blocks with theme_id=3, steps: { s1: {a} ... }), a compact key for theme IDs (0, 3, 300, 200), and the return-format context for params like no_xml_tag and ascii. Some duplication exists — layout, target, and animate_interval wording largely mirrors the schema — but the examples section teaches correct parameter combinations in a way the schema alone does not.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description opens with a specific verb+resource+output: 'Render D2 diagram source code to SVG or ASCII art using the D2 WASM engine.' It further clarifies it compiles D2 code without requiring the d2 binary, making the tool's function unambiguous. The rendering action is inherently distinct from siblings like d2_validate, d2_format, and d2_list_themes, so an agent can select it correctly without opening the schema.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description gives strong when-not guidance: 'elk is extremely slow in WASM — do NOT use unless explicitly requested' and 'skip_fonts ... Only set false if user explicitly requests embedded fonts.' It also routes to a sibling in the Error Handling section: 'Use d2_validate first to check syntax before rendering,' and points to d2_list_themes for theme options. However, it does not systematically contrast with d2_inspect or d2_format (e.g., when to prefer format/inspect over render), leaving some alternative-selection judgment to the agent.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ItsJooL/d2-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server