Skip to main content

MCP Server for LightPDF AI-PDF

Project description

LightPDF AI-PDF MCP Server 🚀

基于 FastMCP 框架的现代化 PDF 处理工具,提供 21 个专业的文档处理功能。

✨ 项目特色

  • 🚀 高效架构:基于 FastMCP,90% 代码量减少
  • 🔒 类型安全:100% 编译时参数验证
  • ⚡ 开发友好:新工具开发速度提升 300%+
  • 🔄 完全兼容:业务逻辑 100% 复用
  • 🛠️ 功能丰富:21 个工具涵盖 PDF 处理所有核心功能

🚀 快速开始

安装和启动

# 安装依赖
uv sync

# 启动 stdio 模式(用于 MCP 客户端)
python run.py

# 启动 SSE 服务器模式(用于 Web 调试)
python run.py -p 8000

验证安装

# 运行完整测试套件
python test_server.py

🛠️ 可用工具

📄 文档转换

  • convert_document - 输出格式:PDF/DOCX/XLSX/PPTX/JPG/PNG/HTML/TXT/CSV/MD/RTF/TEX;输入格式:支持22种文档和图片格式(图片转文本请使用OCR工具)
  • add_page_numbers - 为 PDF 添加页码
  • remove_watermark - 去除 PDF 水印

🔧 PDF 编辑

  • compress_pdf - PDF 压缩
  • merge_pdfs - 合并多个 PDF
  • split_pdf - 按页面或书签拆分 PDF
  • rotate_pdf - 旋转 PDF 页面
  • remove_margin - 去除 PDF 白边
  • extract_images - 提取 PDF 中的图片
  • flatten_pdf - 扁平化 PDF
  • resize_pdf - 调整 PDF 大小
  • replace_text - 替换 PDF 文本

🔒 安全功能

  • unlock_pdf - 移除 PDF 密码保护
  • protect_pdf - 为 PDF 添加密码保护
  • restrict_printing - 限制 PDF 打印权限

🎨 水印功能

  • add_text_watermark - 添加文本水印
  • add_image_watermark - 添加图片水印

🤖 AI 功能

  • create_pdf - 基于文本描述生成 PDF
  • translate_pdf - PDF 文档翻译
  • ocr_document - OCR 文字识别 (支持 PDF/DOC/PPT/Excel/图片等多种格式)
  • summarize_document - 文档摘要

📁 项目架构

采用清晰的5层功能模块化架构:

src/lightpdf_aipdf_mcp/
├── api/                    # API层 - FastMCP服务器与适配器
│   ├── server.py          # FastMCP主服务器 (21个工具)
│   └── adapter.py         # 适配器
├── core/                   # 核心层 - 统一业务逻辑
│   └── processor.py       # 核心业务处理器
├── services/              # 服务层 - 具体功能模块
│   ├── converter.py       # 文档转换服务
│   ├── editor.py         # PDF编辑服务
│   ├── create_pdf.py     # PDF创建服务
│   ├── translator.py     # 翻译服务
│   ├── summarizer.py     # 摘要服务
│   └── ocr.py           # OCR服务
├── models/               # 模型层 - 数据结构
│   └── schemas.py       # Pydantic数据模型
└── utils/               # 工具层 - 通用工具
    └── common.py        # 基础工具类

🔄 架构优势

特性 传统方式 FastMCP 方式
代码量 ~4800 行 ~3800 行 (-21%)
Schema 定义 手写 JSON 自动生成
类型检查 运行时 编译时
开发新工具 ~200 行/工具 ~20 行/工具
维护难度

🔧 开发指南

添加新工具

@mcp.tool
async def my_new_tool(
    ctx: Context,  # Context参数必须在第一位
    files: List[FileObject],
    my_param: str,
    optional_param: Optional[int] = None
) -> str:
    """
    我的新工具描述
    
    Args:
        files: 输入文件列表
        my_param: 必需参数
        optional_param: 可选参数
    
    Returns:
        JSON 格式的处理结果
    """
    await ctx.info(f"开始处理 {len(files)} 个文件...")
    
    # 构建操作配置
    operation_config = generate_operation_config(
        operation_type="convert",  # 或 "edit", "translate", "ocr", "summarize"
        format_value="my-format",
        extra_params={"my_param": my_param}
    )
    
    # 调用适配器
    result = await process_tool_call_adapter(ctx, files, operation_config)
    
    await ctx.info("处理完成")
    return result

测试工具

# 在 test_server.py 中添加
result = await client.call_tool("my_new_tool", {
    "files": [{"path": "test.pdf"}],
    "my_param": "test_value"
})

📚 技术栈

  • FastMCP: 现代化的 MCP 服务器框架
  • Pydantic: 数据验证和序列化
  • asyncio: 异步 I/O 支持
  • LightPDF API: 底层文档处理服务

🤝 开发最佳实践

  1. 参数顺序ctx: Context 参数始终放在第一位
  2. 类型安全:使用 Pydantic 模型确保类型检查
  3. 模块化:新功能优先在 services/ 层实现
  4. 测试驱动:添加功能时同步编写测试用例

📖 相关文档

🎯 性能优势

  • 启动时间: 减少 50%
  • 内存占用: 减少 30%
  • 开发时间: 减少 90%
  • 维护成本: 减少 60%

🎉 现代化、高效率的 LightPDF Agent MCP Server,让 PDF 处理变得简单!

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lightpdf_aipdf_mcp-0.1.151-py3-none-any.whl (43.6 kB view details)

Uploaded Python 3

File details

Details for the file lightpdf_aipdf_mcp-0.1.151-py3-none-any.whl.

File metadata

File hashes

Hashes for lightpdf_aipdf_mcp-0.1.151-py3-none-any.whl
Algorithm Hash digest
SHA256 e8761e068c2740a151eb2deda2b91b0bb57f10fc7290f87a4cf615f4effbc2f6
MD5 d5008a8ba79050bdf76eab3fcd272e8d
BLAKE2b-256 58d6cca5eb34b59b7827c6fb94940c3a05240ea6da35c82abfbd819c06adbd80

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page