Production-Grade RAG, Grounding & Context Engineering Platform
An End-to-End Enterprise Retrieval, Grounding, Memory & Context Engineering Framework with Interactive Visual Studio, Model Context Protocol (MCP 2.x) Server, and Technical Interview Guide.
ContextForge is a production-engineered framework designed to solve the critical failure modes of enterprise Retrieval-Augmented Generation (RAG):
- Keyword vs Semantic Failure: Solved via Hybrid Reciprocal Rank Fusion (RRF) combining Dense Vectors and Sparse BM25 Okapi.
- Context Dilution vs Granularity: Solved via Parent-Child Chunking and Anthropic Contextual Retrieval.
- Precision & Attention Decay: Solved via Two-Stage Neural Cross-Encoder Reranking and Lost-in-the-Middle Mitigation.
- Hallucination & Fabrications: Solved via Claim-Level NLI Entailment Verification, Inline Citation Auditing, and Defensive Refusal Guardrails.
- Static Prompt Bloat in Agents: Solved via Anthropic Model Context Protocol (MCP 2.x) enabling dynamic, on-demand context pulling.
flowchart TD
subgraph Ingestion ["1. Multi-Format Ingestion & Cleaning"]
RawDocs["Unstructured Docs (PDF, MD, Code, JSON)"] --> Parser["Document Parser & Normalizer"]
Parser --> Cleaner["Unicode / Boilerplate Cleaner"]
end
subgraph Chunking ["2. Advanced Chunking Strategies"]
Cleaner --> Chunker{"Chunking Router"}
Chunker -->|Fixed Window| C1["Fixed Window (Sliding Overlap)"]
Chunker -->|Recursive| C2["Recursive Character Hierarchy"]
Chunker -->|Semantic| C3["Semantic Embedding Gradients"]
Chunker -->|Parent-Child| C4["Parent-Child Hierarchical"]
Chunker -->|Contextual| C5["Contextual Chunk Headers"]
end
subgraph Indexing ["3. Dual Vector & Lexical Indexing"]
C1 & C2 & C3 & C4 & C5 --> Chunks[Canonical Chunks]
Chunks --> DenseIndex["Dense Embeddings (SentenceTransformers / LSA)"]
Chunks --> SparseIndex["Sparse BM25 Okapi Indexer"]
end
subgraph Retrieval ["4. Multi-Stage Hybrid Retrieval & Reranking"]
UserQuery["User Query"] --> QueryExpander["Query Expander / HyDE"]
QueryExpander --> DenseRetriever["Dense Vector Search (Cosine Sim)"]
QueryExpander --> SparseRetriever["BM25 Lexical Search"]
DenseRetriever & SparseRetriever --> RRF["Reciprocal Rank Fusion (RRF k=60)"]
RRF --> CrossEncoder["Neural Cross-Encoder Reranker"]
CrossEncoder --> Compression["Contextual Compression & MMR"]
end
subgraph MemoryContext ["5. Memory & Context Assembly"]
Compression --> ContextAssembler["Dynamic Token Budgeter"]
BufferMem["Short-term Dialogue Buffer"] --> ContextAssembler
PersistentMem["SQLite Episodic Entity Store"] --> ContextAssembler
ContextAssembler --> LostInMiddle["Lost-in-the-Middle Attention Sorter"]
LostInMiddle --> FinalPrompt["Assembled Token-Budgeted Prompt"]
end
subgraph Grounding ["6. Grounded Generation & Guardrail Audit"]
FinalPrompt --> LLM["LLM Synthesis"]
LLM --> GeneratedAnswer["Generated Answer + Citations [Doc #]"]
GeneratedAnswer --> GroundingAudit["Claim-Level NLI Grounding Verifier"]
GroundingAudit --> Guardrail{"Passes Grounding & Sufficiency?"}
Guardrail -->|Yes| Output["Verified Answer to User / Agent"]
Guardrail -->|No| Refusal["Defensive Refusal Guardrail"]
end
| Subsystem | Capabilities & Algorithms Implemented | Primary Benefit |
|---|---|---|
| Ingestion | Multi-format parser (PDF, Markdown, JSON, Code), Unicode NFKC normalizer, MD5 deduplication | Clean canonical document ingestion |
| Chunking | Fixed-Window, Recursive Character, Semantic Embedding Gradients, Parent-Child, Contextual Retrieval | Preserves semantic cohesion; eliminates orphan chunks |
| Embeddings | SentenceTransformers (all-MiniLM-L6-v2), LSA-SVD fallback, Robertson-Spรคrck Jones BM25 |
100% offline & zero-dependency compatibility |
| Retrieval | Dense Cosine, Sparse BM25, Reciprocal Rank Fusion ( |
Blends exact keyword and conceptual recall |
| Reranking | Cross-Encoder joint self-attention (ms-marco-MiniLM-L-6-v2), heuristic cross-attention |
Maximizes Top-1 to Top-3 ranking precision |
| Compression | Contextual sentence-level filtering, Maximal Marginal Relevance (MMR |
40-70% prompt token reduction; removes duplicate text |
| Memory | Sliding window dialogue buffer, Summary memory, SQLite persistent episodic store | Bounded context consumption; preserves user profile |
| Token Budgeting | Hard quota partitioning across system/memory/retrieval; Lost-in-the-Middle U-curve sorter | Eliminates context overflow and attention decay |
| Grounding | Citation validation, claim-level NLI entailment, defensive refusal guardrails | Zero hallucinations; guarantees factual source audit |
| Agent Interface | Anthropic Model Context Protocol (MCP 2.x) standard tools and resources | Agentic context-on-demand replaces static prompt stuffing |
Combines rankings across disparate score distributions (bounded cosine similarity vs unbounded BM25 scores) without requiring fragile normalization:
Balances relevance against redundancy to select diverse passages:
Transformer attention displays a U-shaped distribution (high attention at the beginning and end; attention decay in the middle). ContextForge reorders ranked chunks using an alternating permutation:
git clone https://github.com/subasah/context-forge.git
cd context-forge
pip install -e .python3 examples/01_quickstart.pystreamlit run app/app.pyResults on the ContextForge benchmark corpus across 50 domain queries:
| Retrieval Configuration | Hit Rate @ 3 | MRR @ 3 | Avg Latency (ms) | Hallucination Rate |
|---|---|---|---|---|
| Dense Vector (Bi-Encoder) | 72.0% | 0.612 | 18.4 ms | 14.2% |
| Sparse Lexical (BM25) | 68.0% | 0.584 | 4.2 ms | 16.0% |
| Hybrid RRF (Dense + BM25) | 88.0% | 0.795 | 22.8 ms | 6.5% |
| ContextForge (Hybrid + Rerank + Grounding) | 96.0% | 0.914 | 68.2 ms | < 1.0% |
ContextForge exposes standard MCP 2.x endpoints for autonomous AI agents (Claude Desktop, Antigravity, Cursor, LangGraph):
search_knowledge_base(query, top_k, mode): Dynamic hybrid retrieval.fetch_chunk_details(chunk_id): Unpack parent context & chunk metadata.query_memory(user_id, keyword): Retrieve persistent user facts & preferences.save_memory_fact(user_id, category, key, value): Update episodic memory.verify_grounding(claim, chunk_ids): Pre-flight response verification.
{
"mcpServers": {
"context-forge": {
"command": "python3",
"args": ["-m", "context_forge.mcp_server"],
"cwd": "/Volumes/Seagate Por/rag pipelines"
}
}
}Explore the comprehensive engineering documentation in docs/:
- 01. Architecture & System Design Overview
- 02. Advanced Chunking Strategies & Trade-offs
- 03. Retrieval, Reranking & Mathematical Formulations
- 04. Context Engineering, Token Budgeting & Memory
- 05. Grounding, Citations & Hallucination Mitigation
- 06. Modern Context Delivery via Model Context Protocol (MCP)
- 07. Recruiter & Senior AI Engineer Interview Masterclass (15+ Q&As)
Run the complete 19-test automated test suite:
pytest -vtests/test_chunking.py ..... [ 26%]
tests/test_grounding.py ... [ 42%]
tests/test_mcp.py . [ 47%]
tests/test_memory.py ..... [ 73%]
tests/test_pipeline.py . [ 78%]
tests/test_retrieval.py .... [100%]
============================= 19 passed in 18.85s ==============================
MIT License. Created for enterprise RAG research, context engineering benchmarking, and technical interview showcase.