convert_numbers_to_pdf
Convert Apple Numbers files (.numbers) to PDF format for easy sharing and archiving using the MCP Excel to PDF Converter server.
Instructions
Converts Apple Numbers files (.numbers) to PDF format
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| input_path | Yes | Relative path to the Numbers file (.numbers) to convert | |
| output_format | No | Output format (currently only PDF is supported) |
Implementation Reference
- src/handlers/convertNumbers.ts:23-119 (handler)The main handler function that executes the tool logic: validates input, checks for LibreOffice, converts .numbers file to PDF using libreoffice-convert, writes to temp file, and returns the relative path.export const handleConvertNumbersFunc = async ( args: unknown ): Promise<{ content: { type: string; text: string }[] }> => { // Ensure LibreOffice is installed - reuse function from convertExcel.ts const libreOfficeInstalled = await checkLibreOfficeInstalled(); if (!libreOfficeInstalled) { throw new McpError( ErrorCode.InvalidRequest, 'LibreOffice is not installed or not found in PATH. Please install LibreOffice to use this tool.' ); } // Parse and validate arguments let parsedArgs: ConvertNumbersArgs; try { parsedArgs = ConvertNumbersArgsSchema.parse(args); } catch (error) { if (error instanceof z.ZodError) { throw new McpError( ErrorCode.InvalidParams, `Invalid arguments: ${error.errors.map(e => `${e.path.join('.')} (${e.message})`).join(', ')}` ); } const message = error instanceof Error ? error.message : String(error); throw new McpError(ErrorCode.InvalidParams, `Argument validation failed: ${message}`); } try { // Ensure temp directory exists - now await because the function is async const tempDir = await ensureTempDir(); // Resolve input path const inputPath = resolvePath(parsedArgs.input_path); // Check file extension const ext = path.extname(inputPath).toLowerCase(); if (ext !== '.numbers') { throw new McpError( ErrorCode.InvalidParams, 'Invalid file format. Only .numbers files are supported.' ); } // Read the input file const fileContent = await fs.readFile(inputPath); // Create output path const outputPath = createTempFilePath(path.basename(inputPath)); await fs.mkdir(path.dirname(outputPath), { recursive: true }); // Convert to PDF const pdfBuffer = await libreConvert(fileContent, '.pdf', undefined); // Write the output file await fs.writeFile(outputPath, pdfBuffer); // Create a relative path for the result const relativeOutputPath = path.relative(process.cwd(), outputPath); return { content: [ { type: 'text', text: JSON.stringify({ success: true, output_path: relativeOutputPath, message: 'Numbers file successfully converted to PDF', }, null, 2), }, ], }; } catch (error) { console.error('[Excel to PDF MCP] Error in conversion:', error); // Handle specific errors if (error instanceof McpError) { throw error; // Re-throw MCP errors } if (error instanceof Error) { if ('code' in error && error.code === 'ENOENT') { throw new McpError(ErrorCode.InvalidParams, `File not found: ${parsedArgs.input_path}`); } throw new McpError( ErrorCode.InvalidRequest, `Error converting Numbers to PDF: ${error.message}` ); } // Generic error throw new McpError( ErrorCode.InvalidRequest, `Unknown error during Numbers to PDF conversion` ); } };
- src/handlers/convertNumbers.ts:15-18 (schema)Zod schema defining the input parameters for the tool: input_path (string) and output_format (pdf).const ConvertNumbersArgsSchema = z.object({ input_path: z.string().min(1).describe('Relative path to the Numbers file (.numbers) to convert'), output_format: z.enum(['pdf']).default('pdf').describe('Output format (currently only PDF is supported)') }).strict();
- src/handlers/convertNumbers.ts:122-127 (registration)ToolDefinition object that registers the tool's name, description, schema, and handler function.export const convertNumbersToolDefinition: ToolDefinition = { name: 'convert_numbers_to_pdf', description: 'Converts Apple Numbers files (.numbers) to PDF format', schema: ConvertNumbersArgsSchema, handler: handleConvertNumbersFunc, };
- src/handlers/index.ts:18-20 (registration)Registration of all tools, including convert_numbers_to_pdf, into the allToolDefinitions array used by the MCP server.export const allToolDefinitions: ToolDefinition[] = [ convertExcelToolDefinition, convertNumbersToolDefinition