An MCP server that accepts mermaid code and generates diagrams as images.
Project description
MCP Mermaid Image Gen
An MCP server that generates diagrams from Mermaid code in multiple formats (primarily PNG and PDF, with SVG also available). This server enables AI assistants and other MCP clients to create diagrams using the Mermaid diagram syntax, with support for multiple themes, formats, and customizable backgrounds.
Overview
This MCP server wraps the Mermaid CLI (@mermaid-js/mermaid-cli) to provide diagram generation capabilities through the Model Context Protocol (MCP). It offers two operation modes:
- File-based: Save diagrams to disk and return the file path
- Stream-based: Return the diagram directly as base64-encoded data
Both modes work with either STDIO or SSE transport, but the stream-based mode requires an MCP client capable of handling image data.
The server is designed to be used by AI coding assistants and other tools that support the MCP protocol, allowing them to generate diagrams without needing direct access to the Mermaid CLI.
Features
- Generate diagrams from Mermaid syntax (flowcharts, sequence diagrams, class diagrams, etc.)
- Multiple output formats:
- PNG: Raster graphics, recommended for general use and broad compatibility.
- PDF: Document format, perfect for print and formal documentation.
- SVG: Vector graphics, available for web and scalable diagrams where specifically needed.
- Support for multiple themes (default, neutral, dark, forest, base)
- Customizable background colors (hex codes or named colors)
- Two operation modes (both available in STDIO and SSE transport):
- File-based: Save diagrams to disk and return the path
- Stream-based: Return base64-encoded diagram data (requires MCP client with image support)
- Comprehensive error handling and validation
- Detailed logging with configurable levels
Prerequisites for Use
Before configuring your MCP client, ensure you have the following installed on your system:
- Node.js: Version 14 or higher.
- Mermaid CLI: Install globally using
npm install -g @mermaid-js/mermaid-cli. This is required by the server to render diagrams. - UV Package Installer: Install using
pip install uv. This is used byuvxto run the server from PyPI. - Operating Systems: Linux, macOS, Windows are supported.
Configuring Your MCP Client (Recommended Method)
The easiest way to use this MCP server is by adding the following uvx configuration to your MCP client's JSON settings. This command will automatically download mcp-mermaid-image-gen from PyPI (if not already cached or installed) and run it in an isolated environment when your client calls it.
Example: Claude Desktop Configuration
To configure Claude Desktop, open its claude_desktop_config.json file (typically found in ~/Library/Application Support/Claude/ on macOS, %APPDATA%\Claude\ on Windows, or ~/.config/Claude/ on Linux) and add the following to the mcpServers section:
{
"mcpServers": {
"mermaid_image_generator": {
"command": "uvx",
"args": [
"mcp_mermaid_image_gen"
]
}
}
}
After saving the configuration, restart your MCP client. The mermaid_image_generator toolset should now be available. The mcp-mermaid-image-gen-server in the args array is the command-line entry point defined by the package.
For other MCP clients, adapt the command and args to their specific MCP server registration format, using uvx and the mcp-mermaid-image-gen-server argument.
Alternative Installation & Configuration Methods
While using uvx via client configuration is recommended, you might have reasons to install the server differently (e.g., for development).
Installing from PyPI directly (Optional)
If you have a specific need to install the package into a particular Python environment (this is less common for MCP servers primarily used via uvx), you can use:
# Ensure UV is installed (e.g., pip install uv)
uv pip install mcp-mermaid-image-gen
This makes the mcp_mermaid_image_gen-server command available in that environment.
Installing from Source (for Development/Contribution)
If you want to contribute to the server or run a modified version:
# Clone the repository
git clone https://github.com/yourusername/mcp_mermaid_image_gen.git
cd mcp_mermaid_image_gen
# Create and activate a virtual environment using UV
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install in development mode
uv pip install -e .
This installs the server in editable mode within your local virtual environment.
Configuring Client with Local Development Installation
If you have installed the server from source for development purposes, you can configure your client to point directly to the server script in your local virtual environment.
Example: Claude Desktop (Local Development Setup)
{
"mcpServers": {
"mermaid_image_generator_dev": { // Use a different key for the dev version
"command": "/path/to/your/mcp_mermaid_image_gen/.venv/bin/mcp_mermaid_image_gen-server"
}
}
}
Replace /path/to/your/mcp_mermaid_image_gen/ with the absolute path to your project directory.
Testing with MCP Inspector
The MCP SDK includes a development console (MCP Inspector) that allows you to test the server interactively. After installation, run:
mcp dev mcp_mermaid_image_gen/server/app.py
This will:
- Start the MCP Inspector web interface
- Allow testing via both STDIO and SSE transport modes
- Provide a UI for calling the server's tools
- Display returned images directly in the browser
- Show detailed logs and debugging information
The Inspector is particularly useful for:
- Testing different diagram types
- Trying out various formats (PNG, PDF, and SVG)
- Experimenting with themes and colors
- Debugging issues with Mermaid syntax
- Verifying both file and stream-based operation modes
Available Tools
generate_mermaid_diagram_file
Generates a Mermaid diagram and saves it to a local file.
Parameters:
code(str): Mermaid diagram syntaxfolder(str): Output directory pathname(str): Output filename (extension can determine format)theme(str, optional): Theme name (default, neutral, dark, forest, base)backgroundColor(str, optional): Background color (hex or named color)format(str, optional): Output format. Recommended: "png". Also supports "pdf". "svg" is available but not recommended for general use. If not specified:- Inferred from filename extension (e.g., "diagram.png" -> png).
- Defaults to "png" if no valid extension or format is provided.
Returns: Path to the generated file
Format Selection Priority:
- Explicit
formatparameter if provided - File extension in
nameparameter if valid - Defaults to PNG if neither is specified
generate_mermaid_diagram_stream
Generates a Mermaid diagram and returns it directly as base64-encoded data. Requires an MCP client that supports handling image data (like the MCP Inspector).
Parameters:
code(str): Mermaid diagram syntaxtheme(str, optional): Theme name (default, neutral, dark, forest, base)backgroundColor(str, optional): Background color (hex or named color)format(str, optional): Output format. Recommended: "png". Also supports "pdf". "svg" is available but not recommended. Defaults to "png" if not specified.
Returns: Base64-encoded diagram data with appropriate MIME type:
- PNG:
image/png - PDF:
application/pdf - SVG:
image/svg+xml(available if explicitly requested)
Transport Modes
Both STDIO and SSE transport modes support all server functionality. The choice between them depends on your use case:
STDIO Mode (Default)
- Synchronous operation
- Simple command-line integration
- Works with all tools and operation modes
- Ideal for scripts and command-line tools
- Example client: MCP Inspector, Cursor
SSE Mode
- Asynchronous operation
- HTTP-based communication
- Works with all tools and operation modes
- Ideal for web applications and services
- Example client: MCP Inspector, web applications
Client Compatibility
The server supports any MCP client, but some features require specific client capabilities:
-
File-based generation (
generate_mermaid_diagram_file):- Requires client to have file system access
- Works with all MCP clients
- Client must be able to read the returned file path
-
Stream-based generation (
generate_mermaid_diagram_stream):- Requires client capable of handling base64-encoded image data
- Works with MCP Inspector and other image-capable clients
- No file system access needed
The MCP Inspector (included with the MCP SDK) supports all features in both transport modes.
Usage
The server provides two entry points:
-
mcp_mermaid_image_gen-server: The MCP server# Run with stdio transport (default) mcp_mermaid_image_gen-server # Run with SSE transport mcp_mermaid_image_gen-server --transport sse --port 3001
-
mcp_mermaid_image_gen-client: Test client (for development)mcp_mermaid_image_gen-client "your mermaid code here"
Logging
The server logs all activity to both stderr and a rotating log file. Log files are stored in OS-specific locations:
- macOS:
~/Library/Logs/mcp-servers/mcp_mermaid_image_gen.log - Linux:
- Root user:
/var/log/mcp-servers/mcp_mermaid_image_gen.log - Non-root:
~/.local/state/mcp-servers/logs/mcp_mermaid_image_gen.log
- Root user:
- Windows:
%USERPROFILE%\AppData\Local\mcp-servers\logs\mcp_mermaid_image_gen.log
Log files are automatically rotated when they reach 10MB, with up to 5 backup files kept.
Configure logging level with the LOG_LEVEL environment variable:
LOG_LEVEL=DEBUG mcp_mermaid_image_gen-server
Valid log levels: DEBUG, INFO (default), WARNING, ERROR, CRITICAL
Author
Tim Kitchens (timkitch@codingthefuture.ai)
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_mermaid_image_gen-0.1.4.tar.gz.
File metadata
- Download URL: mcp_mermaid_image_gen-0.1.4.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f48266764a19ab18c86b9112ba6c50169a115830c0cb4b189d8b086a9da61445
|
|
| MD5 |
9a6f1821371432cff4bdf7f92b4a6428
|
|
| BLAKE2b-256 |
c488b1bd114146854a7b3e2707888df576a17be520c3921d335d88e9d69fe7f3
|
File details
Details for the file mcp_mermaid_image_gen-0.1.4-py3-none-any.whl.
File metadata
- Download URL: mcp_mermaid_image_gen-0.1.4-py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fc5ddac206f8b301012b0451725c54fcb98759df2861c64f49e8b6c90f9f521
|
|
| MD5 |
ae20d50ef2e175f336422d7cf065dd39
|
|
| BLAKE2b-256 |
8aeb7ba3cc4555915d7ce9db1a0c24a493e5d23b74290c9d21599c950aed1fb3
|