Skip to main content
Glama
betterhyq

Mermaid-Grammer-Inspector

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
NameRequiredDescriptionDefault
textYes

Implementation Reference

  • 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}`;
    		}
    	},
    });
  • Zod schema defining the input parameter 'text' for the 'check' tool.
    parameters: z.object({
    	text: z.string(),
    }),
  • 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 });
    			},
    		);
    	});
    };
  • 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;
    }
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/betterhyq/Mermaid-Grammer-Inspector'

If you have feedback or need assistance with the MCP directory API, please join our Discord server