typeconversion
Convert data between JSON, XML, YAML, CSV, and TOML formats with options for pretty printing, minification, and format-specific settings like CSV delimiters.
Instructions
Convert data between different formats: JSON, XML, YAML, CSV, and TOML. Supports pretty printing, minification, and format-specific options like CSV delimiters and headers.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input | Yes | Input data as string | |
| fromFormat | Yes | Source data format | |
| toFormat | Yes | Target data format | |
| options | No |
Implementation Reference
- src/tools/index.ts:17-31 (registration)Registration function that instantiates the TypeConversionTool and registers it along with other tools to the MCP server using server.registerToolsexport function registerAllTools(server: MCPServer): void { const tools: BaseTool[] = [ new WebSearchTool(), new WebFetchTool(), new TypeConversionTool(), ]; // Register all tools if (tools.length > 0) { server.registerTools(tools); logger.info(`Registered ${tools.length} tool(s): ${tools.map(t => t.name).join(', ')}`); } else { logger.warn('No tools registered - add tool implementations to src/tools/index.ts'); } }
- src/tools/conversion.ts:13-39 (schema)Zod schemas defining supported formats, conversion options, input parameters (TypeConversionParams) for the typeconversion toolconst formatEnum = z.enum(['json', 'xml', 'yaml', 'csv', 'toml']); type DataFormat = z.infer<typeof formatEnum>; /** * Conversion options schema */ const conversionOptionsSchema = z .object({ pretty: z.boolean().optional().default(true).describe('Pretty print output (with indentation)'), indent: z.number().int().min(0).max(8).optional().default(2).describe('Indentation spaces for pretty printing'), csvDelimiter: z.string().optional().default(',').describe('CSV delimiter character'), csvHeaders: z.boolean().optional().default(true).describe('CSV has headers in first row'), }) .optional(); /** * Type conversion tool schema */ const typeConversionSchema = z.object({ input: z.string().min(1).describe('Input data as string'), fromFormat: formatEnum.describe('Source data format'), toFormat: formatEnum.describe('Target data format'), options: conversionOptionsSchema, }); type TypeConversionParams = z.infer<typeof typeConversionSchema>;
- src/tools/conversion.ts:43-66 (handler)TypeConversionTool class with name, description, schema reference, and execute handler method that orchestrates parsing input to JS object and formatting to target output using private helpersexport class TypeConversionTool extends BaseTool<typeof typeConversionSchema> { readonly name = 'typeconversion'; readonly description = 'Convert data between different formats: JSON, XML, YAML, CSV, and TOML. Supports pretty printing, minification, and format-specific options like CSV delimiters and headers.'; readonly schema = typeConversionSchema; protected async execute(params: TypeConversionParams): Promise<string> { logger.info(`Converting from ${params.fromFormat} to ${params.toFormat}`); try { // Parse input to intermediate format (JavaScript object) const data = await this.parseInput(params.input, params.fromFormat, params.options); // Convert to target format const output = await this.formatOutput(data, params.toFormat, params.options); logger.info(`Conversion completed successfully`); return output; } catch (error) { throw new Error( `Conversion failed: ${error instanceof Error ? error.message : 'Unknown error'}` ); } }
- src/tools/conversion.ts:71-90 (helper)parseInput helper: dispatches input parsing based on fromFormat to specific parser methodsprivate async parseInput( input: string, format: DataFormat, options?: z.infer<typeof conversionOptionsSchema> ): Promise<any> { switch (format) { case 'json': return this.parseJson(input); case 'xml': return await this.parseXmlToObject(input); case 'yaml': return this.parseYaml(input); case 'csv': return this.parseCsvToObject(input, options); case 'toml': return this.parseToml(input); default: throw new Error(`Unsupported input format: ${format}`); } }
- src/tools/conversion.ts:95-114 (helper)formatOutput helper: dispatches object formatting based on toFormat to specific formatter methodsprivate async formatOutput( data: any, format: DataFormat, options?: z.infer<typeof conversionOptionsSchema> ): Promise<string> { switch (format) { case 'json': return this.formatJson(data, options); case 'xml': return await this.formatXml(data, options); case 'yaml': return this.formatYaml(data, options); case 'csv': return this.formatCsv(data, options); case 'toml': return this.formatToml(data); default: throw new Error(`Unsupported output format: ${format}`); } }