Logotipo de D2 MCP Server

D2 MCP Server

Comunidad
i2y

A Model Context Protocol (MCP) server that provides D2 diagram generation and manipulation capabilities.

Editori2y
Repositoriod2mcp
IdiomaGo
Forks
7
Estrellas
32
Herramientas disponibles
0
Tipo de transportestdio
Categorías
LicenciaMIT
Enlaces
  • Conecta herramientas a flujos de trabajo con IA

    D2 MCP Server ofrece funciones MCP que pueden usar los clientes y agentes de IA compatibles.

  • 0 herramientas disponibles

    Explora a continuación las acciones que se pueden invocar, con sus nombres y descripciones cuando el servidor los proporcione.

  • Configuración lista para copiar

    Usa los ejemplos de instalación para configurar este servidor en tu cliente MCP preferido.

  • Indicadores de interés en el código abierto

    32 estrellas y 7 forks en el repositorio enlazado.

D2 MCP Server

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:

  • Converting conversations into architecture diagrams
  • Building flowcharts step-by-step as requirements are discussed
  • Creating entity relationship diagrams from database schemas
  • Generating system diagrams from code analysis
  • Refining diagrams based on user feedback without starting over

Features

Basic Diagram Operations

  • d2_create - Create new diagrams with optional initial content (unified approach)
  • d2_export - Export diagrams to various formats (SVG, PNG, PDF)
  • d2_save - Save existing diagrams to files

Oracle API for Incremental Editing

  • d2_oracle_create - Create shapes and connections incrementally
  • d2_oracle_set - Set attributes on existing elements
  • d2_oracle_delete - Delete specific elements from diagrams
  • d2_oracle_move - Move shapes between containers
  • d2_oracle_rename - Rename diagram elements
  • d2_oracle_get_info - Get information about shapes, connections, or containers
  • d2_oracle_serialize - Get the current D2 text representation of the diagram

Additional Features

  • 20 themes - Support for all D2 themes (18 light + 2 dark)

Project Structure

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

Prerequisites

  • Go 1.24.3 or higher
  • D2 v0.6.7 or higher (included as dependency)
  • For PNG/PDF export (optional):
    • rsvg-convert (from librsvg) or
    • ImageMagick (convert command)

Installation

From Source

bash
# 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

Using Go Install

bash
go install github.com/i2y/d2mcp/cmd@latest

Building

bash
# Simple build
make build

# Run directly
make run

# Cross-platform builds
make build-all

Usage

With Claude Desktop

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):

json
{
  "mcpServers": {
    "d2mcp": {
      "command": "/path/to/d2mcp",
      "args": ["-transport=stdio"]
    }
  }
}

For SSE transport:

json
{
  "mcpServers": {
    "d2mcp": {
      "command": "/path/to/d2mcp",
      "args": ["-transport=sse", "-addr=:3000"]
    }
  }
}

Replace /path/to/d2mcp with the actual path to your built binary.

Standalone

bash
# 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

Transport Options

d2mcp now supports multiple transport protocols:

STDIO Transport

The traditional stdio transport for direct process communication:

bash
./d2mcp -transport=stdio

SSE Transport (Server-Sent Events)

HTTP-based transport that allows network connectivity:

bash
# 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: When running in SSE mode, the following endpoints are available:

  • SSE stream: http://localhost:3000/mcp/sse
  • Message endpoint: http://localhost:3000/mcp/message

Streamable HTTP Transport

The modern HTTP-based transport that simplifies bidirectional communication:

bash
# 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 for Streamable HTTP (default: "/mcp")
  • -heartbeat-interval: Heartbeat interval in seconds (default: 30)
  • -stateless: Enable stateless mode (default: false)

Streamable HTTP Endpoint: When running in Streamable HTTP mode, a single endpoint handles all communication:

  • Endpoint: http://localhost:3000/mcp

Tools

d2_create

Create a new diagram with optional initial content (unified approach):

Empty diagram (for Oracle API workflow):

json
{
  "id": "my-diagram"
}

With initial D2 content:

json
{
  "id": "my-diagram",
  "content": "a -> b: Hello\nserver: {shape: cylinder}"
}

d2_export

Export a diagram to a specific format:

json
{
  "diagramId": "my-diagram",
  "format": "png"  // Options: "svg", "png", "pdf"
}

d2_save

Save a diagram to a file:

json
{
  "diagramId": "my-diagram",
  "format": "pdf",
  "path": "/path/to/output.pdf"  // Optional, defaults to temp directory
}

Oracle API Tools

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.

d2_oracle_create

Create a new shape or connection:

json
{
  "diagram_id": "my-diagram",
  "key": "server"  // Creates a shape
}
json
{
  "diagram_id": "my-diagram", 
  "key": "server -> database"  // Creates a connection
}

d2_oracle_set

Set attributes on existing elements:

json
{
  "diagram_id": "my-diagram",
  "key": "server.shape",
  "value": "cylinder"
}
json
{
  "diagram_id": "my-diagram",
  "key": "server.style.fill",
  "value": "#f0f0f0"
}

