AI Tutoring RAG System
Enables personalized AI tutoring by allowing students to upload PDF/DOCX study materials that are processed and indexed for semantic search. Provides intelligent responses based on the student's own learning materials using RAG technology.
README
ai-tutor
AI Tutoring RAG System - Setup & Testing Guide
📋 Table of Contents
- Overview
- Prerequisites
- Installation
- Configuration
- Running the Services
- Testing the System
- API Endpoints
- Troubleshooting
🎯 Overview
This is an AI-powered tutoring system that uses RAG (Retrieval-Augmented Generation) to provide personalized learning experiences. The system consists of two main components:
- RAG MCP Server (Port 9000) - Provides RAG tools via MCP protocol
- MCP Host (Port 8000) - Agent orchestration layer with FastAPI
Key Features:
- Personalized knowledge base for each student
- PDF and DOCX file processing and indexing
- Semantic search across student's learning materials
- Intent analysis and risk detection
- Azure Blob Storage integration
- OpenAI GPT-4 powered responses
🔧 Prerequisites
Before you begin, ensure you have the following installed:
- Python 3.13+
- uv package manager (Installation guide)
- Git
Install uv
# On macOS and Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# On Windows
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Verify installation
uv --version
Required API Keys
You'll need accounts and API keys for:
- OpenAI - Get API Key
- Pinecone - Get API Key
- Azure Storage - Get Connection String
💿 Installation
1. Create Virtual Environment
# Create a virtual environment using uv
uv venv
# Activate the virtual environment
# On macOS/Linux:
source .venv/bin/activate
# On Windows:
.venv\Scripts\activate
2. Install Dependencies
# Install all dependencies using uv
uv pip install -r pyproject.toml
# Or install directly from pyproject.toml
uv pip install -e .
⚙️ Configuration
1. Create Environment File
Create a .env file in the project root:
cp .env.example .env
2. Configure Environment Variables
Edit .env and add your credentials:
# OpenAI Configuration
OPENAI_API_KEY=sk-your-openai-api-key-here
# Pinecone Configuration
PINECONE_API_KEY=your-pinecone-api-key-here
PINECONE_ENVIRONMENT=us-east-1
# Azure Storage Configuration
AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=https;AccountName=...
3. Generate Authentication Token
Generate a JWT token for MCP server authentication:
python get_auth_token.py
Copy the generated token and update it in mcp_host/app.py:
MCP_TOOLS = [
{
"name": "turtor_rag",
"transport_type": "streamable_http",
"url": "http://0.0.0.0:9000/mcp",
"headers": {
"Authorization": "Bearer YOUR_GENERATED_TOKEN_HERE"
},
}
]
🚀 Running the Services
You need to run both services in separate terminal windows.
Terminal 1: Start RAG MCP Server
# Activate virtual environment
source .venv/bin/activate # or .venv\Scripts\activate on Windows
# Run the RAG MCP server
python rag_mcp_server.py
Expected Output:
INFO: Started server process
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:9000
Terminal 2: Start MCP Host
# Activate virtual environment (in new terminal)
source .venv/bin/activate # or .venv\Scripts\activate on Windows
# Run the MCP host
python -m uvicorn mcp_host.app:app --host 0.0.0.0 --port 8000 --reload
Expected Output:
INFO: Will watch for changes in these directories: ['/path/to/ed_mcp']
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: Started reloader process
INFO: Started server process
INFO: Waiting for application startup.
Initializing TutoringRagAgent server...
TutoringRagAgent server initialized successfully
INFO: Application startup complete.
Verify Services are Running
Open your browser and check:
- MCP Host API Docs: http://localhost:8000/docs
- RAG MCP Server: http://localhost:9000/
🧪 Testing the System
Test 1: Create Sample PDF
First, create a sample PDF for testing:
python create_sample_pdf.py
This creates sample.pdf with calculus study content.
Test 2: Upload a File
Use curl to upload a file:
curl -X POST "http://localhost:8000/upload-student-file" \
-F "file=@sample.pdf" \
-F "student_id=test_student_001" \
-F "subject=Mathematics" \
-F "topic=Calculus" \
-F "difficulty_level=7"
Expected Response:
{
"status": "success",
"message": "Your file has been received and is being processed. You'll be able to interact with its content shortly!"
}
Test 3: Chat with the Tutor
Send a chat message to query the uploaded content:
curl -X POST "http://localhost:8000/chats/tutor-rag-agent" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "user",
"content": "What did I learn about derivatives?"
}
],
"session_id": "test_session_001"
}'
Expected Response: The system should retrieve relevant content from the uploaded PDF and provide a personalized response about derivatives.
Test 4: Query Knowledge Base Directly
Test the RAG retrieval directly:
curl -X POST "http://localhost:9000/mcp" \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "knowledge_base_retrieval",
"arguments": {
"user_id": "test_student_001",
"query": "What is the chain rule?",
"subject": "Mathematics",
"topic": "Calculus",
"top_k": 3
}
},
"id": 1
}'
Test 5: View Session History
Get the conversation history:
curl -X GET "http://localhost:8000/session/test_session_001/history"
Test 6: Run Complete Test Workflow
Run the automated test suite:
# Make sure both servers are running first!
python test_workflow.py
This will:
- Upload a file to Azure
- Process and index the file
- Query the indexed content
- List uploaded files
Test 7: Test File Processing
Test PDF and DOCX processing:
python test_file_processor.py
📡 API Endpoints
MCP Host (Port 8000)
POST /chats/tutor-rag-agent
Start a chat session with the AI tutor.
Request:
{
"messages": [
{
"role": "user",
"content": "Explain quadratic equations"
}
],
"session_id": "session_123"
}
Response: Streaming text response
POST /upload-student-file
Upload a PDF or DOCX file for processing.
Form Data:
file: The file to uploadstudent_id: Student identifiersubject: Subject categorytopic: Specific topicdifficulty_level: 1-10
GET /session/{session_id}/history
Get conversation history for a session.
DELETE /session/{session_id}/memory
Clear memory for a specific session.
GET /agent/info
Get information about the agent configuration.
RAG MCP Server (Port 9000)
POST /mcp
MCP protocol endpoint for tool calls.
Available Tools:
knowledge_base_retrieval- Search user's knowledge baseupload_student_file- Process and index uploaded files
🔍 Testing with Python
Interactive Testing
Create a Python script test_interactive.py:
import requests
import json
# Base URLs
MCP_HOST = "http://localhost:8000"
RAG_SERVER = "http://localhost:9000"
# Test chat
def test_chat(message, session_id="test_001"):
response = requests.post(
f"{MCP_HOST}/chats/tutor-rag-agent",
json={
"messages": [{"role": "user", "content": message}],
"session_id": session_id
},
stream=True
)
print("Response:")
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
# Test file upload
def test_upload(file_path, student_id, subject, topic):
with open(file_path, 'rb') as f:
files = {'file': f}
data = {
'student_id': student_id,
'subject': subject,
'topic': topic,
'difficulty_level': 5
}
response = requests.post(
f"{MCP_HOST}/upload-student-file",
files=files,
data=data
)
print(json.dumps(response.json(), indent=2))
# Run tests
if __name__ == "__main__":
print("Testing file upload...")
test_upload("sample.pdf", "student_123", "Mathematics", "Calculus")
print("\nTesting chat...")
test_chat("What did I learn about calculus?")
Run it:
python test_interactive.py
🐛 Troubleshooting
Port Already in Use
Error: Address already in use
Solution:
# Find and kill the process using the port
# On macOS/Linux:
lsof -ti:8000 | xargs kill -9
lsof -ti:9000 | xargs kill -9
# On Windows:
netstat -ano | findstr :8000
taskkill /PID <PID> /F
Pinecone Connection Error
Error: Failed to connect to Pinecone
Solutions:
- Verify your
PINECONE_API_KEYis correct - Check your Pinecone index exists
- Ensure
PINECONE_ENVIRONMENTmatches your Pinecone region
OpenAI API Error
Error: Incorrect API key provided
Solutions:
- Verify your
OPENAI_API_KEYis correct - Check you have credits in your OpenAI account
- Ensure the key has the required permissions
Azure Storage Error
Error: Azure Storage connection failed
Solutions:
- Verify your
AZURE_STORAGE_CONNECTION_STRINGis correct - Check the storage account exists and is accessible
- Ensure the container name matches in
azure_storage.py
MCP Authentication Error
Error: Unauthorized: token verification failed
Solutions:
- Generate a new token:
python get_auth_token.py - Update the token in
mcp_host/app.py - Restart both servers
Dependencies Not Found
Error: ModuleNotFoundError: No module named 'X'
Solution:
# Reinstall dependencies
uv pip install -r pyproject.toml --force-reinstall
# Or install specific package
uv pip install <package-name>
File Processing Fails
Error: Failed to extract text from PDF
Solutions:
- Ensure PDF is not password-protected
- Check file is not corrupted
- Verify PyMuPDF is installed:
uv pip install pymupdf
📊 Monitoring and Logs
View Detailed Logs
Both servers print detailed logs. To save logs:
# RAG MCP Server
python rag_mcp_server.py > rag_server.log 2>&1
# MCP Host
python -m uvicorn mcp_host.app:app --host 0.0.0.0 --port 8000 > mcp_host.log 2>&1
Check System Health
# Check MCP Host
curl http://localhost:8000/agent/info
# Check if services respond
curl -I http://localhost:8000/docs
curl -I http://localhost:9000/
🎓 Example Workflows
Workflow 1: Complete Student Onboarding
# 1. Create sample study materials
python create_sample_pdf.py
# 2. Upload student's notes
curl -X POST "http://localhost:8000/upload-student-file" \
-F "file=@sample.pdf" \
-F "student_id=student_123" \
-F "subject=Mathematics" \
-F "topic=Calculus" \
-F "difficulty_level=7"
# 3. Wait a few seconds for processing
sleep 5
# 4. Start tutoring session
curl -X POST "http://localhost:8000/chats/tutor-rag-agent" \
-H "Content-Type: application/json" \
-d '{
"messages": [{"role": "user", "content": "Help me understand derivatives"}],
"session_id": "session_123"
}'
Workflow 2: Multi-Subject Learning
# Upload multiple files for different subjects
curl -X POST "http://localhost:8000/upload-student-file" \
-F "file=@math_notes.pdf" \
-F "student_id=student_123" \
-F "subject=Mathematics" \
-F "topic=Algebra" \
-F "difficulty_level=6"
curl -X POST "http://localhost:8000/upload-student-file" \
-F "file=@history_notes.pdf" \
-F "student_id=student_123" \
-F "subject=History" \
-F "topic=World War II" \
-F "difficulty_level=5"
# Query across subjects
curl -X POST "http://localhost:8000/chats/tutor-rag-agent" \
-H "Content-Type: application/json" \
-d '{
"messages": [{"role": "user", "content": "What have I been studying?"}],
"session_id": "session_123"
}'
📚 Additional Resources
- FastMCP Documentation
- OmniCoreAgent Documentation
- LlamaIndex Documentation
- Pinecone Documentation
- OpenAI API Documentation
🤝 Support
If you encounter issues:
- Check the Troubleshooting section
- Review logs from both servers
- Ensure all environment variables are set correctly
- Verify all prerequisites are installed
📝 Notes
- The system uses JWT authentication between services
- Files are stored in Azure Blob Storage and indexed in Pinecone
- Each student has an isolated knowledge base
- Sessions maintain conversation history
- The agent autonomously decides when to use RAG tools
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。