Document Q&A MCP Server

Document Q&A MCP Server

A Python-based MCP server that enables document-based question answering by processing PDF, TXT, and Markdown files through OpenAI's API. It provides hallucination-free responses based strictly on document content using semantic search and includes a web interface for management.

Category
访问服务器

README

🚀 Document Q&A MCP Server

A Python-based Model Context Protocol (MCP) server that provides document-based question answering using OpenAI's API. Upload documents, ask questions, and get answers based strictly on document content with zero hallucinations.

Python 3.8+ License: MIT OpenAI

🌟 Live Demo

Web Interface: Start the server and visit http://localhost:8000

Document Q&A Demo

⚡ Quick Start

# 1. Install dependencies
pip install -r requirements.txt

# 2. Set your OpenAI API key
export OPENAI_API_KEY="your-api-key-here"

# 3. Start the web server
python web_server.py

# 4. Open http://localhost:8000 in your browser
# 5. Upload a document and start asking questions!

🎯 Features

  • 📤 Web File Upload: Drag & drop PDF, TXT, Markdown files
  • 🤖 Smart Q&A: GPT-4 powered answers based strictly on your documents
  • 🔍 Semantic Search: OpenAI embeddings with cosine similarity
  • 🚫 Zero Hallucinations: Only answers from document content
  • 📊 Real-time Dashboard: Live status, confidence scores, source attribution
  • 🏗️ MCP Compliant: Standard protocol for AI integration
  • ⚡ Production Ready: Error handling, logging, async support

🏛️ Architecture

  • Multi-format Support: PDF, TXT, and Markdown files
  • Intelligent Chunking: Semantic document splitting with overlap
  • Vector Search: OpenAI embeddings with cosine similarity
  • Hallucination Prevention: Strict adherence to document content
  • MCP Compliant: Standard protocol endpoints
  • Production Ready: Clean architecture with error handling

Architecture

┌─────────────────┐    HTTP/Upload    ┌─────────────────┐    MCP Protocol    ┌─────────────────┐
│   Web Browser   │ ◄────────────────► │   Web Server    │ ◄─────────────────► │ Document Q&A    │
│                 │                    │                 │                    │   MCP Server    │
│  • File Upload  │                    │  • File Handling│                    │                 │
│  • Q&A Interface│                    │  • HTTP Endpoints│                    │  ┌───────────┐  │
│  • Results      │                    │  • JSON API     │                    │  │DocumentLoader│  │
└─────────────────┘                    └─────────────────┘                    │  └───────────┘  │
                                                                              │  ┌───────────┐  │
                                                                              │  │ Chunker   │  │
                                                                              │  └───────────┘  │
                                                                              │  ┌───────────┐  │
                                                                              │  │Embedding  │  │
                                                                              │  │  Store    │  │
                                                                              │  └───────────┘  │
                                                                              │  ┌───────────┐  │
                                                                              │  │  Query    │  │
                                                                              │  │ Handler   │  │
                                                                              │  └───────────┘  │
                                                                              └─────────────────┘

The server consists of five main components:

  1. DocumentLoader: Handles PDF, TXT, and Markdown file parsing
  2. DocumentChunker: Intelligently splits documents into semantic chunks
  3. EmbeddingStore: Manages vector embeddings for similarity search
  4. QueryHandler: Processes questions and generates context-aware answers
  5. MCPServer: Exposes MCP-compliant endpoints

🚀 Usage Options

Option 1: Web Interface (Recommended)

python web_server.py
# Visit http://localhost:8000

Option 2: Interactive CLI

python interactive_client.py

Option 3: Simple Version (No MCP)

python simple_document_qa.py  
# Visit http://localhost:8001

Option 4: Run Tests

python test_server.py

📱 Web Interface Features

  • 📤 File Upload: Click "Choose File" or drag & drop documents
  • ❓ Question Input: Type questions in the text area
  • 📊 Live Dashboard: Real-time status and document info
  • 🎯 Confidence Scores: See how confident the AI is in each answer
  • 📚 Source Attribution: Know exactly which document parts were used
  • ⚡ Real-time Processing: Instant feedback and results

