
D2 MCP Server
io.github.RecursiveFunctions由 GitHub 帳號 RecursiveFunctions 發布v0.5.0更新於 Oct 24, 2026
Create, edit, validate, render, and export D2 diagrams with confined workspace access.
概覽
一個本機 MCP 伺服器,讓助理撰寫、驗證、增量編輯、算繪並儲存 D2 圖表,輸出 SVG、PNG、PDF、PPTX、GIF 或 ASCII。
- 可以做什麼
- 建立、讀取、格式化並原子替換完整的 D2 原始碼,儲存前先驗證並提供修復建議。透過 Oracle 工具增量編輯圖表:新增形狀與連線、設定屬性、移動、重新命名或刪除元素,並檢視或序列化結果。使用內建佈局(Dagre、ELK、TALA)、主題與型別化算繪選項匯出 SVG、PNG、PDF、PPTX、GIF 或 ASCII。將匯出檔儲存到已設定的工作區根目錄內。查詢內建的 D2 v0.9 語言指南與能力目錄。
- 適用情境
- 適合在對話過程中產生或修改圖表:把討論轉成架構圖、逐步建立流程圖、依資料庫結構產生 ER 圖,或在不重新產生整張圖的情況下做局部修改。
- 執行需求
- 以本機程序執行,通常使用 Docker 映像並把工作目錄掛載到 /workspace,或以 Go 從原始碼建置的執行檔。未宣告帳號、API 金鑰或環境變數。儲存匯出檔時,掛載目錄需對容器的非 root 使用者可寫。
安裝
在 SourceWeft 中
- 開啟 儀表板中的 D2 MCP Server,將其新增到工作區。
- 為需要使用其工具的對話啟用該服務。
Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。
其他 MCP 客戶端
參照 儲存庫 中的啟動說明。
README
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 16 MCP tools for complete-source authoring, validation, rendering, embedded D2 guidance, and incremental Oracle editing.
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
Source-First Diagram Operations
- d2_create - Create diagrams from complete D2 source or start empty
- d2_get_source - Retrieve the canonical source, including Oracle edits
- d2_update_source - Validate then atomically replace complete source
- d2_format - Format valid source without storing it
- d2_validate - Validate raw or stored source and suggest conservative repairs
- d2_export - Export with bundled D2 v0.9 renderers (SVG, PNG, PDF, PPTX, GIF, ASCII)
- d2_save - Save an export inside a configured workspace root
- d2_help - Search or retrieve embedded D2 v0.9 guidance
- d2_capabilities - List the versioned capability catalog
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
- Bundled layouts - Dagre, ELK, and TALA
- Typed rendering - Layout, theme overrides, bundled fonts, scale, padding, sketch, animation, board selection, and bounded output options
- 20 themes - Support for all D2 themes (18 light + 2 dark)
Project Structure
Prerequisites
- Go 1.27 or higher
- D2 v0.9.0 (included as a Go dependency)
All export formats use bundled Go renderers; no rsvg-convert, ImageMagick, or Chromium installation is required.
The embedded capability catalog is based on the official D2 language tour, D2 API documentation, and D2 v0.9.0 release.
Installation
From Source
Using Go Install
Using Docker
The image defaults to STDIO transport and confines local imports, assets, and exports to the mounted /workspace directory. The container runs as a non-root user, so the mounted directory must be writable by that user when saving exports.
MCP Registry
Release tags publish the server as io.github.RecursiveFunctions/d2mcp in the official MCP Registry. Registry publication and editor-gallery indexing are separate, so a newly published version might not appear immediately in an editor's search results.
Compatible clients can also use the portable .mcp.json configuration in this repository. It requires Docker and mounts the current workspace at /workspace.
Editor Integrations
Client configurations are generated from server.json, so they always use the same immutable container version.
Run make generate-adapters after changing release metadata. CI runs make verify-adapters and rejects stale client configurations.
Building
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):
For SSE transport:
Replace /path/to/d2mcp with the actual path to your built binary.
Standalone
Transport Options
d2mcp now supports multiple transport protocols:
STDIO Transport
The traditional stdio transport for direct process communication:
SSE Transport (Server-Sent Events)
HTTP-based transport that allows network connectivity:
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:
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
Workspace and Asset Security
The server confines imports, local images, and saved files to named workspace roots. The default root is the server's current working directory.
-workspace-root is repeatable. Local path traversal and symlink escapes are rejected. Remote assets require HTTPS and are subject to DNS/IP, redirect, timeout, MIME, and response-size checks; loopback and private-network destinations are rejected.
Tools
d2_create
Create a new diagram with optional initial content (unified approach):
Empty diagram (for Oracle API workflow):
With initial D2 content and a named workspace for imports/assets:
d2_export
Export a diagram using a bundled renderer and layout engine:
Formats are svg, png, pdf, pptx, gif, and ascii (txt is an alias). Layouts are dagre, elk, and tala. Typed options cover engine spacing, themes and overrides, bundled fonts, padding, scale, sketch mode, animation, board selection, and resource limits.
PDF, PPTX, GIF, and optionally animated SVG compose bounded descendant boards. PNG and ASCII produce one board; use boardPath to select one from a composition.
d2_save
Save a diagram inside a configured workspace root:
If path is omitted, output is written below d2mcp_output/ in the selected root.
d2_validate
Validate raw D2 source or the current source of a stored diagram. Supply exactly one input:
Invalid D2 returns a successful structured result with valid: false and compiler diagnostics. Simple, unambiguous terminator errors may also include repaired_content; validation never modifies a stored diagram.
d2_get_source and d2_update_source
Complete D2 source is canonical, so every bundled v0.9 language feature can be authored without waiting for a specialized structured tool.
Updates compile first and replace atomically. Invalid drafts return structured diagnostics and leave stored source unchanged.
d2_format
Returns formatted source without creating or changing a diagram.
d2_help and d2_capabilities
d2_help searches the embedded v0.9.0 reference or returns an exact topic by ID. d2_capabilities lists its categories and MCP resource URIs. Embedded resources cover the core language, connections, reusable definitions, visuals, rich text, structured/sequence/grid diagrams, composition, layouts, and exports.
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:
d2_oracle_set
Set attributes on existing elements:
d2_oracle_delete
Delete elements from the diagram:
d2_oracle_move
Move elements between containers:
d2_oracle_rename
Rename diagram elements:
d2_oracle_get_info
Get information about diagram elements:
d2_oracle_serialize
Get the current D2 text representation of the 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:
Example with actors and grouping:
Example Oracle API Workflow
Starting from scratch:
Starting with existing content (unified approach):
When to Use Each Tool
- d2_create: Create a stored source document, optionally bound to a named workspace root
- d2_update_source: Author or replace any complete D2 v0.9 document atomically
- d2_oracle_*: Make convenient structured edits; each mutation is revalidated before commit
- d2_get_source: Retrieve the canonical source after either editing workflow
- d2_help / d2_capabilities: Discover supported syntax, composition, layouts, and exports
- d2_export / d2_save: Render in memory or save within an allowed workspace root
Development
Running tests
Code Quality
Adding new features
- Define entities in
internal/domain/entity - Add repository interfaces in
internal/domain/repository - Implement business logic in
internal/usecase - Add infrastructure implementations
- Create MCP handlers in
internal/presentation/handler - 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
Export Limit Errors
PNG, PDF, PPTX, and GIF exports are produced by D2's bundled Go raster pipeline. If a diagram exceeds a resource ceiling, reduce its scale or board count, select a specific boardPath, or set an allowed bounded maxWidth, maxHeight, maxPixels, maxFrames, or maxOutputBytes value.
MCP Connection Issues
- Ensure the binary has execute permissions:
chmod +x d2mcp - Check Claude Desktop logs for error messages
- 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_createfor 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_serializetool 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.
來源:README.md,提交 ecd6ce2
工具
0版本歷史
1- v0.5.0最新Sep 27, 2026
評分
已滿足適用檢查項的 50%
該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。
- 近 30 天內可連通25 分
請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。
- 已宣告認證方式10 分
請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。
- 已列出工具15 分
請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。
- 描述必需15 分
- 圖示10 分
- 文件15 分
- 授權條款10 分
