PDF MCP Server
Enables AI-powered querying of PDF documents using hybrid retrieval (BM25 + vector search) and retrieval-augmented generation, returning structured answers with source citations and confidence scores.
README
PDF Retrieval MCP Server
A completely free Model Context Protocol (MCP) server for retrieving relevant chunks from PDF documents using hybrid search (BM25 + Vector Search).
🚀 Features
- PDF Document Processing: Automatic parsing and indexing of PDF files using Docling
- Hybrid Retrieval: Combines BM25 (keyword) and vector search (semantic) for accurate retrieval
- Free Embeddings: Uses ChromaDB's default sentence-transformers (no API costs!)
- Pure Retrieval Mode: Returns raw document chunks for agent processing (no LLM answer generation)
- Fresh Start: Clears vector database on each startup for clean indexing
- MCP Integration: Exposes
retrieve_pdf_chunkstool via FastMCP for seamless agent integration
📋 Prerequisites
- Python 3.11 or later
- PDF documents to index
- No API keys required! ✨
🛠️ Installation
1. Clone the Repository (if not already done)
git clone <repository-url>
cd pdf_mcpserver
2. Install Dependencies with uv
uv sync
This will automatically:
- Create a virtual environment (
.venv) - Install all dependencies from
pyproject.toml - Set up the project
3. Add PDF Documents
Create a documents directory and add your PDF files:
mkdir documents
# Copy your PDF files to the documents/ directory
That's it! No API keys or additional configuration needed.
🎯 Usage
Running the Server
uv run python main.py
Or activate the virtual environment first:
source .venv/bin/activate # On Windows: .venv\Scripts\activate
python main.py
The server will:
- Start immediately (lazy initialization)
- Load and index PDFs on first query
- Be ready to retrieve document chunks via MCP
Using the retrieve_pdf_chunks Tool
The server exposes a single MCP tool: retrieve_pdf_chunks(query: str, max_chunks: int = 5) -> str
Example Query:
retrieve_pdf_chunks("machine learning algorithms", max_chunks=3)
Example Response:
{
"query": "machine learning algorithms",
"chunks": [
{
"content": "Machine learning algorithms can be categorized into supervised, unsupervised, and reinforcement learning...",
"document_name": "ml_guide.pdf",
"page_number": 12,
"metadata": {"source": "ml_guide.pdf"}
},
{
"content": "Common supervised learning algorithms include linear regression, decision trees, and neural networks...",
"document_name": "ml_guide.pdf",
"page_number": 15,
"metadata": {"source": "ml_guide.pdf"}
}
],
"total_chunks": 2
}
Response Structure
| Field | Type | Description |
|---|---|---|
query |
string | The original search query |
chunks |
array | List of relevant document chunks |
chunks[].content |
string | The text content of the chunk |
chunks[].document_name |
string | Source PDF filename |
chunks[].page_number |
int | Page number (if available) |
chunks[].metadata |
object | Additional metadata |
total_chunks |
int | Number of chunks returned |
How Agents Use This
When an agent (like Claude) calls this tool:
- Agent sends a search query
- Server returns relevant document chunks
- Agent uses chunks in its context to answer questions
Example Agent Flow:
User: "What are the main ML algorithms discussed?"
↓
Agent calls: retrieve_pdf_chunks("machine learning algorithms")
↓
Server returns: 3 relevant chunks from PDFs
↓
Agent reads chunks and generates answer for user
🔍 Testing with MCP Inspector
The MCP Inspector is a web-based tool for testing and debugging MCP servers interactively.
Running the Inspector
npx @modelcontextprotocol/inspector uv run python main.py
This command will:
- Start the MCP Inspector proxy server
- Launch your PDF Retrieval Server
- Open a web browser with the Inspector UI
What You'll See
The Inspector provides:
- Tool Discovery: View available tools (
retrieve_pdf_chunks) - Interactive Testing: Test queries with custom parameters
- Real-time Responses: See JSON responses in real-time
- Request/Response Logs: Debug the MCP protocol communication
Example Inspector Workflow
- Open the Inspector - Browser opens automatically at
http://localhost:6274 - Wait for Initialization - Server loads and indexes PDFs on first query (~1-2 minutes)
- Select Tool - Click on
retrieve_pdf_chunksin the tools list - Enter Query - Type your search query (e.g., "machine learning")
- Set Parameters - Optionally adjust
max_chunks(default: 5) - Execute - Click "Run" to see the results
- View Response - Inspect the returned chunks and metadata
Inspector Tips
- First query is slow: PDF indexing happens on first query (87 seconds for typical PDFs)
- Subsequent queries are fast: Embeddings are cached in ChromaDB
- Fresh start: Server clears ChromaDB on each restart for clean indexing
- Check logs: Terminal shows detailed logging of the indexing process
🏗️ Architecture
pdf_mcpserver/
├── src/
│ ├── config.py # Configuration management
│ ├── constants.py # Configuration constants
│ ├── models.py # Pydantic response models
│ ├── pdf_processor.py # PDF loading and hybrid retrieval
│ └── retrieval_handler.py # Document chunk retrieval
├── main.py # MCP server entry point
├── pyproject.toml # Project metadata
└── .env # Environment configuration
Key Components
- PDFProcessor: Singleton class that loads PDFs, converts to Markdown using Docling, and builds hybrid retriever (BM25 + Vector Search)
- RetrievalHandler: Retrieves relevant chunks for queries - no LLM answer## 🔧 Configuration
Configuration is managed through environment variables. Create a .env file in the project root:
# Optional: PDF Documents Directory (defaults to ./documents)
PDF_DOCUMENTS_DIR=./documents
# Optional: ChromaDB Directory (defaults to ./chroma_db)
CHROMA_DB_DIR=./chroma_db
# Optional: Log Level (defaults to INFO)
LOG_LEVEL=INFO
Configuration Options
| Variable | Required | Default | Description |
|---|---|---|---|
PDF_DOCUMENTS_DIR |
No | ./documents |
Directory containing PDF files to index |
CHROMA_DB_DIR |
No | ./chroma_db |
Directory for ChromaDB vector storage |
LOG_LEVEL |
No | INFO |
Logging level (DEBUG, INFO, WARNING, ERROR) |
Note: No API keys required! ChromaDB uses free local embeddings (sentence-transformers).
🧪 Testing
Run unit tests:
uv run pytest tests/
📝 Troubleshooting
No PDF files found
Error: No PDF files found in ./documents
Solution: Add PDF files to the documents/ directory or update PDF_DOCUMENTS_DIR in .env
Import errors
Error: ModuleNotFoundError: No module named 'docling'
Solution: Ensure all dependencies are installed: uv sync
CUDA out of memory
Error: CUDA out of memory
Solution: The server is configured to use CPU-only mode. If you still see this error, check that CUDA_VISIBLE_DEVICES="" is set in src/pdf_processor.py
📚 Dependencies
- fastmcp: MCP server framework
- docling: Document processing and parsing
- chromadb: Vector database with free sentence-transformers embeddings
- langchain: RAG framework and retrievers
- loguru: Logging
No paid APIs required! All embeddings are generated locally using ChromaDB's default model (all-MiniLM-L6-v2).
🤝 Contributing
This is a Proof of Concept (PoC) implementation. For production use, consider:
- Adding caching for processed documents
- Implementing multi-agent workflow with fact verification
- Supporting additional document formats (DOCX, TXT, etc.)
- Adding authentication and rate limiting
📄 License
[Your License Here]
🙏 Acknowledgments
Based on the docchat-docling architecture.
推荐服务器
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 模型以安全和受控的方式获取实时的网络信息。