使用说明
MCP PDF阅读器
一个强大的 模型上下文协议(MCP) 该服务器使Claude和GitHub Copilot等人工智能助手能够智能地与PDF文档交互。提取文本、元数据、搜索内容和检索嵌入图像——所有这些都是通过一个标准化的、LLM友好的界面完成的。 不基于OCR。
当前版本: 1.0.0
包裹: @rturv/mcp-pdf-reader
许可证: 麻省理工学院
快速开始
安装
npm install -g @rturv/mcp-pdf-reader
运行服务器
mcp-pdf-reader
配置
克劳德桌面版
添加 claude_desktop_config.json:
{
"mcpServers": {
"pdf-reader": {
"command": "mcp-pdf-reader"
}
}
}
地点:
- 窗户:
%APPDATA%\Claude\claude_desktop_config.json - macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - Linux:
~/.config/claude/claude_desktop_config.json
GitHub副本(VS代码)
添加 mcpServers.json:
{
"mcpServers": {
"pdf-reader": {
"command": "mcp-pdf-reader",
"args": [],
"disabled": false
}
}
}
地点: %APPDATA%\Code\User\globalStorage\github.copilot-chat\mcpServers.json
看 COPYLOT_CONFIG.md 了解其他安装方法。
特性
- ✅ 全文提取 -从PDF文件中提取完整文本
- ✅ 元数据抽取 -检索标题、作者、创建日期等
- ✅ 页面范围读取 -从特定页面提取文本
- ✅ 文本搜索 -查找具有周围上下文的文本
- ✅ 页数 -获取总页数
- ✅ 图像提取 -在Base64中列出并提取嵌入图像
- ✅ 遵从标准 -遵循MCP规范,实现无缝LLM集成
工具参考
此MCP服务器提供了7个用于全面PDF操作的工具。所有工具都可以通过Claude Desktop、GitHub Copilot和其他MCP兼容客户端访问。
1. read_pdf
目的: 从PDF文件中提取所有文本内容。将此作为理解PDF文档内容的主要方法。
何时使用:
- 读取整个文档内容
- 获取用于摘要或分析的完整文档文本
- 与元数据结合时提取内容
输入参数:
{
"filePath": "string (required) - Absolute path to the PDF file",
"includeMetadata": "boolean (optional, default: false) - Include PDF metadata in response"
}
请求示例:
{
"filePath": "C:/Documents/report.pdf",
"includeMetadata": true
}
示例响应:
{
"text": "Executive Summary\n\nThis report details Q4 2025 performance...",
"metadata": {
"title": "Q4 2025 Performance Report",
"author": "Analytics Team",
"subject": "Quarterly Report",
"creator": "Microsoft Word",
"producer": "iLovePDF",
"creationDate": "D:20250115120000Z",
"modificationDate": "D:20250115150000Z",
"keywords": "Q4, report, performance",
"totalPages": 12
},
"pageCount": 12
}
2. get_pdf_metadata
目的: 提取文档元数据而不读取全文。非常适合快速检查文档。
何时使用:
- 识别文档属性(作者、标题、日期)
- 快速文档验证
- 构建文档目录
- 检查修改日期
输入参数:
{
"filePath": "string (required) - Absolute path to the PDF file"
}
请求示例:
{
"filePath": "C:/Documents/contract.pdf"
}
示例响应:
{
"title": "Service Agreement 2025",
"author": "Legal Department",
"subject": "Service Terms & Conditions",
"creator": "Adobe InDesign",
"producer": "Adobe PDF Library",
"creationDate": "D:20250101090000Z",
"modificationDate": "D:20250110140000Z",
"keywords": "service, agreement, contract",
"totalPages": 8
}
3. read_pdf_pages
目的: 从特定页面或页面范围中提取文本。将其用于目标内容提取。
何时使用:
- 阅读文档的特定部分
- 分析特定章节或页面
- 在多部分文档中提取封面或特定报告
- 通过阅读部分处理大型PDF
输入参数:
{
"filePath": "string (required) - Absolute path to the PDF file",
"startPage": "number (required) - Starting page number (1-indexed)",
"endPage": "number (optional) - Ending page number. If omitted, defaults to startPage"
}
请求示例(单页):
{
"filePath": "C:/Documents/thesis.pdf",
"startPage": 1
}
请求示例(页面范围):
{
"filePath": "C:/Documents/thesis.pdf",
"startPage": 5,
"endPage": 12
}
示例响应:
{
"text": "Chapter 2: Literature Review\n\nThis chapter examines existing research...",
"startPage": 5,
"endPage": 12,
"totalPages": 45
}
4. search_pdf
目的: 在PDF中搜索文本,并检索与周围上下文的所有匹配项。
何时使用:
- 查找特定术语或短语
- 按关键字定位部分
- 验证内容是否存在
- 构建基于关键字的摘要
- 合规性检查(查找具体条款)
输入参数:
{
"filePath": "string (required) - Absolute path to the PDF file",
"searchTerm": "string (required) - Text to search for",
"caseSensitive": "boolean (optional, default: false) - Case-sensitive search"
}
请求示例:
{
"filePath": "C:/Documents/policy.pdf",
"searchTerm": "termination clause",
"caseSensitive": false
}
示例响应:
[
{
"page": 3,
"text": "termination clause",
"context": "...either party may invoke the termination clause without prior written notice...",
"position": 456
},
{
"page": 7,
"text": "termination clause",
"context": "...In accordance with Section 4.2, the termination clause becomes effective...",
"position": 1230
}
]
5. get_pdf_page_count
目的: 在不阅读内容的情况下获取PDF中的总页数。
何时使用:
- 验证PDF完整性
- 确定PDF是否为空
- 计划页面范围提取
- 基于文档大小的批处理逻辑
输入参数:
{
"filePath": "string (required) - Absolute path to the PDF file"
}
请求示例:
{
"filePath": "C:/Documents/manual.pdf"
}
示例响应:
{
"pageCount": 247
}
6. list_pdf_images
目的: 列出PDF中嵌入的所有图像及其元数据和位置。
何时使用:
- 提取前发现嵌入图像
- 获取图像尺寸和类型
- 规划图像提取操作
- 验证图像内容是否存在
输入参数:
{
"filePath": "string (required) - Absolute path to the PDF file"
}
请求示例:
{
"filePath": "C:/Documents/presentation.pdf"
}
示例响应:
{
"images": [
{
"index": 0,
"page": 2,
"name": "Image42",
"width": 800,
"height": 600,
"type": "JPEG"
},
{
"index": 1,
"page": 5,
"name": "Image43",
"width": 1024,
"height": 768,
"type": "PNG"
},
{
"index": 2,
"page": 8,
"name": "Image44",
"width": 640,
"height": 480,
"type": "TIFF"
}
]
}
7. extract_pdf_image
目的: 从PDF中提取特定图像,并将其作为Base64编码数据返回。
何时使用:
- 从PDF恢复图像
- 将PDF图像转换为标准格式
- 处理视觉内容以供分析
- 归档嵌入式图像
使用前: 呼叫 list_pdf_images 首先发现可用图像及其索引。
输入参数:
{
"filePath": "string (required) - Absolute path to the PDF file",
"imageIndex": "number (required) - Image index from list_pdf_images (0-indexed)"
}
请求示例:
{
"filePath": "C:/Documents/presentation.pdf",
"imageIndex": 0
}
示例响应:
{
"index": 0,
"page": 2,
"name": "Image42",
"width": 800,
"height": 600,
"type": "JPEG",
"data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
}
注: 图像数据采用Base64编码。解码以另存为文件(例如,使用 atob() JavaScript或 base64 -d 在bash中)。
MCP检验员测试
MCP检查器是一个用于测试和调试MCP服务器的交互式工具。它提供了一个基于web的UI来调用工具并实时检查响应。
安装
npm install -g @modelcontextprotocol/inspector
使用检查器运行服务器
-
在调试模式下启动MCP服务器:
mcp-pdf-reader -
在单独的终端中,启动检查器:
mcp-inspector node dist/index.js或者如果使用全局npm包:
mcp-inspector npx @rturv/mcp-pdf-reader -
打开web UI: 检查员将提供一个URL(通常
http://localhost:5173).在浏览器中打开它。
使用检查器
示例:从PDF中提取文本
- 在检查器UI中,找到
read_pdf左侧边栏中的工具 - 点击展开工具界面
- 输入参数:
{ "filePath": "C:/path/to/your/document.pdf", "includeMetadata": true } - 点击 “呼叫工具”
- 在右侧面板中查看响应
示例:搜索文本
- 选择
search_pdf工具 - 输入参数:
{ "filePath": "C:/path/to/your/document.pdf", "searchTerm": "important keyword", "caseSensitive": false } - 点击 “呼叫工具” 并结合上下文查看搜索结果
示例:提取图像
- 首先,打电话
list_pdf_images要查找图像:{ "filePath": "C:/path/to/your/document.pdf" } - 注意你想要的图像的索引(例如。,
index: 0) - 呼叫
extract_pdf_image根据该指数:{ "filePath": "C:/path/to/your/document.pdf", "imageIndex": 0 } - 响应将包括准备解码和保存的Base64编码图像数据
排查检查员问题
- 端口已在使用中: 更改端口
mcp-inspector --port 5174 - 连接被拒绝: 启动检查器之前,确保MCP服务器正在运行
- 工具未出现: 验证中的工具定义
src/index.ts并重建npm run build
发展
构建
npm run build
监视模式
npm run dev
运行测试
npm test
注: 测试需要一个PDF样本,网址为 test-files/sample.pdf。创建一个或跳过PDF相关测试。
观看测试
npm run test:watch
项目结构
mcp-pdf-reader/
├── src/
│ ├── index.ts # MCP server implementation & tool definitions
│ ├── pdf-tools.ts # Core PDF manipulation functions
│ ├── types.ts # TypeScript interfaces & types
│ └── __tests__/
│ └── pdf-tools.test.ts # Unit tests
├── dist/ # Compiled JavaScript (generated)
├── test-files/ # Test PDF files
├── package.json
├── tsconfig.json
└── README.md
技术栈
- @模型上下文协议/sdk (^1.25.2)-MCP协议实施
- pdf解析 (^2.4.5)-PDF文本提取
- pdf库 (^1.17.1)-PDF图像提取
- TypeScript (^5.9.3)-类型安全开发
- 开玩笑 (^29.7.0)-单元测试
局限性
- 无OCR: 仅从PDF中提取可选文本(非扫描图像)
- 基于文本的PDF: 适用于包含嵌入文本的PDF。没有OCR的扫描文档无法读取
- 图像提取: 仅限标准格式(JPEG、PNG、TIFF)
- Base64编码: 所有图像均以Base64字符串形式返回;大图像可能会导致大响应
- 无PDF修改: 此服务器是只读的;无法编辑或创建PDF
配置文件
- CLAUDE_CONFIG.md -Claude桌面配置指南
- COPYLOT_CONFIG.md -GitHub Copilot设置说明
许可证
MIT-有关详细信息,请参阅许可证文件
