MCP Document Processor

by arifazim
GPL 3.0

Integrations

  • Used for API communication between the frontend and backend, enabling efficient HTTP requests for document processing workflows.

  • Integrates with the UI for data visualization of document statistics, providing graphical representation of document processing metrics.

  • Powers the REST interface for the document processing system, enabling API-based interactions with the document processing capabilities.

MCP文档处理器

使用模型上下文协议 (MCP) 自动提取、分析和路由业务文档的智能文档处理系统。

项目概述

该项目演示了如何使用 MCP 解决实际业务挑战:自动化文档处理工作流程。该系统可以:

  • 对收到的文件(发票、合同、电子邮件)进行分类
  • 使用机器学习模型提取相关信息
  • 根据文档类型进行处理 在整个处理流程中维护上下文 通过 REST API 公开功能

关键 MCP 组件

  • 上下文对象:MCP 的核心,这些对象(在MCPContext中实现)在处理步骤之间传递信息并维护文档的状态。
  • 内存系统:存储处理步骤之间的上下文对象,并带有可插入的后端。
  • 协议:为处理器和模型定义清晰的接口,确保模块化。
  • 路由器:根据内容智能地将文档路由到专门的处理器。

商业价值

该解决方案解决了几个业务挑战:

  • 减少手动处理:自动从文档中提取数据
  • 一致性:确保跨文档类型的一致处理
  • 可审计性:维护处理历史和置信度分数
  • 可扩展性:模块化设计允许轻松添加新的文档类型

技术亮点

  • 使用基于 BERT 的模型进行分类和实体提取
  • T5 文档摘要模型
  • 用于 REST 接口的 FastAPI
  • 可插拔架构,易于扩展
  • 全面的日志记录和错误处理
  • 基于 React 的 UI 可提供更好的用户体验

概述

MCP 文档处理器旨在解决以一致且自动化的方式处理各种类型文档(发票、合同、电子邮件等)的常见业务挑战。它利用模型上下文协议框架来管理系统不同组件之间的信息流。

主要特点

  • 文档分类:自动识别文档类型
  • 信息提取:从文档中提取关键信息
  • 文档路由:将文档路由到适当的处理器
  • 上下文管理:在整个处理流程中维护上下文
  • API 接口:提供 RESTful API 以便与其他系统集成

建筑学

该系统围绕模型上下文协议 (MCP) 构建,该协议提供:

  1. 上下文对象:跨处理步骤传递信息
    # Example of MCPContext usage context = MCPContext( document_id=document_id, raw_text=text, metadata=metadata ) # Adding extracted data with confidence scores context.add_extracted_data("invoice_number", "INV-12345", confidence=0.95) # Tracking processing history context.add_to_history( processor_name="InvoiceProcessor", status="completed", details={"processing_time": "0.5s"} )
  2. 内存系统:存储 API 调用之间的上下文对象
    # Storing context in memory memory.store(document_id, context) # Retrieving context from memory context = memory.retrieve(document_id)
  3. 协议:定义处理器和模型的接口
    # Processor protocol example class Processor(Protocol): @abstractmethod def process(self, context: MCPContext) -> MCPContext: """Process the document and update the context.""" pass @abstractmethod def can_handle(self, context: MCPContext) -> bool: """Determine if this processor can handle the given document.""" pass
  4. 路由器:将文档路由到适当的专用处理器
    # Router usage example processor = processor_router.route(context) if processor: processed_context = processor.process(context)

MCP流程图

Document Upload → MCPContext Creation → Memory Storage → Document Processing → Router Selection → Specialized Processor → Entity Extraction → Context Update → Memory Storage → API Response

MCP 实现细节

该项目中的模型上下文协议实现具有几个主要优势:

1. 具有上下文持久性的状态处理

MCPContext类在整个文档处理生命周期中维护状态:

# Context is created during document upload @router.post("/documents/upload") async def upload_document(file: UploadFile, memory: MemoryInterface): # Create a context context = MCPContext( document_id=document_id, raw_text=text, metadata=metadata ) # Store in memory for later retrieval memory.store(document_id, context)

2. 可插拔内存系统

内存系统设计为可插拔的,允许不同的存储后端:

# Factory function in memory.py def get_memory_store(memory_type: str = "in_memory", **kwargs) -> MemoryInterface: if memory_type == "in_memory": return InMemoryStorage(default_ttl=kwargs.get("ttl", 3600)) # Additional implementations can be added here

3. 信心追踪

MCP 跟踪所有提取数据的置信度分数,从而实现更好的决策:

# In entity_extractor.py entity_data = { "text": text[current_entity["start"]:current_entity["end"]], "start": current_entity["start"], "end": current_entity["end"], "confidence": avg_confidence }

