图表生成服务器

一个强大且可扩展的模型上下文协议(MCP)服务器,用于从结构化数据生成高质量的图表和图形,支持多种渲染后端。

  • by jmcarbo
  • Go
  • 本地部署
  • 更新于
在 GitHub 查看 jmcarbo/grapher

使用说明

Grapher MCP服务器

一个强大且可扩展的模型上下文协议(MCP)服务器,用于从结构化数据生成高质量的图形和图表。支持多种渲染后端,包括Graphviz、D2、Mermaid、SVG和Excalidraw。

🎯 特性

  • 多后端支持:Graphviz、D2、Mermaid、原生SVG和Excalidraw
  • 丰富的输出格式:SVG、PNG、PDF、DOT、JSON和本机格式
  • GraphScript DSL:用于复杂图形定义的强大领域特定语言
  • MCP集成:与启用MCP的应用程序无缝协作
  • 灵活的配置:基于JSON的配置,支持环境变量
  • 生产就绪:内置验证、错误处理和性能监控

🚀 快速开始

安装

  1. 安装Go 1.19+

    go version  # Should be 1.19 or higher
    
  2. 克隆和构建

    git clone https://github.com/jmcarbo/grapher.git
    cd grapher
    go build -o grapher ./cmd/grapher
    
  3. 安装后端依赖关系

    # Graphviz (for high-quality graph rendering)
    # macOS
    brew install graphviz
    
    # Ubuntu/Debian
    sudo apt-get install graphviz
    
    # D2 (for modern diagram generation)
    curl -fsSL https://d2lang.com/install.sh | sh -
    

基本用法

  1. 启动服务器

    ./grapher
    
  2. 通过MCP客户端使用

    {
      "method": "tools/call",
      "params": {
        "name": "generate_graph",
        "arguments": {
          "graph": {
            "nodes": [
              {"id": "A", "label": "Start"},
              {"id": "B", "label": "Process"},
              {"id": "C", "label": "End"}
            ],
            "edges": [
              {"from": "A", "to": "B", "label": "begin"},
              {"from": "B", "to": "C", "label": "finish"}
            ],
            "type": "directed",
            "title": "Simple Process Flow"
          },
          "backend": "graphviz",
          "options": {
            "format": "svg",
            "layout": "dot"
          }
        }
      }
    }
    

输出示例

服务器将生成如下图所示的精美图表:

┌─────────┐    begin    ┌─────────┐    finish   ┌─────────┐
│  Start  │ ──────────→ │ Process │ ─────────→  │   End   │
└─────────┘             └─────────┘             └─────────┘

🏗️ 建筑

┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   MCP Client    │───→│  Grapher Server  │───→│    Backends     │
└─────────────────┘    └──────────────────┘    └─────────────────┘
                              │                        │
                              ▼                        ▼
                     ┌─────────────────┐    ┌─────────────────┐
                     │  Graph Parser   │    │   Graphviz      │
                     │  & Validator    │    │   D2            │
                     └─────────────────┘    │   Mermaid       │
                                           │   SVG           │
                                           │   Excalidraw    │
                                           └─────────────────┘

📚 可用后端

后端 格式 布局 描述
Graphviz SVG、PNG、PDF、DOT DOT、neato、fdp、sfdp、twopi、circo 具有精确布局的高质量图形渲染
第2页 SVG、PNG、PDF、JSON dagre、elk、tala 具有智能布局的现代声明性图表
美人鱼 SVG、PNG、PDF 流程图、序列、类 具有web集成的流行图表语法
可缩放矢量图形 SVG 分层、强制、循环 具有自定义布局的原生SVG生成
Excalidraw 的 JSON 有机,手绘 手绘风格的图表和草图

🛠️ MCP工具

服务器提供以下MCP工具:

  • generate_graph -从节点/边数据生成图形
  • list_backends -列出可用的渲染后端
  • get_backend_info -获取详细的后端信息
  • validate_graph -无需渲染即可验证图形结构

📖 文档

⚙️ 配置

创建一个 grapher.json 配置文件:

{
  "server": {
    "name": "grapher",
    "version": "1.0.0",
    "timeout": "30s"
  },
  "backends": {
    "graphviz": {
      "enabled": true,
      "path": "dot",
      "timeout": "15s"
    },
    "d2": {
      "enabled": true,
      "path": "d2",
      "timeout": "30s"
    }
  },
  "logging": {
    "level": "info",
    "format": "json"
  }
}

🔧 命令行选项

# Basic usage
./grapher

# Custom configuration
./grapher -config /path/to/config.json

# List available backends
./grapher -list-backends

# Test a specific backend
./grapher -test-backend graphviz

# Show version
./grapher -version

# Set log level
./grapher -log-level debug

📊 例子

简单有向图

{
  "nodes": [
    {"id": "start", "label": "Start"},
    {"id": "end", "label": "End"}
  ],
  "edges": [
    {"from": "start", "to": "end"}
  ],
  "type": "directed"
}

网络图

{
  "nodes": [
    {"id": "web", "label": "Web Server", "attributes": {"shape": "box"}},
    {"id": "db", "label": "Database", "attributes": {"shape": "cylinder"}},
    {"id": "cache", "label": "Redis Cache", "attributes": {"shape": "ellipse"}}
  ],
  "edges": [
    {"from": "web", "to": "db", "label": "queries"},
    {"from": "web", "to": "cache", "label": "cache"}
  ],
  "type": "undirected"
}

组织结构图

{
  "nodes": [
    {"id": "ceo", "label": "CEO"},
    {"id": "cto", "label": "CTO"},
    {"id": "dev1", "label": "Developer 1"},
    {"id": "dev2", "label": "Developer 2"}
  ],
  "edges": [
    {"from": "ceo", "to": "cto"},
    {"from": "cto", "to": "dev1"},
    {"from": "cto", "to": "dev2"}
  ],
  "type": "tree"
}

🧪 测试

运行测试套件:

# Unit tests
go test ./...

# Integration tests (requires backends)
go test ./... -tags=integration

# Test specific backend
./grapher -test-backend graphviz

🚦 状态和健康检查

服务器提供健康检查端点和状态监视:

# Check if server is running
curl http://localhost:8080/health

# Get backend status
./grapher -list-backends

🤝 贡献

我们欢迎捐款!查看我们的 贡献指南 了解详情。

开发设置

  1. 分叉存储库
  2. 安装依赖项
  3. 运行测试
  4. 提交拉取请求
git clone https://github.com/yourusername/grapher.git
cd grapher
go mod download
go test ./...

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

🙏 致谢

📞 支持


内置于❤️ 对于MCP生态系统