check
Validate Mermaid diagram syntax to detect errors and ensure proper grammar for diagram creation.
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 logic for the 'check' tool: validates input, writes to temp file, and invokes Mermaid parser.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 'check' tool with name, description, Zod input schema, and wrapper execute handler.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 parameter 'text' for the 'check' tool.parameters: z.object({ text: z.string(), }),
- src/parse.ts:65-90 (helper)Helper function that runs the mmdc CLI to parse/validate the Mermaid diagram syntax.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:11-19 (schema)Type definitions for ParseStatus enum and ParseResult interface used throughout the check tool chain.export enum ParseStatus { SUCCESS, FAIL, } export interface ParseResult { status: ParseStatus; message?: string; }