format_convert
Convert document content between Markdown, HTML, XML, and JSON formats to enable cross-format compatibility and processing.
Instructions
Convert between different document formats (Markdown, HTML, XML, JSON)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | Input content to convert | |
| fromFormat | Yes | Source format | |
| toFormat | Yes | Target format |
Implementation Reference
- src/tools/formatConverterPlus.ts:100-154 (handler)The main handler function that performs the actual format conversion logic, validating inputs and dispatching to specific helper converters based on from/to format pair.export async function convertFormat(input: string, fromFormat: FormatType, toFormat: FormatType) { try { console.log(`Converting from ${fromFormat} to ${toFormat}`); // Validate formats if (!Object.values(FormatType).includes(fromFormat)) { return { success: false, error: `Unsupported source format: ${fromFormat}`, }; } if (!Object.values(FormatType).includes(toFormat)) { return { success: false, error: `Unsupported target format: ${toFormat}`, }; } // Handle different conversion paths let result: string; switch (`${fromFormat}-${toFormat}`) { case `${FormatType.MARKDOWN}-${FormatType.HTML}`: result = await markdownToHtml(input); break; case `${FormatType.HTML}-${FormatType.MARKDOWN}`: return { success: false, error: "HTML to Markdown conversion is not supported yet", }; case `${FormatType.XML}-${FormatType.JSON}`: result = await xmlToJson(input); break; case `${FormatType.JSON}-${FormatType.XML}`: result = jsonToXml(input); break; default: return { success: false, error: `Unsupported conversion path: ${fromFormat} to ${toFormat}`, }; } return { success: true, data: result, }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; console.error(`Error converting format: ${errorMessage}`); return { success: false, error: errorMessage, }; } }
- Input JSON schema defining the parameters for the format_convert tool: input content, source format, and target format.inputSchema: { type: "object", properties: { input: { type: "string", description: "Input content to convert", }, fromFormat: { type: "string", enum: Object.values(FormatType), description: "Source format", }, toFormat: { type: "string", enum: Object.values(FormatType), description: "Target format", }, }, required: ["input", "fromFormat", "toFormat"], },
- src/tools/_index.ts:9-9 (registration)Registration of the format_convert tool (as FORMAT_CONVERTER_TOOL) in the central tools array exported for use in the MCP server.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];
- Type guard function for validating input arguments match FormatConverterArgs.export function isFormatConverterArgs(args: unknown): args is FormatConverterArgs { return typeof args === "object" && args !== null && "input" in args && "fromFormat" in args && "toFormat" in args && typeof (args as FormatConverterArgs).input === "string" && Object.values(FormatType).includes((args as FormatConverterArgs).fromFormat) && Object.values(FormatType).includes((args as FormatConverterArgs).toFormat); }
- Enumeration of supported format types used in schema and logic.export enum FormatType { MARKDOWN = "markdown", HTML = "html", XML = "xml", JSON = "json", }