xml-format
Format and organize XML strings with customizable indentation for improved readability and structure. Ideal for developers working with XML data.
Instructions
Format and prettify XML
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| indent | No | Number of spaces for indentation | |
| xml | Yes | XML string to format |
Implementation Reference
- Handler function that takes XML string and optional indent, uses xml-formatter to prettify it, returns formatted XML or error message.}, async ({ xml, indent = 2 }) => { try { const xmlFormatterModule = await import("xml-formatter"); // Use double type assertion to work around NodeNext module resolution issues const formatXML = xmlFormatterModule.default as unknown as (xml: string, options?: any) => string; const formatted = formatXML(xml, { indentation: ' '.repeat(indent), collapseContent: true, }); return { content: [ { type: "text", text: `Formatted XML: ${formatted} ✅ XML formatted successfully 🎯 Features: ${indent}-space indentation, collapsed content, clean structure`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error formatting XML: ${error instanceof Error ? error.message : 'Unknown error'} 💡 Common XML issues: • Check that all tags are properly closed • Ensure proper nesting of elements • Validate attribute syntax (key="value") • Check for special character encoding`, }, ], }; } }
- Zod schema defining input parameters: xml (string, required), indent (number, optional).inputSchema: { xml: z.string().describe("XML string to format"), indent: z.number().describe("Number of spaces for indentation").optional(), },
- src/tools/data_format/format_xml/index.ts:4-58 (registration)Registration of the 'format_xml' tool using McpServer.registerTool, including description, schema, annotations, and inline handler.export function registerFormatXml(server: McpServer) { server.registerTool("format_xml", { description: "Format and prettify XML", inputSchema: { xml: z.string().describe("XML string to format"), indent: z.number().describe("Number of spaces for indentation").optional(), }, // VS Code compliance annotations annotations: { title: "Format Xml", description: "Format and prettify XML", readOnlyHint: false } }, async ({ xml, indent = 2 }) => { try { const xmlFormatterModule = await import("xml-formatter"); // Use double type assertion to work around NodeNext module resolution issues const formatXML = xmlFormatterModule.default as unknown as (xml: string, options?: any) => string; const formatted = formatXML(xml, { indentation: ' '.repeat(indent), collapseContent: true, }); return { content: [ { type: "text", text: `Formatted XML: ${formatted} ✅ XML formatted successfully 🎯 Features: ${indent}-space indentation, collapsed content, clean structure`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error formatting XML: ${error instanceof Error ? error.message : 'Unknown error'} 💡 Common XML issues: • Check that all tags are properly closed • Ensure proper nesting of elements • Validate attribute syntax (key="value") • Check for special character encoding`, }, ], }; } } ); }