# How to Properly Restart Servers
## ⚠️ The Problem
When you change Python code, the running servers **don't automatically reload**. They keep using the old cached bytecode (`.pyc` files).
**That's why you're still seeing the error even after the fix!**
---
## ✅ Solution: Proper Restart Process
### Option 1: Use the Stop Script (Easiest)
```bash
# Stop all servers and clear cache
./stop_servers.sh
# Then start them again in separate terminals
# Terminal 1:
source .venv/bin/activate
python3 -m src.mcp_server.server
# Terminal 2:
source .venv/bin/activate
python3 -m src.web_chat.main
```
### Option 2: Manual Process
**Step 1: Stop the servers**
Go to each terminal where servers are running and press **Ctrl+C**
**Step 2: Clear Python cache**
```bash
./stop_servers.sh
# Or manually:
find . -type d -name __pycache__ -exec rm -rf {} +
find . -type f -name "*.pyc" -delete
```
**Step 3: Start servers again**
```bash
# Terminal 1 - MCP Server
cd /Users/saimanvithmacbookair/Desktop/Updation_MCP_Local
source .venv/bin/activate
python3 -m src.mcp_server.server
# Terminal 2 - Web Chat API
cd /Users/saimanvithmacbookair/Desktop/Updation_MCP_Local
source .venv/bin/activate
python3 -m src.web_chat.main
```
---
## 🧪 Test After Restart
```bash
# Terminal 3
curl -X POST http://localhost:8002/chat \
-H "Content-Type: application/json" \
-d '{
"user_id": 1,
"organization_id": 1,
"dealership_id": 10,
"role": 13,
"message": "Show my dealership contracts"
}'
```
**Expected:** You should get a JSON response with `"success": true`
**Not:** The error `{"detail":[{"type":"missing"...`
---
## 🔍 How to Check if Servers Are Running
```bash
# Check for running servers
ps aux | grep -E "src\.(web_chat|mcp_server)" | grep -v grep
```
**If you see output:** Servers are running (might be old code)
**If empty:** No servers running
---
## 📝 Scripts Available
| Script | What It Does |
|--------|--------------|
| `./stop_servers.sh` | Stops all servers + clears cache |
| `./test_api.sh` | Tests all API endpoints |
| `./restart_servers.sh` | Shows restart instructions |
---
## 🎯 Quick Checklist
When you change code:
1. ✅ **Stop servers** (Ctrl+C in each terminal)
2. ✅ **Clear cache** (`./stop_servers.sh`)
3. ✅ **Start servers** (Terminal 1 & 2)
4. ✅ **Test** (`./test_api.sh` or curl)
---
## 💡 Why This Happens
Python compiles `.py` files to `.pyc` bytecode for faster loading. When you:
- Change the `.py` file
- But the server is still running
- It uses the **old cached `.pyc` file**
**Solution:** Always restart after code changes!
---
## ⚡ Pro Tip: Auto-Reload (Development)
For development, you can enable auto-reload:
```bash
# Start with auto-reload
uvicorn src.web_chat.main:app --reload --host 0.0.0.0 --port 8002
```
This will automatically restart when you change files!
---
## 🚨 Common Mistakes
### ❌ Mistake 1: Not Stopping Old Servers
```bash
# You start a new server without stopping the old one
python3 -m src.web_chat.main
# Error: Address already in use
```
**Fix:** Stop old servers first with `./stop_servers.sh`
### ❌ Mistake 2: Not Clearing Cache
```bash
# You restart but don't clear cache
# Server still uses old .pyc files
```
**Fix:** Run `./stop_servers.sh` which clears cache
### ❌ Mistake 3: Wrong Terminal
```bash
# You press Ctrl+C in the wrong terminal
# Server is still running in another terminal
```
**Fix:** Check all terminals or use `./stop_servers.sh`
---
## ✅ Correct Restart Flow
```
1. ./stop_servers.sh
↓
2. Terminal 1: python3 -m src.mcp_server.server
↓
3. Terminal 2: python3 -m src.web_chat.main
↓
4. Terminal 3: curl ... (test)
```
---
## 🎉 After Proper Restart
Your curl command should now work:
```bash
curl -X POST http://localhost:8002/chat \
-H "Content-Type: application/json" \
-d '{
"user_id": 1,
"organization_id": 1,
"dealership_id": 10,
"role": 13,
"message": "Show my dealership contracts"
}'
```
**Expected response:**
```json
{
"success": true,
"response": "...",
"history": [...],
"metadata": null
}
```
**NOT this error:**
```json
{"detail":[{"type":"missing","loc":["query","chat_request"]...}]}
```
---
## 📞 Still Not Working?
If you still see the error after proper restart:
1. ✅ Check the file was saved: `grep "async def chat" src/web_chat/main.py`
- Should show: `async def chat(chat_request: ChatRequest, request: Request):`
2. ✅ Check no servers running: `ps aux | grep src.web_chat`
- Should be empty after `./stop_servers.sh`
3. ✅ Check cache cleared: `find . -name "*.pyc" | wc -l`
- Should be 0 or very small number
4. ✅ Start fresh:
```bash
./stop_servers.sh
source .venv/bin/activate
# Terminal 1: python3 -m src.mcp_server.server
# Terminal 2: python3 -m src.web_chat.main
```
---
**TL;DR:** Run `./stop_servers.sh` then start both servers again!