create-diagram-using-graphviz
Generate visual diagrams from DOT graph descriptions, providing image URLs or saving files in SVG or PNG format with customizable layouts.
Instructions
Create graph diagrams using GraphViz - get diagram image URL or save diagram image to file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Whether to get graph URL or save as file | |
| outputPath | No | Path where to save the file (only used with action=save_file) | |
| graph | Yes | DOT graph description | |
| layout | No | Graph layout algorithm (default: dot) | |
| format | No | Output format (default: svg) | |
| width | No | Image width in pixels | |
| height | No | Image height in pixels |
Implementation Reference
- src/tools/graphviz.ts:214-325 (handler)The main handler function 'handleGraphvizTool' that executes the tool logic: validates inputs, generates GraphViz diagrams via QuickChart API, returns URLs, base64 images, or saves files.export async function handleGraphvizTool(args: any): Promise<any> { const graph = args.graph as string; const action = args.action as string; validateGraph(graph); validateAction(action); validateOutputPath(args.outputPath, action); validateLayout(args.layout); validateFormat(args.format); validateDimensions(args.width, args.height); const config = buildGraphvizConfig(graph, { layout: args.layout as string, format: args.format as string, width: args.width as number, height: args.height as number, }); const graphvizUrl = buildGraphvizUrl( graph, args.layout as string || "dot", args.format as string || "svg" ); const result: any = { content: [ { type: "text", text: "Below is the GraphViz diagram URL:", }, { type: "text", text: graphvizUrl, }, ], metadata: { graphvizType: args.layout || "dot", generatedAt: new Date().toISOString(), graphvizUrl: graphvizUrl, }, }; try { const pngData = await fetchGraphvizContent(config, "png"); const pngBase64 = Buffer.from(pngData).toString("base64"); result.content.push( { type: "text", text: "Below is the PNG image:", }, { type: "image", data: pngBase64, mimeType: "image/png", } ); result.metadata.pngBase64 = pngBase64; } catch (error) { result.content.unshift({ type: "text", text: "⚠️ Failed to fetch diagram image", }); result.content.push({ type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}`, }); result.metadata.error = error instanceof Error ? error.message : String(error); } if (action === "get_url") { return result; } const format = (args.format as string) || "svg"; const outputPath = getDownloadPath( args.outputPath as string | undefined, format ); try { const dir = path.dirname(outputPath); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } const data = await fetchGraphvizContent(config, format); if (format === "svg") { fs.writeFileSync(outputPath, data, "utf8"); } else { fs.writeFileSync(outputPath, data); } result.metadata.savedPath = outputPath; result.content.push({ type: "text", text: "Below is the saved file path:", }); result.content.push({ type: "text", text: outputPath, }); return result; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to save GraphViz diagram: ${ error instanceof Error ? error.message : String(error) }` ); } }
- src/tools/graphviz.ts:11-53 (schema)The tool definition including name, description, and inputSchema for validation.export const CREATE_DIAGRAM_USING_GRAPHVIZ_TOOL: Tool = { name: "create-diagram-using-graphviz", description: "Create graph diagrams using GraphViz - get diagram image URL or save diagram image to file", inputSchema: { type: "object", properties: { action: { type: "string", enum: ["get_url", "save_file"], description: "Whether to get graph URL or save as file", }, outputPath: { type: "string", description: "Path where to save the file (only used with action=save_file)", }, graph: { type: "string", description: "DOT graph description", }, layout: { type: "string", enum: ["dot", "fdp", "neato", "circo", "twopi", "osage", "patchwork"], description: "Graph layout algorithm (default: dot)", }, format: { type: "string", enum: ["svg", "png"], description: "Output format (default: svg)", }, width: { type: "integer", description: "Image width in pixels", }, height: { type: "integer", description: "Image height in pixels", }, }, required: ["action", "graph"], }, };
- src/tools/index.ts:80-83 (registration)Registration of the tool handler in the TOOL_HANDLERS mapping."create-diagram-using-graphviz": { handler: handleGraphvizTool, toolName: ToolNames.GRAPHVIZ, },
- src/tools/index.ts:15-17 (registration)Import of the tool definition and handler from graphviz.tsCREATE_DIAGRAM_USING_GRAPHVIZ_TOOL, handleGraphvizTool, } from "./graphviz.js";
- src/tools/index.ts:41-41 (registration)Inclusion of the tool in the ALL_TOOLS array for conditional enabling.{ tool: CREATE_DIAGRAM_USING_GRAPHVIZ_TOOL, name: ToolNames.GRAPHVIZ },