Provides document-based question answering using OpenAI's GPT-4 for generating answers and text-embedding-3-small for semantic search, with strict adherence to document content to prevent hallucinations.
๐ 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.
๐ Live Demo
Web Interface: Start the server and visit http://localhost:8000
โก Quick Start
๐ฏ 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
The server consists of five main components:
DocumentLoader: Handles PDF, TXT, and Markdown file parsing
DocumentChunker: Intelligently splits documents into semantic chunks
EmbeddingStore: Manages vector embeddings for similarity search
QueryHandler: Processes questions and generates context-aware answers
MCPServer: Exposes MCP-compliant endpoints
๐ Usage Options
Option 1: Web Interface (Recommended)
Option 2: Interactive CLI
Option 3: Simple Version (No MCP)
Option 4: Run Tests
๐ฑ 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:
Response:
2. Ask Question
Ask a question about loaded documents.
Request:
Response:
3. Get Status
Check server status and loaded documents.
Request:
Response:
๐ Project Structure
๐ง Configuration
Chunking Parameters
Modify chunking behavior in DocumentChunker:
Retrieval Parameters
Adjust retrieval in QueryHandler.answer_question():
OpenAI Model Configuration
Change models in the respective methods:
๐จ 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:
Document Management: Add document metadata tracking
Source Filtering: Filter by specific documents
Cross-Document Search: Search across all loaded documents
Document Removal: Add endpoint to remove specific documents
Example extension:
โก 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-smallbalances 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
Fork the repository
Create a feature branch (
git checkout -b feature/amazing-feature)Commit your changes (
git commit -m 'Add amazing feature')Push to the branch (
git push origin feature/amazing-feature)Open a Pull Request
๐ Support
For issues and questions:
Check the error messages and logs
Verify OpenAI API key and quota
Ensure document formats are supported
Review the example usage patterns
Open an issue on GitHub
๐ Acknowledgments
OpenAI for GPT-4 and embedding models
Model Context Protocol for the MCP specification
Starlette for the web framework
scikit-learn for cosine similarity calculations