Modular RAG System
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Modular RAG SystemWhat's the travel reimbursement limit for meals?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
๐ Modular RAG System
A Modular Retrieval-Augmented Generation (RAG) System with Adaptive Retrieval, Intent-Aware Query Processing, Semantic Query Rewriting, and MCP Server Integration
๐ Overview
Modern enterprise organizations store thousands of pages of policies, reports, technical documents, and internal knowledge bases. Traditional keyword search often fails when users ask questions using natural language, paraphrased wording, or indirect references.
This project implements a Modular Retrieval-Augmented Generation (RAG) System capable of answering natural language questions over enterprise documents while providing grounded, citation-backed responses.
Unlike conventional RAG systems that treat every query identically, this system introduces intent-aware routing, allowing different categories of questions to follow specialized processing pipelines. The architecture further improves retrieval quality through an Adaptive Retrieval Loop, which selectively invokes a local Phi-3 query rewriter only when retrieval confidence is low.
The system follows a layered design emphasizing retrieval quality, grounding, modularity, maintainability, and explainability.
๐ฏ Project Objectives
The primary objectives of this project are:
Build an MCP-compatible document question answering backend.
Support multiple enterprise documents simultaneously.
Generate grounded responses with citations.
Reduce hallucinations through evidence validation.
Improve semantic retrieval for paraphrased questions.
Keep the architecture modular for future scalability.
Maintain compatibility with local LLMs and cloud LLMs.
Optimize retrieval quality while minimizing unnecessary latency.
โจ Key Features
Intelligent Intent Routing
Instead of treating every user query identically, the system automatically classifies incoming questions into specialized pipelines.
Supported query categories include:
Factual Questions
Numeric Questions
Boolean Questions
Procedure Questions
Summary Requests
Comparison Questions
Multi-document Queries
Out-of-Scope Detection
Related MCP server: estudIA-MCP
Adaptive Retrieval
Retrieval is no longer a single-pass process.
When confidence is high:
User Query
โ
โผ
Retrieval
โ
โผ
AnswerWhen confidence is low:
User Query
โ
โผ
Initial Retrieval
โ
โผ
Confidence Evaluation
โ
โผ
Query Rewriter (Phi-3)
โ
โผ
Second Retrieval
โ
โผ
Quality Comparison
โ
โผ
Best Retrieval SelectedThis selective retrieval strategy improves semantic matching while avoiding unnecessary model execution.
Query Rewriting
Instead of maintaining thousands of manually curated synonym mappings, the system leverages a local Phi-3 model to rewrite ambiguous queries into document-friendly terminology.
Example:
Original Query
What's the daily food budget?Rewritten Query
What is the daily meal reimbursement limit during business travel?The rewritten query is only used if retrieval quality objectively improves.
Rewrite Validation
To prevent semantic drift, every rewritten query undergoes strict validation.
Validation rules include:
Number preservation
Currency preservation
Percentage preservation
Date preservation
Entity preservation
Acronym preservation
Logical operator preservation
Polarity preservation
Example:
Original
Can employees NOT claim cash gifts?Rejected Rewrite
Can employees claim cash gifts?The validator immediately rejects such rewrites.
Rewrite Cache
Repeated rewrites are avoided using an LRU cache.
Cache Key
Hash(
Normalized Query
+
Document Collection Hash
)Benefits
Reduced latency
Reduced Phi-3 execution
Deterministic behaviour
Automatic invalidation on document changes
๐ System Architecture
โโโโโโโโโโโโโโโโโโโโโโ
โ User โ
โโโโโโโโโโโฌโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MCP Server โ
โโโโโโโโโโโฌโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Intent Detection & Routing โ
โโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโ
โ โ
โผ โผ
Standard Pipeline Summary Pipeline
โ
โผ
Comparison / Numeric /
Boolean / Procedure etc.
โ
โผ
Adaptive Retrieval Layer
โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
Query Rewriter Validator Rewrite Cache
โ โ โ
โโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโ
โผ
Final Retrieval
โ
โผ
Evidence Validation
โ
โผ
Gemini 2.5 Flash Generator
โ
โผ
JSON Response + Citations๐ง Layered RAG Pipeline
Document Upload
โ
โผ
Document Parsing
โ
โผ
Chunk Generation
โ
โผ
Embedding Generation
โ
โผ
ChromaDB Storage
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Runtime
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
User Query
โ
โผ
Intent Detection
โ
โผ
Vector Retrieval
โ
โผ
Adaptive Retrieval (Optional)
โ
โผ
Evidence Validation
โ
โผ
LLM Generation
โ
โผ
Grounded JSON Response๐งฉ Core Components
Component | Responsibility |
MCP Server | Exposes the QA tool to MCP clients |
Intent Router | Routes queries to specialized pipelines |
Retriever | Retrieves relevant chunks from ChromaDB |
Query Rewriter | Improves low-confidence semantic retrieval |
Rewrite Validator | Prevents semantic drift |
Rewrite Cache | Avoids repeated rewrites |
Evidence Validator | Ensures answer grounding |
Gemini Generator | Produces grounded final responses |
Phi-3 | Local semantic query optimization |
๐ Project Structure
modular-rag-system/
โ
โโโ data/
โ โโโ company_policy.txt
โ โโโ conduct_policy.txt
โ โโโ finance_policy.txt
โ โโโ it_policy.pdf
โ โโโ remote_work_agreement.pdf
โ
โโโ src/
โ โโโ answer_question.py
โ โโโ mcp_server.py
โ โโโ query_rewriter.py
โ โโโ retrieve.py
โ โโโ embed_document.py
โ โโโ chunk_document.py
โ โโโ read_document.py
โ โโโ evidence_engine.py
โ โโโ layer2_validator.py
โ โโโ store_embeddings.py
โ โโโ verify_storage.py
โ โโโ config.py
โ โโโ test_acceptance_suite.py
โ โโโ test_query_rewriter.py
โ โโโ ...
โ
โโโ requirements.txt
โโโ README.md
โโโ .gitignoreโ Technology Stack
Category | Technology |
Language | Python |
Vector Database | ChromaDB |
Embedding Model | all-MiniLM-L6-v2 |
Primary LLM | Gemini 2.5 Flash |
Local Model | Phi-3 (Ollama) |
Protocol | Model Context Protocol (MCP) |
Retrieval | Dense Vector Search |
Validation | Rule-Based Evidence Validation |
Environment | VS Code + Python Virtual Environment |
๐ Installation
Clone the repository
git clone https://github.com/naffss-eng/modular-rag-system.git
cd modular-rag-systemCreate a virtual environment
python -m venv venvActivate the environment
Windows
venv\Scripts\activateLinux
source venv/bin/activateInstall dependencies
pip install -r requirements.txtCreate a .env file
GOOGLE_API_KEY=YOUR_API_KEYโถ Running the Project
Step 1 โ Ingest Documents
Load and preprocess the documents.
python src/read_document.pyStep 2 โ Chunk Documents
Generate semantic chunks.
python src/chunk_document.pyStep 3 โ Generate Embeddings
python src/embed_document.pyStep 4 โ Store Embeddings
python src/store_embeddings.pyStep 5 โ Verify Vector Database
python src/verify_storage.pyStep 6 โ Start the MCP Server
python src/mcp_server.pyStep 7 โ Connect Using Antigravity CLI
antigravity connect localhost:8000Once connected, users can directly ask questions over the uploaded document collection.
๐ฌ Example Queries
Factual
What are the company's core collaboration hours?Numeric
What is the daily meal reimbursement limit?Boolean
Are cash gifts allowed?Procedure
What documents are required to submit a travel reimbursement claim?Summary
Give me an overview of the IT Policy.Comparison
Compare the Finance Policy with the Company Policy.Multi-document
What are the remote work responsibilities mentioned across all documents?Out-of-Scope
How do I bake a chocolate cake?Expected behaviour:
Grounding Status : REJECTED
Answer:
I could not find this information in the uploaded documents.๐ Supported Query Types
Query Type | Supported |
Factual | โ |
Numeric | โ |
Boolean | โ |
Procedure | โ |
Summary | โ |
Comparison | โ |
Multi-document | โ |
Out-of-Scope Detection | โ |
๐ Intent-Aware Processing
Unlike traditional RAG systems that process every query through a single retrieval pipeline, this project dynamically routes each request based on its semantic intent.
User Query
โ
โผ
Intent Classification
โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
Summary Comparison Standard QA
โ โ โ
โผ โผ โผ
Dedicated Balanced Dual Adaptive
Pipeline Retrieval RetrievalThis modular routing significantly improves maintainability while allowing future pipelines to be integrated independently.
๐ Adaptive Retrieval Loop
One of the major architectural enhancements introduced in Backend v2.0 is the Adaptive Retrieval Loop.
Instead of rewriting every query, the system first evaluates retrieval confidence.
User Query
โ
โผ
Initial Retrieval
โ
โผ
Confidence Evaluation
โ
โโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโ
โ โ
โผ โผ
High Confidence Low Confidence
โ โ
โผ โผ
Continue Query Rewriter (Phi-3)
โ
โผ
Rewrite Validator
โ
โผ
Rewrite Cache Check
โ
โผ
Second Retrieval
โ
โผ
Quality Comparison
โ
โโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโ
โผ โผ
Better Retrieval Worse Retrieval
โ โ
โผ โผ
Use New Chunks Keep Original ChunksOnly validated improvements are accepted, ensuring retrieval quality never degrades.
๐ก Grounding & Evidence Validation
Generating an answer is not sufficient; the answer must also be supported by retrieved evidence.
Before returning a response, the system validates whether the retrieved context actually contains sufficient information to answer the question.
Possible grounding outcomes:
Status | Meaning |
PASSED | Sufficient evidence exists |
REJECTED | Retrieved evidence is insufficient |
If grounding fails, the system returns a transparent response rather than hallucinating unsupported information.
๐ JSON Response Format
The backend returns structured responses in JSON.
Example:
{
"answer": "...",
"grounding_status": "PASSED",
"constraint_result": "NOT_APPLICABLE",
"citations": [
"finance_policy_3"
],
"execution_time_ms": 2943,
"initialization_time_ms": 0.01
}๐ Performance Summary
The backend was evaluated using both automated benchmark suites and manual acceptance testing.
Automated Validation
Metric | Result |
Benchmark Queries | 340+ |
Regression Tests | 100% Pass |
Acceptance Suite | 100% Pass |
Rewrite Validator Tests | 29/29 Pass |
Manual Validation
A final manual evaluation was conducted using representative enterprise queries covering:
Factual retrieval
Numeric reasoning
Boolean questions
Summary generation
Procedure extraction
Comparison
Multi-document reasoning
Out-of-scope detection
Result
15 / 15 Queries Passedโก Performance Optimizations
Several optimizations were introduced to balance retrieval quality with runtime efficiency.
Adaptive Query Rewriting
Only low-confidence retrievals trigger semantic rewriting.
Rewrite Validation
Rejects semantic drift before retrieval.
LRU Rewrite Cache
Avoids repeated Phi-3 inference for identical queries.
Feature Flags
Every Backend v2.0 enhancement is protected behind configuration flags.
ENABLE_QUERY_REWRITER = TrueRollback is immediate by disabling the feature.
๐งช Testing Strategy
Testing was performed at multiple levels.
Unit Tests
Query Rewriter
Rewrite Validator
Rewrite Cache
Adaptive Retrieval
Regression Tests
Preprocessing
Retrieval
Empty Queries
Final Verification
Acceptance Tests
Realistic enterprise policy questions covering every supported intent.
Stress Tests
Long queries
Empty input
Malformed characters
Out-of-scope requests
Timeout recovery
Ollama unavailable
Cache failures
๐ฏ Engineering Decisions
Several important architectural decisions shaped the final system.
Why Modular Pipelines?
Each query type requires different retrieval and reasoning behaviour. A modular architecture allows independent optimisation of each pipeline.
Why Local Phi-3?
Using Phi-3 locally preserves Gemini API quota while enabling semantic query rewriting entirely offline.
Why Confidence-Based Rewriting?
Most queries already retrieve correct results.
Running an LLM for every query would unnecessarily increase latency.
Adaptive retrieval ensures rewriting occurs only when beneficial.
Why Rewrite Validation?
LLMs occasionally alter numbers, entities, or logical meaning.
The Rewrite Validator prevents such semantic drift before retrieval.
Why Rewrite Cache?
Repeated enterprise questions are common.
Caching validated rewrites reduces latency and eliminates redundant local inference.
๐ง Current Limitations
Current limitations include:
Dense vector retrieval only (no hybrid BM25 retrieval)
CPU inference latency for local Phi-3
No web interface (backend only)
No authentication layer
Single-session document workspace
๐ฃ Future Roadmap
Backend v2.1
Hybrid Retrieval (Dense + BM25)
Cross-Encoder Reranking
Better telemetry dashboard
Backend v3
Agentic Retrieval
Tool-calling workflows
Multi-agent reasoning
Knowledge graph integration
Frontend
Streamlit / React interface
Drag-and-drop document upload
Citation viewer
Interactive retrieval visualization
Chat history
๐ค Acknowledgements
This project was developed as part of an internship focused on building a modular Retrieval-Augmented Generation system using modern NLP techniques.
It combines dense retrieval, adaptive query rewriting, evidence validation, and Model Context Protocol (MCP) integration into a scalable and extensible architecture.
๐ License
This project is intended for educational and research purposes.
๐ฉโ๐ป Author
Nafisa Hasan
B.Tech Computer Science Engineering
Special interests:
Natural Language Processing
Retrieval-Augmented Generation
Large Language Models
Information Retrieval
Applied Machine Learning
โญ Final Remarks
This project demonstrates how a traditional Retrieval-Augmented Generation pipeline can be transformed into a modular, intent-aware architecture capable of handling diverse enterprise document question-answering tasks with improved retrieval quality, grounded responses, and extensible design.
Rather than relying on a single retrieval strategy, the system incorporates adaptive retrieval, semantic query rewriting, rule-based validation, evidence grounding, and modular routing to improve both reliability and maintainability.
The resulting architecture serves as a strong foundation for future work in hybrid retrieval, agentic workflows, and production-scale document intelligence systems.
"Good retrieval finds information. Great retrieval understands the question first."
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/naffss-eng/modular-rag-system'
If you have feedback or need assistance with the MCP directory API, please join our Discord server