Skip to main content
Glama
cablate

Simple Document Processing MCP Server

text_encoding_converter

Convert text files between different character encodings like UTF-8, Big5, and GBK to ensure compatibility across systems and applications.

Instructions

Convert text between different encodings

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
inputPathYesPath to the input text file
outputDirYesDirectory where converted file should be saved
fromEncodingYesSource encoding (e.g., 'big5', 'gbk', 'utf8')
toEncodingYesTarget encoding (e.g., 'utf8', 'big5', 'gbk')

Implementation Reference

  • The main handler function that performs the text encoding conversion: reads input file, decodes from source encoding, encodes to target encoding using iconv-lite, ensures output directory exists, generates unique filename, and writes the converted file.
    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", }; } }
  • The Tool object definition for text_encoding_converter, including the input schema with parameters for inputPath, outputDir, fromEncoding, and toEncoding.
    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/index.ts:240-263 (registration)
    Dispatch handler in the MCP server that matches tool name 'text_encoding_converter', extracts arguments, calls convertTextEncoding, and formats the response.
    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, }; }
  • Inclusion of TEXT_ENCODING_CONVERT_TOOL in the tools array exported for MCP tool listing, and import from txtTools.js.
    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];
  • Helper function to generate a unique ID for output filenames using crypto.randomBytes.
    function generateUniqueId(): string { return randomBytes(9).toString("hex"); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/cablate/mcp-doc-forge'

If you have feedback or need assistance with the MCP directory API, please join our Discord server