How mouhsineguet/mcp_rag_agent works
Project Type and Purpose
• This is a full-stack document-centric conversational AI application (RAG pipeline)
• Purpose: let end users upload documents (PDF, DOCX, TXT), index them semantically, and chat with them via a large language model (Llama 3.2)
File Structure and Architectural Patterns
• Top-level files:
– app.py: Streamlit frontend (UI)
– mcp_server.py: Document processing microservice (MCP = “multi-chain processing”)
– mcp_client.py: Client library for talking to the MCP server
– ollama_client.py: Client for interacting with the Ollama LLM server
– requirements.txt: Python dependencies
• Pattern: separation of concerns into three main components (frontend, processing backend, LLM backend)
• Uses a microservice-style RPC between app.py and mcp_server.py via mcp_client.py
Key Components
A. Streamlit Frontend
• Upload UI
• Controls “Process Document” and chat interface
B. MCP Server (mcp_server.py)
• Document ingestion and parsing (PDF/DOCX/TXT)
• Chunking logic (DocumentProcessor.chunk_text)
• Embedding service (sentence-transformers model)
• FAISS index creation and search
C. Ollama LLM Server
• Hosted locally via “ollama run llama3.2:3b”
• Exposes REST or socket API for inference
D. Clients
• mcp_client.py calls MCP server endpoints (e.g., /chunk, /embed, /search)
• ollama_client.py issues chat completion requests to Ollama
E. Orchestration / RAG Pipeline (in app.py)
• On user query:
– Call MCP server to retrieve top-k relevant chunks
– Merge chunks + question into prompt
– Call Ollama client for generation
– Display response
Relationships and Data Flow
1. User → Streamlit UI (app.py)
2. Streamlit UI → MCP Server via mcp_client
• Document upload → /process endpoint
• Search query → /search endpoint
3. MCP Server
• Parses document → chunks → embeddings → FAISS index
• Returns chunk IDs and text snippets on search
4. Streamlit UI → Ollama LLM via ollama_client
• Sends prompt (question + context chunks)
• Receives generated answer
5. UI displays answer to user
Architectural Patterns and Design Principles
• Single-Page Web App (Streamlit) + Backend microservices
• RAG pattern: separation of retrieval (FAISS) and generation (LLM)
• External services isolated by client wrappers (mcp_client, ollama_client)
Essential Elements to Capture
• Clear labels for each component (UI, MCP server, Embedding, FAISS, Ollama)
• Annotations for technologies/frameworks (Streamlit, Python, FAISS, sentence-transformers, Ollama)