The D2 MCP Server provides a Model Context Protocol server that enables AI assistants to create, render, export, and save D2 diagrams programmatically. It offers both basic diagram operations and an Oracle API for incremental diagram building, making it ideal for creating architecture diagrams, flowcharts, and other visual representations during conversations.
rsvg-convert
(from librsvg) orconvert
command)# Clone the repository
git clone https://github.com/i2y/d2mcp.git
cd d2mcp
# Build the binary
make build
# Or build for all platforms
make build-all
go install github.com/i2y/d2mcp/cmd@latest
Add to your Claude Desktop configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
Linux: ~/.config/Claude/claude_desktop_config.json
For STDIO transport (recommended for Claude Desktop):
{
"mcpServers": {
"d2mcp": {
"command": "/path/to/d2mcp",
"args": ["-transport=stdio"]
}
}
}
For SSE transport:
{
"mcpServers": {
"d2mcp": {
"command": "/path/to/d2mcp",
"args": ["-transport=sse", "-addr=:3000"]
}
}
}
Replace /path/to/d2mcp
with the actual path to your built binary.
# Run the MCP server (stdio transport)
./d2mcp -transport=stdio
# Run with SSE transport (default)
./d2mcp
# or explicitly
./d2mcp -transport=sse
# Run with Streamable HTTP transport
./d2mcp -transport=streamable
For direct process communication:
./d2mcp -transport=stdio
HTTP-based transport for network connectivity:
# Basic SSE mode (defaults to :3000)
./d2mcp -transport=sse
# Custom configuration
./d2mcp -transport=sse \
-addr=:8080 \
-base-url=http://localhost:8080 \
-base-path=/mcp \
-keep-alive=30
SSE Configuration Options:
-addr
: Address to listen on (default: ":3000")-base-url
: Base URL for SSE endpoints (auto-generated if not specified)-base-path
: Base path for SSE endpoints (default: "/mcp")-keep-alive
: Keep-alive interval in seconds (default: 30)SSE Endpoints:
http://localhost:3000/mcp/sse
http://localhost:3000/mcp/message
Modern HTTP-based transport for bidirectional communication:
# Basic Streamable HTTP mode
./d2mcp -transport=streamable
# Custom configuration
./d2mcp -transport=streamable \
-addr=:8080 \
-endpoint-path=/mcp \
-heartbeat-interval=30 \
-stateless
Streamable HTTP Configuration Options:
-addr
: Address to listen on (default: ":3000")-endpoint-path
: Endpoint path (default: "/mcp")-heartbeat-interval
: Heartbeat interval in seconds (default: 30)-stateless
: Enable stateless mode (default: false)Streamable HTTP Endpoint:
http://localhost:3000/mcp
Create a new diagram with optional initial content:
Empty diagram (for Oracle API workflow):
{
"id": "my-diagram"
}
With initial D2 content:
{
"id": "my-diagram",
"content": "a -> b: Hello\nserver: {shape: cylinder}"
}
Export a diagram to a specific format:
{
"diagramId": "my-diagram",
"format": "png" // Options: "svg", "png", "pdf"
}
Save a diagram to a file:
{
"diagramId": "my-diagram",
"format": "pdf",
"path": "/path/to/output.pdf" // Optional, defaults to temp directory
}
Create a new shape or connection:
{
"diagram_id": "my-diagram",
"key": "server" // Creates a shape
}
{
"diagram_id": "my-diagram",
"key": "server -> database" // Creates a connection
}
Set attributes on existing elements:
{
"diagram_id": "my-diagram",
"key": "server.shape",
"value": "cylinder"
}
{
"diagram_id": "my-diagram",
"key": "server.style.fill",
"value": "#f0f0f0"
}
Delete elements from the diagram:
{
"diagram_id": "my-diagram",
"key": "server" // Deletes the server and its children
}
Move elements between containers:
{
"diagram_id": "my-diagram",
"key": "server",
"new_parent": "network.internal", // Moves server into network.internal
"include_descendants": "true" // Also moves child elements
}
Rename diagram elements:
{
"diagram_id": "my-diagram",
"key": "server",
"new_name": "web_server"
}
Get information about diagram elements:
{
"diagram_id": "my-diagram",
"key": "server",
"info_type": "object" // Options: "object", "edge", "children"
}
Get the current D2 text representation of the diagram:
{
"diagram_id": "my-diagram"
}
D2 has built-in support for sequence diagrams:
{
"id": "api-flow",
"content": "shape: sequence_diagram\n\nClient -> Server: HTTP Request\nServer -> Database: Query\nDatabase -> Server: Results\nServer -> Client: HTTP Response\n\n# Add styling\nClient -> Server.\"HTTP Request\": {style.stroke-dash: 3}\nDatabase -> Server.\"Results\": {style.stroke-dash: 3}"
}
Example with actors and grouping:
{
"id": "auth-flow",
"content": "shape: sequence_diagram\n\ntitle: Authentication Flow {near: top-center}\n\n# Define actors\nClient: {shape: person}\nAuth Server: {shape: cloud}\nDatabase: {shape: cylinder}\n\n# Interactions\nClient -> Auth Server: Login Request\nAuth Server -> Database: Validate Credentials\nDatabase -> Auth Server: User Data\n\ngroup: Success Case {\n Auth Server -> Client: Access Token\n Client -> Auth Server: API Request + Token\n Auth Server -> Client: API Response\n}\n\ngroup: Failure Case {\n Auth Server -> Client: 401 Unauthorized\n}"
}
Starting from scratch:
// 1. Create an empty diagram
d2_create({ id: "architecture" })
// 2. Add shapes incrementally
d2_oracle_create({ diagram_id: "architecture", key: "web" })
d2_oracle_create({ diagram_id: "architecture", key: "api" })
d2_oracle_create({ diagram_id: "architecture", key: "db" })
// 3. Set properties
d2_oracle_set({ diagram_id: "architecture", key: "db.shape", value: "cylinder" })
d2_oracle_set({ diagram_id: "architecture", key: "web.label", value: "Web Server" })
// 4. Create connections
d2_oracle_create({ diagram_id: "architecture", key: "web -> api" })
d2_oracle_create({ diagram_id: "architecture", key: "api -> db" })
// 5. Export final result
d2_export({ diagramId: "architecture", format: "svg" })
Starting with existing content:
// 1. Create diagram with initial content
d2_create({
id: "architecture",
content: "web -> api -> db\ndb: {shape: cylinder}"
})
// 2. Enhance incrementally using Oracle API
d2_oracle_set({ diagram_id: "architecture", key: "web.label", value: "Web Server" })
d2_oracle_create({ diagram_id: "architecture", key: "cache" })
d2_oracle_create({ diagram_id: "architecture", key: "api -> cache" })
// 3. Export final result
d2_export({ diagramId: "architecture", format: "svg" })
If you get errors when exporting to PNG or PDF formats, install one of these tools:
macOS:
# Using Homebrew
brew install librsvg
# or
brew install imagemagick
Ubuntu/Debian:
sudo apt-get install librsvg2-bin
# or
sudo apt-get install imagemagick
Windows: Download and install ImageMagick from the official website.
chmod +x d2mcp
To add this MCP server to Claude Code, run this command in your terminal:
claude mcp add-json "d2mcp" '{"command":"/path/to/d2mcp"}'
See the official Claude Code MCP documentation for more details.
There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json
file so that it is available in all of your projects.
If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json
file.
To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".
When you click that button the ~/.cursor/mcp.json
file will be opened and you can add your server like this:
{
"mcpServers": {
"d2mcp": {
"command": "/path/to/d2mcp"
}
}
}
To add an MCP server to a project you can create a new .cursor/mcp.json
file or add it to the existing one. This will look exactly the same as the global MCP server example above.
Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.
The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.
You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.
To add this MCP server to Claude Desktop:
1. Find your configuration file:
~/Library/Application Support/Claude/claude_desktop_config.json
%APPDATA%\Claude\claude_desktop_config.json
~/.config/Claude/claude_desktop_config.json
2. Add this to your configuration file:
{
"mcpServers": {
"d2mcp": {
"command": "/path/to/d2mcp"
}
}
}
3. Restart Claude Desktop for the changes to take effect