Skip to main content
Glama
cablate

Simple Document Processing MCP Server

pdf_merger

Combine multiple PDF files into a single document using the Simple Document Processing MCP Server. Specify input file paths and output directory to merge PDFs.

Instructions

Merge multiple PDF files into one

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
inputPathsYesPaths to the input PDF files
outputDirYesDirectory where merged PDFs should be saved

Implementation Reference

  • The core handler function that performs the PDF merging logic: reads input PDFs, copies pages using pdf-lib, saves the merged file with a unique ID in the output directory.
    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", }; } }
  • The Tool object definition for pdf_merger, including name, description, and inputSchema specifying inputPaths array and outputDir.
    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"], }, };
  • Registration of pdf_merger tool (as PDF_MERGE_TOOL) in the central tools array exported for the MCP server's ListTools handler.
    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:95-111 (registration)
    Dispatch/registration in the main CallToolRequestSchema handler: checks name === 'pdf_merger', extracts args, calls mergePDFs, and returns formatted response.
    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, }; }

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