PDF处理服务

pdf4vllm是一个为视觉大语言模型优化的PDF阅读MCP服务器,自动检测文本损坏并转换为图像,保留阅读顺序,防止上下文溢出,并过滤不必要的图像。

  • by PyJudge
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 PyJudge/pdf4vllm-mcp

使用说明

pdf4vllm

针对视觉LLM优化的PDF阅读MCP服务器。

한국어

问题

方式 问题
文本提取 编码中断→垃圾输出,图像-文本顺序混合
图像转换 令牌爆炸(特别是当页面较多时)

解决

pdf4vllm 假设PDF很脏进行。

  • 自动检测文本损坏→自动切换到图像
  • 保留读取顺序(按文本→表格→图像块的顺序)
  • 页面限制防止上下文溢出
  • 自动过滤不必要的图像(徽标、线条、标题/推特)

安装

pip install pdf4vllm-mcp
# 또는
uvx pdf4vllm-mcp

Claude桌面设置

git clone https://github.com/PyJudge/pdf4vllm-mcp.git
cd pdf4vllm-mcp
python scripts/install_mcp.py

或直接设置(~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "pdf4vllm": {
      "command": "/python/경로",
      "args": ["/pdf4vllm-mcp/경로/src/server.py"]
    }
  }
}

工具

工具 说明
list_pdfs 查找PDF文件(glob模式) name_pattern 支持)
read_pdf 提取为PDF内容块
grep_pdf 搜索PDF中的文本(pdfgrep 需要安装)

提取模式

模式 说明
auto (默认) 尝试提取文本→检测到损坏时切换到图像
text_only 仅提取文本/表格,无图像
image_only 仅以图像呈现页面

问题

方法 问题
文本提取 编码损坏→ 垃圾输出、混合文本图像排序
图像转换 代币爆炸(尤其是多页)

解决方案

pdf4vllm 假设PDF文件很乱.

  • 自动检测文本损坏→ 自动切换到图像
  • 保持阅读顺序(文本→ 桌子→ 按顺序排列的图像块)
  • 页面限制防止上下文溢出
  • 过滤不必要的图像(徽标、线条、页眉/页脚)
PDF Input
    ↓
Corruption Detection (pdfminer.six + pattern analysis)
    ↓
┌─────────────┬─────────────┐
│  Corrupted  │    Clean    │
│  → Image    │  → Text +   │
│    only     │    Tables + │
│             │    Images   │
└─────────────┴─────────────┘
    ↓
Ordered Blocks (JSON)

安装

pip install pdf4vllm-mcp
# or run without installing
uvx pdf4vllm-mcp

Claude桌面设置

git clone https://github.com/PyJudge/pdf4vllm-mcp.git
cd pdf4vllm-mcp
python scripts/install_mcp.py

或手动编辑 ~/Library/Application Support/Claude/claude_desktop_config.json:

{
  "mcpServers": {
    "pdf4vllm": {
      "command": "/path/to/python",
      "args": ["/path/to/pdf4vllm-mcp/src/server.py"]
    }
  }
}

Claude代码设置

创建 .mcp.json 在您的项目中:

{
  "mcpServers": {
    "pdf4vllm": {
      "command": "uvx",
      "args": ["pdf4vllm-mcp"]
    }
  }
}

工具

工具 说明
list_pdfs 使用glob过滤查找PDF文件(name_pattern)
read_pdf 将PDF内容提取为有序块
grep_pdf 使用pdfgrep在PDF中搜索文本(需要 pdfgrep 已安装)

提取模式

模式 描述
auto (默认) 尝试文本提取→ 如果损坏,请切换到图像
text_only 仅文本/表格,无图像
image_only 仅将页面渲染为图像

输出格式

{
  "pages": [
    {
      "page_number": 1,
      "content_blocks": [
        {"type": "text", "content": "..."},
        {"type": "table", "content": "| A | B |"},
        {"type": "image", "content": "[IMAGE_0]"}
      ]
    }
  ]
}

当文本损坏时:

{
  "page_number": 2,
  "content_blocks": [],
  "text_corrupted": true,
  "page_image": "[IMAGE_1]"
}

配置

config.json 或环境变量:

{
  "max_pages_per_request": 10,
  "max_image_dimension": 842,
  "page_image_dpi": 100
}
export PDF_MAX_PAGES=20
export PDF_PAGE_IMAGE_DPI=150

测试服务器

pip install pdf4vllm-mcp[test]
python test_server.py
# → http://localhost:8000

许可证

麻省理工学院


GitHub · PyPI