Skip to content

briceanu/fastapi-mcp-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MCP Server — Agentic RAG System

A full-stack application that lets users upload documents, chat with an AI agent that retrieves relevant information from those documents, and manage data categories.


What It Does

Feature Description
Upload Files Upload .txt documents and assign them to a category (cars or addresses)
Chat Ask questions — the AI agent retrieves relevant chunks from your uploaded documents and answers using GPT-4.1
Remove Category Delete all vector data stored under a specific namespace/category

Architecture Overview

┌──────────────────────────────────────────────────┐
│              Frontend  (React 19 + Vite)          │
│                                                    │
│  ┌────────────┐  ┌──────────┐  ┌───────────────┐  │
│  │ Upload     │  │  Chat    │  │ Remove        │  │
│  │ File       │  │  Page    │  │ Category      │  │
│  └────────────┘  └──────────┘  └───────────────┘  │
└───────────────────────┬──────────────────────────┘
                        │ HTTP / REST
                        ▼
┌──────────────────────────────────────────────────┐
│         FastAPI Backend  (port 8000)              │
│                                                    │
│  POST   /api/v1/upload                            │
│  DELETE /api/v1/delete-namespace                  │
│  POST   /api/v1/agent-response                    │
│                                                    │
│  ┌────────────────────────────────────────────┐   │
│  │  LangGraph Agent  (GPT-4.1 via OpenAI)     │   │
│  │  • Loads conversation history (Postgres)   │   │
│  │  • Connects to MCP Server for tools        │   │
│  │  • ContentFilter middleware (security)     │   │
│  └────────────────────┬───────────────────────┘   │
└───────────────────────┼──────────────────────────┘
          ┌─────────────┼────────────────┐
          ▼             ▼                ▼
   ┌─────────────┐  ┌──────────┐  ┌──────────────┐
   │  MCP Server │  │Postgres  │  │   Celery     │
   │  (port 9000)│  │(memory)  │  │   Worker     │
   │             │  │          │  │              │
   │ Tools:      │  │ Thread   │  │ Async file   │
   │ • namespaces│  │ checkpoint│  │ processing   │
   │ • search_   │  │ storage  │  │ & embedding  │
   │   cars      │  └──────────┘  └──────┬───────┘
   │ • search_   │                        │
   │   addresses │         ┌──────────────┘
   └──────┬──────┘         ▼
          │          ┌──────────┐
          │          │  Redis   │
          │          │ (broker) │
          │          └──────────┘
          ▼
   ┌─────────────┐
   │  Pinecone   │
   │  (vectors)  │
   │             │
   │ Embedding:  │
   │ llama-text- │
   │ embed-v2    │
   └─────────────┘

Tech Stack

Backend

Layer Technology
API Framework FastAPI
AI Agent LangChain + LangGraph
LLM OpenAI GPT-4.1
Vector Store Pinecone (llama-text-embed-v2 embeddings, cosine similarity)
Conversation Memory LangGraph Postgres Checkpointer
Async Tasks Celery + Redis
MCP Server FastMCP (port 9000)
Database PostgreSQL 17
Message Broker Redis 7

Frontend

Layer Technology
Framework React 19
Routing React Router 7
Styling Tailwind CSS 4
Build Tool Vite
Language TypeScript

Data Flow — Chat Query

  1. User sends a question from the Chat page
  2. Frontend POST /api/v1/agent-response?question=...&thread_id=default
  3. Backend loads conversation history from PostgreSQL (by thread_id)
  4. LangGraph agent initialises with GPT-4.1 and connects to the MCP Server
  5. Agent calls retrive_all_name_spaces() to discover available categories
  6. Agent decides which category the question is about (cars or addresses)
  7. Agent calls search_cars_vectors() or search_addresses_vectors()Pinecone returns top 3 semantic matches
  8. GPT-4.1 generates a final answer using the retrieved context
  9. Conversation state saved back to PostgreSQL
  10. { "answer": "..." } returned to the frontend

Data Flow — File Upload

  1. User selects a file and category in the Upload page
  2. Frontend POST /api/v1/upload?category=cars with multipart/form-data
  3. FastAPI enqueues a Celery task (via Redis broker)
  4. Celery worker processes the file:
    • Splits text into ~200-character chunks
    • Embeds each chunk with Pinecone's llama-text-embed-v2
    • Stores vectors under the matching namespace (cars or addresses)
  5. Success response returned

Project Structure

mcp_server/
├── backend/
│   ├── app/
│   │   ├── main.py              # FastAPI app + CORS + router
│   │   ├── routes.py            # 3 API endpoints
│   │   ├── agent_logic.py       # LangGraph agent, upload logic, namespace removal
│   │   ├── mcp_server.py        # FastMCP server with 3 tools
│   │   ├── celery_tasks.py      # Async Celery tasks for file processing
│   │   ├── pinecone_client.py   # Pinecone connection + embedding
│   │   ├── schemas.py           # Pydantic request/response models
│   │   ├── middleware.py        # ContentFilterMiddleware
│   │   ├── logger.py            # Coloured logging
│   │   └── aws_secretes.py      # AWS Secrets Manager integration
│   ├── docker-compose.yml       # PostgreSQL + Redis + FastAPI + Celery + MCP
│   ├── Dockerfile
│   └── pyproject.toml
├── frontend/
│   ├── src/
│   │   ├── App.tsx              # BrowserRouter + route definitions
│   │   ├── pages/
│   │   │   ├── UploadFilePage.tsx
│   │   │   ├── ChatPage.tsx
│   │   │   └── RemoveCategoryPage.tsx
│   │   └── components/
│   │       ├── Nav.tsx          # Hamburger sidebar navigation
│   │       ├── Layout.tsx
│   │       └── UploadFile.tsx
│   ├── .env                     # VITE_SERVER_URL=http://localhost:8000
│   └── vite.config.ts
└── README.md

Running the App

Backend

cd backend
docker compose up --build

This starts 5 services:

  • fastapi — API server on port 8000
  • mcp_server — MCP tool server on port 9000
  • celery_worker — async task processor
  • postgres — PostgreSQL 17 on port 5432
  • redis — Redis 7 on port 6379

Frontend

cd frontend
npm install
npm run dev

App runs on http://localhost:5173.

Environment Variables

Backend (via Docker or .env):

Variable Description
OPENAI_API_KEY OpenAI API key
PINECONE_API_KEY Pinecone API key
POSTGRES_USER PostgreSQL username
POSTGRES_PASSWORD PostgreSQL password
POSTGRES_HOST PostgreSQL host
POSTGRES_PORT PostgreSQL port
POSTGRES_DB PostgreSQL database name

Frontend (.env):

Variable Description
VITE_SERVER_URL Backend base URL (default: http://localhost:8000)

API Reference

POST /api/v1/upload

Upload a file and embed it into the vector store.

Parameter Type Location Description
user_file file form-data The file to upload
category cars | addresses query Target namespace

Response: { "success": "..." }


POST /api/v1/agent-response

Send a question to the AI agent.

Parameter Type Location Description
question string (max 100) query The user's question
thread_id string query Conversation ID for memory (default: "default")

Response: { "answer": "..." }


DELETE /api/v1/delete-namespace

Remove all vectors stored under a category.

Parameter Type Location Description
category string (max 50) query Namespace to delete

Response: { "response": "..." }

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors