Skip to main content
Glama

Get PDF Page Count

get_page_count
Read-onlyIdempotent

Retrieve 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

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYesAbsolute path to a local PDF file (e.g., "/path/to/document.pdf")

Implementation Reference

  • 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,
            };
          }
        },
      );
    }
  • 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();
  • 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);
  • 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;
    }
  • 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>;
Behavior4/5

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

Annotations already declare readOnlyHint true and idempotentHint true. Description adds that it 'only reads the PDF header, not the full content', which discloses the lightweight nature beyond annotations.

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

Conciseness5/5

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

Description is concise, well-structured with Args, Returns, Examples sections. Front-loaded with purpose, no wasted sentences.

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?

For a simple read-only tool with one parameter and comprehensive annotations, the description covers purpose, usage, return type, and examples. No gaps identified.

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

Parameters3/5

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

Schema has 100% coverage with description for file_path. Description adds 'Absolute path' and example format but does not significantly add beyond the schema. Baseline 3 applies.

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?

Clearly states 'Get the total number of pages in a PDF document', which is a specific verb+resource. Differentiates from sibling tools like extract_tables or read_text.

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

Usage Guidelines4/5

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

Provides examples of when to use (quick check, validation), implying lightweight operation. Does not explicitly mention when not to use, but context is clear.

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/shuji-bonji/pdf-reader-mcp'

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