Skip to main content
Glama

write_pdf

Destructive

Create new PDF files from markdown content or modify existing PDFs by inserting or deleting pages using Desktop Commander MCP's file management capabilities.

Instructions

                    Create a new PDF file or modify an existing one.

                    THIS IS THE ONLY TOOL FOR CREATING AND MODIFYING PDF FILES.

                    RULES ABOUT FILENAMES:
                    - When creating a new PDF, 'outputPath' MUST be provided and MUST use a new unique filename (e.g., "result_01.pdf", "analysis_2025_01.pdf", etc.).

                    MODES:
                    1. CREATE NEW PDF:
                       - Pass a markdown string as 'content'.
                       write_pdf(path="doc.pdf", content="# Title\n\nBody text...")

                    2. MODIFY EXISTING PDF:
                       - Pass array of operations as 'content'.
                       - NEVER overwrite the original file.
                       - ALWAYS provide a new filename in 'outputPath'.
                       - After modifying, show original file path and new file path to user.

                       write_pdf(path="doc.pdf", content=[
                           { type: "delete", pageIndexes: [0, 2] },
                           { type: "insert", pageIndex: 1, markdown: "# New Page" }
                       ])

                    OPERATIONS:
                    - delete: Remove pages by 0-based index.
                      { type: "delete", pageIndexes: [0, 1, 5] }

                    - insert: Add pages at a specific 0-based index.
                      { type: "insert", pageIndex: 0, markdown: "..." }
                      { type: "insert", pageIndex: 5, sourcePdfPath: "/path/to/source.pdf" }

                    PAGE BREAKS:
                    To force a page break, use this HTML element:
                    <div style="page-break-before: always;"></div>
                    
                    Example:
                    "# Page 1\n\n<div style=\"page-break-before: always;\"></div>\n\n# Page 2"

                    ADVANCED STYLING:
                    HTML/CSS and inline SVG are supported for:
                    - Text styling: colors, sizes, alignment, highlights
                    - Boxes: borders, backgrounds, padding, rounded corners
                    - SVG graphics: charts, diagrams, icons, shapes
                    - Images: <img src="/absolute/path/image.jpg" width="300" /> or ![alt](/path/image.jpg)

                    Supports standard markdown features including headers, lists, code blocks, tables, and basic formatting.

                    Only works within allowed directories.

                    IMPORTANT: Always use absolute paths for reliability. Paths are automatically normalized regardless of slash direction. Relative paths may fail as they depend on the current working directory. Tilde paths (~/...) might not work in all contexts. Unless the user explicitly asks for relative paths, use absolute paths.
                    This command can be referenced as "DC: ..." or "use Desktop Commander to ..." in your instructions.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes
contentYes
outputPathNo
optionsNo

