PDF处理服务器

一个基于FastMCP构建的PDF处理服务器,提供文本提取、图像提取和OCR功能,适用于文档分析和数据处理场景。

  • by labeveryday
  • Python
  • 本地部署
  • 13 星标
  • 更新于
在 GitHub 查看 labeveryday/mcp_pdf_reader

使用说明

MCP PDF阅读器服务器(Python+FastMCP)

使用FastMCP构建的强大的模型上下文协议(MCP)服务器,提供全面的PDF处理功能,包括文本提取、图像提取和OCR,用于读取图像中的文本。

特性

  • 文本提取:从PDF页面提取文本内容
  • 图像提取:从PDF文件中提取所有图像
  • OCR功能:使用Tesseract OCR从图像中读取文本
  • 综合分析:获取详细的PDF结构和元数据
  • 页面范围支持:特定于流程的页面范围
  • 多种语言:支持多种语言的OCR

先决条件

系统相关性

Tesseract OCR

您需要在系统上安装Tesseract OCR:

Ubuntu/Debian:

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

macOS:

brew install tesseract

窗户:

  1. 下载地址:https://github.com/UB-Mannheim/tesseract/wiki
  2. 安装并添加到PATH
  3. 或使用: conda install -c conda-forge tesseract

附加语言包(可选)

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

安装

UV快速入门

  1. 安装UV(如果尚未安装):
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh

# Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
  1. 克隆/创建项目:
mkdir mcp-pdf-reader-server
cd mcp-pdf-reader-server
  1. 使用UV进行初始化和安装:
# Copy the files (pdf_reader_server.py and pyproject.toml)
# Then install dependencies
uv sync
  1. 验证安装:
uv run python -c "import pytesseract; print(pytesseract.get_tesseract_version())"

替代方案:手动设置

如果您更喜欢传统设置:

  1. 创建虚拟环境:
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. 安装依赖项:
pip install fastmcp PyMuPDF pytesseract Pillow

用法

运行服务器

使用紫外线:

uv run python pdf_reader_server.py

或者,如果您已激活环境:

python pdf_reader_server.py

服务器将启动并监听stdin/stdout上的MCP请求。

可用工具

1. read_pdf_text

从PDF页面提取文本内容。

参数:

  • file_path (字符串,必填):PDF文件的路径
  • page_range (对象,可选):用骰子 start 和 end 页码

例子:

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

2. extract_pdf_images

从PDF文件中提取所有图像。

参数:

  • file_path (字符串,必填):PDF文件的路径
  • output_dir (字符串,可选):保存图像的目录
  • page_range (对象,可选):要处理的页面范围

例子:

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

3. read_pdf_with_ocr

使用OCR从常规文本和图像中提取文本。

参数:

  • file_path (字符串,必填):PDF文件的路径
  • page_range (对象,可选):要处理的页面范围
  • ocr_language (字符串,可选):OCR语言代码(默认:“eng”)

例子:

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

支持的OCR语言:

  • eng -英语
  • fra -法语
  • deu -德语
  • spa -西班牙语
  • eng+fra -多种语言

4. get_pdf_info

获取PDF的全面元数据和统计数据。

参数:

  • file_path (字符串,必填):PDF文件的路径

5. analyze_pdf_structure

分析PDF的结构和内容分布。

参数:

  • file_path (字符串,必填):PDF文件的路径

使用Claude Desktop进行配置

带紫外线

将此添加到您的 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"
    }
  }
}

虚拟环境

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

系统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"
      }
    }
  }
}

示例响应

文本提取响应

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

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

性能注意事项

OCR性能

  • 对于大图像,OCR处理可能很慢
  • 考虑处理较小的页面范围以获得更快的结果
  • 小于50x50像素的图像会自动跳过

内存使用

  • 包含许多图像的大型PDF可能会占用大量内存
  • 服务器按顺序处理页面以管理内存使用情况
  • 提取的图像保存到磁盘以减少内存压力

优化提示

  1. 使用页面范围 对于大型文档
  2. 指定输出目录 用于图像提取以避免临时文件堆积
  3. 选择合适的OCR语言 提高精度和速度
  4. 预处理图像 如果OCR质量差(考虑添加OpenCV)

故障排除

常见问题

  1. 未找到Tesseract:

    TesseractNotFoundError: tesseract is not installed
    
    • 安装Tesseract OCR系统包
    • 确保它在你的路径中
  2. 权限错误:

    • 确保Python进程具有读取PDF文件的权限
    • 确保对输出目录的写访问权限
  3. OCR结果不佳:

    • 尝试不同的OCR语言代码
    • 考虑图像预处理
    • 检查图像的分辨率是否足够高
  4. 内存错误:

    • 处理较小的页面范围
    • 关闭其他应用程序
    • 考虑增加可用RAM

调试模式

使用UV运行调试日志:

PYTHONUNBUFFERED=1 uv run python pdf_reader_server.py

或者使用常规Python:

PYTHONUNBUFFERED=1 python pdf_reader_server.py

测试OCR

直接测试Tesseract:

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

依赖项

  • fastmcp:现代MCP服务器框架
  • PyMuPDF:快速PDF处理和渲染
  • 小公主:Tesseract OCR的Python包装器
  • 枕头:图像处理库
  • 镶嵌ocr:系统OCR引擎

高级功能

自定义OCR配置

您可以在代码中修改OCR配置:

ocr_text = pytesseract.image_to_string(
    pil_image, 
    lang=ocr_language,
    config='--psm 6 -c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz '
)

图像预处理

为了获得更好的OCR结果,请考虑添加图像预处理:

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

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 添加新功能的测试
  4. 提交拉取请求

许可证

MIT许可证-有关详细信息,请参阅许可证文件。