📡 MCP Endpoints

1. Load Document

Load a document into the system for question answering.

Request:

{
  "method": "load_document",
  "params": {
    "file_path": "/path/to/document.pdf"
  }
}

Response:

{
  "status": "success",
  "message": "Successfully loaded document: /path/to/document.pdf",
  "metadata": {
    "file_path": "/path/to/document.pdf",
    "content_length": 15420,
    "num_chunks": 12,
    "total_chunks_in_store": 12
  }
}

2. Ask Question

Ask a question about loaded documents.

Request:

{
  "method": "ask_question",
  "params": {
    "question": "What are the main features?"
  }
}

Response:

{
  "status": "success",
  "question": "What are the main features?",
  "answer": "Based on the document, the main features include...",
  "sources": [
    {
      "file": "/path/to/document.pdf",
      "chunk_id": "document_0",
      "similarity_score": 0.892
    }
  ],
  "confidence": 0.892
}

3. Get Status

Check server status and loaded documents.

Request:

{
  "method": "get_status",
  "params": {}
}

Response:

{
  "status": "active",
  "loaded_documents": ["/path/to/document.pdf"],
  "total_chunks": 12,
  "supported_formats": [".pdf", ".txt", ".md", ".markdown"]
}

📁 Project Structure

document-qa-mcp-server/
├── 📄 document_qa_server.py      # Main MCP server implementation
├── 🌐 web_server.py              # Web interface with file upload
├── 🖥️  simple_document_qa.py     # Simplified version (no MCP)
├── 💬 interactive_client.py      # CLI interface
├── 🧪 test_server.py             # Test suite
├── 📖 example_usage.py           # Usage examples
├── 📋 requirements.txt           # Dependencies
├── 📚 MCP_SERVER_DOCUMENTATION.md # Complete MCP guide
├── 🎨 web_interface.py           # Static HTML generator
└── 📄 README.md                  # This file

🔧 Configuration

Chunking Parameters

Modify chunking behavior in DocumentChunker:

chunker = DocumentChunker(
    chunk_size=1000,  # Target chunk size in characters
    overlap=200       # Overlap between chunks
)

Retrieval Parameters

Adjust retrieval in QueryHandler.answer_question():

similar_chunks = await self.embedding_store.search_similar(
    question, 
    top_k=3  # Number of chunks to retrieve
)

OpenAI Model Configuration

Change models in the respective methods:

# Embeddings model
model="text-embedding-3-small"

# Chat completion model  
model="gpt-4"

🚨 Error Handling

The server handles common errors gracefully:

  • File not found: Clear error with file path
  • Unsupported format: Lists supported formats
  • API errors: Returns OpenAI error messages
  • No documents loaded: Prompts to load documents first
  • Missing information: Returns "The document does not contain this information"

🔮 Extending for Multiple Documents

The current architecture supports multiple documents. To extend:

  1. Document Management: Add document metadata tracking
  2. Source Filtering: Filter by specific documents
  3. Cross-Document Search: Search across all loaded documents
  4. Document Removal: Add endpoint to remove specific documents

Example extension:

async def remove_document(self, file_path: str) -> Dict[str, Any]:
    """Remove a specific document from the store."""
    self.embedding_store.chunks = [
        chunk for chunk in self.embedding_store.chunks 
        if chunk.source_file != file_path
    ]
    # Rebuild embeddings matrix...

⚡ Performance Considerations

  • Chunk Size: Larger chunks = more context but slower search
  • Overlap: More overlap = better context continuity but more storage
  • Top-K: More retrieved chunks = better context but higher API costs
  • Embedding Model: text-embedding-3-small balances cost and quality

🔒 Security Notes

  • Store API keys securely (environment variables, secrets management)
  • Input validation implemented for file paths and parameters
  • Consider rate limiting for production deployments
  • Sanitize file paths to prevent directory traversal

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📞 Support

For issues and questions:

  1. Check the error messages and logs
  2. Verify OpenAI API key and quota
  3. Ensure document formats are supported
  4. Review the example usage patterns
  5. Open an issue on GitHub

🙏 Acknowledgments

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选