Python MCP server for validating Mermaid diagrams and (optionally) rendering them as PNG images. It uses the Mermaid CLI tool to perform the validation and rendering.
Python MCP Server abstracting the official mermaid-cli for ease of use
The server provides LLMs with structured validation results including:
Boolean validation status (is_valid: true/false) indicating whether the Mermaid diagram syntax is correct
Detailed error messages explaining exactly what went wrong if validation fails (e.g., syntax errors, unsupported diagram types, malformed nodes)
Optional base64-encoded PNG images of successfully rendered diagrams for visual verification
This enables LLMs to programmatically validate Mermaid diagram syntax, understand specific errors to provide helpful corrections, and optionally receive visual confirmation of the rendered output.
Also provides a simple Pydantic-AI MCP Client to invoke the MCP server using a Gemini model for testing.
Prerequisites
Important: This MCP server requires Node.js to be installed on your system, even if you're only using the server component (not the client). The server internally calls npx @mermaid-js/mermaid-cli as a subprocess to perform diagram validation and rendering.
Required Dependencies
Node.js with npm (required for all usage)
Mermaid CLI: Install with npm install -g @mermaid-js/mermaid-cli
# Simple function call with diagram text
result = await validate_mermaid_diagram("graph TD; A-->B")
# All file handling, configuration, and cleanup is automatic
# Returns structured result with validation status and base64-encoded image
Key Abstractions:
Temporary File Management: Automatically creates and cleans up temporary .mmd input files
Output File Handling: Manages temporary .png output files and converts them to base64 strings
Puppeteer Configuration: Automatically generates the required sandboxing configuration for headless browser rendering
Error Handling: Captures and returns structured error messages instead of raw stderr output
Command Construction: Builds the complete npx @mermaid-js/mermaid-cli command with all necessary flags
Resource Cleanup: Ensures all temporary files are properly deleted after processing
This abstraction allows users to focus on diagram validation and rendering without dealing with the underlying file system operations and command-line complexities.
Local Development
This repository can be used standalone to test the functionality of the Mermaid MCP validator programmatically.
Requirements
See the Prerequisites section above for required dependencies (Node.js, Mermaid CLI, and Python with uv).
Quick Setup (Recommended)
Use the provided Makefile for streamlined setup:
# Install all dependencies (Python + Node.js + Mermaid CLI)
make install
# Run validation tests
make test
Copy .env.example to .env and fill in your API key
Run the server: uv run mermaid_mcp_server.py
Usage
The server exposes a tool for validating Mermaid diagrams:
validate_mermaid_diagram: Validates a Mermaid diagram and returns validation results
Tool Parameters
diagram_text (required): The Mermaid diagram text to validate
return_image (optional, default: false): Whether to return the base64-encoded PNG image
Context Length Optimisation
Important: By default, the tool does not return the base64-encoded image (return_image=false) to preserve context length in LLM conversations. Base64-encoded images can be very long strings (often 10KB-100KB+) that significantly impact the available context for the conversation.
When to use each setting:
return_image=false (default): Use for diagram validation only. Fast and context-efficient.
return_image=true: Use only when you specifically need the rendered image data. Warning: This will consume significant context length.
Example Usage
# Validation only (recommended for most cases)
result = await validate_mermaid_diagram("graph TD; A-->B")
# Returns: MermaidValidationResult(is_valid=True, error_message=None, diagram_image=None)
# Validation with image (use sparingly)
result = await validate_mermaid_diagram("graph TD; A-->B", return_image=True)
# Returns: MermaidValidationResult(is_valid=True, error_message=None, diagram_image="iVBORw0KGgoAAAANSUhEUg...")
Testing
The project includes convenient testing commands:
# Run all tests
make test
# Or run the test script directly
uv run test_pydantic.py
The test script uses Pydantic AI with Gemini models to validate the MCP server functionality.
M
Mermaid
andrewginns·11 स्टार·
Python MCP Server abstracting the official mermaid-cli for ease of use
अभी टूल सूची नहीं है। रिमोट सर्वर को पहले हेल्थ चेक के बाद सूची मिलती है।
FAQ
सवाल
Mermaid कैसे इंस्टॉल करें?
इस पेज पर Cursor, Claude Desktop, Claude Code या VS Code के लिए stdio कॉन्फ़िग कॉपी करें। यह पब्लिशर का इंस्टॉल इस्तेमाल करता है, PlugMCP प्रॉक्सी नहीं।
क्या Mermaid के लिए API key चाहिए?
इस रिकॉर्ड में कोई ज़रूरी एनवायरनमेंट वेरिएबल दर्ज नहीं है।
Mermaid कौन सा ट्रांसपोर्ट इस्तेमाल करता है?
Mermaid stdio पर लोकल चलता है।
Mermaid की पिछली जाँच कब हुई थी?
पिछली जाँच 2026-10-28 को हुई। Python MCP Server abstracting the official mermaid-cli for ease of use