Labsco
labeveryday logo

MCP PDF Reader

β˜… 12

from labeveryday

Extract text, images, and perform OCR on PDF documents using Tesseract OCR.

πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedFreeQuick setup

MCP PDF Reader Server (Python + FastMCP)

A powerful Model Context Protocol (MCP) server built with FastMCP that provides comprehensive PDF processing capabilities including text extraction, image extraction, and OCR for reading text within images.

Features

  • Text Extraction: Extract text content from PDF pages
  • Image Extraction: Extract all images from PDF files
  • OCR Capabilities: Read text from images using Tesseract OCR
  • Comprehensive Analysis: Get detailed PDF structure and metadata
  • Page Range Support: Process specific page ranges
  • Multiple Languages: OCR support for multiple languages

Example Responses

Text Extraction Response

Copy & paste β€” that's it
{
  "success": true,
  "file_path": "/path/to/document.pdf",
  "pages_processed": "1-3",
  "total_pages": 10,
  "pages_text": [
    {
      "page_number": 1,
      "text": "Page 1 content...",
      "word_count": 125
    }
  ],
  "combined_text": "All text combined...",
  "total_word_count": 1250,
  "total_character_count": 8750
}

OCR Response

Copy & paste β€” that's it
{
  "success": true,
  "file_path": "/path/to/document.pdf",
  "pages_processed": "1-2",
  "ocr_language": "eng",
  "pages_data": [
    {
      "page_number": 1,
      "text": "Regular text from PDF...",
      "ocr_text": "Text extracted from images...",
      "images_with_text": [
        {
          "image_index": 1,
          "ocr_text": "Text from image 1",
          "confidence": "high"
        }
      ],
      "combined_text": "Combined text and OCR...",
      "text_word_count": 100,
      "ocr_word_count": 25
    }
  ],
  "summary": {
    "total_text_word_count": 200,
    "total_ocr_word_count": 50,
    "combined_word_count": 250,
    "images_processed": 3
  },
  "all_text_combined": "All extracted text..."
}

Performance Considerations

OCR Performance

  • OCR processing can be slow for large images
  • Consider processing smaller page ranges for faster results
  • Images smaller than 50x50 pixels are automatically skipped

Memory Usage

  • Large PDFs with many images may consume significant memory
  • The server processes pages sequentially to manage memory usage
  • Extracted images are saved to disk to reduce memory pressure

Optimization Tips

  1. Use page ranges for large documents
  2. Specify output directories for image extraction to avoid temp file buildup
  3. Choose appropriate OCR languages to improve accuracy and speed
  4. Preprocess images if OCR quality is poor (consider adding OpenCV)

Dependencies

  • fastmcp: Modern MCP server framework
  • PyMuPDF: Fast PDF processing and rendering
  • pytesseract: Python wrapper for Tesseract OCR
  • Pillow: Image processing library
  • tesseract-ocr: System OCR engine

Advanced Features

Custom OCR Configuration

You can modify the OCR configuration in the code:

Copy & paste β€” that's it
ocr_text = pytesseract.image_to_string(
    pil_image, 
    lang=ocr_language,
    config='--psm 6 -c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '
)

Image Preprocessing

For better OCR results, consider adding image preprocessing:

Copy & paste β€” that's it
# Add to requirements: opencv-python, numpy
import cv2
import numpy as np

# Preprocessing example
def preprocess_image(image):
    gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    return Image.fromarray(thresh)

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

License

MIT License - see LICENSE file for details.