文档处理服务器

MCP Docling Server是一个提供文档处理能力的服务器,支持文档转换、表格提取、批量处理和问答生成等功能。

  • by zanetworker
  • Python
  • 本地部署
  • 19 星标
  • 更新于
在 GitHub 查看 zanetworker/mcp-docling

使用说明

MCP文档服务器

使用文档库提供文档处理功能的MCP服务器。

安装

您可以使用pip安装该软件包:

pip install -e .

用法

使用stdio(默认)或SSE传输启动服务器:

# Using stdio transport (default)
mcp-server-lls

# Using SSE transport on custom port
mcp-server-lls --transport sse --port 8000

如果你使用的是uv,你可以直接运行服务器,而无需安装:

# Using stdio transport (default)
uv run mcp-server-lls

# Using SSE transport on custom port
uv run mcp-server-lls --transport sse --port 8000

可用工具

服务器公开了以下工具:

  1. convert_文档:将文档从URL或本地路径转换为markdown格式

    • source:文档的URL或本地文件路径(必需)
    • enable_ocr:是否为扫描的文档启用OCR(可选,默认:false)
    • ocr_language:OCR的语言代码列表,例如[“en”、“fr”](可选)
  2. convert_document_with_images:转换文档并提取嵌入图像

    • source:文档的URL或本地文件路径(必需)
    • enable_ocr:是否为扫描的文档启用OCR(可选,默认:false)
    • ocr_language:OCR语言代码列表(可选)
  3. 可提取表:从文档中提取表格作为结构化数据

    • source:文档的URL或本地文件路径(必需)
  4. convert_batch:以批处理模式处理多个文档

    • sources:文档的URL或文件路径列表(必填)
    • enable_ocr:是否为扫描的文档启用OCR(可选,默认:false)
    • ocr_language:OCR语言代码列表(可选)
  5. qna_from_document:从URL或本地路径创建YAML格式的问答文档

    • source:文档的URL或本地文件路径(必需)
    • no_of_qnas:预期的问答次数(可选,默认值:5)
    • 备注:此工具要求将IBM Watson X凭据设置为环境变量:
      • WATSONX_PROJECT_ID:您的Watson X项目ID
      • WATSONX_APIKEY:您的IBM Cloud API密钥
      • WATSONX_URL:Watson X API URL(默认值:https://us-south.ml.cloud.ibm.com)
  6. get_system_info:获取有关系统配置和加速状态的信息

带有火焰堆栈的示例

https://github.com/user-attachments/assets/8ad34e50-cbf7-4ec8-aedd-71c42a5de0a1

您可以将此服务器与 火焰堆栈 为您的LLM应用程序提供文档处理功能。确保您有一个正在运行的Llama Stack服务器,然后配置您的 INFERENCE_MODEL

from llama_stack_client.lib.agents.agent import Agent
from llama_stack_client.lib.agents.event_logger import EventLogger
from llama_stack_client.types.agent_create_params import AgentConfig
from llama_stack_client.types.shared_params.url import URL
from llama_stack_client import LlamaStackClient
import os

# Set your model ID
model_id = os.environ["INFERENCE_MODEL"]
client = LlamaStackClient(
    base_url=f"http://localhost:{os.environ.get('LLAMA_STACK_PORT', '8080')}"
)

# Register MCP tools
client.toolgroups.register(
    toolgroup_id="mcp::docling",
    provider_id="model-context-protocol",
    mcp_endpoint=URL(uri="http://0.0.0.0:8000/sse"))

# Define an agent with MCP toolgroup
agent_config = AgentConfig(
    model=model_id,
    instructions="""You are a helpful assistant with access to tools to manipulate documents.
Always use the appropriate tool when asked to process documents.""",
    toolgroups=["mcp::docling"],
    tool_choice="auto",
    max_tool_calls=3,
)

# Create the agent
agent = Agent(client, agent_config)

# Create a session
session_id = agent.create_session("test-session")

def _summary_and_qna(source: str):
    # Define the prompt
    run_turn(f"Please convert the document at {source} to markdown and summarize its content.")
    run_turn(f"Please generate a Q&A document with 3 items for source at {source} and display it in YAML format.")

def _run_turn(prompt):
    # Create a turn
    response = agent.create_turn(
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        session_id=session_id,
    )

    # Log the response
    for log in EventLogger().log(response):
        log.print()

_summary_and_qna('https://arxiv.org/pdf/2004.07606')

缓存

服务器将已处理的文档缓存在 ~/.cache/mcp-docling/ 以提高重复请求的性能。