text_encoding_converter
Convert text file encodings using specified source and target formats, saving the output to a designated directory. Ideal for transforming text data across multiple encoding standards.
Instructions
Convert text between different encodings
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fromEncoding | Yes | Source encoding (e.g., 'big5', 'gbk', 'utf8') | |
| inputPath | Yes | Path to the input text file | |
| outputDir | Yes | Directory where converted file should be saved | |
| toEncoding | Yes | Target encoding (e.g., 'utf8', 'big5', 'gbk') |
Input Schema (JSON Schema)
{
"properties": {
"fromEncoding": {
"description": "Source encoding (e.g., 'big5', 'gbk', 'utf8')",
"type": "string"
},
"inputPath": {
"description": "Path to the input text file",
"type": "string"
},
"outputDir": {
"description": "Directory where converted file should be saved",
"type": "string"
},
"toEncoding": {
"description": "Target encoding (e.g., 'utf8', 'big5', 'gbk')",
"type": "string"
}
},
"required": [
"inputPath",
"outputDir",
"fromEncoding",
"toEncoding"
],
"type": "object"
}
Implementation Reference
- src/tools/txtTools.ts:114-157 (handler)The core handler function that reads the input file, decodes from source encoding using iconv-lite, encodes to target encoding, and writes the converted file to output directory with a unique filename.export async function convertTextEncoding( inputPath: string, outputDir: string, fromEncoding: string, toEncoding: string ) { try { console.error(`Starting text encoding conversion...`); console.error(`Input file: ${inputPath}`); console.error(`Output directory: ${outputDir}`); console.error(`From encoding: ${fromEncoding}`); console.error(`To encoding: ${toEncoding}`); // 確保輸出目錄存在 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 content = await fs.readFile(inputPath); const text = iconv.decode(content, fromEncoding); const converted = iconv.encode(text, toEncoding); const outputPath = path.join(outputDir, `converted_${uniqueId}.txt`); await fs.writeFile(outputPath, converted); console.error(`Written converted text to ${outputPath}`); return { success: true, data: `Successfully converted text encoding: ${outputPath}`, }; } catch (error) { console.error(`Error in convertTextEncoding:`, error); return { success: false, error: error instanceof Error ? error.message : "Unknown error", }; } }
- src/tools/txtTools.ts:13-38 (schema)Defines the Tool object with name, description, and inputSchema for the text_encoding_converter tool, used for MCP tool listing and validation.export const TEXT_ENCODING_CONVERT_TOOL: Tool = { name: "text_encoding_converter", description: "Convert text between different encodings", inputSchema: { type: "object", properties: { inputPath: { type: "string", description: "Path to the input text file", }, outputDir: { type: "string", description: "Directory where converted file should be saved", }, fromEncoding: { type: "string", description: "Source encoding (e.g., 'big5', 'gbk', 'utf8')", }, toEncoding: { type: "string", description: "Target encoding (e.g., 'utf8', 'big5', 'gbk')", }, }, required: ["inputPath", "outputDir", "fromEncoding", "toEncoding"], }, };
- src/tools/_index.ts:7-9 (registration)Imports the TEXT_ENCODING_CONVERT_TOOL and includes it in the exported 'tools' array, which is used by the MCP server for listing available tools.import { TEXT_DIFF_TOOL, TEXT_ENCODING_CONVERT_TOOL, TEXT_FORMAT_TOOL, TEXT_SPLIT_TOOL } from "./txtTools.js"; 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/index.ts:240-263 (registration)In the main CallToolRequest handler, matches tool name 'text_encoding_converter' and calls the convertTextEncoding function with parsed arguments, handling success/error responses.if (name === "text_encoding_converter") { const { inputPath, outputDir, fromEncoding, toEncoding } = args as { inputPath: string; outputDir: string; fromEncoding: string; toEncoding: string; }; const result = await convertTextEncoding( inputPath, outputDir, fromEncoding, toEncoding ); 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/txtTools.ts:8-10 (helper)Helper function to generate unique ID for output filenames using crypto.randomBytes.function generateUniqueId(): string { return randomBytes(9).toString("hex"); }