4. 处理历史记录

每个处理步骤都记录在上下文的历史记录中,提供可审计性:

# In router.py context.add_to_history( processor_name=processor.__class__.__name__, status="completed" )

5.智能文档路由

ProcessorRouter为每个文档确定适当的处理器:

# In router.py def route(self, context: MCPContext) -> Optional[Processor]: for processor in self.processors: if processor.can_handle(context): return processor return None

6.可扩展性

通过实现Processor协议,可以轻松添加新的文档类型:

# Example of adding a new processor class NewDocumentProcessor(BaseProcessor): def can_handle(self, context: MCPContext) -> bool: # Logic to determine if this processor can handle the document pass def process(self, context: MCPContext) -> MCPContext: # Document processing logic pass

文件处理器

该系统包括针对不同文档类型的专用处理器:

  • 发票处理器:提取供应商、客户、明细项目、总计等。
  • 合同处理器:提取当事人、关键日期、条款等。
  • 电子邮件处理器:提取发件人、收件人、主题、正文等。

机器学习模型

有几种 ML 模型可用于不同的任务:

  • 文档分类器:基于 BERT 的文档类型分类模型
  • 实体提取器:用于提取关键信息的命名实体识别模型
  • Summarizer :基于T5的文档摘要生成模型

用户界面

MCP 文档处理器包含一个基于 React 的现代用户界面,该界面提供了与文档处理系统直观的交互方式。该 UI 使用 Material-UI 构建,并提供以下功能:

UI 功能

  • 仪表板:已处理文档的概览,包含统计数据并可快速访问文档详细信息
  • 文档上传:用于上传新文档的拖放界面
  • 文档处理:处理文档的分步工作流程
  • 文档查看器:查看已处理文档的详细视图以及提取的信息
  • 处理历史:所有处理步骤的时间线视图,方便审计

UI架构

前端构建如下:

  • React :用于构建用户界面组件
  • Material-UI :一致的响应式设计
  • React Router :用于不同视图之间的导航
  • Axios :用于与后端进行 API 通信
  • Chart.js :用于文档统计的数据可视化

UI-后端集成

前端通过 RESTful API 与后端通信,主要端点如下:

  • GET /api/documents :检索所有文档
  • POST /api/documents/upload :上传新文档
  • POST /api/documents/{document_id}/process :处理文档
  • GET /api/documents/{document_id} :获取文档详细信息
  • DELETE /api/documents/{document_id} :删除文档

完整的系统架构

MCP 文档处理器遵循分层架构,集成了前端、API 层、处理组件和机器学习模型:

┌─────────────────────────────────────────────────────────────────────────┐ │ Frontend Layer │ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │ │ │ Dashboard │ │ Upload │ │ Document Viewer │ │ │ └─────────────┘ └─────────────┘ └─────────────────────────┘ │ │ │ │ │ │ └──────────┼───────────────────┼─────────────────────────┼────────────────┘ │ │ │ │ │ │ ▼ ▼ ▼ ┌─────────────────────────────────────────────────────────────────────────┐ │ API Layer │ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │ │ │ Document │ │ Document │ │ Document │ │ │ │ Upload API │ │ Process API │ │ Retrieval API │ │ │ └─────────────┘ └─────────────┘ └─────────────────────────┘ │ │ │ │ │ │ └──────────┼───────────────────┼─────────────────────────┼────────────────┘ │ │ │ │ │ │ ▼ ▼ ▼ ┌─────────────────────────────────────────────────────────────────────────┐ │ MCP Core Components │ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │ │ │ MCPContext │◄────►│ Memory │◄────►│ Processor Router │ │ │ └─────────────┘ └─────────────┘ └─────────────────────────┘ │ │ │ │ │ └──────────┼────────────────────────────────────────────┼─────────────────┘ │ │ │ │ ▼ ▼ ┌─────────────────────────────────────────────────────────────────────────┐ │ Document Processors │ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │ │ │ Invoice │ │ Contract │ │ Email │ │ │ │ Processor │ │ Processor │ │ Processor │ │ │ └─────────────┘ └─────────────┘ └─────────────────────────┘ │ │ │ │ │ │ └──────────┼───────────────────┼─────────────────────────┼────────────────┘ │ │ │ │ │ │ ▼ ▼ ▼ ┌─────────────────────────────────────────────────────────────────────────┐ │ ML Models Layer │ │ │ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │ │ │ Document │ │ Entity │ │ Summarizer │ │ │ │ Classifier │ │ Extractor │ │ │ │ │ └─────────────┘ └─────────────┘ └─────────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────────┘

完整的工作流程

