D2 MCP 服务器是一个模型上下文协议(MCP)服务器,提供 D2 图表生成和操作功能。D2 是一种现代图表脚本语言,可将文本转换为图表。此 MCP 服务器允许像 Claude 这样的 AI 助手以编程方式创建、渲染、导出和保存 D2 图表。
该服务器通过 MCP 协议提供 10 种工具,并带有增强描述,以实现与 AI 助手的最佳集成,既支持简单的图表渲染,也支持使用 Oracle API 进行复杂的增量式图表构建。借助新的 Oracle API 集成,AI 助手现在可以增量式地构建和修改图表,非常适合以下场景:
# 克隆仓库
git clone https://github.com/i2y/d2mcp.git
cd d2mcp
# 构建二进制文件
make build
# 或为所有平台构建
make build-all
go install github.com/i2y/d2mcp/cmd@latest
将以下内容添加到 Claude Desktop 配置文件中:
~/Library/Application Support/Claude/claude_desktop_config.json%APPDATA%\Claude\claude_desktop_config.json~/.config/Claude/claude_desktop_config.json使用 STDIO 传输(推荐用于 Claude Desktop)
{
"mcpServers": {
"d2mcp": {
"command": "/path/to/d2mcp",
"args": ["-transport=stdio"]
}
}
}
使用 SSE 传输
{
"mcpServers": {
"d2mcp": {
"command": "/path/to/d2mcp",
"args": ["-transport=sse", "-addr=:3000"]
}
}
}
请将 /path/to/d2mcp 替换为你实际构建的二进制文件的路径。
# 运行 MCP 服务器(stdio 传输)
./d2mcp -transport=stdio
# 使用 SSE 传输运行(默认)
./d2mcp
# 或显式指定
./d2mcp -transport=sse
# 使用 Streamable HTTP 传输运行
./d2mcp -transport=streamable
d2mcp 现在支持多种传输协议:
用于直接进程通信的传统 stdio 传输:
./d2mcp -transport=stdio
基于 HTTP 的传输,支持网络连接:
# 基本 SSE 模式(默认端口 :3000)
./d2mcp -transport=sse
# 自定义配置
./d2mcp -transport=sse \
-addr=:8080 \
-base-url=http://localhost:8080 \
-base-path=/mcp \
-keep-alive=30
SSE 配置选项:
-addr:监听地址(默认:":3000")-base-url:SSE 端点的基本 URL(未指定时自动生成)-base-path:SSE 端点的基本路径(默认:"/mcp")-keep-alive:保活间隔(秒)(默认:30)SSE 端点: 在 SSE 模式下运行时,可使用以下端点:
http://localhost:3000/mcp/ssehttp://localhost:3000/mcp/message简化双向通信的现代基于 HTTP 的传输:
# 基本 Streamable HTTP 模式
./d2mcp -transport=streamable
# 自定义配置
./d2mcp -transport=streamable \
-addr=:8080 \
-endpoint-path=/mcp \
-heartbeat-interval=30 \
-stateless
Streamable HTTP 配置选项:
-addr:监听地址(默认:":3000")-endpoint-path:Streamable HTTP 的端点路径(默认:"/mcp")-heartbeat-interval:心跳间隔(秒)(默认:30)-stateless:启用无状态模式(默认:false)Streamable HTTP 端点: 在 Streamable HTTP 模式下运行时,单个端点处理所有通信:
http://localhost:3000/mcp创建带有可选初始内容的新图表(统一方法):
空图表(用于 Oracle API 工作流)
{
"id": "my-diagram"
}
带有初始 D2 内容
{
"id": "my-diagram",
"content": "a -> b: Hello\nserver: {shape: cylinder}"
}
将图表导出为特定格式:
{
"diagramId": "my-diagram",
"format": "png" // 选项: "svg", "png", "pdf"
}
将图表保存到文件:
{
"diagramId": "my-diagram",
"format": "pdf",
"path": "/path/to/output.pdf" // 可选,默认为临时目录
}
Oracle API 工具允许在不重新生成整个图表的情况下进行增量式图表操作。这些工具非常适合逐步构建图表或进行精细编辑。
创建新的形状或连接:
{
"diagram_id": "my-diagram",
"key": "server" // 创建一个形状
}
{
"diagram_id": "my-diagram",
"key": "server -> database" // 创建一个连接
}
设置现有元素的属性:
{
"diagram_id": "my-diagram",
"key": "server.shape",
"value": "cylinder"
}
{
"diagram_id": "my-diagram",
"key": "server.style.fill",
"value": "#f0f0f0"
}
从图表中删除元素:
{
"diagram_id": "my-diagram",
"key": "server" // 删除服务器及其子元素
}
在容器之间移动元素:
{
"diagram_id": "my-diagram",
"key": "server",
"new_parent": "network.internal", // 将服务器移动到 network.internal 中
"include_descendants": "true" // 同时移动子元素
}
重命名图表元素:
{
"diagram_id": "my-diagram",
"key": "server",
"new_name": "web_server"
}
获取图表元素的信息:
{
"diagram_id": "my-diagram",
"key": "server",
"info_type": "object" // 选项: "object", "edge", "children"
}
获取图表当前的 D2 文本表示:
{
"diagram_id": "my-diagram"
}
返回包含通过 Oracle API 所做所有修改的完整 D2 文本。
D2 内置支持序列图。使用 d2_create 和正确的 D2 序列图语法:
{
"id": "api-flow",
"content": "shape: sequence_diagram\n\nClient -> Server: HTTP Request\nServer -> Database: Query\nDatabase -> Server: Results\nServer -> Client: HTTP Response\n\n# 添加样式\nClient -> Server.\"HTTP Request\": {style.stroke-dash: 3}\nDatabase -> Server.\"Results\": {style.stroke-dash: 3}"
}
带有参与者和分组的示例
{
"id": "auth-flow",
"content": "shape: sequence_diagram\n\ntitle: Authentication Flow {near: top-center}\n\n# 定义参与者\nClient: {shape: person}\nAuth Server: {shape: cloud}\nDatabase: {shape: cylinder}\n\n# 交互\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}"
}
从头开始
// 1. 创建一个空图表
d2_create({ id: "architecture" })
// 2. 增量式添加形状
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. 设置属性
d2_oracle_set({ diagram_id: "architecture", key: "db.shape", value: "cylinder" })
d2_oracle_set({ diagram_id: "architecture", key: "web.label", value: "Web Server" })
// 4. 创建连接
d2_oracle_create({ diagram_id: "architecture", key: "web -> api" })
d2_oracle_create({ diagram_id: "architecture", key: "api -> db" })
// 5. 导出最终结果
d2_export({ diagramId: "architecture", format: "svg" })
从现有内容开始(统一方法)
// 1. 使用初始内容创建图表
d2_create({
id: "architecture",
content: "web -> api -> db\ndb: {shape: cylinder}"
})
// 2. 使用 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. 导出最终结果
d2_export({ diagramId: "architecture", format: "svg" })
d2mcp/
├── cmd/ # 应用程序入口点
├── internal/
│ ├── domain/ # 业务实体和接口
│ │ ├── entity/ # 领域实体
│ │ └── repository/ # 存储库接口
│ ├── usecase/ # 业务逻辑
│ ├── infrastructure/ # 外部实现
│ │ ├── d2/ # D2 库集成
│ │ └── mcp/ # MCP 服务器实现
│ └── presentation/ # MCP 处理程序
│ └── handler/ # 工具处理程序
└── pkg/ # 公共包
# 运行所有测试
make test
# 运行带覆盖率的测试
go test -cover ./...
# 运行特定测试
go test -v ./internal/presentation/handler
# 格式化代码
make fmt
# 运行代码检查
make lint
# 清理构建产物
make clean
internal/domain/entity 中定义实体internal/domain/repository 中添加存储库接口internal/usecase 中实现业务逻辑internal/presentation/handler 中创建 MCP 处理程序cmd/main.go 中连接依赖项如果在导出为 PNG 或 PDF 格式时遇到错误,请安装以下工具之一:
macOS
# 使用 Homebrew
brew install librsvg
# 或
brew install imagemagick
Ubuntu/Debian
sudo apt-get install librsvg2-bin
# 或
sudo apt-get install imagemagick
Windows 从官方网站下载并安装 ImageMagick。
chmod +x d2mcp欢迎贡献代码!请随时提交拉取请求。
d2_create 满足所有图表创建需求d2_oracle_serialize 工具,用于获取图表当前的 D2 文本表示本项目采用 MIT 许可证 - 详情请参阅 LICENSE 文件。