DocFill Agent MCP Server
An MCP server that enables LLM agents to read, understand, and fill DOCX templates while preserving formatting like bold labels and fonts. It exposes tools for document upload, AST inspection, editing, and validation.
README
DocFill Agent
An LLM agent that reads, understands, and fills DOCX templates — preserving bold labels, fonts, and structure.
Built on Agno + FastMCP + python-docx.
What it does
Paste meeting notes or raw clinical data into the agent. It:
- Parses the DOCX template into a structured AST (headings, paragraphs, table cells — each with a stable
element_idand its run-level formatting) - Exposes 5 MCP tools the agent can call:
upload_document,get_session_documents,load_document_ast,edit_document,validate_document_state - The LLM reads the AST, matches fields to content, and calls
edit_documentwith the rightelement_id - A run-level formatter re-applies the template's bold/italic labels around the new plain-text values — automatically
No HTML conversion. No regex scraping. Pure AST surgery.
Architecture
┌─────────────────────────────────────────────────────────┐
│ Agno Agent (Claude) │
│ system prompt: 5-step workflow │
│ tool calls ──────────────────────────────────────┐ │
└────────────────────────────────────────────────────│────┘
│ HTTP/MCP
┌────────────────────────────────────────────────────▼────┐
│ FastMCP Server (:8000) │
│ │
│ upload_document ──► converter.build_ast() │
│ load_document_ast ─► LocalDocumentStore.get_ast() │
│ edit_document ─────► converter.apply_cell_edit() │
│ + run_formatter.create_runs() │
│ validate_document_state ──► build_ast() comparison │
└─────────────────────────────────────────────────────────┘
│ python-docx read/write
┌────────▼────────────────────┐
│ DOCX template on disk │
│ + AST JSON (local store) │
└─────────────────────────────┘
Run-level formatting preservation
The core algorithmic challenge: the template has cells like
[bold] "Study Title: " [/bold][plain] "" [/plain]
The LLM produces "A Phase III randomised trial of Zetaribumab".
The formatter:
- Finds the longest common prefix between the new text and the original template text
- Maps that prefix to template runs (preserving bold)
- For remaining text, scans for known bold-label fragments and applies their formatting
- LLM-generated values always end up plain — no false bold
Quick start
# 1. Install
pip install -e ".[dev]"
# 2. Configure
cp .env.example .env
# → edit .env, add ANTHROPIC_API_KEY=sk-ant-...
# 3. Create the CSR template
python examples/csr_demo/create_template.py
# 4. Start the MCP server
uvicorn docfill.mcp.server:app --port 8000
# 5. Run the demo notebook
jupyter notebook examples/csr_demo/csr_fill_demo.ipynb
Project structure
docfill-agent/
├── src/
│ └── docfill/
│ ├── ast/
│ │ ├── models.py # DocumentAST, TextRun, TableCellElement, …
│ │ ├── converter.py # build_ast(), apply_cell_edit(), apply_heading_edit()
│ │ └── run_formatter.py # create_runs_from_template() — the formatting engine
│ ├── storage/
│ │ ├── local.py # LocalDocumentStore (filesystem-backed)
│ │ └── session.py # SessionRegistry (in-memory)
│ └── mcp/
│ └── server.py # FastMCP server — 5 tools
├── examples/
│ └── csr_demo/
│ ├── create_template.py # Generates the fictional CSR DOCX template
│ ├── csr_fill_demo.ipynb # End-to-end demo notebook
│ └── templates/ # csr_template.docx (generated by create_template.py)
├── pyproject.toml
├── .env.example
└── README.md
MCP tools
| Tool | Args | Description |
|---|---|---|
upload_document |
file_path, file_id? |
Parse DOCX → AST, register in session |
get_session_documents |
include_metadata? |
List uploaded documents |
load_document_ast |
file_id |
Return full element list with element_id, text, runs |
edit_document |
file_id, edits |
Apply cell/heading edits with formatting preservation |
validate_document_state |
file_id, create_new_version? |
Verify AST integrity, bump version |
edit_document edit format:
[
{
"type": "table_cell",
"element_id": "cell-t0-r1-c1",
"changes": { "text": "A Phase III randomised trial of Zetaribumab 150 mg SC Q4W" }
}
]
Replacing the LLM provider
The agent uses Agno's model abstraction. To switch providers:
# Anthropic (default)
from agno.models.anthropic import Claude
model = Claude(id="claude-sonnet-5")
# OpenAI
from agno.models.openai import OpenAIChat
model = OpenAIChat(id="gpt-4o")
# Any LiteLLM-compatible endpoint
from agno.models.litellm import LiteLLM
model = LiteLLM(id="bedrock/anthropic.claude-sonnet-4-5", api_key="...")
Extending to cloud storage
LocalDocumentStore mirrors the interface of an S3-backed store. To switch:
- Implement
store_file,store_ast,get_file,get_astwithboto3 - Pass the new store instance into
server.py
No changes to the converter or formatter needed.
Limitations & known gaps
apply_cell_editrebuilds paragraphs from scratch — complex nested tables with merged cells may lose merge state. (Works for standard clinical templates.)- The formatter heuristic works well for
Label: valuepatterns. Multi-column mixed formatting may need manual tuning. - The MCP server is single-process; the
SessionRegistryis in-memory and not shared across workers.
License
MIT
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。