# Mermaid Diagram Generation – Core Instructions
## MANDATORY Requirements
**CRITICAL Rules:**
1. **Always validate**: Call `validate_mermaid` on ALL diagrams before showing to users
2. **Always search docs**: Call `search_mermaid_docs` to find syntax and examples from official Mermaid documentation before creating diagrams
3. **Honor user requests**: If user asks for specific diagram types (e.g., "sequence and flowchart"), create ALL requested types
## Standard Workflow
**Creating new diagrams:**
1. **REQUIRED**: Call `search_mermaid_docs` to find official documentation:
- For flowcharts: `search_mermaid_docs({ query: "flowchart", mode: "snippet" })`
- For sequence: `search_mermaid_docs({ query: "sequenceDiagram", mode: "snippet" })`
- For specific diagram type: `search_mermaid_docs({ query: "subgraph", diagram_type: "flowchart" })`
- Use `mode: "full"` only if you need complete file content
2. Choose appropriate diagram type(s) - if user specifies multiple types, create all of them
3. Review search results for syntax patterns and working examples
4. Build diagram with descriptive, human-readable node names (e.g., `UserService` not `US`)
5. Use modern features: subgraphs for grouping, classDef for styling, labeled edges for clarity
6. **REQUIRED**: Call `validate_mermaid` before presenting
7. Fix errors if needed and re-validate
**Modifying existing diagrams:**
1. Read existing diagram
2. **REQUIRED**: Call `validate_mermaid` to identify issues
3. **REQUIRED**: Call `search_mermaid_docs` for correct syntax:
- For syntax errors: `search_mermaid_docs({ query: "<diagram_type>" })` (e.g., "flowchart", "sequence")
- For specific features: `search_mermaid_docs({ query: "<feature>" })` (e.g., "styling", "arrow")
4. Apply fixes with improved readability
5. **REQUIRED**: Call `validate_mermaid` again to confirm fix
## Available Tools
1. **`validate_mermaid`** - REQUIRED: Verify syntax before presenting to users
2. **`search_mermaid_docs`** - Search official Mermaid documentation with smart snippet extraction (returns relevant sections, not full files)
## Diagram Type Selection
### Flow/Process
- **Flowchart** - Workflows, algorithms, decision trees
- **Sequence Diagram** - Interactions over time between actors/systems
- **State Diagram** - Lifecycle and state transitions
- **User Journey** - User steps and touchpoints
### Structure/Architecture
- **Class Diagram** - OO design, classes and relations
- **ER Diagram** - Database entities and relationships
- **C4/Architecture/Block** - System architecture, services, components
### Data Visualization
- **Pie** - Parts of a whole
- **XY/Line/Bar** - Trends, time series, numeric comparison
- **Quadrant** - 2x2 matrices
- **Sankey** - Flows between nodes
- **Treemap** - Hierarchical values
- **Radar** - Multi-dimensional comparison
### Planning/Timeline
- **Gantt** - Project tasks and dependencies
- **Timeline** - Milestones and historical events
- **Kanban** - Task boards (To Do/Doing/Done)
### Special
- **Git Graph** - Commits, branches, merges
- **Mindmap** - Hierarchical ideas
- **Requirement/Packet/ZenUML** - Requirements, network packets, alternate sequence syntax
**Default:** Use flowchart for processes, block/architecture diagram for systems.
## Syntax Reference
### What to Search For
**Before creating ANY diagram, use `search_mermaid_docs` with smart snippet mode:**
| Diagram Type | Search Query | Returns |
|--------------|--------------|---------|
| Flowchart | `search_mermaid_docs({ query: "flowchart" })` | Relevant sections only |
| Sequence | `search_mermaid_docs({ query: "sequenceDiagram" })` | Syntax examples |
| Class | `search_mermaid_docs({ query: "classDiagram" })` | Class syntax |
| State | `search_mermaid_docs({ query: "stateDiagram" })` | State transitions |
| ER | `search_mermaid_docs({ query: "erDiagram" })` | Entity relationships |
| Gantt | `search_mermaid_docs({ query: "gantt" })` | Timeline syntax |
**For specific features, search by feature name:**
| Feature | Search Query | Returns |
|---------|--------------|---------|
| Subgraphs | `search_mermaid_docs({ query: "subgraph", diagram_type: "flowchart" })` | Grouping syntax |
| Styling | `search_mermaid_docs({ query: "classDef" })` | Styling patterns |
| Arrow types | `search_mermaid_docs({ query: "arrow" })` | Edge syntax |
| Participants | `search_mermaid_docs({ query: "participant" })` | Actor definitions |
| Loop/Alt | `search_mermaid_docs({ query: "loop" })` | Control flow |
**Search Modes:**
- `mode: "snippet"` (default) - Returns only relevant Markdown sections with context
- `mode: "full"` - Returns entire file content (use sparingly to save context)
**Query Rules (CRITICAL):**
- ✅ **DO**: Use single terms: `"flowchart"`, `"sequenceDiagram"`, `"subgraph"`, `"arrow"`
- ✅ **DO**: Use exact diagram type names: `"sequenceDiagram"` (camelCase as it appears in code)
- ✅ **DO**: Use `diagram_type` filter for focused results: `{ query: "subgraph", diagram_type: "flowchart" }`
- ❌ **DON'T**: Multi-word queries: `"flowchart syntax"`, `"flowchart syntax mermaid"` (fewer matches)
- ❌ **DON'T**: Conversational: `"how to create flowchart"`, `"show me examples"`
- ❌ **DON'T**: Include "mermaid" in queries (docs are already Mermaid-specific)
**Why smart snippets are better:**
- Old approach: Returns full file content → wastes context window
- New approach: Extracts only the Markdown section containing your search term → focused, relevant context
- Files are cached at startup → instant search results
## Validation Protocol
**Before returning ANY diagram:**
```javascript
validate_mermaid({ code: "FULL DIAGRAM SOURCE HERE" })
```
**If validation fails:**
1. Read error message
2. Use `search_mermaid_docs` for correct syntax
3. Fix the diagram
4. Run `validate_mermaid` again
5. Repeat until validation passes
**Only return diagrams that pass validation.**
## Critical Syntax Rules
### Quote Complex Labels
Quote labels containing spaces, `+`, `,`, `:`, `/`, `(`, `)`, etc.
Applies to node labels, subgraph titles, and edge labels.
```mermaid
A["Agent (Planner + Orchestrator)"]
subgraph MCPLayer["MCP Layer\n(Model Context Protocol)"]
A -->|"tool invocation / calls"| B
```
### Reserved Words
Don't use bare reserved words like `end` as nodes.
```mermaid
End
or
endNode["end"]
```
### classDef Styling
Use colons, not equals:
```mermaid
classDef highlight fill:#e3f2fd,stroke:#1e88e5,stroke-width:2px;
class RAG highlight;
```
### Balanced Quotes and Brackets
Check that all `[]`, `{}`, `()`, and `"` are balanced before validation.
## Layout Guidelines
- Choose direction (`LR`, `RL`, `TB`, `BT`) to optimize diagram dimensions
- Use subgraphs to group related nodes
- Use invisible edges (`A ~~~ B`) for spacing
- Prefer descriptive but concise node names
## Best Practices for Modern, Professional Diagrams
### Use Human-Readable Names
- Full words over abbreviations: `UserAuthenticationService` not `UAS`
- Descriptive labels: `["API Gateway\n(Load Balancer)"]` provides context
- Clear participants: `participant Frontend as "React Application"`
### Leverage Modern Features
- **Subgraphs with titles**: Group components by layer or domain
```mermaid
subgraph Backend["Backend Services"]
API[REST API]
DB[(Database)]
end
```
- **classDef styling**: Color-code by type, layer, or importance
```mermaid
classDef frontend fill:#e1f5ff,stroke:#01579b
classDef backend fill:#fff3e0,stroke:#e65100
class UI,Client frontend
class API,Auth backend
```
- **Labeled edges**: Show data flow or interaction type
```mermaid
User -->|"HTTP POST /api/login"| API
API -->|"JWT token"| User
```
- **Node shapes**: Use appropriate shapes for semantics
- `[(Database)]` for data stores
- `([User])` for actors
- `{{Decision}}` for decision points
- `[/Input/]` or `[\Output\]` for I/O
### Backward Compatibility
- Stick to stable diagram types: `flowchart`, `sequenceDiagram`, `classDiagram`, `stateDiagram-v2`
- Avoid beta features (`-beta` suffix) in production diagrams
- Test complex diagrams with `validate_mermaid` before committing
### Example: Professional Flowchart
```mermaid
flowchart LR
subgraph Client["Client Layer"]
User([User])
Browser["Web Browser"]
end
subgraph Application["Application Layer"]
API["REST API Gateway"]
Auth["Authentication Service"]
Logic["Business Logic"]
end
subgraph Data["Data Layer"]
Cache[("Redis Cache")]
DB[("PostgreSQL Database")]
end
User -->|"Opens app"| Browser
Browser -->|"HTTPS Request"| API
API -->|"Verify token"| Auth
Auth -->|"Query user"| DB
API -->|"Process"| Logic
Logic -->|"Check cache"| Cache
Logic -->|"Query data"| DB
DB -->|"Results"| Logic
Logic -->|"JSON Response"| API
API -->|"Render"| Browser
classDef client fill:#f0f9ff,stroke:#0369a1
classDef app fill:#fef3c7,stroke:#d97706
classDef data fill:#fce7f3,stroke:#be185d
class User,Browser client
class API,Auth,Logic app
class Cache,DB data
```
## Behavior Principles
- **Preserve intent** - Keep original logical flow when fixing diagrams
- **Be explicit** - Explain syntax and structural changes
- **Use search_mermaid_docs** - Query official docs with smart snippets instead of guessing syntax
- **Human-readable names** - Use descriptive variable and node names
- **Modern features** - Leverage subgraphs, styling, and labeled edges for clarity
## Quick Reference
**Complete workflow for any Mermaid task (DO NOT SKIP STEPS):**
1. **REQUIRED**: Call `search_mermaid_docs` with single-term query:
- `search_mermaid_docs({ query: "flowchart" })` → Focused flowchart sections ✅
- `search_mermaid_docs({ query: "sequenceDiagram" })` → Sequence syntax snippets ✅
- `search_mermaid_docs({ query: "subgraph", diagram_type: "flowchart" })` → Targeted results ✅
- `mode: "snippet"` (default) for context efficiency, `mode: "full"` only if needed
2. Review search results for syntax patterns and examples
3. If user requests multiple diagram types (e.g., "sequence AND flowchart"), create ALL requested types
4. Build diagram(s) with human-readable names and modern features
5. **REQUIRED**: Call `validate_mermaid` on each diagram before presenting
6. Fix errors if any using error messages + `search_mermaid_docs`
7. **REQUIRED**: Re-validate after fixes
8. Present ALL diagrams to user only after validation passes
**Search query best practices:**
- ✅ DO use: `"flowchart"`, `"sequenceDiagram"`, `"subgraph"`, `"arrow"`
- ✅ DO use: `diagram_type` filter for focused results
- ✅ DO use: `mode: "snippet"` (default) to save context window
- ❌ DON'T use: `"flowchart syntax"`, `"flowchart syntax mermaid"` (fewer matches)
- ❌ DON'T use: `"how to create flowchart"`, `"show me examples"`
- ❌ DON'T use: `mode: "full"` unless you really need entire file
**Validation and tool usage:**
- ALWAYS call `search_mermaid_docs` first to query official Mermaid docs (mandatory)
- ALWAYS validate before presenting (mandatory)
- If user specifies multiple diagram types, create ALL of them (mandatory)
- Build incrementally, validate frequently
- Use comments (`%% comment`) for documentation
- Use clear, meaningful node identifiers (e.g., `AuthService` not `AS`)
- Use subgraphs for organization
- Never guess syntax - search first