check
Validate Mermaid diagram syntax to identify and return error messages for invalid diagrams.
Instructions
Check if the text is a valid mermaid diagram. Returns an empty string if valid, otherwise returns the error message.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes |
Implementation Reference
- src/check.ts:17-47 (handler)Core handler function for the 'check' tool. Performs input validation, writes the Mermaid diagram text to a temporary file (input.mmd), and invokes parseMermaid to check syntax.export const checkMermaid = async (text: string): Promise<ParseResult> => { // Input validation if (typeof text !== "string") { return { status: ParseStatus.FAIL, message: "Input must be a string", }; } // Check for empty or whitespace-only content if (!text.trim()) { return { status: ParseStatus.FAIL, message: "Input cannot be empty or contain only whitespace", }; } const inputFilePath = path.join(__dirname, "input.mmd"); try { fs.writeFileSync(inputFilePath, text, { encoding: "utf-8" }); return await parseMermaid(); } catch (error) { const errorMessage = error instanceof Error ? error.message : "File write failed"; return { status: ParseStatus.FAIL, message: `Unable to write temporary file: ${errorMessage}`, }; } };
- src/index.ts:34-52 (registration)Registers the MCP tool named 'check' with its description, input schema (Zod), and execute function that wraps checkMermaid.server.addTool({ name: "check", description: "Check if the text is a valid mermaid diagram. Returns an empty string if valid, otherwise returns the error message.", parameters: z.object({ text: z.string(), }), execute: async (args) => { try { const { status, message } = await checkMermaid(args.text); return status === ParseStatus.SUCCESS ? "" : message || "Unknown error"; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unexpected error occurred"; console.error("Error in check tool:", errorMessage); return `Internal error: ${errorMessage}`; } }, });
- src/index.ts:38-40 (schema)Zod schema defining the input parameters for the 'check' tool: a single 'text' string.parameters: z.object({ text: z.string(), }),
- src/parse.ts:65-90 (helper)Helper function that performs the actual Mermaid syntax check by executing 'npx mmdc' CLI tool on the input file and processing the result or error.export const parseMermaid = ( inputFile: string = "input.mmd", outputFile: string = "output.svg", ): Promise<ParseResult> => { return new Promise((resolve) => { const inputPath = path.join(__dirname, inputFile); const outputPath = path.join(__dirname, outputFile); exec( `npx mmdc -i ${inputPath} -o ${outputPath}`, (err, _stdout, stderr) => { if (err) { const errorOutput = stderr || err.message || ""; const filteredMessage = filterErrorOutput(errorOutput); resolve({ status: ParseStatus.FAIL, message: filteredMessage || err.message || "Unknown error", }); return; } resolve({ status: ParseStatus.SUCCESS }); }, ); }); };
- src/parse.ts:16-19 (schema)TypeScript interface defining the return type of parsing functions, used for output schema.export interface ParseResult { status: ParseStatus; message?: string; }