Mermaid ER数据库API生成器

一个开源工具,解析Mermaid ER图,自动创建PostgreSQL表并生成REST/GraphQL CRUD API。

  • by dixitayush
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 dixitayush/mcp-api

使用说明

MCP Mermaid ER服务器

开源 模型上下文协议(MCP) 解析的服务器 美人鱼ER图,创建 PostgreSQL数据库表,并自动曝光 REST/GraphQL CRUD API.

特性

  • 🔍 解析美人鱼ER图 -提取实体、属性和关系
  • 🗄️ 自动创建PostgreSQL表 -生成DDL并对数据库执行
  • 🚀 REST API生成 -所有实体的自动CRUD端点
  • 📊 GraphQL API生成 -类型安全查询和突变
  • 🔑 密钥检测 -确定PK、FK和UK限制
  • ⚙️ 可配置的 -数据库和API设置的环境变量

安装

git clone https://github.com/yourusername/mcp-mermaid-er-server.git
cd mcp-mermaid-er-server
npm install
npm run build

配置

创建一个 .env 文件(复制自 .env.example):

# Mermaid Source
MERMAID_DIAGRAM_PATH=./examples/sample-er.mmd

# Database (PostgreSQL)
DB_HOST=localhost
DB_PORT=5432
DB_NAME=mydb
DB_USER=postgres
DB_PASSWORD=password

# API Server
API_TYPE=rest       # rest, graphql, or both
API_PORT=3000
API_HOST=0.0.0.0

Claude桌面集成

增添 claude_desktop_config.json:

{
  "mcpServers": {
    "mermaid-er": {
      "command": "node",
      "args": ["/path/to/mcp-tools/dist/index.js"],
      "env": {
        "MERMAID_DIAGRAM_PATH": "/path/to/your/diagram.mmd",
        "DB_HOST": "localhost",
        "DB_NAME": "mydb",
        "DB_USER": "postgres",
        "DB_PASSWORD": "password"
      }
    }
  }
}

可用MCP工具(共12个)

解析工具

工具 说明
parse_er_diagram 解析ER图,返回完整模式
list_entities 列出所有实体名称
get_entity_details 获取实体的属性
get_relationships 获取所有关系
validate_diagram 验证图表语法

数据库工具

工具 说明
test_connection 测试PostgreSQL连接
generate_sql 生成DDL而不执行
create_schema 在数据库中创建表
drop_schema 删除所有表格(需要确认)

API工具

工具 说明
start_api_server 启动REST/GraphQL服务器
stop_api_server 停止API服务器
get_api_endpoints 列出所有端点

工作流示例

  1. 解析ER图 → parse_er_diagram
  2. 查看SQL → generate_sql
  3. 创建数据库表 → create_schema
  4. 启动API服务器 → start_api_server
  5. 使用自动生成的端点!

API终点

休息(当 API_TYPE=rest 或 both)

GET    /api/{entity}       - List all records
GET    /api/{entity}/:id   - Get by ID
POST   /api/{entity}       - Create record
PUT    /api/{entity}/:id   - Update record
DELETE /api/{entity}/:id   - Delete record

GraphQL(当 API_TYPE=graphql 或 both)

# Queries
query { customers { customer_id email } }
query { customer(id: "1") { email } }

# Mutations
mutation { createCustomer(input: { email: "test@example.com" }) { customer_id } }
mutation { updateCustomer(id: "1", input: { email: "new@example.com" }) { email } }
mutation { deleteCustomer(id: "1") { customer_id } }

支持的ER语法

erDiagram
    CUSTOMER {
        int customer_id PK "Primary key"
        string email UK "Unique email"
        string name
    }
    
    ORDER {
        int order_id PK
        int customer_id FK
        datetime order_date
    }
    
    CUSTOMER ||--o{ ORDER : "places"

发展

npm run dev      # Run with ts-node
npm run build    # Compile TypeScript
npm test         # Run tests

许可证

MIT许可证-请参阅 许可证