
D2 Diagram
STDIOMCP server for creating and manipulating D2 diagrams through AI assistants.
MCP server for creating and manipulating D2 diagrams through AI assistants.
A Model Context Protocol (MCP) server that provides D2 diagram generation and manipulation capabilities.
D2 is a modern diagram scripting language that turns text to diagrams. This MCP server allows AI assistants like Claude to create, render, export, and save D2 diagrams programmatically.
The server provides 10 tools through the MCP protocol with enhanced descriptions for optimal AI assistant integration, enabling both simple diagram rendering and sophisticated incremental diagram building using the Oracle API.
With the new Oracle API integration, AI assistants can now build and modify diagrams incrementally, making it perfect for:
d2mcp/
├── cmd/ # Application entry point
├── internal/
│ ├── domain/ # Business entities and interfaces
│ │ ├── entity/ # Domain entities
│ │ └── repository/ # Repository interfaces
│ ├── usecase/ # Business logic
│ ├── infrastructure/ # External implementations
│ │ ├── d2/ # D2 library integration
│ │ └── mcp/ # MCP server implementation
│ └── presentation/ # MCP handlers
│ └── handler/ # Tool handlers
└── pkg/ # Public packages
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
# Simple build make build # Run directly make run # Cross-platform builds make build-all
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
{ "mcpServers": { "d2mcp": { "command": "/path/to/d2mcp" } } }
Replace /path/to/d2mcp
with the actual path to your built binary.
# Run the MCP server (stdio transport) ./d2mcp
Create a new diagram with optional initial content (unified approach):
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 }
The Oracle API tools enable incremental diagram manipulation without regenerating the entire diagram. These tools are ideal for building diagrams step-by-step or making surgical edits.
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" }
Returns the complete D2 text of the diagram including all modifications made through Oracle API.
D2 has built-in support for sequence diagrams. Use d2_create
with proper D2 sequence diagram syntax:
{ "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 (unified approach):
// 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" })
# Run all tests make test # Run with coverage go test -cover ./... # Run specific test go test -v ./internal/presentation/handler
# Format code make fmt # Run linter make lint # Clean build artifacts make clean
internal/domain/entity
internal/domain/repository
internal/usecase
internal/presentation/handler
cmd/main.go
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
Contributions are welcome! Please feel free to submit a Pull Request.
d2_create
for all diagram creation needsd2_oracle_serialize
tool to get current D2 text representationThis project is licensed under the MIT License - see the LICENSE file for details.