Skip to content

Commit a34c480

Browse files
authored
feat: Added url output type (#26)
* feat: add mermaid ink url output types * Address review feedback
1 parent e5ab5e7 commit a34c480

File tree

7 files changed

+48
-7
lines changed

7 files changed

+48
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Generate <img src="https://mermaid.js.org/favicon.svg" height="14"/> [mermaid](h
77

88
- Fully support all features and syntax of `Mermaid`.
99
- Support configuration of `backgroundColor` and `theme`, enabling large AI models to output rich style configurations.
10-
- Support exporting to `base64`, `svg`, `mermaid`, and `file` formats, with validation for `Mermaid` to facilitate the model's multi-round output of correct syntax and graphics. Use `outputType: "file"` to automatically save PNG diagrams to disk for AI agents.
10+
11+
- Support exporting to `base64`, `svg`, `mermaid`, `file`, and remote-friendly `svg_url`, `png_url` formats, with validation for `Mermaid` to facilitate the model's multi-round output of correct syntax and graphics. Use `outputType: "file"` to automatically save PNG diagrams to disk for AI agents, or the URL modes to share diagrams through public mermaid.ink links.
12+
1113

1214
<img width="720" alt="mcp-mermaid" src="https://mermaid.js.org/header.png" />
1315

__tests__/tools/mermaid.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
},
2323
"outputType": {
2424
"type": "string",
25-
"enum": ["base64", "svg", "mermaid", "file"],
26-
"description": "The output type of the diagram. Can be 'base64', 'svg', 'mermaid', or 'file'. Default is 'base64'. 'base64' returns PNG image as base64 encoded string. 'file' will save the PNG image to disk and return the file path.",
25+
"enum": ["base64", "svg", "mermaid", "file", "svg_url", "png_url"],
26+
"description": "The output type of the diagram. Can be 'base64', 'svg', 'mermaid', 'file', 'svg_url', or 'png_url'. Default is 'base64'. 'base64' returns PNG image as base64 encoded string. 'file' saves the PNG image to disk. The *_url options return public mermaid.ink links for remote-friendly sharing.",
2727
"default": "base64"
2828
}
2929
},

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mcp-mermaid",
33
"description": "❤️ Generate mermaid diagram and chart with AI MCP dynamically.",
4-
"version": "0.3.0",
4+
"version": "0.4.0",
55
"main": "build/index.js",
66
"type": "module",
77
"scripts": {

src/server.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
startStdioMcpServer,
1515
} from "./services";
1616
import { schema, tool } from "./tools";
17-
import { renderMermaid } from "./utils";
17+
import { createMermaidInkUrl, renderMermaid } from "./utils";
1818
import { Logger } from "./utils/logger";
1919

2020
/**
@@ -88,6 +88,18 @@ function setupToolHandlers(server: Server): void {
8888
],
8989
};
9090
}
91+
if (outputType === "svg_url" || outputType === "png_url") {
92+
const variant = outputType === "svg_url" ? "svg" : "img";
93+
const url = createMermaidInkUrl(mermaid as string, variant);
94+
return {
95+
content: [
96+
{
97+
type: "text",
98+
text: url,
99+
},
100+
],
101+
};
102+
}
91103
if (outputType === "file") {
92104
if (!screenshot) {
93105
throw new McpError(

src/tools/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ C-->D;.`)
2323
.optional()
2424
.default("white"),
2525
outputType: z
26-
.enum(["base64", "svg", "mermaid", "file"])
26+
.enum(["base64", "svg", "mermaid", "file", "svg_url", "png_url"])
2727
.describe(
28-
"The output type of the diagram. Can be 'base64', 'svg', 'mermaid', or 'file'. Default is 'base64'. 'base64' returns PNG image as base64 encoded string. 'file' will save the PNG image to disk and return the file path.",
28+
"The output type of the diagram. Can be 'base64', 'svg', 'mermaid', 'file', 'svg_url', or 'png_url'. Default is 'base64'. 'base64' returns PNG image as base64 encoded string. 'file' saves the PNG image to disk. The *_url options return public mermaid.ink links for remote-friendly sharing.",
2929
)
3030
.optional()
3131
.default("base64"),

src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export { zodToJsonSchema } from "./schema";
22
export { renderMermaid } from "./render";
33
export { Logger } from "./logger";
4+
export { createMermaidInkUrl } from "./mermaidUrl";
5+

src/utils/mermaidUrl.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { deflateSync } from "node:zlib";
2+
3+
/**
4+
* Encodes mermaid text into the Base64URL deflated format used by mermaid.ink.
5+
*/
6+
7+
function encodeMermaidToBase64Url(mermaid: string): string {
8+
const compressed = deflateSync(mermaid, { level: 9 });
9+
return compressed
10+
.toString("base64")
11+
.replace(/\+/g, "-")
12+
.replace(/\//g, "_")
13+
.replace(/=+$/g, "");
14+
}
15+
16+
/**
17+
* Creates a public mermaid.ink URL for the given mermaid definition.
18+
*/
19+
export function createMermaidInkUrl(
20+
mermaid: string,
21+
variant: "svg" | "img",
22+
): string {
23+
const encoded = encodeMermaidToBase64Url(mermaid);
24+
return `https://mermaid.ink/${variant}/pako:${encoded}`;
25+
}

0 commit comments

Comments
 (0)