graph2agent — Mermaid diagrams, explained for agents

7 min read Original article ↗

Mermaid in · graph2agent · explicit context out

graph2agent turns Mermaid diagrams into explicit text for coding agents. Keep the diagram for people; add deterministic context that spells out the elements, connections, branches, order, topology, and what the notation does not prove. Humans can scan the picture visually; an agent receiving only Mermaid source must reconstruct those relationships from compact syntax.

In one frozen paired benchmark, adding that text cut exact-comprehension failures from 121 to 60.

  • Diagram stays for humans
  • Rich text goes to agents
  • Deterministic · no model call

diagram + generated contextagent context on

```mermaid
flowchart TD
  request[Request] --> auth{Authorized?}
  auth -->|yes| api(API)
  auth -->|no| reject[Reject]
```
A request flows to an authorization decision, then branches to API on yes or Reject on no.
Rendered Mermaid · same source, compiled for people

Exact interpreted-v3 agent context · v0.4.0

Diagram contract
Type: Mermaid flowchart.
Layout direction: top to bottom. This controls presentation and does not by itself declare execution order.
The diagram contains 4 elements, 3 explicit relationships, 0 ordered events, 0 groups, and 0 notes.

Elements
- `Request` is a rectangle.
- `Authorized?` uses the "decision" shape.
- `API` uses the "rounded" shape.
- `Reject` is a rectangle.

Connections
- `Request` points to `Authorized?` with an arrow.
- `Authorized?` points to `API` with an arrow labeled "yes".
- `Authorized?` points to `Reject` with an arrow labeled "no".

Direct connection summary
- `Request`: incoming from none; outgoing to `Authorized?`.
- `Authorized?`: incoming from `Request`; outgoing to `API` and `Reject`.
- `API`: incoming from `Authorized?`; outgoing to none.
- `Reject`: incoming from `Authorized?`; outgoing to none.

Derived topology
- Elements with no incoming directed connections: `Request`.
- Elements with no outgoing directed connections: `API` and `Reject`.
- Branch candidates, based only on multiple outgoing directed connections: `Authorized?`.
- Merge candidates, based only on multiple incoming directed connections: none.
- Weakly connected components: [`API`, `Authorized?`, `Reject`, and `Request`].
- Directed cycles, shown as strongly connected element sets: none.

Meaning and limits
- Recognized Mermaid or UML markers are modeling cues. Other kinds, styles, and presentation do not establish executable behavior or unstated rules. Color, placement, and styling are presentation unless explicitly labeled as contract semantics. Layout direction is presentation, not execution order. Missing links are unspecified, not forbidden. A conventional reading never overrides explicit local structure.
- Only authored explicit shapes receive roles: `Request` has an authored rectangle, commonly read as a process step; `Authorized?` has an authored decision, commonly read as a point where outgoing paths represent alternatives; `API` has an authored rounded, commonly read as an event; `Reject` has an authored rectangle, commonly read as a process step. Mermaid guarantees those shapes, not runtime components, boolean evaluation, persistence, or execution; implicit rectangles and label-only metadata do not become processes.
- A one-way connector records source-to-target direction without execution timing.
- Connector labels are literal contract text without normalization or evaluation.
- Shape cues guide interpretation but alone define no executable behavior.
- Branch and merge candidates are topology observations, not proof of decision or synchronization semantics.

Why agents need the text

Humans see the picture.
Agents need the structure spelled out.

Mermaid compresses many relationships into a few lines. As diagrams gain branches, loops, and participants, graph2agent expands the authored structure into explicit text while preserving the original diagram.

Input A Mermaid only

flowchart TD
  request[Request] --> auth{Authorized?}
  auth -->|yes| api(API)
  auth -->|no| reject[Reject]
A request flows to an authorization decision, then branches to API on yes or Reject on no.
Rendered Mermaid · the picture humans scan

Agent must reconstruct:

  • which node branches
  • the two possible terminal paths
  • what shape and arrows do—or do not—mean

Input B Mermaid + graph2agent interpreted-v3

Diagram contract
Type: Mermaid flowchart.
Layout direction: top to bottom. This controls presentation and does not by itself declare execution order.
The diagram contains 4 elements, 3 explicit relationships, 0 ordered events, 0 groups, and 0 notes.

Elements
- `Request` is a rectangle.
- `Authorized?` uses the "decision" shape.
- `API` uses the "rounded" shape.
- `Reject` is a rectangle.

Connections
- `Request` points to `Authorized?` with an arrow.
- `Authorized?` points to `API` with an arrow labeled "yes".
- `Authorized?` points to `Reject` with an arrow labeled "no".

Direct connection summary
- `Request`: incoming from none; outgoing to `Authorized?`.
- `Authorized?`: incoming from `Request`; outgoing to `API` and `Reject`.
- `API`: incoming from `Authorized?`; outgoing to none.
- `Reject`: incoming from `Authorized?`; outgoing to none.

