Skip to main content
Glama
dixitayush
by dixitayush
README.md
# MCP Mermaid ER Server

An open-source **Model Context Protocol (MCP)** server that parses **Mermaid ER diagrams**, creates **PostgreSQL database tables**, and exposes automatic **REST/GraphQL CRUD APIs**.

![MCP](https://img.shields.io/badge/MCP-Compatible-brightgreen)
![TypeScript](https://img.shields.io/badge/TypeScript-100%25-blue)
![License](https://img.shields.io/badge/License-MIT-yellow)

## Features

- 🔍 **Parse Mermaid ER Diagrams** - Extract entities, attributes, and relationships
- 🗄️ **Auto-Create PostgreSQL Tables** - Generate DDL and execute against your database
- 🚀 **REST API Generation** - Automatic CRUD endpoints for all entities
- 📊 **GraphQL API Generation** - Type-safe queries and mutations
- 🔑 **Key Detection** - Identifies PK, FK, and UK constraints
- ⚙️ **Configurable** - Environment variables for database and API settings

## Installation

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

## Configuration

Create a `.env` file (copy from `.env.example`):

```env
# 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 Desktop Integration

Add to `claude_desktop_config.json`:

```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"
      }
    }
  }
}
```

## Available MCP Tools (12 Total)

### Parsing Tools
| Tool | Description |
|------|-------------|
| `parse_er_diagram` | Parse ER diagram, return full schema |
| `list_entities` | List all entity names |
| `get_entity_details` | Get attributes for an entity |
| `get_relationships` | Get all relationships |
| `validate_diagram` | Validate diagram syntax |

### Database Tools
| Tool | Description |
|------|-------------|
| `test_connection` | Test PostgreSQL connection |
| `generate_sql` | Generate DDL without executing |
| `create_schema` | Create tables in database |
| `drop_schema` | Drop all tables (requires confirmation) |

### API Tools
| Tool | Description |
|------|-------------|
| `start_api_server` | Start REST/GraphQL server |
| `stop_api_server` | Stop the API server |
| `get_api_endpoints` | List all endpoints |

## Workflow Example

1. **Parse your ER diagram** → `parse_er_diagram`
2. **Review the SQL** → `generate_sql`
3. **Create database tables** → `create_schema`
4. **Start the API server** → `start_api_server`
5. **Use the auto-generated endpoints!**

## API Endpoints

### REST (when `API_TYPE=rest` or `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 (when `API_TYPE=graphql` or `both`)
```graphql
# 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 } }
```

## Supported ER Syntax

```mermaid
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"
```

## Development

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

## License

MIT License - see [LICENSE](LICENSE)

TDQS

A3.7/5.0

Scored across 12 tools

Disambiguation5/5

Each tool targets a distinct action or resource: read operations are separated into entities, relationships, and diagram parsing, while generate_sql is clearly distinct from create_schema since one only outputs DDL and the other executes it. The API server lifecycle is also cleanly split into start, stop, and endpoint inspection. No two tools appear likely to be confused.

Naming Consistency5/5

All tool names follow a consistent lower_snake_case verb_noun convention. The verbs clearly indicate the action being taken, such as list, get, validate, generate, create, drop, start, and stop. The minor use of both list and get for read operations is not a meaningful inconsistency.

Tool Count5/5

With 12 tools, the server is well-scoped for its combined purpose of parsing Mermaid ER diagrams, managing PostgreSQL schema, and controlling an API server. Each tool supports a distinct part of the workflow without unnecessary duplication. The count fits comfortably within the ideal range.

Completeness4/5

The core lifecycle is well covered: parse and validate the diagram, inspect entities and relationships, generate and execute SQL, drop the schema, and start/stop the API server. Minor gaps exist, such as no tool for incremental schema updates or altering existing tables, but these are not essential to the stated purpose. Overall, the surface is complete enough for the primary workflow.

Maintenance

ActivityInactive
ResponsivenessNo issues