html_formatter
Format and organize HTML code by inputting a file path and specifying an output directory. Enhance readability and structure of HTML documents efficiently.
Instructions
Format and beautify HTML code
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| inputPath | Yes | Path to the input HTML file | |
| outputDir | Yes | Directory where formatted HTML should be saved |
Implementation Reference
- src/tools/htmlTools.ts:300-338 (handler)The core handler function for the 'html_formatter' tool. It reads an HTML file, uses JSDOM to parse and serialize it (which beautifies/formats the HTML), generates a unique output filename, and saves the formatted HTML to the specified output directory.export async function formatHtml(inputPath: string, outputDir: string) { try { console.error(`Starting HTML formatting...`); console.error(`Input file: ${inputPath}`); console.error(`Output directory: ${outputDir}`); // 確保輸出目錄存在 try { await fs.access(outputDir); console.error(`Output directory exists: ${outputDir}`); } catch { console.error(`Creating output directory: ${outputDir}`); await fs.mkdir(outputDir, { recursive: true }); console.error(`Created output directory: ${outputDir}`); } const uniqueId = generateUniqueId(); const htmlContent = await fs.readFile(inputPath, "utf-8"); const dom = new JSDOM(htmlContent); const { document } = dom.window; // 格式化 HTML const formattedHtml = dom.serialize(); const outputPath = path.join(outputDir, `formatted_${uniqueId}.html`); await fs.writeFile(outputPath, formattedHtml); console.error(`Written formatted HTML to ${outputPath}`); return { success: true, data: `Successfully formatted HTML: ${outputPath}`, }; } catch (error) { console.error(`Error in formatHtml:`, error); return { success: false, error: error instanceof Error ? error.message : "Unknown error", }; } }
- src/tools/htmlTools.ts:93-110 (schema)The Tool schema definition for 'html_formatter', including name, description, and input schema requiring inputPath and outputDir.export const HTML_FORMAT_TOOL: Tool = { name: "html_formatter", description: "Format and beautify HTML code", inputSchema: { type: "object", properties: { inputPath: { type: "string", description: "Path to the input HTML file", }, outputDir: { type: "string", description: "Directory where formatted HTML should be saved", }, }, required: ["inputPath", "outputDir"], }, };
- src/index.ts:222-238 (registration)Registration and dispatch logic in the main MCP server handler: matches tool name 'html_formatter' and invokes the formatHtml handler function.if (name === "html_formatter") { const { inputPath, outputDir } = args as { inputPath: string; outputDir: string; }; const result = await formatHtml(inputPath, outputDir); if (!result.success) { return { content: [{ type: "text", text: `Error: ${result.error}` }], isError: true, }; } return { content: [{ type: "text", text: fileOperationResponse(result.data) }], isError: false, }; }
- src/tools/_index.ts:9-9 (registration)The 'html_formatter' tool (via HTML_FORMAT_TOOL) is included in the exported 'tools' array used by the MCP server for listing available tools.export const tools = [DOCUMENT_READER_TOOL, PDF_MERGE_TOOL, PDF_SPLIT_TOOL, DOCX_TO_PDF_TOOL, DOCX_TO_HTML_TOOL, HTML_CLEAN_TOOL, HTML_TO_TEXT_TOOL, HTML_TO_MARKDOWN_TOOL, HTML_EXTRACT_RESOURCES_TOOL, HTML_FORMAT_TOOL, TEXT_DIFF_TOOL, TEXT_SPLIT_TOOL, TEXT_FORMAT_TOOL, TEXT_ENCODING_CONVERT_TOOL, EXCEL_READ_TOOL, FORMAT_CONVERTER_TOOL];
- src/tools/_index.ts:5-5 (registration)Re-export of the HTML_FORMAT_TOOL schema from htmlTools.ts into the tools index.import { HTML_CLEAN_TOOL, HTML_EXTRACT_RESOURCES_TOOL, HTML_FORMAT_TOOL, HTML_TO_MARKDOWN_TOOL, HTML_TO_TEXT_TOOL } from "./htmlTools.js";