Skip to main content
Glama
raksha39

mcp-agentic-rag

by raksha39

MCP Agentic RAG

A production-ready Retrieval-Augmented Generation (RAG) system exposed as an MCP tool. The system searches a local vector knowledge base first; if the query doesn't match well enough, it falls back to live web search. Either way, an LLM generates a grounded, cited answer.


Architecture

User query (via MCP client e.g. Cursor, Claude Desktop)
          │
          ▼
  ┌───────────────────┐
  │   MCP Server      │  server.py
  │  ask_ml_knowledge │
  └────────┬──────────┘
           │
           ▼
  ┌───────────────────┐
  │  Embed query      │  nomic-embed-text-v1.5
  └────────┬──────────┘
           │
           ▼
  ┌───────────────────┐
  │  Search Qdrant    │  cosine similarity
  └────────┬──────────┘
           │
     score >= 0.45?
      /           \
    YES            NO
     │              │
     ▼              ▼
  RAG context   Bright Data
  (local KB)    web search
     │              │
     └──────┬───────┘
            ▼
   ┌─────────────────┐
   │  LLM generates  │  OpenAI / local model
   │  grounded answer│
   └────────┬────────┘
            ▼
   Answer + numbered sources

Related MCP server: MCP RAG Server

Project Structure

mcp-agentic-rag/
├── data/                        # Knowledge base documents
│   ├── machine_learning.txt
│   ├── deep_learning.txt
│   ├── neural_networks.txt
│   ├── cnn.txt
│   ├── rnn.txt
│   ├── transformers.txt
│   ├── optimization.txt
│   └── regularization.txt
├── eval/
│   └── eval_retrieval.py        # Recall@3 evaluation (30 questions)
├── rag_code.py                  # EmbedData, QdrantVDB, Retriever, AnswerGenerator
├── ingest.py                    # One-time ingestion script
├── server.py                    # MCP server entry point
├── docker-compose.yml           # Qdrant vector database
├── requirements.txt
├── .env.example                 # Copy to .env and fill in credentials
└── notebook.ipynb               # Interactive exploration notebook

Setup

1. Prerequisites

2. Install dependencies

pip install -r requirements.txt

3. Configure environment

cp .env.example .env

Edit .env and fill in at minimum:

Variable

Required

Description

OPENAI_API_KEY

Yes

OpenAI API key for answer generation

OPENAI_MODEL

No

Model name (default: gpt-4o-mini)

OPENAI_BASE_URL

No

Custom endpoint for local LLMs (Ollama, LM Studio)

QDRANT_URL

No

Qdrant URL (default: http://localhost:6333)

SCORE_THRESHOLD

No

Similarity cutoff for web fallback (default: 0.45)

BRIGHT_DATA_USERNAME

No

Bright Data SERP credentials (web fallback only)

BRIGHT_DATA_PASSWORD

No

Bright Data SERP credentials (web fallback only)

4. Start Qdrant

docker compose up -d

Qdrant dashboard: http://localhost:6333/dashboard

5. Ingest the knowledge base

python ingest.py

This embeds all documents in data/ and uploads them to Qdrant. Re-run with --reset after updating documents:

python ingest.py --reset

6. Register the MCP server

Add the following to your MCP client configuration (e.g. Cursor mcp.json, Claude Desktop claude_desktop_config.json):

{
  "mcpServers": {
    "mcp-agentic-rag": {
      "command": "python",
      "args": ["/absolute/path/to/server.py"]
    }
  }
}

The server is now available. Your AI assistant will automatically call ask_ml_knowledge_base when relevant.


Using a Local LLM (no OpenAI account needed)

Install Ollama and pull a model:

ollama pull llama3.2

Set in .env:

OPENAI_BASE_URL=http://localhost:11434/v1
OPENAI_API_KEY=ollama
OPENAI_MODEL=llama3.2

Evaluation

Run the built-in Recall@3 evaluation on 30 domain-specific questions (requires Qdrant running with the collection ingested):

python eval/eval_retrieval.py

Save full results to JSON:

python eval/eval_retrieval.py --output eval/results.json

Example output:

============================================================
  Results Summary
============================================================
  Total questions : 30
  Correct (hit)   : 26
  Missed          : 4
  Recall@3        : 26/30 = 86.7%
============================================================

Adding More Documents

  1. Create a .txt file in data/ with this header format:

TITLE: Your Document Title
SOURCE: your_file.txt
URL: https://optional-reference-url.com

---

First chunk of text goes here.

---

Second chunk of text goes here.
  1. Re-ingest:

python ingest.py --reset

License

MIT

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    C
    maintenance
    Enables AI assistants to search and retrieve information from your knowledge base using RAG (Retrieval-Augmented Generation) with hybrid search, document indexing, and ChromaDB vector storage.
    15 npm
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables AI coding assistants to search and retrieve information from a locally ingested knowledge base using hybrid search, grounded in user-curated documentation.
    17
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables AI assistants to perform semantic, hybrid, and filtered search on indexed local documentation with RAG capabilities.
    2
    MIT