Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Generate <img src="https://mermaid.js.org/favicon.svg" height="14"/> [mermaid](h

- Fully support all features and syntax of `Mermaid`.
- Support configuration of `backgroundColor` and `theme`, enabling large AI models to output rich style configurations.
- Support exporting to `png`, `svg`, and `mermaid` formats, with validation for `Mermaid` to facilitate the model's multi-round output of correct syntax and graphics.
- 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.

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

Expand Down
6 changes: 3 additions & 3 deletions __tests__/tools/mermaid.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
},
"outputType": {
"type": "string",
"enum": ["png", "svg", "mermaid"],
"description": "The output type of the diagram. Can be 'png', 'svg' or 'mermaid'. Default is 'png'.",
"default": "png"
"enum": ["base64", "svg", "mermaid", "file"],
"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.",
"default": "base64"
}
},
"required": ["mermaid"],
Expand Down
38 changes: 37 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import {
CallToolRequestSchema,
Expand Down Expand Up @@ -58,7 +61,7 @@ function setupToolHandlers(server: Server): void {
);
}

const { mermaid, theme, backgroundColor, outputType = "png" } = args;
const { mermaid, theme, backgroundColor, outputType = "base64" } = args;
const { id, svg, screenshot } = await renderMermaid(
mermaid as string,
theme as string,
Expand All @@ -85,6 +88,39 @@ function setupToolHandlers(server: Server): void {
],
};
}
if (outputType === "file") {
if (!screenshot) {
throw new McpError(
ErrorCode.InternalError,
"Failed to generate screenshot for file output.",
);
}

// Create a unique filename with timestamp and random suffix
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
const randomSuffix = Math.random().toString(36).substring(2, 8);
const filename = `mermaid-${timestamp}-${randomSuffix}.png`;

// Use current working directory to save the file
const filePath = path.resolve(process.cwd(), filename);

try {
fs.writeFileSync(filePath, screenshot);
return {
content: [
{
type: "text",
text: `Mermaid diagram saved to file: ${filePath}`,
},
],
};
} catch (fileError) {
throw new McpError(
ErrorCode.InternalError,
`Failed to save file: ${fileError instanceof Error ? fileError.message : "Unknown file error"}`,
);
}
}
return {
content: [
{
Expand Down
6 changes: 3 additions & 3 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ C-->D;.`)
.optional()
.default("white"),
outputType: z
.enum(["png", "svg", "mermaid"])
.enum(["base64", "svg", "mermaid", "file"])
.describe(
"The output type of the diagram. Can be 'png', 'svg' or 'mermaid'. Default is 'png'.",
"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.",
)
.optional()
.default("png"),
.default("base64"),
});

export const tool = {
Expand Down