Get PDF Page Count
get_page_countRetrieve the total page count of a PDF by reading only its header, enabling fast validation and extraction planning without loading the full file.
Instructions
Get the total number of pages in a PDF document.
This is a lightweight operation that only reads the PDF header, not the full content.
Args:
file_path (string): Absolute path to a local PDF file
Returns: Page count as a number.
Examples:
Quick check before deciding which pages to extract
Validate a PDF file is readable
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | Absolute path to a local PDF file (e.g., "/path/to/document.pdf") |
Implementation Reference
- src/tools/tier1/get-page-count.ts:10-56 (handler)Core handler function that registers the 'get_page_count' tool. Loads a PDF via loadDocument(), reads doc.numPages, and returns the page count as text. Includes error handling with structured errors.
export function registerGetPageCount(server: McpServer): void { server.registerTool( 'get_page_count', { title: 'Get PDF Page Count', description: `Get the total number of pages in a PDF document. This is a lightweight operation that only reads the PDF header, not the full content. Args: - file_path (string): Absolute path to a local PDF file Returns: Page count as a number. Examples: - Quick check before deciding which pages to extract - Validate a PDF file is readable`, inputSchema: GetPageCountSchema, annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, }, async (params: GetPageCountInput) => { try { const doc = await loadDocument(params.file_path); try { const count = doc.numPages; return { content: [{ type: 'text' as const, text: String(count) }], }; } finally { await doc.destroy(); } } catch (error) { const err = handleStructuredError(error); return { content: [{ type: 'text' as const, text: JSON.stringify(err, null, 2) }], isError: true, }; } }, ); } - src/schemas/tier1.ts:10-14 (schema)Zod input schema for get_page_count: requires a single 'file_path' string (absolute path to a PDF).
export const GetPageCountSchema = z .object({ file_path: FilePathSchema, }) .strict(); - src/tools/index.ts:9-32 (registration)Import and registration call in the main tool registration entry point.
import { registerGetPageCount } from './tier1/get-page-count.js'; import { registerReadImages } from './tier1/read-images.js'; import { registerReadText } from './tier1/read-text.js'; import { registerReadUrl } from './tier1/read-url.js'; import { registerSearchText } from './tier1/search-text.js'; import { registerSummarize } from './tier1/summarize.js'; // Tier 2: Structure analysis import { registerExtractTables } from './tier2/extract-tables.js'; import { registerInspectAnnotations } from './tier2/inspect-annotations.js'; import { registerInspectFonts } from './tier2/inspect-fonts.js'; import { registerInspectSignatures } from './tier2/inspect-signatures.js'; import { registerInspectStructure } from './tier2/inspect-structure.js'; import { registerInspectTags } from './tier2/inspect-tags.js'; // Tier 3: Validation & analysis import { registerCompareStructure } from './tier3/compare-structure.js'; import { registerValidateMetadata } from './tier3/validate-metadata.js'; import { registerValidateTagged } from './tier3/validate-tagged.js'; /** * Register all tools with the MCP server. */ export function registerAllTools(server: McpServer): void { // Tier 1: Basic PDF operations registerGetPageCount(server); - src/services/pdfjs-service.ts:43-47 (helper)Helper that reads a PDF file and loads it via pdfjs-dist's getDocument(), used by the handler to get the document proxy from which numPages is read.
export async function loadDocument(filePath: string): Promise<PDFDocumentProxy> { const data = await readPdfFile(filePath); const doc = await getDocument({ data, useSystemFonts: true, verbosity: PDFJS_VERBOSITY }).promise; return doc; } - src/schemas/tier1.ts:134-137 (registration)TypeScript type inferred from the schema, used in the handler's parameter typing.
export type GetPageCountInput = z.infer<typeof GetPageCountSchema>; export type GetMetadataInput = z.infer<typeof GetMetadataSchema>; export type ReadTextInput = z.infer<typeof ReadTextSchema>; export type SearchTextInput = z.infer<typeof SearchTextSchema>;