文档处理工作流程涉及系统组件的多个步骤:

  1. 文件上传
    • 用户通过 UI 上传文档
    • 前端将文档发送到后端 API
    • 后端使用文档元数据创建 MCPContext 对象
    • 上下文存储在记忆系统中
  2. 文件分类
    • 用户通过 UI 启动处理
    • 后端从内存中检索文档上下文
    • 文档分类器模型确定文档类型
    • 使用文档类型信息更新上下文
  3. 文件处理
    • 处理器路由器根据文档类型选择适当的处理器
    • 选定的处理器(发票、合同或电子邮件)处理文档
    • 处理器使用实体提取器识别关键信息
    • 提取的数据与置信度分数一起添加到上下文中
  4. 结果检索
    • 更新后的上下文存储回内存
    • UI检索并显示处理后的文档信息
    • 用户可以查看提取的数据、置信度分数和处理历史记录
  5. 审计与审查
    • 所有处理步骤都记录在上下文的处理历史记录中
    • UI 提供提取数据的置信度分数的可视化
    • 用户可以查看文档文本以及提取的信息

入门

先决条件

  • Python 3.8+
  • Node.js 14+ 和 npm(用于前端)
  • requirements.txt 中列出的依赖项

安装和设置

后端设置
  1. 克隆存储库
    git clone https://github.com/yourusername/mcp_document_processor.git cd mcp_document_processor
  2. 创建并激活虚拟环境
    python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
  3. 安装后端依赖项
    pip install -r requirements.txt
  4. 创建用于文档存储的数据目录(如果不存在)
    mkdir -p data
前端设置
  1. 导航到前端目录
    cd frontend
  2. 安装前端依赖项
    npm install

运行应用程序

启动后端服务器
  1. 从项目的根目录(激活虚拟环境):
    python app.py
    这将在http://localhost:8000上启动 FastAPI 服务器。
  2. 您可以通过http://localhost:8000/docs访问 API 文档
启动前端开发服务器
  1. 打开一个新的终端窗口/选项卡
  2. 导航到前端目录:
    cd /path/to/mcp_document_processor/frontend
  3. 启动 React 开发服务器:
    npm start
    这将在http://localhost:3000上启动前端。

使用应用程序

  1. 打开浏览器并导航至http://localhost:3000
  2. 使用侧边栏导航可以:
    • 查看仪表板
    • 上传新文件
    • 处理并查看文档详细信息
示例工作流程
  1. 上传文件
    • 点击侧边栏中的“上传文档”
    • 拖放文档(PDF、图像或文本文件)
    • 点击“上传文档”按钮
  2. 处理文件
    • 上传成功后,点击“处理文档”
    • 等待处理完成
  3. 查看结果
    • 查看提取的数据、置信度分数和处理历史记录
    • 导航到仪表板查看所有已处理的文档

API 使用

您还可以直接与 API 进行交互:

  • GET /api/documents :检索所有文档
  • POST /api/documents/upload :上传新文档
  • POST /api/documents/{document_id}/process :处理文档
  • GET /api/documents/{document_id} :获取文档详细信息
  • DELETE /api/documents/{document_id} :删除文档

扩展系统

添加新的文档处理器

  1. 创建一个继承自BaseProcessor新处理器类
  2. 实现can_handleprocess方法
  3. api/routes.py处理器添加到路由器

添加新模型

  1. 创建一个新的模型类,实现适当的协议
  2. config/config.yaml中添加配置
  3. 将模型与相关处理器集成

执照

MIT 许可证

Related MCP Servers

  • -
    security
    A
    license
    -
    quality
    A Model Context Protocol (MCP) server that enables semantic search and retrieval of documentation using a vector database (Qdrant). This server allows you to add documentation from URLs or local files and then search through them using natural language queries.
    Last updated -
    14
    74
    JavaScript
    Apache 2.0
    • Apple
  • -
    security
    A
    license
    -
    quality
    A tool for Model Context Protocol (MCP) that allows you to analyze web content and add it to your knowledge base, storing content as Markdown files for easy viewing with tools like Obsidian.
    Last updated -
    6
    Python
    MIT License
  • -
    security
    A
    license
    -
    quality
    A server that provides document processing capabilities using the Model Context Protocol, allowing conversion of documents to markdown, extraction of tables, and processing of document images.
    Last updated -
    6
    Python
    MIT License
    • Linux
    • Apple
  • A
    security
    A
    license
    A
    quality
    A Model Context Protocol (MCP) server that provides tools for reading Excel (xlsx) files, enabling extraction of data from entire workbooks or specific sheets with results returned in structured JSON format.
    Last updated -
    3
    2
    Python
    Apache 2.0
    • Linux
    • Apple

View all related MCP servers

ID: 3bjbgpxsfc