d2_oracle_delete

Delete elements from the diagram:

json
{
  "diagram_id": "my-diagram",
  "key": "server"  // Deletes the server and its children
}

d2_oracle_move

Move elements between containers:

json
{
  "diagram_id": "my-diagram",
  "key": "server",
  "new_parent": "network.internal",  // Moves server into network.internal
  "include_descendants": "true"       // Also moves child elements
}

d2_oracle_rename

Rename diagram elements:

json
{
  "diagram_id": "my-diagram",
  "key": "server",
  "new_name": "web_server"
}

d2_oracle_get_info

Get information about diagram elements:

json
{
  "diagram_id": "my-diagram",
  "key": "server",
  "info_type": "object"  // Options: "object", "edge", "children"
}

d2_oracle_serialize

Get the current D2 text representation of the diagram:

json
{
  "diagram_id": "my-diagram"
}

Returns the complete D2 text of the diagram including all modifications made through Oracle API.

Creating Sequence Diagrams

D2 has built-in support for sequence diagrams. Use d2_create with proper D2 sequence diagram syntax:

json
{
  "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:

json
{
  "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}"
}

Example Oracle API Workflow

Starting from scratch:

javascript
// 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):

javascript
// 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" })

When to Use Each Tool

  • d2_create: Always use for new diagrams - both empty (for incremental building) and with initial D2 content
  • d2_oracle_*: Use for incremental modifications to any diagram created with d2_create
  • d2_export: Use to render the final diagram in your desired format

Development

Running tests

bash
# Run all tests
make test

# Run with coverage
go test -cover ./...

# Run specific test
go test -v ./internal/presentation/handler

Code Quality

bash
# Format code
make fmt

# Run linter
make lint

# Clean build artifacts
make clean

Adding new features

  1. Define entities in internal/domain/entity
  2. Add repository interfaces in internal/domain/repository
  3. Implement business logic in internal/usecase
  4. Add infrastructure implementations
  5. Create MCP handlers in internal/presentation/handler
  6. Wire dependencies in cmd/main.go

Project Structure

  • cmd/: Application entry point
  • internal/domain/: Core business logic and entities
  • internal/infrastructure/: External service integrations
  • internal/presentation/: MCP protocol handlers
  • internal/usecase/: Application business logic

Troubleshooting

PNG/PDF Export Not Working

If you get errors when exporting to PNG or PDF formats, install one of these tools:

macOS:

bash
# Using Homebrew
brew install librsvg
# or
brew install imagemagick

Ubuntu/Debian:

bash
sudo apt-get install librsvg2-bin
# or
sudo apt-get install imagemagick

Windows: Download and install ImageMagick from the official website.

MCP Connection Issues

  1. Ensure the binary has execute permissions: chmod +x d2mcp
  2. Check Claude Desktop logs for error messages
  3. Verify the path in your configuration is absolute

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Changelog

v0.5.0 (Latest)

  • Added SSE (Server-Sent Events) transport support for network connectivity
  • Added Streamable HTTP transport support for modern bidirectional communication
  • New command-line flags for transport configuration
  • Support for stateful and stateless modes in Streamable HTTP
  • Maintained backward compatibility with stdio transport
  • Improved error handling and logging for different transport modes

v0.4.0

  • Simplified API to unified d2_create for all diagram creation needs
  • Enhanced tool descriptions for better AI assistant integration
  • Improved Oracle API error handling and validation
  • Reduced API surface from 14 to 10 tools
  • Breaking Change: Removed d2_render, d2_render_to_file, d2_import, d2_from_text - use d2_create instead

v0.3.0

  • Added d2_oracle_serialize tool to get current D2 text representation

v0.2.0

  • Added D2 Oracle API integration for incremental diagram manipulation
  • 6 new MCP tools for creating, modifying, and querying diagram elements
  • Support for stateful diagram editing sessions

v0.1.0

  • Initial release with basic D2 diagram operations
  • Support for rendering, creating, exporting, and saving diagrams
  • 20 built-in themes
  • MCP protocol integration

License

This project is licensed under the MIT License - see the LICENSE file for details.

Instalación

TypingMind
{
  "mcpServers": {
    "d2mcp": {
      "command": "d2mcp",
      "args": []
    }
  }
}

Usa el MCP de D2 MCP Server con varios modelos de IA

TypingMind conecta las herramientas MCP a nivel del espacio de trabajo, por lo que, una vez conectado D2 MCP Server, puedes usarlo con distintos modelos de IA en TypingMind sin tener que configurarlo por separado para cada modelo. Esta integración MCP se ejecuta localmente en tu dispositivo a través del conector MCP de TypingMind.

Guía de configuración para usar el conector local

Usa esta opción si el servidor MCP necesita acceder a archivos locales, aplicaciones o recursos privados de tu ordenador.

1

Abre los ajustes de MCP

En TypingMind, ve a Ajustes, luego a Ajustes avanzados y a Model Context Protocol, y elige Configurar conector.

  1. Abre TypingMind en tu navegador.
  2. Haz clic en el icono de Ajustes.
  3. Ve a Ajustes avanzados.
  4. Abre la sección Model Context Protocol.
  5. Haz clic en Configurar conector y elige Este dispositivo.
Pantalla de configuración del conector MCP de TypingMind con «Este dispositivo» seleccionado
2

Ejecuta el comando del conector

Elige Este dispositivo, copia el comando de TypingMind y ejecútalo en Terminal. Mantén el proceso en ejecución mientras uses MCP.

  1. Copia el comando de configuración que muestra TypingMind.
  2. Abre Terminal en macOS o Windows Terminal en Windows.
  3. Pega y ejecuta el comando.
  4. Acepta la instalación del paquete si Terminal te pide confirmación para continuar.
  5. Mantén el proceso en ejecución en la ventana de Terminal mientras uses las herramientas MCP.
3

Añade D2 MCP Server como servidor

Cuando el estado del conector sea «Listo», haz clic en «Edita servidores» y pega la configuración del servidor MCP.

  1. Espera hasta que el estado del conector indique «Listo».
  2. Haz clic en Edita servidores.
  3. Pega la configuración del servidor MCP de D2 MCP Server.
  4. Guarda la lista de servidores.
  5. Actualiza para comprobar que el conector sigue listo.
Ajustes de MCP de TypingMind con un servidor activo y el botón «Edita servidores»
{
  "mcpServers": {
    "d2mcp": {
      "command": "npx",
      "args": [
        "-y",
        "<mcp-server-package>"
      ]
    }
  }
}
4

Úsala con distintos modelos

Guarda la lista de servidores, abre Plugins, activa las herramientas MCP de D2 MCP Server y selecciona cualquier modelo de IA compatible en TypingMind para usar las herramientas en el chat o asignárselas a un agente de IA.

  1. Abre la página Plugins en TypingMind.
  2. Activa las herramientas MCP de D2 MCP Server.
  3. Inicia un chat y elige el modelo de IA que quieras usar.
  4. Usa las herramientas MCP en el chat o asígnalas a un agente de IA.
  5. Cambia a otro modelo de IA cuando lo necesites sin volver a conectar MCP.
Chat de TypingMind que usa herramientas MCP activadas con un modelo de IA seleccionado
¿Puedes usar D2 MCP Server para ayudarme con esta tarea?
D2 MCP Server
Claro. Lo he leído.
Esto he encontrado usando D2 MCP Server.

Preguntas frecuentes

¿Para qué sirve el servidor MCP D2 MCP Server?

D2 MCP Server es un servidor MCP que permite a los clientes de IA compatibles conectarse a herramientas y contexto externos. En TypingMind, basta con añadir este servidor MCP una vez para que sus herramientas estén disponibles en tu espacio de trabajo de IA.

¿Puedo usar el servidor MCP D2 MCP Server con varios modelos de IA en TypingMind?

Sí. TypingMind conecta las herramientas MCP a nivel del espacio de trabajo, por lo que puedes usar D2 MCP Server con distintos modelos de IA, como Claude, ChatGPT, Gemini u otros que hayas configurado en TypingMind, sin configurar el servidor MCP por separado para cada modelo.

¿Por qué usar el conector MCP de D2 MCP Server con TypingMind?

TypingMind es una de las mejores interfaces de chat con LLM porque reúne varios modelos de IA, prompts, plugins, agentes de IA, claves de API y herramientas MCP en un solo espacio de trabajo. Con D2 MCP Server conectado, puedes usar sus herramientas MCP con tus modelos preferidos y mantener organizado tu flujo de trabajo de chat en TypingMind.

¿Cómo conecto el servidor MCP D2 MCP Server a TypingMind?

D2 MCP Server funciona a través del conector MCP local de TypingMind. Es la mejor opción cuando el servidor MCP necesita acceder a archivos locales, aplicaciones de escritorio, herramientas de línea de comandos o recursos privados de tu ordenador.

¿Qué herramientas ofrece el servidor MCP D2 MCP Server en TypingMind?

D2 MCP Server ofrece funciones MCP que puedes activar desde la página Plugins de TypingMind y usar en el chat o asignar a agentes de IA.

¿Tengo que compartir mis claves de API con TypingMind para usar el servidor MCP D2 MCP Server?

No. TypingMind prioriza el funcionamiento local y te permite mantener el control de tus proveedores de modelos, claves de API, prompts y configuración MCP. Si D2 MCP Server requiere autenticación, añade las cabeceras, los ajustes de OAuth o la configuración local necesarios para ese servidor MCP al crear la conexión.

Servidores MCP relacionados

Ver todos

Crea ya tu propio espacio de trabajo IA

Suscríbete a nuestro boletín para recibir avisos sobre nuevas funciones y próximos sorteos 👇