文档编辑管理服务

企业级文档编辑和管理服务器,支持MCP(模型上下文协议)和OpenAPI协议的DOCX文档操作。

  • by Fu-Jie
  • Python
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 Fu-Jie/MCP-OPENAPI-DOCX

使用说明

MCP-OPENAPI-DOCX

企业级文档编辑和管理服务器,支持用于DOCX文档的MCP(模型上下文协议)和OpenAPI协议。

🌟 特性

  • 完整的DOCX支持:完全支持阅读、写作和编辑Microsoft Word文档
  • MCP协议:为AI驱动的文档操作实施模型上下文协议
  • RESTful API:全面的符合OpenAPI的REST API和Swagger文档
  • 异步架构:采用FastAPI和async/await构建,实现高性能
  • 文档处理:段落、表格、列表、图像、样式等
  • 版本控制:跟踪文档版本并比较更改
  • 模板管理:创建和管理文档模板
  • 导出选项:导出为PDF、HTML、Markdown和纯文本
  • 评论和修订:添加评论并跟踪更改
  • 安全:文档加密、密码保护和权限
  • 后台任务:用于长时间运行的Celery集成
  • 容器化:Docker和Kubernetes就绪

📋 目录

🚀 安装

先决条件

  • Python 3.10或更高版本
  • Docker和Docker Compose(可选)
  • PostgreSQL(可选,SQLite可用于开发)
  • Redis(用于Celery任务)

使用pip和venv(推荐)

# Clone the repository
git clone https://github.com/Fu-Jie/MCP-OPENAPI-DOCX.git
cd MCP-OPENAPI-DOCX

# Create virtual environment
python -m venv venv

# Activate virtual environment
# On Linux/macOS:
source venv/bin/activate
# On Windows (Command Prompt):
venv\Scripts\activate.bat
# On Windows (PowerShell):
venv\Scripts\Activate.ps1

# Upgrade pip
pip install --upgrade pip

# Install dependencies
pip install -r requirements.txt

# Copy environment configuration
cp .env.example .env

# Run the application
python main.py

使用Docker

# Clone the repository
git clone https://github.com/Fu-Jie/MCP-OPENAPI-DOCX.git
cd MCP-OPENAPI-DOCX

# Start with Docker Compose
docker-compose up -d

# View logs
docker-compose logs -f api

🏃 快速开始

启动API服务器

# Method 1: Using main.py (Recommended)
python main.py

# Method 2: Using uvicorn directly
python -m uvicorn src.api.main:app --reload --port 8000

# Production mode with multiple workers
python -m uvicorn src.api.main:app --host 0.0.0.0 --port 8000 --workers 4

访问API

  • API文档: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc
  • OpenAPI架构: http://localhost:8000/openapi.json

启动MCP服务器

# Run MCP server (for AI integrations)
python main.py --mcp

📖 API文档

API为文档管理提供了全面的端点:

核心终点

端点 方法 描述
/api/v1/documents GET 列出所有文档
/api/v1/documents POST 创建新文档
/api/v1/documents/{id} GET 获取文档详细信息
/api/v1/documents/{id} PUT 更新文档
/api/v1/documents/{id} DELETE 删除文档

文本操作

端点 方法 描述
/api/v1/text/{doc_id}/paragraphs GET 列出段落
/api/v1/text/{doc_id}/paragraphs POST 添加段落
/api/v1/text/{doc_id}/paragraphs/{idx} PUT 更新段落
/api/v1/text/{doc_id}/format POST 应用格式

表操作

端点 方法 描述
/api/v1/tables/{doc_id} GET 列出表
/api/v1/tables/{doc_id} POST 创建表
/api/v1/tables/{doc_id}/{idx}/cell PUT 更新单元格
/api/v1/tables/{doc_id}/{idx}/row POST 添加行

查看完整 API文档 了解完整细节。

🔌 MCP协议

此服务器实现了用于AI集成的模型上下文协议:

可用工具

  • create_document -创建新的DOCX文档
  • open_document -打开现有文档
  • add_paragraph -在文档中添加段落
  • add_table -在文档中添加表格
  • format_text -应用文本格式
  • search_replace -搜索和替换文本
  • export_document -导出为各种格式
  • 还有80多种工具。..

示例用法

# Using MCP client
async with MCPClient("http://localhost:8000/mcp") as client:
    result = await client.call_tool(
        "create_document",
        {"name": "My Document"}
    )

看 MCP协议指南 详细用法。

⚙️ 配置

配置是通过环境变量进行管理的。复制 .env.example 到 .env:

cp .env.example .env

关键配置选项

变量 描述 默认值
DATABASE_URL 数据库连接URL sqlite+aiosqlite:///./docx_db.sqlite
REDIS_URL Redis连接URL redis://localhost:6379/0
SECRET_KEY JWT密钥 (生产中需要)
DEBUG 启用调试模式 false
ENVIRONMENT 环境名称 development
UPLOAD_DIR 上传目录 ./uploads
MAX_UPLOAD_SIZE 最大上传大小(字节) 104857600

看 .env.example 所有可用选项。

🛠️ 发展

项目结构

mcp-openapi-docx/
├── src/
│   ├── api/            # FastAPI application
│   │   ├── routes/     # API endpoints
│   │   └── dependencies.py
│   ├── core/           # Core configuration
│   ├── database/       # Database setup
│   ├── handlers/       # DOCX file handlers
│   ├── mcp/            # MCP server
│   ├── middleware/     # HTTP middleware
│   ├── models/         # Data models
│   ├── services/       # Business logic
│   ├── tasks/          # Celery tasks
│   └── utils/          # Utility functions
├── tests/              # Test suite
├── docs/               # Documentation
├── alembic/            # Database migrations
├── kubernetes/         # K8s configurations
└── scripts/            # Utility scripts

本地运行

# Activate virtual environment
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dev dependencies
pip install -r requirements.txt

# Run linting
ruff check src/
black --check src/
isort --check-only src/

# Run type checking
mypy src/

# Start development server
python main.py
# Or with auto-reload:
python -m uvicorn src.api.main:app --reload

数据库迁移

# Create a new migration
alembic revision --autogenerate -m "description"

# Apply migrations
alembic upgrade head

# Rollback
alembic downgrade -1

🧪 测试

# Run all tests
pytest

# Run with coverage
pytest --cov=src --cov-report=html

# Run specific test file
pytest tests/unit/handlers/test_document_handler.py

# Run integration tests
pytest tests/integration/ -v

🚢 部署

Docker Compose(推荐用于开发)

docker-compose up -d

Kubernetes

# Apply configurations
kubectl apply -f kubernetes/

# Check status
kubectl get pods -n docx

手动部署

# Build the image
docker build -t mcp-openapi-docx:latest .

# Run the container
docker run -p 8000:8000 mcp-openapi-docx:latest

看 部署指导 关于生产建议。

🤝 贡献

欢迎投稿!在提交pull请求之前,请阅读我们的投稿指南。

  1. 克隆该仓库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

📞 支持


由...制作❤️ 面向文档自动化社区