Download the complete workshop package with all code files, documentation, and dependencies list.
Welcome to the MCP Vector Database Server Workshop!
This workshop demonstrates how to build a Model Context Protocol (MCP) server that provides vector database capabilities through a standardized interface. MCP is an open protocol that enables seamless integration between AI applications and data sources, making it easier to build context-aware AI systems.
Model Context Protocol (MCP) is an open standard created by Anthropic that allows AI assistants to securely access external data sources and tools. Think of it as a universal adapter that lets AI applications connect to various services through a standardized interface.
Key Benefits: - Standardized Communication: One protocol for all integrations - Security: Controlled access to resources - Flexibility: Easy to add new tools and data sources - Interoperability: Works with any MCP-compatible client
This project implements an MCP server that exposes two main capabilities: 1. Vector Database Creation: Generate FAISS indexes from knowledge bases 2. Semantic Search: Query the vector database using natural language
Docker is a platform that allows you to run applications in isolated containers. This workshop uses Docker to simplify setup and ensure consistency across different systems.
First, check if you already have Docker installed:
# Check Docker version
docker --version
# Check Docker Compose version
docker-compose --version
If both commands work, you can skip to Step 2.
If Docker is not installed, follow the instructions for your operating system:
.dmg filedocker --version
docker-compose --version
Docker Desktop Installer.exe)docker --version
docker-compose --version
Ubuntu/Debian:
# Update package index
sudo apt-get update
# Install Docker
sudo apt-get install docker.io docker-compose
# Start Docker service
sudo systemctl start docker
sudo systemctl enable docker
# Add your user to docker group (to run without sudo)
sudo usermod -aG docker $USER
# Log out and back in, then verify
docker --version
docker-compose --version
Fedora/RHEL/CentOS:
# Install Docker
sudo dnf install docker docker-compose
# Start Docker service
sudo systemctl start docker
sudo systemctl enable docker
# Add your user to docker group
sudo usermod -aG docker $USER
# Log out and back in, then verify
docker --version
docker-compose --version
Test that Docker is working correctly:
# Run a test container
docker run hello-world
If you see a "Hello from Docker!" message, Docker is installed and working correctly! ✅
💡 Troubleshooting: If you encounter permission errors on Linux, make sure you've added your user to the docker group and logged out/in again. Alternatively, you can run commands with
sudo.
Now that Docker is installed, let's run the MCP Vector Database Server project!
First, extract the MCP.zip file you downloaded and navigate to the directory:
# Navigate to where you extracted the files
cd /path/to/MCP
Choose one of the following options to run the project:
cd /path/to/MCP
# 1. Build the Docker image
./docker-run.sh build
# 2. Start the container
./docker-run.sh start
# 3. Generate vector database (first time only)
docker-compose exec mcp-server python generate_vector_db.py
# 4. Test with the MCP client
docker-compose exec mcp-server python client_vector.py mcp_vector_server.py
# Build and start
docker-compose up -d --build
# Generate vector database
docker-compose exec mcp-server python generate_vector_db.py
# Run client
docker-compose exec mcp-server python client_vector.py mcp_vector_server.py
# Stop container
docker-compose down
# Build image
docker build -t oss-chatbot-mcp .
# Run container
docker run -it --name mcp-container -v $(pwd):/app oss-chatbot-mcp
# Execute commands in running container
docker exec -it mcp-container python generate_vector_db.py
The docker-run.sh script provides convenient commands:
| Command | Description |
|---|---|
./docker-run.sh build |
Build the Docker image |
./docker-run.sh start |
Start the container in background |
./docker-run.sh run-server |
Run the MCP server |
./docker-run.sh run-client |
Run the MCP client (requires server path argument) |
./docker-run.sh shell |
Open a bash shell in the container |
./docker-run.sh stop |
Stop and remove the container |
# 1. Build and start container
./docker-run.sh build
./docker-run.sh start
# 2. Generate vector database (first time)
docker-compose exec mcp-server python generate_vector_db.py
# Expected output:
# ✅ FAISS vector DB created successfully!
# 📁 Location: faiss_index
# 📦 Entries: 4 | Dimensions: 384
# 3. Test the system
docker-compose exec mcp-server python client_vector.py mcp_vector_server.py
# Expected output:
# 🔌 Starting MCP client for server: mcp_vector_server.py
# ✅ MCP Handshake Complete
# 🛠 Tools detected: ['create_vector_db', 'search_vector_db']
# 📌 RAW RESPONSE FROM SERVER:
# 1) score=0.7131
# Critical TechWorks Quality...
# 4. When done
./docker-run.sh stop
┌─────────────────────────────────────┐
│ Docker Container │
│ (oss-chatbot-mcp) │
├─────────────────────────────────────┤
│ │
│ 📁 /app (mounted from host) │
│ ├── mcp_vector_server.py │
│ ├── client_vector.py │
│ ├── generate_vector_db.py │
│ ├── search_vector_db.py │
│ ├── knowledge_base.json │
│ └── faiss_index/ │
│ ├── faiss.index │
│ ├── embeddings.npy │
│ └── texts.json │
│ │
│ 🐍 Python 3.12 + Dependencies │
│ 📦 sentence-transformers │
│ 📦 faiss-cpu │
│ 📦 mcp + fastmcp │
│ │
└─────────────────────────────────────┘
↕ Volume Mount (sync)
┌─────────────────────────────────────┐
│ Host Machine │
│ /path/to/MCP/ │
└─────────────────────────────────────┘
Error: service "mcp-server" is not running
Solution:
./docker-run.sh start
# or
docker-compose up -d
Warning: the attribute 'version' is obsolete
Solution: This is just a warning and can be safely ignored. The project works normally. To
remove it, you can delete the version: '3.8' line from docker-compose.yml.
Error: address already in use
Solution:
# Stop existing containers
docker-compose down
# Or find and stop the conflicting container
docker ps
docker stop <container-id>
Error: ERROR: faiss.index missing
Solution:
# Generate the index first
docker-compose exec mcp-server python generate_vector_db.py
Error: Permission denied when running docker-run.sh
Solution:
# Make the script executable
chmod +x docker-run.sh
# View container logs
docker-compose logs -f mcp-server
# Access container shell
./docker-run.sh shell
# or
docker-compose exec mcp-server bash
# Restart container
docker-compose restart mcp-server
# Rebuild image (after changing Dockerfile)
docker-compose build --no-cache
# Remove all stopped containers and images
docker system prune -a
💡 Pro Tip: The Docker volume mount (
-v .:/app) automatically syncs files between your host machine and the container. You can edit files on your Mac and they'll be immediately available in the container!
Now that you have the project running, let's understand what it does and how it works!
This project implements a Model Context Protocol (MCP) server that provides vector database capabilities. It allows AI assistants to:
The system has four main components:
knowledge_base.json) - Contains the text data to be searched
generate_vector_db.py) - Converts text into
searchable vectorssearch_vector_db.py) - Finds relevant information using
similarity searchmcp_vector_server.py) - Exposes these capabilities through MCP
protocol1. 📝 Add knowledge to knowledge_base.json
2. 🔄 Generate vector database (embeddings)
3. 🚀 Start MCP server
4. 💬 AI assistant queries the server
5. 🎯 Server returns relevant results
MCP/
├── generate_vector_db.py # Creates FAISS vector database
├── search_vector_db.py # Searches the vector database
├── mcp_vector_server.py # MCP server implementation
├── client_vector.py # MCP client for testing
├── knowledge_base.json # Knowledge base content
├── docker-compose.yml # Docker Compose configuration
├── Dockerfile # Docker image definition
├── docker-run.sh # Helper script for Docker commands
├── faiss_index/ # Generated vector database directory
│ ├── faiss.index # FAISS index (binary)
│ ├── embeddings.npy # Stored embeddings (numpy array)
│ └── texts.json # Text references for retrieval
└── README.md # Documentation
| File | Purpose | Type |
|---|---|---|
generate_vector_db.py |
Standalone script to create vector DB | Utility |
search_vector_db.py |
Standalone script to search vector DB | Utility |
mcp_vector_server.py |
MCP server exposing vector DB tools | Server |
client_vector.py |
MCP client for testing the server | Client |
knowledge_base.json |
Source knowledge base | Data |
faiss.index |
FAISS index file | Generated |
embeddings.npy |
Embedding vectors | Generated |
texts.json |
Text chunks for retrieval | Generated |
Let's dive into each file and understand how the code works. We'll go through the four main Python files that make up this project.
File: generate_vector_db.py
This script creates a FAISS vector database from a knowledge base. It demonstrates: - Loading text data from JSON - Generating embeddings using SentenceTransformers - Creating a FAISS index with cosine similarity - Saving the index for later use
📦 Pre-Generated Vector Database Available!
This workshop includes a pre-generated vector database ready to use:
- Location:
faiss_index_mcp/- Documents: 13 files (Python scripts, documentation, guides)
- Chunks: 66 text chunks
- Size: ~164 KB (index.faiss + index.pkl)
- Embedding Model: all-MiniLM-L6-v2 (384 dimensions)
The database contains knowledge about:
- MCP server and client implementation
- Vector database generation and search scripts
- Docker setup and configuration
- Installation guides (INSTALL.md, GUIA_DOCKER.md)
- Critical TechWorks principles and best practices
The MCP server exposes this database through standardized tools that AI assistants can query!
all-MiniLM-L6-v2 (lightweight, 384 dimensions)IndexFlatIP for inner product (cosine similarity on normalized
vectors)# 1. Load knowledge base from JSON
with open("knowledge_base.json", "r") as f:
data = json.load(f)
texts = data["Texts"]
# 2. Generate embeddings
model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = model.encode(texts)
embeddings = normalize(embeddings) # For cosine similarity
# 3. Create FAISS index
index = faiss.IndexFlatIP(dim)
index.add(embeddings)
# 4. Save index and metadata
faiss.write_index(index, "faiss.index")
np.save("embeddings.npy", embeddings)
json.dump(texts, "texts.json")
python3 generate_vector_db.py
🧠 Loading knowledge base from: knowledge_base.json
📚 4 knowledge chunks loaded.
⚙️ Generating embeddings with model: all-MiniLM-L6-v2
✅ FAISS vector DB created successfully!
📁 Location: faiss_index
📦 Entries: 4 | Dimensions: 384
After running, you'll have:
- faiss_index/faiss.index - FAISS index file
- faiss_index/embeddings.npy - Numpy array of embeddings
- faiss_index/texts.json - Original text chunks
- knowledge_base.json - Source knowledge base
File: search_vector_db.py
This script provides a search function for querying the FAISS vector database. It demonstrates: - Loading a pre-built FAISS index - Encoding query text to embeddings - Performing similarity search - Ranking and formatting results
search_in_faiss(query_text, top_k, index_dir)# 1. Load FAISS index and texts
index = faiss.read_index("faiss_index/faiss.index")
with open("faiss_index/texts.json") as f:
texts = json.load(f)
# 2. Encode query
model = SentenceTransformer("all-MiniLM-L6-v2")
query_embedding = model.encode([query])
query_embedding = normalize(query_embedding)
# 3. Search for similar vectors
similarities, indices = index.search(query_embedding, top_k)
# 4. Return ranked results
for rank, (idx, score) in enumerate(zip(indices[0], similarities[0])):
print(f"Rank {rank+1}: {texts[idx]} (Score: {score:.4f})")
Prerequisites: Run generate_vector_db.py first!
# Import and use in Python
python3 -c "
from search_vector_db import search_in_faiss
result = search_in_faiss('What are the quality principles?', top_k=2)
print(result)
"
🔎 Query: What are the quality principles?
Top 2 similar results (cosine)
============================================================
🏅 Rank 1 (Cosine: 0.8234)
Critical TechWorks Quality (Quality, Reliability, and Resilience):
We ensure delivery of high-quality, reliable, and resilient software...
------------------------------------------------------------
🏅 Rank 2 (Cosine: 0.5421)
Critical TechWorks Principles:
A principle is a rule, belief, or idea that guides action...
------------------------------------------------------------
File: mcp_vector_server.py
This is the core MCP server that exposes vector database functionality through the Model Context Protocol. It demonstrates: - Creating an MCP server with FastMCP - Defining MCP tools (functions callable by clients) - Handling asynchronous operations - Logging and error handling
create_vector_db: Creates a FAISS index from JSONsearch_vector_db: Searches the vector databasecreate_vector_dbPurpose: Create a FAISS vector database from a JSON knowledge base
Parameters:
- json_path (str): Path to the knowledge base JSON file
- output_dir (str): Directory to save the FAISS index
Returns: Success message with entry count
Example:
{
"json_path": "knowledge_base.json",
"output_dir": "faiss_index"
}
search_vector_dbPurpose: Search the vector database using semantic similarity
Parameters:
- query (str): Search query in natural language
- index_dir (str): Directory containing the FAISS index
- top_k (int): Number of results to return (default: 5)
Returns: Formatted search results with scores
Example:
{
"query": "collaboration principles",
"index_dir": "faiss_index",
"top_k": 3
}
# Start the MCP server
python3 mcp_vector_server.py
The server will start and wait for MCP client connections via stdio.
┌─────────────────────────────────────┐
│ MCP Vector Server │
│ (mcp_vector_server.py) │
├─────────────────────────────────────┤
│ │
│ Tool 1: create_vector_db() │
│ ├─ Load JSON knowledge base │
│ ├─ Generate embeddings │
│ ├─ Create FAISS index │
│ └─ Save index files │
│ │
│ Tool 2: search_vector_db() │
│ ├─ Load FAISS index │
│ ├─ Encode query │
│ ├─ Perform similarity search │
│ └─ Return ranked results │
│ │
└─────────────────────────────────────┘
↕ (MCP Protocol via stdio)
┌─────────────────────────────────────┐
│ MCP Client │
│ (client_vector.py or any │
│ MCP-compatible client) │
└─────────────────────────────────────┘
File: client_vector.py
This is a test client for the MCP server. It demonstrates: - Connecting to an MCP server via stdio - Discovering available tools - Calling server tools with parameters - Handling responses
# 1. Create client and connect to server
client = MCPClient()
await client.connect_to_server("mcp_vector_server.py")
# 2. List available tools
tools = await client.session.list_tools()
print("Available tools:", [t.name for t in tools.tools])
# 3. Call a tool
response = await client.call_tool(
"search_vector_db",
query="quality principles",
index_dir="faiss_index",
top_k=2
)
# 4. Process response
print(response)
# Run the client (it will start the server automatically)
python3 client_vector.py mcp_vector_server.py
🔌 Starting MCP client for server: mcp_vector_server.py
[DEBUG] Initializing MCP session...
✅ MCP Handshake Complete
🛠 Tools detected: ['create_vector_db', 'search_vector_db']
⚙ Calling tool 'search_vector_db' with: {'query': 'critical techworks quality', 'index_dir': '/Users/...', 'top_k': 1}
📌 RAW RESPONSE FROM SERVER:
1) score=0.8234
Critical TechWorks Quality (Quality, Reliability, and Resilience):
We ensure delivery of high-quality, reliable, and resilient software...
🔚 Client closed.
MCP is an open protocol that standardizes how AI applications communicate with external data sources and tools.
Key Features: - Tools: Functions that can be called by AI assistants - Resources: Data sources that can be read - Prompts: Reusable prompt templates - Stdio Communication: Uses standard input/output for communication
Why MCP? - Eliminates the need for custom integrations - Provides security through controlled access - Enables interoperability between different AI systems - Simplifies development of AI-powered applications
FastMCP is a Python library that simplifies MCP server creation.
Benefits: - Decorator-based tool definition - Automatic type validation - Built-in error handling - Easy to use and understand
Example:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("MyServer")
@mcp.tool()
def my_tool(param: str) -> str:
return f"Processed: {param}"
mcp.run()
Vector databases store and retrieve data based on semantic similarity rather than exact matches.
How They Work: 1. Embedding: Convert text to numerical vectors 2. Indexing: Store vectors in an efficient data structure 3. Querying: Find vectors similar to a query vector 4. Ranking: Return results sorted by similarity
Use Cases: - Semantic search - Recommendation systems - Document retrieval - Question answering
FAISS is a library for efficient similarity search and clustering of dense vectors.
Key Features: - Fast: Optimized for large-scale search - Flexible: Supports various distance metrics - Scalable: Can handle billions of vectors - CPU & GPU: Works on both architectures
Index Types:
- IndexFlatIP: Exact search with inner product (used in this project)
- IndexFlatL2: Exact search with L2 distance
- IndexIVFFlat: Approximate search with clustering
- And many more...
Embeddings are dense vector representations of text that capture semantic meaning.
Properties: - Similar texts have similar embeddings - Dimensionality typically ranges from 128 to 1536 - Generated by neural networks trained on large text corpora
Model Used: all-MiniLM-L6-v2
- Dimensions: 384
- Size: ~120MB
- Speed: Fast inference
- Quality: Good for general-purpose tasks
Cosine similarity measures the cosine of the angle between two vectors.
Formula: similarity = (A · B) / (||A|| × ||B||)
Range: -1 to 1 (for normalized vectors: 0 to 1) - 1 = Identical direction (very similar) - 0 = Orthogonal (unrelated) - -1 = Opposite direction (very dissimilar)
Why Cosine? - Invariant to vector magnitude - Works well for text embeddings - Efficient to compute
Error: ModuleNotFoundError: No module named 'mcp'
Solution:
pip install -U mcp fastmcp
Error: ERROR: faiss.index missing
Solution:
# Generate the index first
python3 generate_vector_db.py
Error: Dimension mismatch: query=384, index=768
Solution: Ensure you're using the same embedding model (all-MiniLM-L6-v2) for both
indexing and searching. Delete the old index and regenerate:
rm -rf faiss_index/
python3 generate_vector_db.py
Symptoms: Client hangs or times out
Solutions: 1. Check if the server script path is correct 2. Ensure Python 3.8+ is being used 3. Check server logs in stderr 4. Verify all dependencies are installed
Error: PermissionError: [Errno 13] Permission denied
Solution:
# Make scripts executable
chmod +x generate_vector_db.py
chmod +x mcp_vector_server.py
chmod +x client_vector.py
Error: System runs out of RAM during embedding generation
Solutions: - Close other applications - Process texts in smaller batches - Use a smaller embedding model - Increase system swap space
Note: This section is for users who prefer not to use Docker. If you're using Docker (recommended), you can skip this section.
# Navigate to the project directory
# (Replace with the path where you extracted the MCP.zip file)
cd /path/to/MCP
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
venv\Scripts\activate
# Install required packages
pip install -U sentence-transformers faiss-cpu numpy mcp fastmcp
# Check if packages are installed
python3 -c "import faiss; import sentence_transformers; import mcp; print('✅ All packages installed successfully!')"
After installing dependencies, you can run the scripts directly:
# Generate vector database
python3 generate_vector_db.py
# Run the MCP client
python3 client_vector.py mcp_vector_server.py
Create your own knowledge base by modifying knowledge_base.json:
{
"Texts": [
"Your first knowledge chunk here...",
"Your second knowledge chunk here...",
"Add as many as you need..."
]
}
Then regenerate the index:
python3 generate_vector_db.py
Modify the EMBED_MODEL constant in the scripts:
# Smaller, faster model
EMBED_MODEL = "all-MiniLM-L6-v2" # 384 dims, ~120MB
# Larger, more accurate model
EMBED_MODEL = "all-mpnet-base-v2" # 768 dims, ~420MB
# Multilingual model
EMBED_MODEL = "paraphrase-multilingual-MiniLM-L12-v2" # 384 dims
Important: Use the same model for both indexing and searching!
Add the MCP server to Claude Desktop's configuration:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"vector-db": {
"command": "python3",
"args": ["/absolute/path/to/mcp_vector_server.py"],
"env": {
"PYTHONPATH": "/absolute/path/to/project"
}
}
}
}
Restart Claude Desktop, and the tools will be available!
For larger knowledge bases (10,000+ documents):
# Instead of IndexFlatIP
quantizer = faiss.IndexFlatIP(dim)
index = faiss.IndexIVFFlat(quantizer, dim, nlist=100)
index.train(embeddings)
index.add(embeddings)
# Process embeddings in batches
batch_size = 1000
for i in range(0, len(texts), batch_size):
batch = texts[i:i+batch_size]
batch_embeddings = model.encode(batch)
index.add(batch_embeddings)
pip install faiss-gpu
python3 generate_vector_db.py
Expected: Index files created in faiss_index/ directory
from search_vector_db import search_in_faiss
# Test different queries
queries = [
"What are the collaboration principles?",
"Tell me about quality",
"How do we ensure visibility?"
]
for query in queries:
print(f"\n{'='*60}")
print(search_in_faiss(query, top_k=1))
python3 client_vector.py mcp_vector_server.py
Expected: Client connects, discovers tools, executes search, displays results
After completing this workshop, you can:
Happy Learning! 🚀