Derived topology
- Elements with no incoming directed connections: `Request`.
- Elements with no outgoing directed connections: `API` and `Reject`.
- Branch candidates, based only on multiple outgoing directed connections: `Authorized?`.
- Merge candidates, based only on multiple incoming directed connections: none.
- Weakly connected components: [`API`, `Authorized?`, `Reject`, and `Request`].
- Directed cycles, shown as strongly connected element sets: none.

Meaning and limits
- Recognized Mermaid or UML markers are modeling cues. Other kinds, styles, and presentation do not establish executable behavior or unstated rules. Color, placement, and styling are presentation unless explicitly labeled as contract semantics. Layout direction is presentation, not execution order. Missing links are unspecified, not forbidden. A conventional reading never overrides explicit local structure.
- Only authored explicit shapes receive roles: `Request` has an authored rectangle, commonly read as a process step; `Authorized?` has an authored decision, commonly read as a point where outgoing paths represent alternatives; `API` has an authored rounded, commonly read as an event; `Reject` has an authored rectangle, commonly read as a process step. Mermaid guarantees those shapes, not runtime components, boolean evaluation, persistence, or execution; implicit rectangles and label-only metadata do not become processes.
- A one-way connector records source-to-target direction without execution timing.
- Connector labels are literal contract text without normalization or evaluation.
- Shape cues guide interpretation but alone define no executable behavior.
- Branch and merge candidates are topology observations, not proof of decision or synchronization semantics.

Structure and limits are explicit

Illustrative compilation example; the measured study is reported below.

+18.48pp

exact contract-comprehension lift in one frozen, paired benchmark of330 private contracts

Read the evidence boundary

One compiler, four launch surfaces

Generate once. Keep every consumer current.

Core v0.4.0, Action v0.4.0, Homebrew, and direct Debian downloads are public. MCP v0.4.0 is live through npm on macOS and Linux; native Windows downloads are live on GitHub, while one-command npm activation on Windows is pending. The signed APT repository is staged but not live.

  1. 01

    Paste one, or generate them all

    Install with Homebrew or a verified amd64 or arm64 Debian package. Paste one diagram for rich text on stdout, or put the repository update after your existing generators.

    brew install graph2agent/tap/graph2agent
    graph2agent describe --profile interpreted-v3 -
    make generate
  2. 02

    Plug-and-play MCP

    On macOS and Linux, the live npm package gives an MCP-capable agent the tool without a manual native download. Windows users can use the verified native executables from the v0.4.0 GitHub release; one-command npm activation on Windows is pending.

    npx -y graph2agent-mcp@0.4.0
  3. 03

    Block stale merges

    Make a read-only graph2agent check . job required before a pull request can merge.

    uses: graph2agent/github-action/.github/workflows/check-markdown.yml@48bc59a4742c2fd0311e81214b6571ce10601a4b
    with:
      graph2agent-version: v0.4.0
  4. 04

    Let the bot maintain it

    Run daily, change only generated Markdown context, and reuse one focused refresh PR until it merges.

    uses: graph2agent/github-action/.github/workflows/maintain-markdown.yml@48bc59a4742c2fd0311e81214b6571ce10601a4b
    with:
      graph2agent-version: v0.4.0

Measured, with the caveats attached

The digest helped on the frozen task set.

The frozen paired result supports efficacy on this benchmark. It does not by itself prove broader model, task, or production generalization.

Mermaid alone

63.33%

209 / 330 exact

Mermaid + standard digest

81.82%

270 / 330 exact

Relative failure reduction

50.41%

121 failures reduced to 60; this is a relative error metric

Exact contract-comprehension accuracy across 330 paired contracts: Mermaid alone passed 209; Mermaid plus the standard digest passed 270.
Exact paired accuracy. Bootstrap 95% sensitivity interval for the lift:14.24 to 22.73 percentage points.

Observed aggregate token counts. Mermaid + graph2agent relative to Mermaid-only across the same 330 paired cases.

8.17%more input tokens

14.13%fewer output tokens

46.30%fewer reasoning-output tokens

What this does not establish

Evidence is a boundary, not decoration.

  • One frozen, generated contract benchmark is not every software-engineering task.
  • The requested configuration was Codex CLI 0.146.1, gpt-5.6-sol, high reasoning; resolved backend identity was unavailable.
  • Only the supported Mermaid subset and the frozen standard digest were tested in this confirmatory result.
  • Monetary cost was missing from native traces, so it is unavailable—not zero.
  • Some Markdown ingestion pipelines strip HTML comments; end-to-end agent integrations still require validation.

Keep the diagram. Add the explanation.

Give every coding agent the text behind the graph.

Open graph2agentbrew install graph2agent/tap/graph2agentnpx -y graph2agent-mcp@0.4.0