validation-tools-cli
Comprehensive guide to CLI tools for validating Mermaid diagram syntax in markdown files.
Overview
Several CLI tools exist for validating Mermaid diagrams. They differ in implementation language, validation approach, and integration capabilities.
1. mermaid-validate (Node.js)
GitHub: cloud-on-prem/mermaid-validator
Installation
npm install -g mermaid-validateUsage
Validate a diagram file:
mermaid-validate validate diagram.mmdValidate a diagram string:
mermaid-validate validate "<diagram-string>" --stringValidate all Mermaid diagrams in a markdown file:
mermaid-validate validate-md document.mdKey Features
- Dedicated markdown file validation with
validate-mdcommand - String validation for inline testing
- File-based validation for
.mmdfiles - CLI and programmatic API available
Use Case
Best for validating multiple diagrams across markdown documentation in Node.js projects.
2. go-mermaid (Go)
GitHub: sammcj/go-mermaid Package: pkg.go.dev/github.com/sammcj/go-mermaid
Installation
go install github.com/sammcj/go-mermaid@latestFeatures
- Pure Go parser and validator
- Custom linting rules support
- Extract diagrams from markdown files
- Parse Mermaid syntax from raw text
- Programmatic validation API
Use Case
Best for Go projects or systems where Node.js/JavaScript runtime is unavailable. Enables custom linting rules tailored to project needs.
3. @mermaid-js/mermaid-cli (Official)
GitHub: mermaid-js/mermaid-cli NPM: @mermaid-js/mermaid-cli
Installation
npm install -g @mermaid-js/mermaid-cliUsage
Validate by attempting to render:
mmdc -i diagram.mmd -o /tmp/test.svgPipe input from stdin:
cat diagram.mmd | mmdc -i - -o /tmp/test.svgUse with npx (package name differs from command):
npx -p @mermaid-js/mermaid-cli mmdc -i diagram.mmd -o output.pngValidation Approach
The CLI validates syntax indirectly by attempting to render diagrams:
- If syntax is valid → SVG/PNG/PDF generated successfully
- If syntax is invalid → Error output to stderr
Key Features
- Official Mermaid project tool
- Renders diagrams to multiple formats (SVG, PNG, PDF)
- Theme and background customization
- Markdown file transformation (finds diagrams, creates SVGs, updates references)
- Stdin support for piping
Use Case
Best for projects already using the official Mermaid ecosystem. Validates syntax while producing visual output for documentation.
4. MCP Mermaid Validator (for AI Agents)
GitHub: rtuin/mcp-mermaid-validator NPM: @rtuin/mcp-mermaid-validator MCP Market: Mermaid Validator
Installation for Claude Code
claude mcp add-json "mermaid-validator" '{"command":"npx","args":["-y","@rtuin/mcp-mermaid-validator@latest"]}'Features
- Model Context Protocol (MCP) server for LLM integration
- Validates and renders Mermaid diagrams
- Provides PNG output for visual confirmation
- Enables AI agents to verify diagram syntax
Validation Approach
The diagram is passed to mmdc via stdin, mmdc validates syntax and renders PNG if valid, and output/errors are captured from stdout/stderr.
Use Case
Best for AI-assisted documentation workflows where LLMs generate diagrams and need automated validation feedback.
Alternative: Mermaider
GitHub: vtomilin/mermaider
More efficient than traditional MCP validators:
- Uses
puppeteer-coreandmermaid-jsAPI directly - Leverages already-installed Chrome/Firefox browsers
- Avoids spawning
mmdcprocess for every validation - Provides
validate_syntaxtool for syntax error checking - Supports all Mermaid diagram types
5. markdown-mermaid-cli (Python)
PyPI: markdown-mermaid-cli
Installation
pip install markdown-mermaid-cliFeatures
- Python-Markdown extension
- Uses Mermaid-CLI under the hood
- Converts diagrams to Base64 encoded data URIs
- Enables PDF generation without client-side JavaScript
- Compatible with MkDocs, WeasyPrint
Use Case
Best for Python documentation projects using MkDocs or Sphinx that need to generate PDFs with embedded diagrams.
Comparison Matrix
| Tool | Language | Validates Markdown | Renders Output | Pre-commit Ready | AI Integration |
|---|---|---|---|---|---|
| mermaid-validate | Node.js | ✅ Yes (validate-md) | ❌ No | ✅ Yes | ❌ No |
| go-mermaid | Go | ✅ Yes (extract) | ❌ No | ✅ Yes | ❌ No |
| @mermaid-js/mermaid-cli | Node.js | ⚠️ Indirect | ✅ SVG/PNG/PDF | ⚠️ Manual | ❌ No |
| MCP Mermaid Validator | Node.js | ✅ Yes | ✅ PNG | ✅ Yes | ✅ Yes (MCP) |
| Mermaider | Node.js | ✅ Yes | ✅ PNG | ✅ Yes | ✅ Yes (MCP) |
| markdown-mermaid-cli | Python | ✅ Yes | ✅ Base64 URI | ⚠️ Manual | ❌ No |
Claude Code Validation Skill
Blog Post: A Mermaid Validation Skill for Claude Code Another Post: Agent Mermaid reporting for duty
Claude Code skills teach Claude domain-specific workflows. The Mermaid validation skill:
- Uses
mmdccommand for validation - Command:
mmdc -i <file-path> -o /tmp/mermaid-validation.svg 2>&1 - Validates diagrams before marking work complete
- Provides feedback loop for AI-generated diagrams
Validation Strategy Recommendations
For Node.js Projects
Use mermaid-validate for dedicated markdown validation:
npm install --save-dev mermaid-validatemermaid-validate validate-md docs/**/*.mdFor Go Projects
Use go-mermaid for native Go integration:
go install github.com/sammcj/go-mermaid@latestgo-mermaid validate docs/For Python/MkDocs Projects
Use markdown-mermaid-cli for PDF generation:
pip install markdown-mermaid-cliFor AI-Assisted Workflows
Use MCP Mermaid Validator or Mermaider:
claude mcp add-json "mermaid-validator" '{"command":"npx","args":["-y","@rtuin/mcp-mermaid-validator@latest"]}'For Visual Output + Validation
Use @mermaid-js/mermaid-cli (official):
mmdc -i diagram.mmd -o output.png -t dark -b transparentKnown Issues
.md File Extension Confusion
Issue: mermaid-js/mermaid-cli#160
When using .md file extensions with raw Mermaid syntax (without proper markdown code fences), mmdc may produce errors.
Solution:
- Use
.mmdextension for raw Mermaid syntax files - Use proper markdown code fences in
.mdfiles:```mermaidgraph TDA --> B```
Syntax Errors During Pre-commit
Issue: lukesdm/mermaid-gen
Syntax errors can cause hangs during pre-commit hooks when using automated diagram generation tools.
Solution:
- Validate diagrams locally before committing
- Use fast validators like Mermaider (1-2ms latency)
- Configure pre-commit timeout limits
Sources
- GitHub - cloud-on-prem/mermaid-validator
- mermaid package - github.com/sammcj/go-mermaid
- A Mermaid Validation Skill for Claude Code
- GitHub - mermaid-js/mermaid-cli
- Mermaid Validator MCP server for AI agents
- markdown-mermaid-cli · PyPI
- Agent Mermaid reporting for duty - Korny’s Blog
- @mermaid-js/mermaid-cli - npm
- @rtuin/mcp-mermaid-validator - npm
- GitHub - vtomilin/mermaider
- GitHub - rtuin/mcp-mermaid-validator
- Provide better error message for .md files - Issue #160