Implementation Reference

  • MCP handler function for 'write_pdf' tool that validates input using WritePdfArgsSchema and delegates to the writePdf utility function.
    export async function handleWritePdf(args: unknown): Promise<ServerResult> {
        try {
            const parsed = WritePdfArgsSchema.parse(args);
            await writePdf(parsed.path, parsed.content, parsed.outputPath, parsed.options);
            const targetPath = parsed.outputPath || parsed.path;
            return {
                content: [{ type: "text", text: `Successfully wrote PDF to ${targetPath}${parsed.outputPath ? `\nOriginal file: ${parsed.path}` : ''}` }],
            };
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            return createErrorResponse(errorMessage);
        }
    }
  • Zod schema for validating arguments to the write_pdf tool, supporting both markdown string and PDF operations array.
    export const WritePdfArgsSchema = z.object({
      path: z.string(),
      // Preprocess content to handle JSON strings that should be parsed as arrays
      content: z.preprocess(
        (val) => {
          // If it's a string that looks like JSON array, parse it
          if (typeof val === 'string' && val.trim().startsWith('[')) {
            try {
              return JSON.parse(val);
            } catch {
              // If parsing fails, return as-is (might be markdown content)
              return val;
            }
          }
          // Otherwise return as-is
          return val;
        },
        z.union([z.string(), z.array(PdfOperationSchema)])
      ),
      outputPath: z.string().optional(),
      options: z.object({}).passthrough().optional(), // Allow passing options to md-to-pdf
    });
  • Core utility function implementing PDF creation from markdown or editing existing PDFs using operations like insert/delete pages.
    export async function writePdf(
        filePath: string,
        content: string | PdfOperations[],
        outputPath?: string,
        options: any = {}
    ): Promise<void> {
        const validPath = await validatePath(filePath);
        const fileExtension = getFileExtension(validPath);
    
        if (typeof content === 'string') {
            // --- PDF CREATION MODE ---
            capture('server_write_pdf', {
                fileExtension: fileExtension,
                contentLength: content.length,
                mode: 'create'
            });
    
            const pdfBuffer = await parseMarkdownToPdf(content, options);
            // Use outputPath if provided, otherwise overwrite input file
            const targetPath = outputPath ? await validatePath(outputPath) : validPath;
            await fs.writeFile(targetPath, pdfBuffer);
        } else if (Array.isArray(content)) {
    
            // Use outputPath if provided, otherwise overwrite input file
            const targetPath = outputPath ? await validatePath(outputPath) : validPath;
    
            const operations: PdfOperations[] = [];
    
            // Validate paths in operations
            for (const o of content) {
                if (o.type === 'insert') {
                    if (o.sourcePdfPath) {
                        o.sourcePdfPath = await validatePath(o.sourcePdfPath);
                    }
                }
                operations.push(o);
            }
    
            capture('server_write_pdf', {
                fileExtension: fileExtension,
                operationCount: operations.length,
                mode: 'modify',
                deleteCount: operations.filter(op => op.type === 'delete').length,
                insertCount: operations.filter(op => op.type === 'insert').length
            });
    
            // Perform the PDF editing
            const modifiedPdfBuffer = await editPdf(validPath, operations);
    
            // Write the modified PDF to the output path
            await fs.writeFile(targetPath, modifiedPdfBuffer);
        } else {
            throw new Error('Invalid content type for writePdf. Expected string (markdown) or array of operations.');
        }
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already indicate destructiveHint=true and readOnlyHint=false, but the description adds valuable behavioral context beyond this: it explains that modifications never overwrite original files, always create new files, and require showing both paths to users. It also mentions directory restrictions and path normalization behavior, though it doesn't cover rate limits or authentication needs.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with clear sections (RULES, MODES, OPERATIONS, etc.) and uses bullet points effectively. While comprehensive, some sections like ADVANCED STYLING and the final IMPORTANT note about paths could be more concise. Most sentences earn their place by providing essential guidance.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (4 parameters, nested objects, destructive operations) and lack of output schema, the description provides complete context: it explains both creation and modification modes, detailed operation types, formatting capabilities, path requirements, and behavioral constraints. It adequately compensates for the missing structured documentation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters5/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description fully compensates by explaining all 4 parameters in detail: 'path' is the input file, 'content' can be markdown string or operations array, 'outputPath' is required for new/modified files with unique names, and 'options' is mentioned but not detailed. It provides examples for each parameter and clarifies their semantics in different modes.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Create a new PDF file or modify an existing one.' It specifies the exact actions (create/modify) and resource (PDF files), and explicitly distinguishes it from siblings by stating 'THIS IS THE ONLY TOOL FOR CREATING AND MODIFYING PDF FILES.'

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use this tool versus alternatives: it's the only PDF creation/modification tool. It includes detailed rules for different modes (CREATE NEW PDF vs. MODIFY EXISTING PDF), when to provide outputPath, and when to use markdown versus operations array. It also specifies path requirements and directory restrictions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/wonderwhy-er/ClaudeComputerCommander'

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