MCP PDF Reader Server

MCP PDF Reader Server

Enables comprehensive PDF processing including text extraction, image extraction, and OCR capabilities for reading text within images across multiple languages.

Category
访问服务器

README

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

Prerequisites

System Dependencies

Tesseract OCR

You need to install Tesseract OCR on your system:

Ubuntu/Debian:

sudo apt update
sudo apt install tesseract-ocr tesseract-ocr-eng

macOS:

brew install tesseract

Windows:

  1. Download from: https://github.com/UB-Mannheim/tesseract/wiki
  2. Install and add to PATH
  3. Or use: conda install -c conda-forge tesseract

Additional Language Packs (Optional)

# For multiple languages
sudo apt install tesseract-ocr-fra tesseract-ocr-deu tesseract-ocr-spa

Installation

Quick Start with UV

  1. Install UV (if not already installed):
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh

# Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
  1. Clone/Create the project:
mkdir mcp-pdf-reader-server
cd mcp-pdf-reader-server
  1. Initialize and install with UV:
# Copy the files (pdf_reader_server.py and pyproject.toml)
# Then install dependencies
uv sync
  1. Verify installation:
uv run python -c "import pytesseract; print(pytesseract.get_tesseract_version())"

Alternative: Manual Setup

If you prefer traditional setup:

  1. Create virtual environment:
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install dependencies:
pip install fastmcp PyMuPDF pytesseract Pillow

Usage

Running the Server

With UV:

uv run python pdf_reader_server.py

Or if you have the environment activated:

python pdf_reader_server.py

The server will start and listen for MCP requests on stdin/stdout.

Available Tools

1. read_pdf_text

Extract text content from PDF pages.

Parameters:

  • file_path (string, required): Path to the PDF file
  • page_range (object, optional): Dict with start and end page numbers

Example:

{
  "file_path": "/path/to/document.pdf",
  "page_range": {"start": 1, "end": 5}
}

2. extract_pdf_images

Extract all images from a PDF file.

Parameters:

  • file_path (string, required): Path to the PDF file
  • output_dir (string, optional): Directory to save images
  • page_range (object, optional): Page range to process

Example:

{
  "file_path": "/path/to/document.pdf",
  "output_dir": "/path/to/images/",
  "page_range": {"start": 1, "end": 3}
}

3. read_pdf_with_ocr

Extract text from both regular text and images using OCR.

Parameters:

  • file_path (string, required): Path to the PDF file
  • page_range (object, optional): Page range to process
  • ocr_language (string, optional): OCR language code (default: "eng")

Example:

{
  "file_path": "/path/to/document.pdf",
  "ocr_language": "eng+fra",
  "page_range": {"start": 1, "end": 10}
}

Supported OCR Languages:

  • eng - English
  • fra - French
  • deu - German
  • spa - Spanish
  • eng+fra - Multiple languages

4. get_pdf_info

Get comprehensive metadata and statistics about a PDF.

Parameters:

  • file_path (string, required): Path to the PDF file

5. analyze_pdf_structure

Analyze the structure and content distribution of a PDF.

Parameters:

  • file_path (string, required): Path to the PDF file

Configuration with Claude Desktop

With UV

Add this to your claude_desktop_config.json:

{
  "mcpServers": {
    "pdf-reader": {
      "command": "uv",
      "args": ["run", "python", "/path/to/your/pdf_reader_server.py"],
      "cwd": "/path/to/your/mcp-pdf-reader-server"
    }
  }
}

With Virtual Environment

{
  "mcpServers": {
    "pdf-reader": {
      "command": "/path/to/your/.venv/bin/python",
      "args": ["/path/to/your/pdf_reader_server.py"]
    }
  }
}

System Python

{
  "mcpServers": {
    "pdf-reader": {
      "command": "python",
      "args": ["/path/to/your/pdf_reader_server.py"],
      "env": {
        "PYTHONPATH": "/path/to/your/.venv/lib/python3.x/site-packages"
      }
    }
  }
}

Example Responses

Text Extraction Response

{
  "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

{
  "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)

Troubleshooting

Common Issues

  1. Tesseract not found:

    TesseractNotFoundError: tesseract is not installed
    
    • Install Tesseract OCR system package
    • Ensure it's in your PATH
  2. Permission errors:

    • Ensure the Python process has read access to PDF files
    • Ensure write access to output directories
  3. Poor OCR results:

    • Try different OCR language codes
    • Consider image preprocessing
    • Check if images are high enough resolution
  4. Memory errors:

    • Process smaller page ranges
    • Close other applications
    • Consider increasing available RAM

Debug Mode

Run with debug logging using UV:

PYTHONUNBUFFERED=1 uv run python pdf_reader_server.py

Or with regular Python:

PYTHONUNBUFFERED=1 python pdf_reader_server.py

Testing OCR

Test Tesseract directly:

tesseract --list-langs
tesseract image.png output.txt

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:

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:

# 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.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选