pdf_merger
Combine multiple PDF files into a single document using the Simple Document Processing MCP Server. Specify input PDF paths and output directory for efficient file management.
Instructions
Merge multiple PDF files into one
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| inputPaths | Yes | Paths to the input PDF files | |
| outputDir | Yes | Directory where merged PDFs should be saved |
Implementation Reference
- src/tools/pdfTools.ts:65-131 (handler)The core handler function `mergePDFs` that implements the PDF merging logic using pdf-lib. It reads multiple PDF files, copies all pages into a new PDFDocument, generates a unique filename, and saves the result.export async function mergePDFs(inputPaths: string[], outputDir: string) { try { console.error(`Starting PDF merge operation...`); console.error(`Input files:`, inputPaths); console.error(`Output directory: ${outputDir}`); // 確保輸出目錄存在 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(); console.error(`Generated unique ID for this batch: ${uniqueId}`); // 修改輸出檔案名稱,加入 uniqueId const outputPath = path.join(outputDir, `merged_${uniqueId}.pdf`); console.error(`New output path with unique ID: ${outputPath}`); const mergedPdf = await PDFDocument.create(); for (const filePath of inputPaths) { console.error(`Processing input file: ${filePath}`); const pdfBytes = await fs.readFile(filePath); console.error(`Read ${pdfBytes.length} bytes from ${filePath}`); const pdf = await PDFDocument.load(pdfBytes); const pageCount = pdf.getPageCount(); console.error(`Loaded PDF with ${pageCount} pages from ${filePath}`); const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices()); console.error(`Copied ${copiedPages.length} pages from ${filePath}`); copiedPages.forEach((page, index) => { mergedPdf.addPage(page); console.error(`Added page ${index + 1} from ${filePath}`); }); } const mergedPdfBytes = await mergedPdf.save(); console.error(`Generated merged PDF: ${mergedPdfBytes.length} bytes`); await fs.writeFile(outputPath, mergedPdfBytes); console.error(`Successfully wrote merged PDF to ${outputPath}`); return { success: true, data: `Successfully merged ${inputPaths.length} PDFs into ${outputPath}`, }; } catch (error) { console.error(`Error in mergePDFs:`); console.error(error); if (error instanceof Error) { console.error(`Error name: ${error.name}`); console.error(`Error message: ${error.message}`); console.error(`Error stack: ${error.stack}`); } return { success: false, error: error instanceof Error ? error.message : "Unknown error", }; } }
- src/tools/pdfTools.ts:13-31 (schema)The `PDF_MERGE_TOOL` definition providing the tool's name, description, and input schema for validation.export const PDF_MERGE_TOOL: Tool = { name: "pdf_merger", description: "Merge multiple PDF files into one", inputSchema: { type: "object", properties: { inputPaths: { type: "array", items: { type: "string" }, description: "Paths to the input PDF files", }, outputDir: { type: "string", description: "Directory where merged PDFs should be saved", }, }, required: ["inputPaths", "outputDir"], }, };
- src/index.ts:95-111 (registration)Dispatch logic in the main server request handler for CallToolRequestSchema that matches tool name 'pdf_merger' and invokes the mergePDFs handler.if (name === "pdf_merger") { const { inputPaths, outputDir } = args as { inputPaths: string[]; outputDir: string; }; const result = await mergePDFs(inputPaths, outputDir); 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/_index.ts:6-9 (registration)Re-export and inclusion of PDF_MERGE_TOOL in the central tools array used for ListTools response and server capabilities.import { PDF_MERGE_TOOL, PDF_SPLIT_TOOL } from "./pdfTools.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];
- src/tools/pdfTools.ts:8-10 (helper)Helper function generateUniqueId used to create unique filenames for merged PDFs.function generateUniqueId(): string { return randomBytes(9).toString("hex"); }