Commit
·
63ff505
1
Parent(s):
0bd0fd1
deleted unnecessary files
Browse files- AUTHENTICATION_COMPLETION_GUIDE.md +0 -231
- AUTHENTICATION_DOCS.md +0 -505
- DRIVER_CREATION_GUIDE.md +0 -318
- GITHUB_ACTIONS_SETUP.md +0 -315
- HUGGINGFACE_DEPLOY.md +0 -215
- IMPLEMENTATION_STATUS.md +0 -252
- MCP_TOOLS_SUMMARY.md +0 -107
- MIGRATION_SUMMARY.md +0 -478
- PROXY_SETUP.md +0 -202
- QUICK_START.md +0 -138
- README_MCP.md +0 -497
- ROUTES_API_IMPLEMENTATION.md +0 -305
- TEAM_COLLABORATION_GUIDE.md +0 -305
- TECHNICAL_COMPARISON.md +0 -779
- VEHICLE_SPECIFIC_ROUTING.md +0 -346
- apply_auth_pattern.py +0 -140
- cleanup_bad_user_ids.py +0 -124
- fix_user_id.py +0 -53
- flow.md +0 -828
- launcher.py +0 -65
- scripts/test_db.py +0 -169
- server.py +5 -1
- test_assignment_system.py +0 -225
- test_auto_assignment.py +0 -185
- test_complete_delivery.py +0 -178
- test_delivery_timing.py +0 -273
- test_directions.py +0 -111
- test_driver_validation.py +0 -187
- test_duplicate_assignment.py +0 -145
- test_fail_delivery.py +0 -230
- test_intelligent_assignment.py +0 -232
- test_oauth.html +0 -239
- unified_app.py +0 -210
- verify_fix.py +0 -51
AUTHENTICATION_COMPLETION_GUIDE.md
DELETED
|
@@ -1,231 +0,0 @@
|
|
| 1 |
-
# Authentication Implementation - Completion Guide
|
| 2 |
-
|
| 3 |
-
## What's Done ✅
|
| 4 |
-
|
| 5 |
-
### Phase 1: Database (Complete)
|
| 6 |
-
- ✅ Stytch library installed
|
| 7 |
-
- ✅ Migration created and run successfully
|
| 8 |
-
- ✅ `user_id` column added to orders, drivers, assignments tables
|
| 9 |
-
- ✅ Indexes created for fast filtering
|
| 10 |
-
|
| 11 |
-
### Phase 2: Authentication Module (Complete)
|
| 12 |
-
- ✅ `database/user_context.py` created
|
| 13 |
-
- ✅ Stytch integration working
|
| 14 |
-
- ✅ Token verification function
|
| 15 |
-
- ✅ Permission checking system
|
| 16 |
-
|
| 17 |
-
### Phase 3: Tool Handlers (4 of 27 Complete)
|
| 18 |
-
- ✅ `handle_create_order` - adds user_id, checks auth
|
| 19 |
-
- ✅ `handle_fetch_orders` - filters by user_id
|
| 20 |
-
- ✅ `handle_create_driver` - adds user_id, checks auth
|
| 21 |
-
- ✅ `handle_fetch_drivers` - filters by user_id
|
| 22 |
-
|
| 23 |
-
### Phase 4: MCP Tools (1 of 27 Complete)
|
| 24 |
-
- ✅ `create_order` - full authentication example
|
| 25 |
-
- ✅ OAuth metadata endpoint
|
| 26 |
-
- ✅ Authentication helper function
|
| 27 |
-
|
| 28 |
-
---
|
| 29 |
-
|
| 30 |
-
## What's Remaining ⚠️
|
| 31 |
-
|
| 32 |
-
### Remaining Tool Handlers (23 handlers in chat/tools.py)
|
| 33 |
-
|
| 34 |
-
Apply this exact pattern to each:
|
| 35 |
-
|
| 36 |
-
```python
|
| 37 |
-
# BEFORE:
|
| 38 |
-
def handle_some_tool(tool_input: dict) -> dict:
|
| 39 |
-
# ... existing code ...
|
| 40 |
-
|
| 41 |
-
# AFTER:
|
| 42 |
-
def handle_some_tool(tool_input: dict, user_id: str = None) -> dict:
|
| 43 |
-
# Add auth check at start
|
| 44 |
-
if not user_id:
|
| 45 |
-
return {
|
| 46 |
-
"success": False,
|
| 47 |
-
"error": "Authentication required. Please login first.",
|
| 48 |
-
"auth_required": True
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
# ... existing code ...
|
| 52 |
-
|
| 53 |
-
# For CREATE operations: Add user_id to INSERT query
|
| 54 |
-
query = """INSERT INTO table_name (..., user_id) VALUES (..., %s)"""
|
| 55 |
-
params = (..., user_id)
|
| 56 |
-
|
| 57 |
-
# For FETCH operations: Add user_id filter FIRST
|
| 58 |
-
where_clauses = ["user_id = %s"]
|
| 59 |
-
params = [user_id]
|
| 60 |
-
# ... then add other filters ...
|
| 61 |
-
```
|
| 62 |
-
|
| 63 |
-
**List of handlers to update:**
|
| 64 |
-
|
| 65 |
-
**Order Handlers (4 remaining):**
|
| 66 |
-
- [ ] `handle_count_orders` - line ~2330
|
| 67 |
-
- [ ] `handle_get_order_details` - line ~2550
|
| 68 |
-
- [ ] `handle_search_orders` - line ~2590
|
| 69 |
-
- [ ] `handle_get_incomplete_orders` - line ~2620
|
| 70 |
-
- [ ] `handle_update_order` - line ~2650
|
| 71 |
-
- [ ] `handle_delete_order` - line ~2730
|
| 72 |
-
- [ ] `handle_delete_all_orders` - line ~2750
|
| 73 |
-
|
| 74 |
-
**Driver Handlers (4 remaining):**
|
| 75 |
-
- [ ] `handle_count_drivers` - line ~2800
|
| 76 |
-
- [ ] `handle_get_driver_details` - line ~2920
|
| 77 |
-
- [ ] `handle_search_drivers` - line ~2950
|
| 78 |
-
- [ ] `handle_get_available_drivers` - line ~2990
|
| 79 |
-
- [ ] `handle_update_driver` - line ~3020
|
| 80 |
-
- [ ] `handle_delete_driver` - line ~3100
|
| 81 |
-
- [ ] `handle_delete_all_drivers` - line ~3120
|
| 82 |
-
|
| 83 |
-
**Assignment Handlers (7 total):**
|
| 84 |
-
- [ ] `handle_create_assignment` - line ~3200
|
| 85 |
-
- [ ] `handle_auto_assign_order` - line ~3300
|
| 86 |
-
- [ ] `handle_intelligent_assign_order` - line ~3400
|
| 87 |
-
- [ ] `handle_get_assignment_details` - line ~3500
|
| 88 |
-
- [ ] `handle_update_assignment` - line ~3600
|
| 89 |
-
- [ ] `handle_unassign_order` - line ~3700
|
| 90 |
-
- [ ] `handle_complete_delivery` - line ~3800
|
| 91 |
-
- [ ] `handle_fail_delivery` - line ~3900
|
| 92 |
-
|
| 93 |
-
### Remaining MCP Tools (26 tools in server.py)
|
| 94 |
-
|
| 95 |
-
Apply this exact pattern to each:
|
| 96 |
-
|
| 97 |
-
```python
|
| 98 |
-
# BEFORE:
|
| 99 |
-
@mcp.tool()
|
| 100 |
-
def some_tool(param1: str, param2: int) -> dict:
|
| 101 |
-
"""Tool description"""
|
| 102 |
-
from chat.tools import handle_some_tool
|
| 103 |
-
logger.info(f"Tool: some_tool(...)")
|
| 104 |
-
return handle_some_tool({"param1": param1, "param2": param2})
|
| 105 |
-
|
| 106 |
-
# AFTER:
|
| 107 |
-
@mcp.tool()
|
| 108 |
-
def some_tool(param1: str, param2: int) -> dict:
|
| 109 |
-
"""Tool description"""
|
| 110 |
-
# STEP 1: Authenticate
|
| 111 |
-
user = get_authenticated_user()
|
| 112 |
-
if not user:
|
| 113 |
-
return {
|
| 114 |
-
"success": False,
|
| 115 |
-
"error": "Authentication required. Please login first.",
|
| 116 |
-
"auth_required": True
|
| 117 |
-
}
|
| 118 |
-
|
| 119 |
-
# STEP 2: Check permissions
|
| 120 |
-
required_scope = get_required_scope('some_tool')
|
| 121 |
-
if not check_permission(user.get('scopes', []), required_scope):
|
| 122 |
-
return {
|
| 123 |
-
"success": False,
|
| 124 |
-
"error": f"Permission denied. Required scope: {required_scope}"
|
| 125 |
-
}
|
| 126 |
-
|
| 127 |
-
# STEP 3: Execute with user_id
|
| 128 |
-
from chat.tools import handle_some_tool
|
| 129 |
-
logger.info(f"Tool: some_tool by user {user.get('email')}")
|
| 130 |
-
|
| 131 |
-
return handle_some_tool(
|
| 132 |
-
tool_input={"param1": param1, "param2": param2},
|
| 133 |
-
user_id=user['user_id']
|
| 134 |
-
)
|
| 135 |
-
```
|
| 136 |
-
|
| 137 |
-
**List of tools to update (line numbers in server.py):**
|
| 138 |
-
|
| 139 |
-
**Order Tools (7 remaining):**
|
| 140 |
-
- [ ] `count_orders` - line 427
|
| 141 |
-
- [ ] `fetch_orders` - line 479
|
| 142 |
-
- [ ] `get_order_details` - line 543
|
| 143 |
-
- [ ] `search_orders` - line 564
|
| 144 |
-
- [ ] `get_incomplete_orders` - line 586
|
| 145 |
-
- [ ] `update_order` - line 612
|
| 146 |
-
- [ ] `delete_order` - line 689
|
| 147 |
-
|
| 148 |
-
**Driver Tools (7 total):**
|
| 149 |
-
- [ ] `create_driver` - line 714
|
| 150 |
-
- [ ] `count_drivers` - line 772
|
| 151 |
-
- [ ] `fetch_drivers` - line 804
|
| 152 |
-
- [ ] `get_driver_details` - line 848
|
| 153 |
-
- [ ] `search_drivers` - line 871
|
| 154 |
-
- [ ] `get_available_drivers` - line 893
|
| 155 |
-
- [ ] `update_driver` - line 915
|
| 156 |
-
- [ ] `delete_driver` - line 975
|
| 157 |
-
|
| 158 |
-
**Assignment Tools (7 total):**
|
| 159 |
-
- [ ] `create_assignment` - line 1000
|
| 160 |
-
- [ ] `auto_assign_order` - line 1050
|
| 161 |
-
- [ ] `intelligent_assign_order` - line 1100
|
| 162 |
-
- [ ] `get_assignment_details` - line 1150
|
| 163 |
-
- [ ] `update_assignment` - line 1200
|
| 164 |
-
- [ ] `unassign_order` - line 1250
|
| 165 |
-
- [ ] `complete_delivery` - line 1300
|
| 166 |
-
- [ ] `fail_delivery` - line 1350
|
| 167 |
-
|
| 168 |
-
**Bulk Operations (2 total):**
|
| 169 |
-
- [ ] `delete_all_orders` - line 1400
|
| 170 |
-
- [ ] `delete_all_drivers` - line 1450
|
| 171 |
-
|
| 172 |
-
**Public Tools (3 - NO AUTH NEEDED):**
|
| 173 |
-
- `geocode_address` - line 188 (public, no auth)
|
| 174 |
-
- `calculate_route` - line 212 (public, no auth)
|
| 175 |
-
- `calculate_intelligent_route` - line 284 (public, no auth)
|
| 176 |
-
|
| 177 |
-
---
|
| 178 |
-
|
| 179 |
-
## Quick Implementation Script
|
| 180 |
-
|
| 181 |
-
You can use this bash script to help identify which functions still need updating:
|
| 182 |
-
|
| 183 |
-
```bash
|
| 184 |
-
# Find all @mcp.tool() functions
|
| 185 |
-
grep -n "@mcp.tool()" server.py
|
| 186 |
-
|
| 187 |
-
# Find all handler functions
|
| 188 |
-
grep -n "^def handle_" chat/tools.py
|
| 189 |
-
|
| 190 |
-
# Check which handlers already have user_id parameter
|
| 191 |
-
grep -n "def handle_.*user_id" chat/tools.py
|
| 192 |
-
```
|
| 193 |
-
|
| 194 |
-
---
|
| 195 |
-
|
| 196 |
-
## Testing Checklist
|
| 197 |
-
|
| 198 |
-
After completing all updates:
|
| 199 |
-
|
| 200 |
-
- [ ] Start server: `python app.py`
|
| 201 |
-
- [ ] Check OAuth endpoint: `curl http://localhost:7860/.well-known/oauth-protected-resource`
|
| 202 |
-
- [ ] Should return JSON with authorization_servers
|
| 203 |
-
- [ ] Test in Claude Desktop:
|
| 204 |
-
- [ ] Create order → Browser opens for login
|
| 205 |
-
- [ ] Login with email → Get magic link
|
| 206 |
-
- [ ] Click link → Logged in
|
| 207 |
-
- [ ] Order created successfully
|
| 208 |
-
- [ ] Fetch orders → Only shows user's orders
|
| 209 |
-
- [ ] Test with second user:
|
| 210 |
-
- [ ] Login with different email
|
| 211 |
-
- [ ] Create order as second user
|
| 212 |
-
- [ ] First user can't see second user's orders ✅
|
| 213 |
-
|
| 214 |
-
---
|
| 215 |
-
|
| 216 |
-
## Estimated Time to Complete
|
| 217 |
-
|
| 218 |
-
- **Remaining handlers (23):** ~45 minutes (2 min each)
|
| 219 |
-
- **Remaining tools (26):** ~1 hour (2-3 min each)
|
| 220 |
-
- **Testing:** ~20 minutes
|
| 221 |
-
- **Total:** ~2 hours
|
| 222 |
-
|
| 223 |
-
---
|
| 224 |
-
|
| 225 |
-
## Need Help?
|
| 226 |
-
|
| 227 |
-
If you get stuck, refer to the completed examples:
|
| 228 |
-
- **Handler example:** `handle_create_order` (line 1331 in chat/tools.py)
|
| 229 |
-
- **Tool example:** `create_order` (line 354 in server.py)
|
| 230 |
-
|
| 231 |
-
Both show the complete pattern with authentication, permission checks, and user_id handling.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AUTHENTICATION_DOCS.md
DELETED
|
@@ -1,505 +0,0 @@
|
|
| 1 |
-
# FleetMind Authentication System - Complete Documentation
|
| 2 |
-
|
| 3 |
-
## Overview
|
| 4 |
-
|
| 5 |
-
FleetMind uses **API Key authentication** for secure multi-tenant access with complete data isolation. This document explains how user_id is maintained and propagated throughout the system.
|
| 6 |
-
|
| 7 |
-
---
|
| 8 |
-
|
| 9 |
-
## User ID Lifecycle
|
| 10 |
-
|
| 11 |
-
### 1. Generation (Deterministic)
|
| 12 |
-
|
| 13 |
-
When a user generates an API key, their `user_id` is created deterministically from their email:
|
| 14 |
-
|
| 15 |
-
```python
|
| 16 |
-
# database/api_keys.py:76
|
| 17 |
-
user_id = f"user_{hashlib.md5(email.encode()).hexdigest()[:12]}"
|
| 18 |
-
```
|
| 19 |
-
|
| 20 |
-
**Example:**
|
| 21 |
-
- Email: `admin@gmail.com`
|
| 22 |
-
- User ID: `user_a85ddf4d664b` (always the same for this email)
|
| 23 |
-
|
| 24 |
-
**Benefits:**
|
| 25 |
-
- Same email always gets same user_id
|
| 26 |
-
- Even if API key is revoked and regenerated, user_id stays the same
|
| 27 |
-
- Enables consistent data ownership across key rotations
|
| 28 |
-
|
| 29 |
-
---
|
| 30 |
-
|
| 31 |
-
### 2. Storage (api_keys table)
|
| 32 |
-
|
| 33 |
-
The association between API key and user_id is stored in the database:
|
| 34 |
-
|
| 35 |
-
```sql
|
| 36 |
-
CREATE TABLE api_keys (
|
| 37 |
-
key_id SERIAL PRIMARY KEY,
|
| 38 |
-
user_id VARCHAR(100) NOT NULL UNIQUE,
|
| 39 |
-
email VARCHAR(255) NOT NULL,
|
| 40 |
-
name VARCHAR(255),
|
| 41 |
-
api_key_hash VARCHAR(64) NOT NULL UNIQUE, -- SHA-256 hash
|
| 42 |
-
api_key_prefix VARCHAR(20) NOT NULL, -- First 12 chars for display
|
| 43 |
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 44 |
-
last_used_at TIMESTAMP,
|
| 45 |
-
is_active BOOLEAN DEFAULT true
|
| 46 |
-
);
|
| 47 |
-
```
|
| 48 |
-
|
| 49 |
-
**Stored Values:**
|
| 50 |
-
- `api_key_hash`: SHA-256 hash of the actual API key (never stored plaintext)
|
| 51 |
-
- `api_key_prefix`: First 12 characters for display (e.g., `fm_3bGKV40B4...`)
|
| 52 |
-
- `user_id`: Deterministic ID from email
|
| 53 |
-
- `email`: For lookup and revocation
|
| 54 |
-
|
| 55 |
-
---
|
| 56 |
-
|
| 57 |
-
### 3. Authentication Flow
|
| 58 |
-
|
| 59 |
-
#### Step A: User Configuration
|
| 60 |
-
User adds API key to Claude Desktop config:
|
| 61 |
-
|
| 62 |
-
```json
|
| 63 |
-
{
|
| 64 |
-
"mcpServers": {
|
| 65 |
-
"fleetmind": {
|
| 66 |
-
"command": "npx",
|
| 67 |
-
"args": [
|
| 68 |
-
"mcp-remote",
|
| 69 |
-
"http://localhost:7860/sse?api_key=fm_3bGKV40B47NJ43zQ_4d3j-60a2gyoeLOVffoyyhRtBc"
|
| 70 |
-
]
|
| 71 |
-
}
|
| 72 |
-
}
|
| 73 |
-
}
|
| 74 |
-
```
|
| 75 |
-
|
| 76 |
-
#### Step B: Server Extracts API Key (server.py:125-143)
|
| 77 |
-
|
| 78 |
-
```python
|
| 79 |
-
def get_authenticated_user():
|
| 80 |
-
# METHOD 1: Extract from SSE connection URL
|
| 81 |
-
request = get_http_request()
|
| 82 |
-
if request:
|
| 83 |
-
api_key = request.query_params.get('api_key')
|
| 84 |
-
if api_key:
|
| 85 |
-
user = verify_api_key(api_key)
|
| 86 |
-
if user:
|
| 87 |
-
return user # {'user_id': 'user_a85ddf4d664b', ...}
|
| 88 |
-
|
| 89 |
-
# METHOD 2: Development bypass (ENV-protected)
|
| 90 |
-
env = os.getenv("ENV", "production").lower()
|
| 91 |
-
skip_auth = os.getenv("SKIP_AUTH", "false").lower() == "true"
|
| 92 |
-
|
| 93 |
-
if skip_auth:
|
| 94 |
-
if env == "production":
|
| 95 |
-
logger.error("SKIP_AUTH enabled but ENV=production - DENYING")
|
| 96 |
-
return None
|
| 97 |
-
else:
|
| 98 |
-
logger.warning(f"SKIP_AUTH enabled in {env} - using dev user")
|
| 99 |
-
return {'user_id': 'dev-user', ...}
|
| 100 |
-
|
| 101 |
-
return None
|
| 102 |
-
```
|
| 103 |
-
|
| 104 |
-
#### Step C: Verify API Key (database/api_keys.py:134-195)
|
| 105 |
-
|
| 106 |
-
```python
|
| 107 |
-
def verify_api_key(api_key: str) -> Optional[Dict[str, str]]:
|
| 108 |
-
"""Verify API key and return user info"""
|
| 109 |
-
|
| 110 |
-
if not api_key or not api_key.startswith("fm_"):
|
| 111 |
-
return None
|
| 112 |
-
|
| 113 |
-
# Hash the provided key
|
| 114 |
-
api_key_hash = hashlib.sha256(api_key.encode()).hexdigest()
|
| 115 |
-
|
| 116 |
-
# Look up in database
|
| 117 |
-
cursor.execute("""
|
| 118 |
-
SELECT user_id, email, name, is_active
|
| 119 |
-
FROM api_keys
|
| 120 |
-
WHERE api_key_hash = %s
|
| 121 |
-
""", (api_key_hash,))
|
| 122 |
-
|
| 123 |
-
result = cursor.fetchone()
|
| 124 |
-
|
| 125 |
-
if not result or not result['is_active']:
|
| 126 |
-
return None
|
| 127 |
-
|
| 128 |
-
# Access RealDictRow fields by key (FIXED - was using tuple unpacking)
|
| 129 |
-
user_id = result['user_id'] # e.g., 'user_a85ddf4d664b'
|
| 130 |
-
email = result['email']
|
| 131 |
-
name = result['name']
|
| 132 |
-
|
| 133 |
-
# Update last_used_at timestamp
|
| 134 |
-
cursor.execute("""
|
| 135 |
-
UPDATE api_keys
|
| 136 |
-
SET last_used_at = CURRENT_TIMESTAMP
|
| 137 |
-
WHERE api_key_hash = %s
|
| 138 |
-
""", (api_key_hash,))
|
| 139 |
-
|
| 140 |
-
return {
|
| 141 |
-
'user_id': user_id,
|
| 142 |
-
'email': email,
|
| 143 |
-
'name': name or 'FleetMind User',
|
| 144 |
-
'scopes': ['orders:read', 'orders:write', 'drivers:read', 'drivers:write', 'assignments:manage']
|
| 145 |
-
}
|
| 146 |
-
```
|
| 147 |
-
|
| 148 |
-
#### Step D: Session Context (server.py:47-48)
|
| 149 |
-
|
| 150 |
-
User info is stored per-request using ContextVar:
|
| 151 |
-
|
| 152 |
-
```python
|
| 153 |
-
from contextvars import ContextVar
|
| 154 |
-
current_user: ContextVar[Optional[Dict]] = ContextVar('current_user', default=None)
|
| 155 |
-
```
|
| 156 |
-
|
| 157 |
-
---
|
| 158 |
-
|
| 159 |
-
### 4. Tool Handler Pattern (chat/tools.py)
|
| 160 |
-
|
| 161 |
-
Every tool extracts `user_id` from the authenticated user:
|
| 162 |
-
|
| 163 |
-
```python
|
| 164 |
-
async def handle_create_order(input: CreateOrderInput) -> CreateOrderResult:
|
| 165 |
-
# Get authenticated user
|
| 166 |
-
user = get_authenticated_user()
|
| 167 |
-
|
| 168 |
-
# Extract user_id
|
| 169 |
-
user_id = user.get('user_id') if user else None
|
| 170 |
-
|
| 171 |
-
# Validate with ENV check
|
| 172 |
-
if not user_id:
|
| 173 |
-
env = os.getenv("ENV", "production").lower()
|
| 174 |
-
skip_auth = os.getenv("SKIP_AUTH", "false").lower() == "true"
|
| 175 |
-
|
| 176 |
-
if skip_auth and env != "production":
|
| 177 |
-
user_id = "dev-user"
|
| 178 |
-
else:
|
| 179 |
-
return {"error": "Authentication required"}
|
| 180 |
-
|
| 181 |
-
# Use in database query
|
| 182 |
-
cursor.execute("""
|
| 183 |
-
INSERT INTO orders (order_id, user_id, customer_name, ...)
|
| 184 |
-
VALUES (%s, %s, %s, ...)
|
| 185 |
-
""", (order_id, user_id, customer_name, ...))
|
| 186 |
-
|
| 187 |
-
return {"success": True, "order_id": order_id}
|
| 188 |
-
```
|
| 189 |
-
|
| 190 |
-
**Applied to all 29 tools:**
|
| 191 |
-
- 8 Order Management tools
|
| 192 |
-
- 8 Driver Management tools
|
| 193 |
-
- 8 Assignment Management tools
|
| 194 |
-
- 3 Geocoding & Routing tools
|
| 195 |
-
- 2 Bulk Operations tools
|
| 196 |
-
|
| 197 |
-
---
|
| 198 |
-
|
| 199 |
-
### 5. Database Schema (Multi-Tenant)
|
| 200 |
-
|
| 201 |
-
All core tables have `user_id` column for data isolation:
|
| 202 |
-
|
| 203 |
-
```sql
|
| 204 |
-
-- orders table
|
| 205 |
-
CREATE TABLE orders (
|
| 206 |
-
order_id VARCHAR(50) PRIMARY KEY,
|
| 207 |
-
user_id VARCHAR(100) NOT NULL, -- Multi-tenant isolation
|
| 208 |
-
customer_name VARCHAR(255) NOT NULL,
|
| 209 |
-
delivery_address TEXT NOT NULL,
|
| 210 |
-
status VARCHAR(20),
|
| 211 |
-
...
|
| 212 |
-
FOREIGN KEY (user_id) REFERENCES api_keys(user_id)
|
| 213 |
-
);
|
| 214 |
-
CREATE INDEX idx_orders_user_id ON orders(user_id);
|
| 215 |
-
CREATE INDEX idx_orders_user_status ON orders(user_id, status);
|
| 216 |
-
|
| 217 |
-
-- drivers table
|
| 218 |
-
CREATE TABLE drivers (
|
| 219 |
-
driver_id VARCHAR(50) PRIMARY KEY,
|
| 220 |
-
user_id VARCHAR(100) NOT NULL, -- Multi-tenant isolation
|
| 221 |
-
name VARCHAR(255) NOT NULL,
|
| 222 |
-
status VARCHAR(20),
|
| 223 |
-
...
|
| 224 |
-
FOREIGN KEY (user_id) REFERENCES api_keys(user_id)
|
| 225 |
-
);
|
| 226 |
-
CREATE INDEX idx_drivers_user_id ON drivers(user_id);
|
| 227 |
-
CREATE INDEX idx_drivers_user_status ON drivers(user_id, status);
|
| 228 |
-
|
| 229 |
-
-- assignments table
|
| 230 |
-
CREATE TABLE assignments (
|
| 231 |
-
assignment_id VARCHAR(50) PRIMARY KEY,
|
| 232 |
-
user_id VARCHAR(100) NOT NULL, -- Multi-tenant isolation
|
| 233 |
-
order_id VARCHAR(50),
|
| 234 |
-
driver_id VARCHAR(50),
|
| 235 |
-
status VARCHAR(20),
|
| 236 |
-
...
|
| 237 |
-
FOREIGN KEY (user_id) REFERENCES api_keys(user_id)
|
| 238 |
-
);
|
| 239 |
-
CREATE INDEX idx_assignments_user_id ON assignments(user_id);
|
| 240 |
-
```
|
| 241 |
-
|
| 242 |
-
---
|
| 243 |
-
|
| 244 |
-
### 6. MCP Resources (server.py:145-218)
|
| 245 |
-
|
| 246 |
-
Resources also enforce user_id filtering:
|
| 247 |
-
|
| 248 |
-
```python
|
| 249 |
-
@mcp.resource("orders://all")
|
| 250 |
-
def get_orders_resource() -> str:
|
| 251 |
-
"""Get all orders for authenticated user"""
|
| 252 |
-
user = get_authenticated_user()
|
| 253 |
-
if not user:
|
| 254 |
-
return json.dumps({"error": "Authentication required"})
|
| 255 |
-
|
| 256 |
-
query = """
|
| 257 |
-
SELECT * FROM orders
|
| 258 |
-
WHERE user_id = %s
|
| 259 |
-
AND created_at > NOW() - INTERVAL '30 days'
|
| 260 |
-
ORDER BY created_at DESC
|
| 261 |
-
LIMIT 1000
|
| 262 |
-
"""
|
| 263 |
-
orders = execute_query(query, (user['user_id'],))
|
| 264 |
-
return json.dumps(orders, default=str)
|
| 265 |
-
```
|
| 266 |
-
|
| 267 |
-
---
|
| 268 |
-
|
| 269 |
-
## Security Features
|
| 270 |
-
|
| 271 |
-
### 1. Hashed Storage
|
| 272 |
-
- API keys hashed with SHA-256 before storage
|
| 273 |
-
- Never stored in plaintext
|
| 274 |
-
- Can't be reverse-engineered from database
|
| 275 |
-
|
| 276 |
-
### 2. One-Time Display
|
| 277 |
-
- API keys shown only once during generation
|
| 278 |
-
- User must copy immediately
|
| 279 |
-
- Can't be retrieved later
|
| 280 |
-
|
| 281 |
-
### 3. Deterministic User IDs
|
| 282 |
-
- Same email always generates same user_id
|
| 283 |
-
- Enables key rotation without losing data
|
| 284 |
-
- user_id persists across key regeneration
|
| 285 |
-
|
| 286 |
-
### 4. Database-Level Isolation
|
| 287 |
-
- All queries filter by user_id
|
| 288 |
-
- Impossible to access other users' data
|
| 289 |
-
- Enforced at database level with indexes
|
| 290 |
-
|
| 291 |
-
### 5. Production Safeguards
|
| 292 |
-
- SKIP_AUTH only works when ENV != production
|
| 293 |
-
- Automatic blocking of dev bypass in production
|
| 294 |
-
- ENV-based security controls
|
| 295 |
-
|
| 296 |
-
### 6. Session Context
|
| 297 |
-
- ContextVar for thread-safe user info
|
| 298 |
-
- Per-request isolation
|
| 299 |
-
- No cross-request data leakage
|
| 300 |
-
|
| 301 |
-
---
|
| 302 |
-
|
| 303 |
-
## Environment Configuration
|
| 304 |
-
|
| 305 |
-
```bash
|
| 306 |
-
# Environment mode (controls SKIP_AUTH behavior)
|
| 307 |
-
ENV=development # Options: development, staging, production
|
| 308 |
-
|
| 309 |
-
# Authentication bypass (ONLY works when ENV != production)
|
| 310 |
-
SKIP_AUTH=false # Set to true ONLY for local testing
|
| 311 |
-
|
| 312 |
-
# Database connection
|
| 313 |
-
DB_HOST=your-postgres-host.neon.tech
|
| 314 |
-
DB_PORT=5432
|
| 315 |
-
DB_NAME=fleetmind
|
| 316 |
-
DB_USER=your_user
|
| 317 |
-
DB_PASSWORD=your_password
|
| 318 |
-
```
|
| 319 |
-
|
| 320 |
-
---
|
| 321 |
-
|
| 322 |
-
## API Key Management
|
| 323 |
-
|
| 324 |
-
### Generate Key
|
| 325 |
-
|
| 326 |
-
```bash
|
| 327 |
-
python generate_api_key.py --email user@example.com --name "John Doe"
|
| 328 |
-
```
|
| 329 |
-
|
| 330 |
-
Or visit: `http://localhost:7860/generate-key`
|
| 331 |
-
|
| 332 |
-
### List Keys
|
| 333 |
-
|
| 334 |
-
```bash
|
| 335 |
-
python generate_api_key.py --list
|
| 336 |
-
```
|
| 337 |
-
|
| 338 |
-
Output:
|
| 339 |
-
```
|
| 340 |
-
user_id email key_preview created_at last_used_at active
|
| 341 |
-
user_a85ddf4d664b admin@gmail.com fm_3bGKV40B4... 2025-11-19 10:30:00 2025-11-19 15:45:00 True
|
| 342 |
-
```
|
| 343 |
-
|
| 344 |
-
### Revoke Key
|
| 345 |
-
|
| 346 |
-
```bash
|
| 347 |
-
python generate_api_key.py --revoke user@example.com
|
| 348 |
-
```
|
| 349 |
-
|
| 350 |
-
---
|
| 351 |
-
|
| 352 |
-
## Visual Flow Diagram
|
| 353 |
-
|
| 354 |
-
```
|
| 355 |
-
┌─────────────────────────────────────────────────────┐
|
| 356 |
-
│ 1. User generates API key via /generate-key │
|
| 357 |
-
│ - Email: admin@gmail.com │
|
| 358 |
-
│ - API Key: fm_3bGKV40B47NJ43zQ_4d3j-60a2gyo... │
|
| 359 |
-
│ - User ID: user_a85ddf4d664b (deterministic) │
|
| 360 |
-
└─────────────────┬───────────────────────────────────┘
|
| 361 |
-
│
|
| 362 |
-
↓
|
| 363 |
-
┌───────────────────────────────────────��─────────────┐
|
| 364 |
-
│ 2. Stored in api_keys table │
|
| 365 |
-
│ - user_id: user_a85ddf4d664b │
|
| 366 |
-
│ - api_key_hash: SHA-256(fm_3bGKV...) │
|
| 367 |
-
│ - email: admin@gmail.com │
|
| 368 |
-
└─────────────────┬───────────────────────────────────┘
|
| 369 |
-
│
|
| 370 |
-
↓
|
| 371 |
-
┌─────────────────────────────────────────────────────┐
|
| 372 |
-
│ 3. User adds to Claude Desktop config │
|
| 373 |
-
│ URL: /sse?api_key=fm_3bGKV40B47... │
|
| 374 |
-
└─────────────────┬───────────────────────────────────┘
|
| 375 |
-
│
|
| 376 |
-
↓
|
| 377 |
-
┌─────────────────────────────────────────────────────┐
|
| 378 |
-
│ 4. Server extracts & validates API key │
|
| 379 |
-
│ - Extract from query params │
|
| 380 |
-
│ - Hash with SHA-256 │
|
| 381 |
-
│ - Look up in database │
|
| 382 |
-
│ - Return user_id: user_a85ddf4d664b │
|
| 383 |
-
└─────────────────┬───────────────────────────────────┘
|
| 384 |
-
│
|
| 385 |
-
↓
|
| 386 |
-
┌─────────────────────────────────────────────────────┐
|
| 387 |
-
│ 5. Tool calls extract user_id │
|
| 388 |
-
│ user = get_authenticated_user() │
|
| 389 |
-
│ user_id = user['user_id'] │
|
| 390 |
-
└─────────────────┬───────────────────────────────────┘
|
| 391 |
-
│
|
| 392 |
-
↓
|
| 393 |
-
┌─────────────────────────────────────────────────────┐
|
| 394 |
-
│ 6. Database queries filter by user_id │
|
| 395 |
-
│ SELECT * FROM orders WHERE user_id = %s │
|
| 396 |
-
│ INSERT INTO orders (..., user_id, ...) │
|
| 397 |
-
└─────────────────┬───────────────────────────────────┘
|
| 398 |
-
│
|
| 399 |
-
↓
|
| 400 |
-
┌─────────────────────────────────────────────────────┐
|
| 401 |
-
│ 7. Result: Complete multi-tenant data isolation │
|
| 402 |
-
│ User only sees data with user_id=user_a85ddf... │
|
| 403 |
-
└─────────────────────────────────────────────────────┘
|
| 404 |
-
```
|
| 405 |
-
|
| 406 |
-
---
|
| 407 |
-
|
| 408 |
-
## Critical Bug Fixed
|
| 409 |
-
|
| 410 |
-
### Issue
|
| 411 |
-
The `verify_api_key()` function at `database/api_keys.py:166` was attempting tuple unpacking on a RealDictRow (dictionary), causing it to return column NAMES instead of VALUES:
|
| 412 |
-
|
| 413 |
-
```python
|
| 414 |
-
# BEFORE (WRONG - returns column names):
|
| 415 |
-
user_id, email, name, is_active = result
|
| 416 |
-
|
| 417 |
-
# Result: {'user_id': 'user_id', 'email': 'email', 'name': 'name'}
|
| 418 |
-
# This caused all orders to have user_id field set to literal string "user_id"
|
| 419 |
-
```
|
| 420 |
-
|
| 421 |
-
### Fix
|
| 422 |
-
Changed to proper dictionary access:
|
| 423 |
-
|
| 424 |
-
```python
|
| 425 |
-
# AFTER (CORRECT - returns actual values):
|
| 426 |
-
user_id = result['user_id']
|
| 427 |
-
email = result['email']
|
| 428 |
-
name = result['name']
|
| 429 |
-
is_active = result['is_active']
|
| 430 |
-
|
| 431 |
-
# Result: {'user_id': 'user_a85ddf4d664b', 'email': 'admin@gmail.com', ...}
|
| 432 |
-
```
|
| 433 |
-
|
| 434 |
-
### Data Cleanup
|
| 435 |
-
Created cleanup script to fix existing records:
|
| 436 |
-
- Found 1 order with incorrect `user_id='user_id'`
|
| 437 |
-
- Updated to correct user_id: `user_a85ddf4d664b`
|
| 438 |
-
- All tables now have correct user_id values
|
| 439 |
-
|
| 440 |
-
---
|
| 441 |
-
|
| 442 |
-
## Documentation Updates
|
| 443 |
-
|
| 444 |
-
### README.md
|
| 445 |
-
- Added comprehensive "Authentication & Security" section
|
| 446 |
-
- Documented complete authentication flow with diagram
|
| 447 |
-
- Explained user_id generation and maintenance
|
| 448 |
-
- Added database schema examples with user_id
|
| 449 |
-
- Documented environment configuration
|
| 450 |
-
|
| 451 |
-
### app.py (Web Landing Page)
|
| 452 |
-
- Added "Authentication & Security" section to home page
|
| 453 |
-
- Explained how authentication works (6 steps)
|
| 454 |
-
- Listed security features with checkmarks
|
| 455 |
-
- Updated feature list to include multi-tenant isolation
|
| 456 |
-
|
| 457 |
-
### app.py (Generate Key Page)
|
| 458 |
-
- Added explanation of what user gets (API key + user_id)
|
| 459 |
-
- Noted that user_id is deterministic from email
|
| 460 |
-
- Added multi-tenant isolation explanation on success page
|
| 461 |
-
- Added authentication flow diagram on success page
|
| 462 |
-
|
| 463 |
-
---
|
| 464 |
-
|
| 465 |
-
## Testing
|
| 466 |
-
|
| 467 |
-
### Verify Authentication
|
| 468 |
-
|
| 469 |
-
```bash
|
| 470 |
-
# Test API key generation
|
| 471 |
-
python generate_api_key.py --email test@example.com
|
| 472 |
-
|
| 473 |
-
# Test verification
|
| 474 |
-
python -c "from database.api_keys import verify_api_key; print(verify_api_key('fm_your_key_here'))"
|
| 475 |
-
|
| 476 |
-
# Expected output:
|
| 477 |
-
# {'user_id': 'user_xxxxx', 'email': 'test@example.com', 'name': 'Test User', 'scopes': [...]}
|
| 478 |
-
```
|
| 479 |
-
|
| 480 |
-
### Verify Data Isolation
|
| 481 |
-
|
| 482 |
-
```bash
|
| 483 |
-
# Create orders with different users
|
| 484 |
-
# User 1: Create order
|
| 485 |
-
# User 2: Try to view User 1's order (should fail)
|
| 486 |
-
|
| 487 |
-
# Verify database filtering
|
| 488 |
-
psql -h host -U user -d fleetmind -c "SELECT order_id, user_id FROM orders;"
|
| 489 |
-
```
|
| 490 |
-
|
| 491 |
-
---
|
| 492 |
-
|
| 493 |
-
## Summary
|
| 494 |
-
|
| 495 |
-
The FleetMind authentication system ensures:
|
| 496 |
-
|
| 497 |
-
1. **Secure API Key Management** - SHA-256 hashing, one-time display
|
| 498 |
-
2. **Deterministic User IDs** - Same email → same user_id
|
| 499 |
-
3. **Complete Data Isolation** - All queries filter by user_id
|
| 500 |
-
4. **Production Safeguards** - ENV-based SKIP_AUTH protection
|
| 501 |
-
5. **Session Context** - Thread-safe user info per request
|
| 502 |
-
6. **Database-Level Enforcement** - Foreign keys and indexes
|
| 503 |
-
7. **Multi-Client Support** - Works with any MCP client via SSE
|
| 504 |
-
|
| 505 |
-
All 29 tools enforce authentication and multi-tenant isolation at every level.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DRIVER_CREATION_GUIDE.md
DELETED
|
@@ -1,318 +0,0 @@
|
|
| 1 |
-
# Driver Creation Feature Guide
|
| 2 |
-
|
| 3 |
-
## ✅ **Feature Status: READY**
|
| 4 |
-
|
| 5 |
-
The driver creation feature has been successfully implemented and tested!
|
| 6 |
-
|
| 7 |
-
---
|
| 8 |
-
|
| 9 |
-
## 🚀 **How to Use**
|
| 10 |
-
|
| 11 |
-
### **Step 1: Restart Your Application**
|
| 12 |
-
|
| 13 |
-
Since we updated Gemini's system prompt and tools, restart the app:
|
| 14 |
-
|
| 15 |
-
```bash
|
| 16 |
-
# Stop the current app (Ctrl+C)
|
| 17 |
-
python ui/app.py
|
| 18 |
-
```
|
| 19 |
-
|
| 20 |
-
---
|
| 21 |
-
|
| 22 |
-
### **Step 2: Create Drivers Using Natural Language**
|
| 23 |
-
|
| 24 |
-
Open the chat at http://127.0.0.1:7860 and type naturally!
|
| 25 |
-
|
| 26 |
-
---
|
| 27 |
-
|
| 28 |
-
## 📝 **Example Commands**
|
| 29 |
-
|
| 30 |
-
### **Example 1: Complete Driver Info**
|
| 31 |
-
|
| 32 |
-
```
|
| 33 |
-
Add new driver Tom Wilson, phone 555-0101, drives a van, plate ABC-123
|
| 34 |
-
```
|
| 35 |
-
|
| 36 |
-
**Gemini will create:**
|
| 37 |
-
- Driver ID: DRV-20251114HHMMSS (auto-generated)
|
| 38 |
-
- Name: Tom Wilson
|
| 39 |
-
- Phone: 555-0101
|
| 40 |
-
- Vehicle: van
|
| 41 |
-
- Plate: ABC-123
|
| 42 |
-
- Status: active (default)
|
| 43 |
-
- Capacity: 1000 kg (default for van)
|
| 44 |
-
|
| 45 |
-
---
|
| 46 |
-
|
| 47 |
-
### **Example 2: Driver with Skills**
|
| 48 |
-
|
| 49 |
-
```
|
| 50 |
-
Create driver Sarah Martinez, phone 555-0202, refrigerated truck, plate XYZ-789,
|
| 51 |
-
skills: medical_certified, refrigerated
|
| 52 |
-
```
|
| 53 |
-
|
| 54 |
-
**Gemini will create:**
|
| 55 |
-
- Name: Sarah Martinez
|
| 56 |
-
- Phone: 555-0202
|
| 57 |
-
- Vehicle: truck
|
| 58 |
-
- Plate: XYZ-789
|
| 59 |
-
- Skills: ["medical_certified", "refrigerated"]
|
| 60 |
-
- Capacity: 1000 kg (default)
|
| 61 |
-
|
| 62 |
-
---
|
| 63 |
-
|
| 64 |
-
### **Example 3: Minimal Info (Name Only)**
|
| 65 |
-
|
| 66 |
-
```
|
| 67 |
-
Add driver Mike Chen
|
| 68 |
-
```
|
| 69 |
-
|
| 70 |
-
**Gemini will create:**
|
| 71 |
-
- Name: Mike Chen
|
| 72 |
-
- Vehicle: van (default)
|
| 73 |
-
- Capacity: 1000 kg (default)
|
| 74 |
-
- Status: active (default)
|
| 75 |
-
- Skills: [] (empty by default)
|
| 76 |
-
|
| 77 |
-
Gemini might ask: "Would you like to provide phone number or vehicle details?"
|
| 78 |
-
|
| 79 |
-
---
|
| 80 |
-
|
| 81 |
-
### **Example 4: Motorcycle Courier**
|
| 82 |
-
|
| 83 |
-
```
|
| 84 |
-
New driver: Lisa Anderson, phone 555-0303, motorcycle, express delivery specialist
|
| 85 |
-
```
|
| 86 |
-
|
| 87 |
-
**Gemini will create:**
|
| 88 |
-
- Name: Lisa Anderson
|
| 89 |
-
- Phone: 555-0303
|
| 90 |
-
- Vehicle: motorcycle
|
| 91 |
-
- Skills: ["express_delivery"]
|
| 92 |
-
- Capacity: 50 kg (you can specify)
|
| 93 |
-
|
| 94 |
-
---
|
| 95 |
-
|
| 96 |
-
## 🎯 **Available Fields**
|
| 97 |
-
|
| 98 |
-
### **Required:**
|
| 99 |
-
- ✅ **name** - Driver's full name
|
| 100 |
-
|
| 101 |
-
### **Optional:**
|
| 102 |
-
- **phone** - Contact number (e.g., "+1-555-0101")
|
| 103 |
-
- **email** - Email address (e.g., "driver@fleet.com")
|
| 104 |
-
- **vehicle_type** - van | truck | car | motorcycle (default: van)
|
| 105 |
-
- **vehicle_plate** - License plate (e.g., "ABC-1234")
|
| 106 |
-
- **capacity_kg** - Cargo weight capacity in kg (default: 1000.0)
|
| 107 |
-
- **capacity_m3** - Cargo volume capacity in m³ (default: 12.0)
|
| 108 |
-
- **skills** - List of certifications/skills:
|
| 109 |
-
- `refrigerated` - Can handle cold storage
|
| 110 |
-
- `medical_certified` - Medical deliveries
|
| 111 |
-
- `fragile_handler` - Fragile items expert
|
| 112 |
-
- `overnight` - Overnight/late deliveries
|
| 113 |
-
- `express_delivery` - Express/rush deliveries
|
| 114 |
-
- **status** - active | busy | offline | unavailable (default: active)
|
| 115 |
-
|
| 116 |
-
---
|
| 117 |
-
|
| 118 |
-
## 🧪 **Testing**
|
| 119 |
-
|
| 120 |
-
### **Test 1: Verify Creation**
|
| 121 |
-
|
| 122 |
-
After creating a driver, check the database:
|
| 123 |
-
|
| 124 |
-
```bash
|
| 125 |
-
python verify_drivers.py
|
| 126 |
-
```
|
| 127 |
-
|
| 128 |
-
This will show all drivers including the newly created one.
|
| 129 |
-
|
| 130 |
-
---
|
| 131 |
-
|
| 132 |
-
### **Test 2: Check in UI**
|
| 133 |
-
|
| 134 |
-
Go to the **Orders** tab in the UI and you should see new drivers available for assignment.
|
| 135 |
-
|
| 136 |
-
---
|
| 137 |
-
|
| 138 |
-
## 📊 **What Happens Behind the Scenes**
|
| 139 |
-
|
| 140 |
-
### **User Input:**
|
| 141 |
-
```
|
| 142 |
-
"Add new driver Tom Wilson, phone 555-0101, drives a van, plate ABC-123"
|
| 143 |
-
```
|
| 144 |
-
|
| 145 |
-
### **Gemini Processing:**
|
| 146 |
-
1. **Parses** your natural language input
|
| 147 |
-
2. **Extracts** driver information:
|
| 148 |
-
- name: "Tom Wilson"
|
| 149 |
-
- phone: "555-0101"
|
| 150 |
-
- vehicle_type: "van"
|
| 151 |
-
- vehicle_plate: "ABC-123"
|
| 152 |
-
3. **Calls** `create_driver` tool with extracted data
|
| 153 |
-
4. **Database** inserts the driver with auto-generated ID
|
| 154 |
-
5. **Returns** confirmation message
|
| 155 |
-
|
| 156 |
-
### **Database Record Created:**
|
| 157 |
-
```sql
|
| 158 |
-
INSERT INTO drivers (
|
| 159 |
-
driver_id, -- DRV-20251114113250 (auto)
|
| 160 |
-
name, -- Tom Wilson
|
| 161 |
-
phone, -- 555-0101
|
| 162 |
-
vehicle_type, -- van
|
| 163 |
-
vehicle_plate, -- ABC-123
|
| 164 |
-
status, -- active (default)
|
| 165 |
-
capacity_kg, -- 1000.0 (default)
|
| 166 |
-
capacity_m3, -- 12.0 (default)
|
| 167 |
-
skills, -- [] (empty)
|
| 168 |
-
current_lat, -- 37.7749 (default SF)
|
| 169 |
-
current_lng, -- -122.4194 (default SF)
|
| 170 |
-
last_location_update -- 2025-11-14 11:32:50
|
| 171 |
-
) VALUES (...)
|
| 172 |
-
```
|
| 173 |
-
|
| 174 |
-
---
|
| 175 |
-
|
| 176 |
-
## 🔄 **Comparison: Orders vs Drivers**
|
| 177 |
-
|
| 178 |
-
### **Creating an Order:**
|
| 179 |
-
```
|
| 180 |
-
User: "Create order for John Doe, 123 Main St SF, phone 555-1234"
|
| 181 |
-
↓
|
| 182 |
-
Gemini: [geocode_address] → [create_order] → "✅ Order created!"
|
| 183 |
-
```
|
| 184 |
-
**2 tools called** (geocoding required for addresses)
|
| 185 |
-
|
| 186 |
-
### **Creating a Driver:**
|
| 187 |
-
```
|
| 188 |
-
User: "Add driver Tom Wilson, phone 555-0101, van"
|
| 189 |
-
↓
|
| 190 |
-
Gemini: [create_driver] → "✅ Driver created!"
|
| 191 |
-
```
|
| 192 |
-
**1 tool called** (no geocoding needed)
|
| 193 |
-
|
| 194 |
-
---
|
| 195 |
-
|
| 196 |
-
## ⚡ **Quick Reference**
|
| 197 |
-
|
| 198 |
-
### **Conversational Examples:**
|
| 199 |
-
|
| 200 |
-
✅ "I need to onboard a new driver named Alex"
|
| 201 |
-
✅ "Add Sarah to the fleet, she drives a truck"
|
| 202 |
-
✅ "New driver: Mike, phone 555-9999, motorcycle"
|
| 203 |
-
✅ "Create driver with medical certification"
|
| 204 |
-
✅ "Add a refrigerated truck driver named Bob"
|
| 205 |
-
|
| 206 |
-
### **Structured Examples:**
|
| 207 |
-
|
| 208 |
-
✅ "Create driver: Name: Tom Wilson, Phone: 555-0101, Vehicle: van, Plate: ABC-123"
|
| 209 |
-
✅ "New driver - Name: Sarah, Email: sarah@fleet.com, Vehicle type: truck, Skills: refrigerated, medical_certified"
|
| 210 |
-
|
| 211 |
-
---
|
| 212 |
-
|
| 213 |
-
## 🎨 **Sample Responses from Gemini**
|
| 214 |
-
|
| 215 |
-
### **Successful Creation:**
|
| 216 |
-
```
|
| 217 |
-
✅ Driver DRV-20251114113250 created successfully!
|
| 218 |
-
|
| 219 |
-
Driver Details:
|
| 220 |
-
• Driver ID: DRV-20251114113250
|
| 221 |
-
• Name: Tom Wilson
|
| 222 |
-
• Phone: 555-0101
|
| 223 |
-
• Vehicle: van (ABC-123)
|
| 224 |
-
• Capacity: 1000 kg
|
| 225 |
-
• Status: Active
|
| 226 |
-
• Skills: None
|
| 227 |
-
|
| 228 |
-
The driver has been added to your fleet and is ready for order assignments!
|
| 229 |
-
```
|
| 230 |
-
|
| 231 |
-
### **Missing Information:**
|
| 232 |
-
```
|
| 233 |
-
I can create a driver for you! I have:
|
| 234 |
-
• Name: Tom Wilson
|
| 235 |
-
|
| 236 |
-
To complete the driver profile, please provide (optional):
|
| 237 |
-
• Phone number
|
| 238 |
-
• Vehicle type (van/truck/car/motorcycle)
|
| 239 |
-
• License plate number
|
| 240 |
-
• Any special skills or certifications
|
| 241 |
-
|
| 242 |
-
Or I can create the driver now with default settings?
|
| 243 |
-
```
|
| 244 |
-
|
| 245 |
-
---
|
| 246 |
-
|
| 247 |
-
## 🛠️ **Technical Details**
|
| 248 |
-
|
| 249 |
-
### **Function:** `handle_create_driver()`
|
| 250 |
-
**Location:** `chat/tools.py:245-331`
|
| 251 |
-
|
| 252 |
-
### **Tool Definition:**
|
| 253 |
-
**Location:** `chat/providers/gemini_provider.py:140-186`
|
| 254 |
-
|
| 255 |
-
### **System Prompt:**
|
| 256 |
-
**Location:** `chat/providers/gemini_provider.py:32-89`
|
| 257 |
-
|
| 258 |
-
---
|
| 259 |
-
|
| 260 |
-
## ✨ **Next Steps**
|
| 261 |
-
|
| 262 |
-
After creating drivers, you can:
|
| 263 |
-
|
| 264 |
-
1. **Assign orders to drivers** (coming soon)
|
| 265 |
-
2. **View driver list** in the UI
|
| 266 |
-
3. **Update driver status** (active/busy/offline)
|
| 267 |
-
4. **Track driver locations** (coming soon)
|
| 268 |
-
|
| 269 |
-
---
|
| 270 |
-
|
| 271 |
-
## 🐛 **Troubleshooting**
|
| 272 |
-
|
| 273 |
-
### **Issue: "Unknown tool: create_driver"**
|
| 274 |
-
|
| 275 |
-
**Solution:** Restart the application to reload the new tools:
|
| 276 |
-
```bash
|
| 277 |
-
# Stop app (Ctrl+C)
|
| 278 |
-
python ui/app.py
|
| 279 |
-
```
|
| 280 |
-
|
| 281 |
-
### **Issue: Driver created but not showing in database**
|
| 282 |
-
|
| 283 |
-
**Solution:** Check database connection and verify:
|
| 284 |
-
```bash
|
| 285 |
-
python verify_drivers.py
|
| 286 |
-
```
|
| 287 |
-
|
| 288 |
-
### **Issue: "Missing required field: name"**
|
| 289 |
-
|
| 290 |
-
**Solution:** Make sure you provide at least the driver's name:
|
| 291 |
-
```
|
| 292 |
-
"Add driver John Smith" ✅
|
| 293 |
-
"Add a new driver" ❌ (no name)
|
| 294 |
-
```
|
| 295 |
-
|
| 296 |
-
---
|
| 297 |
-
|
| 298 |
-
## 📈 **Feature Comparison**
|
| 299 |
-
|
| 300 |
-
| Feature | Orders | Drivers |
|
| 301 |
-
|---------|--------|---------|
|
| 302 |
-
| **Required Fields** | 3 (name, address, contact) | 1 (name) |
|
| 303 |
-
| **Geocoding Needed** | ✅ Yes | ❌ No |
|
| 304 |
-
| **Tools Called** | 2 (geocode + create) | 1 (create) |
|
| 305 |
-
| **Default Values** | Priority, weight | Vehicle type, capacity, status |
|
| 306 |
-
| **Complex Data** | Time windows, coordinates | Skills array, JSON |
|
| 307 |
-
|
| 308 |
-
---
|
| 309 |
-
|
| 310 |
-
## 🎉 **Ready to Use!**
|
| 311 |
-
|
| 312 |
-
Your FleetMind system can now:
|
| 313 |
-
- ✅ Create customer orders
|
| 314 |
-
- ✅ Create delivery drivers
|
| 315 |
-
- ✅ Geocode addresses
|
| 316 |
-
- ✅ Store everything in PostgreSQL
|
| 317 |
-
|
| 318 |
-
Just talk naturally to Gemini and it handles the rest! 🚀
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GITHUB_ACTIONS_SETUP.md
DELETED
|
@@ -1,315 +0,0 @@
|
|
| 1 |
-
# GitHub Actions Auto-Sync Setup Guide
|
| 2 |
-
|
| 3 |
-
This guide walks you through setting up automatic synchronization from your GitHub repository to your Hugging Face Space using GitHub Actions.
|
| 4 |
-
|
| 5 |
-
## ✅ What's Already Done
|
| 6 |
-
|
| 7 |
-
- [x] Created GitHub repository directory: `F:\github-fleetmind-team`
|
| 8 |
-
- [x] Copied all HF Space code to GitHub repo
|
| 9 |
-
- [x] Created GitHub Actions workflows:
|
| 10 |
-
- `.github/workflows/sync-to-huggingface.yml` - Auto-sync on push
|
| 11 |
-
- `.github/workflows/check-file-size.yml` - Check file sizes on PRs
|
| 12 |
-
|
| 13 |
-
## 🎯 What You Need to Do Now
|
| 14 |
-
|
| 15 |
-
### STEP 3: Get Your Hugging Face Token
|
| 16 |
-
|
| 17 |
-
1. **Go to:** https://huggingface.co/settings/tokens
|
| 18 |
-
|
| 19 |
-
2. **Click:** "Create new token"
|
| 20 |
-
|
| 21 |
-
3. **Fill in:**
|
| 22 |
-
- Name: `GitHub Actions Sync`
|
| 23 |
-
- Type: **Write** (important!)
|
| 24 |
-
- Scope: Select **all** or at least:
|
| 25 |
-
- ✅ Write access to repos
|
| 26 |
-
- ✅ Write access to spaces
|
| 27 |
-
|
| 28 |
-
4. **Click:** "Generate token"
|
| 29 |
-
|
| 30 |
-
5. **COPY THE TOKEN** (you won't see it again!)
|
| 31 |
-
- It looks like: `hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`
|
| 32 |
-
|
| 33 |
-
---
|
| 34 |
-
|
| 35 |
-
### STEP 4: Push Code to GitHub
|
| 36 |
-
|
| 37 |
-
Open PowerShell or Command Prompt:
|
| 38 |
-
|
| 39 |
-
```bash
|
| 40 |
-
# Navigate to the GitHub repo
|
| 41 |
-
cd F:\github-fleetmind-team
|
| 42 |
-
|
| 43 |
-
# Add all files
|
| 44 |
-
git add .
|
| 45 |
-
|
| 46 |
-
# Commit
|
| 47 |
-
git commit -m "Initial commit: FleetMind MCP with GitHub Actions sync"
|
| 48 |
-
|
| 49 |
-
# Add GitHub remote (REPLACE YOUR-USERNAME with your actual GitHub username)
|
| 50 |
-
git remote add origin https://github.com/YOUR-USERNAME/fleetmind-mcp.git
|
| 51 |
-
|
| 52 |
-
# Push to GitHub
|
| 53 |
-
git push -u origin main
|
| 54 |
-
```
|
| 55 |
-
|
| 56 |
-
**If you get an error about 'main' branch:**
|
| 57 |
-
```bash
|
| 58 |
-
# Rename branch to main
|
| 59 |
-
git branch -M main
|
| 60 |
-
|
| 61 |
-
# Push again
|
| 62 |
-
git push -u origin main
|
| 63 |
-
```
|
| 64 |
-
|
| 65 |
-
---
|
| 66 |
-
|
| 67 |
-
### STEP 5: Add HF_TOKEN as GitHub Secret
|
| 68 |
-
|
| 69 |
-
1. **Go to your GitHub repo:**
|
| 70 |
-
- URL: `https://github.com/YOUR-USERNAME/fleetmind-mcp`
|
| 71 |
-
|
| 72 |
-
2. **Click:** Settings (top right of repo page)
|
| 73 |
-
|
| 74 |
-
3. **In left sidebar, click:**
|
| 75 |
-
- Secrets and variables → Actions
|
| 76 |
-
|
| 77 |
-
4. **Click:** "New repository secret"
|
| 78 |
-
|
| 79 |
-
5. **Fill in:**
|
| 80 |
-
- Name: `HF_TOKEN`
|
| 81 |
-
- Secret: Paste the Hugging Face token you copied in Step 3
|
| 82 |
-
|
| 83 |
-
6. **Click:** "Add secret"
|
| 84 |
-
|
| 85 |
-
---
|
| 86 |
-
|
| 87 |
-
### STEP 6: Add HF Space as Git Remote (Optional but Recommended)
|
| 88 |
-
|
| 89 |
-
This allows you to manually push to HF Space if needed:
|
| 90 |
-
|
| 91 |
-
```bash
|
| 92 |
-
cd F:\github-fleetmind-team
|
| 93 |
-
|
| 94 |
-
# Add HF Space as a remote
|
| 95 |
-
git remote add space https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai
|
| 96 |
-
|
| 97 |
-
# Verify remotes
|
| 98 |
-
git remote -v
|
| 99 |
-
```
|
| 100 |
-
|
| 101 |
-
You should see:
|
| 102 |
-
```
|
| 103 |
-
origin https://github.com/YOUR-USERNAME/fleetmind-mcp.git (fetch)
|
| 104 |
-
origin https://github.com/YOUR-USERNAME/fleetmind-mcp.git (push)
|
| 105 |
-
space https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai (fetch)
|
| 106 |
-
space https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai (push)
|
| 107 |
-
```
|
| 108 |
-
|
| 109 |
-
---
|
| 110 |
-
|
| 111 |
-
### STEP 7: Test the Auto-Sync
|
| 112 |
-
|
| 113 |
-
Let's make a test change to verify everything works:
|
| 114 |
-
|
| 115 |
-
```bash
|
| 116 |
-
cd F:\github-fleetmind-team
|
| 117 |
-
|
| 118 |
-
# Make a small change to README
|
| 119 |
-
echo "\n\n## 🤖 Auto-Synced with GitHub Actions" >> README.md
|
| 120 |
-
|
| 121 |
-
# Commit the change
|
| 122 |
-
git add README.md
|
| 123 |
-
git commit -m "Test: GitHub Actions auto-sync"
|
| 124 |
-
|
| 125 |
-
# Push to GitHub
|
| 126 |
-
git push origin main
|
| 127 |
-
```
|
| 128 |
-
|
| 129 |
-
**What happens next:**
|
| 130 |
-
|
| 131 |
-
1. ✅ Code pushes to GitHub
|
| 132 |
-
2. ✅ GitHub Actions triggers automatically
|
| 133 |
-
3. ✅ Workflow runs and pushes to HF Space
|
| 134 |
-
4. ✅ HF Space rebuilds with new code
|
| 135 |
-
|
| 136 |
-
**Check the progress:**
|
| 137 |
-
|
| 138 |
-
1. **On GitHub:**
|
| 139 |
-
- Go to: `https://github.com/YOUR-USERNAME/fleetmind-mcp/actions`
|
| 140 |
-
- You'll see "Sync to Hugging Face Space" workflow running
|
| 141 |
-
- Wait for green checkmark ✅
|
| 142 |
-
|
| 143 |
-
2. **On Hugging Face:**
|
| 144 |
-
- Go to: https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai
|
| 145 |
-
- Check the "Files" tab - you should see the new commit
|
| 146 |
-
- Space will rebuild automatically
|
| 147 |
-
|
| 148 |
-
---
|
| 149 |
-
|
| 150 |
-
## 🎉 Success! Your Setup is Complete
|
| 151 |
-
|
| 152 |
-
From now on:
|
| 153 |
-
|
| 154 |
-
### **Team Members Workflow:**
|
| 155 |
-
|
| 156 |
-
```bash
|
| 157 |
-
# 1. Clone the GitHub repo (one time)
|
| 158 |
-
git clone https://github.com/YOUR-USERNAME/fleetmind-mcp.git
|
| 159 |
-
cd fleetmind-mcp
|
| 160 |
-
|
| 161 |
-
# 2. Make changes
|
| 162 |
-
# ... edit files ...
|
| 163 |
-
|
| 164 |
-
# 3. Commit and push to GitHub
|
| 165 |
-
git add .
|
| 166 |
-
git commit -m "Add new feature"
|
| 167 |
-
git push origin main
|
| 168 |
-
|
| 169 |
-
# 4. GitHub Actions automatically syncs to HF Space
|
| 170 |
-
# ✨ DONE! Nothing else needed!
|
| 171 |
-
```
|
| 172 |
-
|
| 173 |
-
### **What GitHub Actions Does:**
|
| 174 |
-
|
| 175 |
-
Every time someone pushes to the `main` branch on GitHub:
|
| 176 |
-
|
| 177 |
-
1. ✅ GitHub Actions workflow starts
|
| 178 |
-
2. ✅ Checks out the code
|
| 179 |
-
3. ✅ Pushes to HF Space using your HF_TOKEN
|
| 180 |
-
4. ✅ HF Space rebuilds automatically
|
| 181 |
-
5. ✅ Your app goes live with the new changes
|
| 182 |
-
|
| 183 |
-
---
|
| 184 |
-
|
| 185 |
-
## 📋 Adding Team Members
|
| 186 |
-
|
| 187 |
-
### On GitHub (Full Access):
|
| 188 |
-
|
| 189 |
-
1. Go to: `https://github.com/YOUR-USERNAME/fleetmind-mcp/settings/access`
|
| 190 |
-
2. Click "Add people"
|
| 191 |
-
3. Enter their GitHub username
|
| 192 |
-
4. Select role: **Write** (they can push directly)
|
| 193 |
-
5. Click "Add"
|
| 194 |
-
|
| 195 |
-
### On Hugging Face (Documentation Only):
|
| 196 |
-
|
| 197 |
-
Team members don't need HF access! The GitHub Actions bot handles all HF Space updates using your HF_TOKEN.
|
| 198 |
-
|
| 199 |
-
Just make sure they're listed in the README:
|
| 200 |
-
- Edit `README.md` lines 29-42
|
| 201 |
-
- Add their real names and HF usernames
|
| 202 |
-
- Commit and push
|
| 203 |
-
|
| 204 |
-
---
|
| 205 |
-
|
| 206 |
-
## 🔧 Troubleshooting
|
| 207 |
-
|
| 208 |
-
### ❌ "Error: Process completed with exit code 128"
|
| 209 |
-
|
| 210 |
-
**Solution:** Check that HF_TOKEN is correctly set in GitHub Secrets
|
| 211 |
-
- Go to: `https://github.com/YOUR-USERNAME/fleetmind-mcp/settings/secrets/actions`
|
| 212 |
-
- Verify `HF_TOKEN` exists
|
| 213 |
-
- If not, add it (see Step 5)
|
| 214 |
-
|
| 215 |
-
### ❌ "Error: failed to push some refs"
|
| 216 |
-
|
| 217 |
-
**Solution:** HF Space has newer commits
|
| 218 |
-
```bash
|
| 219 |
-
# Pull from HF Space first
|
| 220 |
-
git pull space main --allow-unrelated-histories
|
| 221 |
-
|
| 222 |
-
# Then push again
|
| 223 |
-
git push origin main
|
| 224 |
-
```
|
| 225 |
-
|
| 226 |
-
### ❌ GitHub Actions workflow doesn't run
|
| 227 |
-
|
| 228 |
-
**Solution:** Check that workflow file is in correct location
|
| 229 |
-
- Must be: `.github/workflows/sync-to-huggingface.yml`
|
| 230 |
-
- Check GitHub repo → Actions tab
|
| 231 |
-
- Click "I understand my workflows, go ahead and enable them"
|
| 232 |
-
|
| 233 |
-
### ❌ Files larger than 10MB
|
| 234 |
-
|
| 235 |
-
**Solution:** Use Git LFS
|
| 236 |
-
```bash
|
| 237 |
-
# Install Git LFS
|
| 238 |
-
git lfs install
|
| 239 |
-
|
| 240 |
-
# Track large files
|
| 241 |
-
git lfs track "*.psd" # Example: Photoshop files
|
| 242 |
-
git lfs track "*.pkl" # Example: Model files
|
| 243 |
-
|
| 244 |
-
# Commit .gitattributes
|
| 245 |
-
git add .gitattributes
|
| 246 |
-
git commit -m "Add Git LFS tracking"
|
| 247 |
-
git push
|
| 248 |
-
```
|
| 249 |
-
|
| 250 |
-
---
|
| 251 |
-
|
| 252 |
-
## 🚀 Advanced: Manual Sync
|
| 253 |
-
|
| 254 |
-
If you ever need to manually sync to HF Space:
|
| 255 |
-
|
| 256 |
-
```bash
|
| 257 |
-
cd F:\github-fleetmind-team
|
| 258 |
-
|
| 259 |
-
# Option 1: Push directly to HF Space remote
|
| 260 |
-
git push space main
|
| 261 |
-
|
| 262 |
-
# Option 2: Trigger GitHub Actions manually
|
| 263 |
-
# Go to: https://github.com/YOUR-USERNAME/fleetmind-mcp/actions
|
| 264 |
-
# Click "Sync to Hugging Face Space"
|
| 265 |
-
# Click "Run workflow" → "Run workflow"
|
| 266 |
-
```
|
| 267 |
-
|
| 268 |
-
---
|
| 269 |
-
|
| 270 |
-
## 📊 Workflow Diagram
|
| 271 |
-
|
| 272 |
-
```
|
| 273 |
-
Developer → GitHub Repo → GitHub Actions → HF Space → Live App
|
| 274 |
-
↓ ↓ ↓ ↓ ↓
|
| 275 |
-
Codes Receives Triggers Updates Rebuilds
|
| 276 |
-
Push Event Sync with new & Serves
|
| 277 |
-
code
|
| 278 |
-
```
|
| 279 |
-
|
| 280 |
-
---
|
| 281 |
-
|
| 282 |
-
## 🎯 Benefits of This Setup
|
| 283 |
-
|
| 284 |
-
✅ **Team Collaboration:** Everyone works on GitHub (familiar workflow)
|
| 285 |
-
✅ **Automatic Deployment:** Push to GitHub = Deploy to HF Space
|
| 286 |
-
✅ **No Permission Issues:** GitHub Actions uses your HF_TOKEN
|
| 287 |
-
✅ **Version Control:** Full history on GitHub
|
| 288 |
-
✅ **CI/CD Ready:** Can add tests, linting, etc.
|
| 289 |
-
✅ **Hackathon Compliant:** Final Space is on HF organization
|
| 290 |
-
|
| 291 |
-
---
|
| 292 |
-
|
| 293 |
-
## 📝 Next Steps
|
| 294 |
-
|
| 295 |
-
After setup is complete:
|
| 296 |
-
|
| 297 |
-
1. **Invite team members to GitHub repo**
|
| 298 |
-
2. **Update README with team information**
|
| 299 |
-
3. **Continue building your project**
|
| 300 |
-
4. **Create demo video (1-5 minutes)**
|
| 301 |
-
5. **Post on social media**
|
| 302 |
-
6. **Submit before November 30, 2025**
|
| 303 |
-
|
| 304 |
-
---
|
| 305 |
-
|
| 306 |
-
## 📚 Resources
|
| 307 |
-
|
| 308 |
-
- **GitHub Actions Docs:** https://docs.github.com/en/actions
|
| 309 |
-
- **HF Spaces Docs:** https://huggingface.co/docs/hub/spaces
|
| 310 |
-
- **Git LFS:** https://git-lfs.github.com/
|
| 311 |
-
- **Hackathon Info:** https://huggingface.co/MCP-1st-Birthday
|
| 312 |
-
|
| 313 |
-
---
|
| 314 |
-
|
| 315 |
-
**You're all set! Happy coding! 🚀**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
HUGGINGFACE_DEPLOY.md
DELETED
|
@@ -1,215 +0,0 @@
|
|
| 1 |
-
# Deploy FleetMind MCP to HuggingFace Spaces
|
| 2 |
-
|
| 3 |
-
## Prerequisites
|
| 4 |
-
|
| 5 |
-
- HuggingFace account
|
| 6 |
-
- Git installed locally
|
| 7 |
-
- PostgreSQL database (from previous setup)
|
| 8 |
-
|
| 9 |
-
## Deployment Steps
|
| 10 |
-
|
| 11 |
-
### 1. Create HuggingFace Space
|
| 12 |
-
|
| 13 |
-
1. Go to https://huggingface.co/new-space
|
| 14 |
-
2. **Name**: `fleetmind-dispatch-ai` (or your choice)
|
| 15 |
-
3. **SDK**: Select `Docker`
|
| 16 |
-
4. **Visibility**: Public (or Private)
|
| 17 |
-
5. Click **Create Space**
|
| 18 |
-
|
| 19 |
-
### 2. Configure Environment Variables
|
| 20 |
-
|
| 21 |
-
In your Space settings, add these **Secrets**:
|
| 22 |
-
|
| 23 |
-
```
|
| 24 |
-
# Database Configuration
|
| 25 |
-
DATABASE_URL=postgresql://user:password@host:5432/fleetmind_mcp
|
| 26 |
-
|
| 27 |
-
# Google Maps API (optional - only if using routing features)
|
| 28 |
-
GOOGLE_MAPS_API_KEY=your_google_maps_api_key
|
| 29 |
-
|
| 30 |
-
# Port Configuration (HuggingFace automatically sets this to 7860)
|
| 31 |
-
PORT=7860
|
| 32 |
-
```
|
| 33 |
-
|
| 34 |
-
**IMPORTANT**: Do NOT add PORT to secrets on HuggingFace - it's automatically set!
|
| 35 |
-
|
| 36 |
-
### 3. Push Your Code
|
| 37 |
-
|
| 38 |
-
#### Option A: Using HuggingFace Web Interface
|
| 39 |
-
|
| 40 |
-
1. In your Space, click **Files** → **Add file**
|
| 41 |
-
2. Upload all files from your `fleetmind-mcp` directory:
|
| 42 |
-
- `app.py`
|
| 43 |
-
- `proxy.py`
|
| 44 |
-
- `start_with_proxy.py`
|
| 45 |
-
- `server.py`
|
| 46 |
-
- `requirements.txt`
|
| 47 |
-
- `Dockerfile`
|
| 48 |
-
- `.env.example`
|
| 49 |
-
- All other Python files
|
| 50 |
-
|
| 51 |
-
#### Option B: Using Git (Recommended)
|
| 52 |
-
|
| 53 |
-
```bash
|
| 54 |
-
# Clone your HuggingFace space
|
| 55 |
-
git clone https://huggingface.co/spaces/YOUR_USERNAME/fleetmind-dispatch-ai
|
| 56 |
-
cd fleetmind-dispatch-ai
|
| 57 |
-
|
| 58 |
-
# Copy your files
|
| 59 |
-
cp -r /path/to/fleetmind-mcp/* .
|
| 60 |
-
|
| 61 |
-
# Commit and push
|
| 62 |
-
git add .
|
| 63 |
-
git commit -m "Deploy FleetMind MCP with authentication proxy"
|
| 64 |
-
git push
|
| 65 |
-
```
|
| 66 |
-
|
| 67 |
-
### 4. Verify Deployment
|
| 68 |
-
|
| 69 |
-
1. HuggingFace will automatically build your Docker image
|
| 70 |
-
2. Check the **Logs** tab for build progress
|
| 71 |
-
3. Look for these success messages:
|
| 72 |
-
```
|
| 73 |
-
[1/2] Starting FastMCP server on port 7861...
|
| 74 |
-
[OK] FastMCP server running
|
| 75 |
-
[2/2] Starting authentication proxy on port 7860...
|
| 76 |
-
[OK] Auth proxy running
|
| 77 |
-
[OK] FleetMind MCP Server is READY!
|
| 78 |
-
```
|
| 79 |
-
|
| 80 |
-
4. Your Space will be available at:
|
| 81 |
-
```
|
| 82 |
-
https://YOUR_USERNAME-fleetmind-dispatch-ai.hf.space
|
| 83 |
-
```
|
| 84 |
-
|
| 85 |
-
### 5. Connect from Claude Desktop
|
| 86 |
-
|
| 87 |
-
Update your Claude Desktop config:
|
| 88 |
-
|
| 89 |
-
```json
|
| 90 |
-
{
|
| 91 |
-
"mcpServers": {
|
| 92 |
-
"fleetmind_hf": {
|
| 93 |
-
"command": "npx",
|
| 94 |
-
"args": [
|
| 95 |
-
"mcp-remote",
|
| 96 |
-
"https://YOUR_USERNAME-fleetmind-dispatch-ai.hf.space/sse?api_key=fm_YOUR_API_KEY"
|
| 97 |
-
]
|
| 98 |
-
}
|
| 99 |
-
}
|
| 100 |
-
}
|
| 101 |
-
```
|
| 102 |
-
|
| 103 |
-
**Replace**:
|
| 104 |
-
- `YOUR_USERNAME` with your HuggingFace username
|
| 105 |
-
- `fm_YOUR_API_KEY` with an actual API key from your database
|
| 106 |
-
|
| 107 |
-
### 6. Test the Connection
|
| 108 |
-
|
| 109 |
-
1. Restart Claude Desktop
|
| 110 |
-
2. Try a command: "How many orders are there?"
|
| 111 |
-
3. Check Space logs for:
|
| 112 |
-
```
|
| 113 |
-
[AUTH] Captured API key from SSE connection
|
| 114 |
-
[AUTH] Linked session ... to API key
|
| 115 |
-
[AUTH] Authenticated user: <username>
|
| 116 |
-
```
|
| 117 |
-
|
| 118 |
-
## Architecture on HuggingFace
|
| 119 |
-
|
| 120 |
-
```
|
| 121 |
-
Claude Desktop
|
| 122 |
-
↓
|
| 123 |
-
https://YOU-fleetmind.hf.space/sse?api_key=xxx
|
| 124 |
-
↓
|
| 125 |
-
Proxy (0.0.0.0:7860)
|
| 126 |
-
↓ (captures & injects API key)
|
| 127 |
-
FastMCP Server (localhost:7861)
|
| 128 |
-
↓
|
| 129 |
-
PostgreSQL Database
|
| 130 |
-
```
|
| 131 |
-
|
| 132 |
-
## Troubleshooting
|
| 133 |
-
|
| 134 |
-
### Build Fails
|
| 135 |
-
|
| 136 |
-
**Check Dockerfile syntax:**
|
| 137 |
-
```bash
|
| 138 |
-
# Test locally first
|
| 139 |
-
docker build -t fleetmind-test .
|
| 140 |
-
docker run -p 7860:7860 fleetmind-test
|
| 141 |
-
```
|
| 142 |
-
|
| 143 |
-
### Space Crashes
|
| 144 |
-
|
| 145 |
-
**Check logs for:**
|
| 146 |
-
- Database connection errors
|
| 147 |
-
- Missing environment variables
|
| 148 |
-
- Port conflicts
|
| 149 |
-
|
| 150 |
-
**Common fixes:**
|
| 151 |
-
```bash
|
| 152 |
-
# Verify DATABASE_URL format
|
| 153 |
-
postgresql://user:password@host:5432/database_name
|
| 154 |
-
|
| 155 |
-
# Check if FastMCP started before proxy
|
| 156 |
-
# Look for "FastMCP server running" before "Auth proxy running"
|
| 157 |
-
```
|
| 158 |
-
|
| 159 |
-
### Authentication Fails
|
| 160 |
-
|
| 161 |
-
**Check:**
|
| 162 |
-
1. API key is valid in database:
|
| 163 |
-
```sql
|
| 164 |
-
SELECT * FROM api_keys WHERE key_hash = encode(sha256('fm_YOUR_KEY'::bytea), 'hex');
|
| 165 |
-
```
|
| 166 |
-
|
| 167 |
-
2. Logs show key capture:
|
| 168 |
-
```
|
| 169 |
-
[AUTH] Captured API key from SSE connection: fm_...
|
| 170 |
-
```
|
| 171 |
-
|
| 172 |
-
3. Session is linked:
|
| 173 |
-
```
|
| 174 |
-
[AUTH] Linked session abc123... to API key
|
| 175 |
-
```
|
| 176 |
-
|
| 177 |
-
### Connection Times Out
|
| 178 |
-
|
| 179 |
-
**Possible causes:**
|
| 180 |
-
- HuggingFace Space is sleeping (first request takes longer)
|
| 181 |
-
- Database is unreachable from HuggingFace
|
| 182 |
-
- Firewall blocking database access
|
| 183 |
-
|
| 184 |
-
**Fix:**
|
| 185 |
-
- For database: Ensure PostgreSQL allows connections from HuggingFace IPs
|
| 186 |
-
- For sleeping: Use a monitoring service to ping your Space every 10 minutes
|
| 187 |
-
|
| 188 |
-
## Performance Tips
|
| 189 |
-
|
| 190 |
-
1. **Keep Space Active**: Free tier sleeps after inactivity
|
| 191 |
-
- Use UptimeRobot to ping `/health` every 5 minutes
|
| 192 |
-
|
| 193 |
-
2. **Database Connection Pooling**: Already configured in `server.py`
|
| 194 |
-
|
| 195 |
-
3. **Monitor Logs**: Check for authentication errors and slow queries
|
| 196 |
-
|
| 197 |
-
## Multi-Tenant Setup
|
| 198 |
-
|
| 199 |
-
Each user can have their own API key:
|
| 200 |
-
|
| 201 |
-
```json
|
| 202 |
-
// User A's Claude Desktop
|
| 203 |
-
"args": ["mcp-remote", "https://...hf.space/sse?api_key=fm_USER_A_KEY"]
|
| 204 |
-
|
| 205 |
-
// User B's Claude Desktop
|
| 206 |
-
"args": ["mcp-remote", "https://...hf.space/sse?api_key=fm_USER_B_KEY"]
|
| 207 |
-
```
|
| 208 |
-
|
| 209 |
-
All requests are properly isolated by API key!
|
| 210 |
-
|
| 211 |
-
## Support
|
| 212 |
-
|
| 213 |
-
- **GitHub**: https://github.com/mashrur-rahman-fahim/fleetmind-mcp
|
| 214 |
-
- **HuggingFace**: Your Space settings �� Community tab
|
| 215 |
-
- **Docs**: https://docs.fastmcp.com
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IMPLEMENTATION_STATUS.md
DELETED
|
@@ -1,252 +0,0 @@
|
|
| 1 |
-
# FleetMind Multi-Tenant Authentication - Implementation Status
|
| 2 |
-
|
| 3 |
-
## ✅ COMPLETED (Production Ready for Testing)
|
| 4 |
-
|
| 5 |
-
### Infrastructure (100% Complete)
|
| 6 |
-
- ✅ Stytch library installed
|
| 7 |
-
- ✅ Database migration executed successfully
|
| 8 |
-
- ✅ `user_id` columns added to: orders, drivers, assignments
|
| 9 |
-
- ✅ Indexes created for performance
|
| 10 |
-
- ✅ `database/user_context.py` authentication module created
|
| 11 |
-
- ✅ OAuth metadata endpoint configured
|
| 12 |
-
- ✅ Stytch credentials configured in `.env`
|
| 13 |
-
|
| 14 |
-
### Handler Functions (6 of 27 Complete - 22%)
|
| 15 |
-
**✅ Fully Updated:**
|
| 16 |
-
1. `handle_create_order` - Creates orders with user_id
|
| 17 |
-
2. `handle_fetch_orders` - Filters by user_id
|
| 18 |
-
3. `handle_create_driver` - Creates drivers with user_id
|
| 19 |
-
4. `handle_fetch_drivers` - Filters by user_id
|
| 20 |
-
5. `handle_count_orders` - Counts only user's orders
|
| 21 |
-
6. `handle_get_order_details` - Returns only if user owns order
|
| 22 |
-
|
| 23 |
-
**Status:** Core CRUD operations functional with authentication
|
| 24 |
-
|
| 25 |
-
### MCP Tools (1 of 27 Complete - 4%)
|
| 26 |
-
**✅ Fully Updated:**
|
| 27 |
-
1. `create_order` (line 354, server.py) - Complete authentication example
|
| 28 |
-
|
| 29 |
-
**Status:** One complete example, pattern established
|
| 30 |
-
|
| 31 |
-
---
|
| 32 |
-
|
| 33 |
-
## ⚠️ REMAINING WORK
|
| 34 |
-
|
| 35 |
-
### Remaining Handlers (21 functions in chat/tools.py)
|
| 36 |
-
|
| 37 |
-
**Order Handlers (4 remaining):**
|
| 38 |
-
- [ ] `handle_search_orders`
|
| 39 |
-
- [ ] `handle_get_incomplete_orders`
|
| 40 |
-
- [ ] `handle_update_order`
|
| 41 |
-
- [ ] `handle_delete_order`
|
| 42 |
-
- [ ] `handle_delete_all_orders`
|
| 43 |
-
|
| 44 |
-
**Driver Handlers (6 remaining):**
|
| 45 |
-
- [ ] `handle_count_drivers`
|
| 46 |
-
- [ ] `handle_get_driver_details`
|
| 47 |
-
- [ ] `handle_search_drivers`
|
| 48 |
-
- [ ] `handle_get_available_drivers`
|
| 49 |
-
- [ ] `handle_update_driver`
|
| 50 |
-
- [ ] `handle_delete_driver`
|
| 51 |
-
- [ ] `handle_delete_all_drivers`
|
| 52 |
-
|
| 53 |
-
**Assignment Handlers (8 remaining):**
|
| 54 |
-
- [ ] `handle_create_assignment`
|
| 55 |
-
- [ ] `handle_auto_assign_order`
|
| 56 |
-
- [ ] `handle_intelligent_assign_order`
|
| 57 |
-
- [ ] `handle_get_assignment_details`
|
| 58 |
-
- [ ] `handle_update_assignment`
|
| 59 |
-
- [ ] `handle_unassign_order`
|
| 60 |
-
- [ ] `handle_complete_delivery`
|
| 61 |
-
- [ ] `handle_fail_delivery`
|
| 62 |
-
|
| 63 |
-
### Remaining MCP Tools (26 functions in server.py)
|
| 64 |
-
|
| 65 |
-
**Order Tools (7 remaining):**
|
| 66 |
-
- [ ] `count_orders`
|
| 67 |
-
- [ ] `fetch_orders`
|
| 68 |
-
- [ ] `get_order_details`
|
| 69 |
-
- [ ] `search_orders`
|
| 70 |
-
- [ ] `get_incomplete_orders`
|
| 71 |
-
- [ ] `update_order`
|
| 72 |
-
- [ ] `delete_order`
|
| 73 |
-
|
| 74 |
-
**Driver Tools (8 remaining):**
|
| 75 |
-
- [ ] `create_driver`
|
| 76 |
-
- [ ] `count_drivers`
|
| 77 |
-
- [ ] `fetch_drivers`
|
| 78 |
-
- [ ] `get_driver_details`
|
| 79 |
-
- [ ] `search_drivers`
|
| 80 |
-
- [ ] `get_available_drivers`
|
| 81 |
-
- [ ] `update_driver`
|
| 82 |
-
- [ ] `delete_driver`
|
| 83 |
-
|
| 84 |
-
**Assignment Tools (8 remaining):**
|
| 85 |
-
- [ ] `create_assignment`
|
| 86 |
-
- [ ] `auto_assign_order`
|
| 87 |
-
- [ ] `intelligent_assign_order`
|
| 88 |
-
- [ ] `get_assignment_details`
|
| 89 |
-
- [ ] `update_assignment`
|
| 90 |
-
- [ ] `unassign_order`
|
| 91 |
-
- [ ] `complete_delivery`
|
| 92 |
-
- [ ] `fail_delivery`
|
| 93 |
-
|
| 94 |
-
**Bulk Tools (2 remaining):**
|
| 95 |
-
- [ ] `delete_all_orders`
|
| 96 |
-
- [ ] `delete_all_drivers`
|
| 97 |
-
|
| 98 |
-
**Public Tools (3 - NO AUTH NEEDED):**
|
| 99 |
-
- `geocode_address` ✅ (public tool, no auth required)
|
| 100 |
-
- `calculate_route` ✅ (public tool, no auth required)
|
| 101 |
-
- `calculate_intelligent_route` ✅ (public tool, no auth required)
|
| 102 |
-
|
| 103 |
-
---
|
| 104 |
-
|
| 105 |
-
## 🎯 CURRENT STATE
|
| 106 |
-
|
| 107 |
-
### What Works Right Now:
|
| 108 |
-
```
|
| 109 |
-
✅ User can create account via Stytch
|
| 110 |
-
✅ User can login (email magic link)
|
| 111 |
-
✅ Token is verified
|
| 112 |
-
✅ create_order tool is fully protected
|
| 113 |
-
✅ Orders are saved with user_id
|
| 114 |
-
✅ fetch_orders filters by user_id
|
| 115 |
-
✅ Users can't see each other's orders
|
| 116 |
-
✅ Drivers are saved with user_id
|
| 117 |
-
✅ Users can't see each other's drivers
|
| 118 |
-
```
|
| 119 |
-
|
| 120 |
-
### What Doesn't Work Yet:
|
| 121 |
-
```
|
| 122 |
-
❌ Other 26 MCP tools not protected
|
| 123 |
-
❌ Can call unprotected tools without auth
|
| 124 |
-
❌ Some handlers don't filter by user_id yet
|
| 125 |
-
```
|
| 126 |
-
|
| 127 |
-
---
|
| 128 |
-
|
| 129 |
-
## 🚀 TESTING THE CURRENT IMPLEMENTATION
|
| 130 |
-
|
| 131 |
-
### Step 1: Start the Server
|
| 132 |
-
```bash
|
| 133 |
-
cd "C:\Users\Mashrur Rahman\Documents\MCP_Server\fleetmind-mcp"
|
| 134 |
-
python app.py
|
| 135 |
-
```
|
| 136 |
-
|
| 137 |
-
### Step 2: Test OAuth Endpoint
|
| 138 |
-
```bash
|
| 139 |
-
curl http://localhost:7860/.well-known/oauth-protected-resource
|
| 140 |
-
```
|
| 141 |
-
|
| 142 |
-
**Expected Response:**
|
| 143 |
-
```json
|
| 144 |
-
{
|
| 145 |
-
"resource": "http://localhost:7860",
|
| 146 |
-
"authorization_servers": [
|
| 147 |
-
"https://test.stytch.com/v1/public"
|
| 148 |
-
],
|
| 149 |
-
"scopes_supported": ["orders:read", "orders:write", ...]
|
| 150 |
-
}
|
| 151 |
-
```
|
| 152 |
-
|
| 153 |
-
### Step 3: Test in Claude Desktop
|
| 154 |
-
|
| 155 |
-
**Update Claude Desktop config:**
|
| 156 |
-
```json
|
| 157 |
-
{
|
| 158 |
-
"mcpServers": {
|
| 159 |
-
"fleetmind": {
|
| 160 |
-
"command": "npx",
|
| 161 |
-
"args": ["mcp-remote", "http://localhost:7860/sse"]
|
| 162 |
-
}
|
| 163 |
-
}
|
| 164 |
-
}
|
| 165 |
-
```
|
| 166 |
-
|
| 167 |
-
**Test create_order (the one protected tool):**
|
| 168 |
-
1. Ask Claude: "Create an order for pizza delivery to 123 Main St"
|
| 169 |
-
2. Browser should open for Stytch login
|
| 170 |
-
3. Enter your email
|
| 171 |
-
4. Check email for magic link
|
| 172 |
-
5. Click link → Should redirect back
|
| 173 |
-
6. Claude Desktop saves token
|
| 174 |
-
7. Order created successfully
|
| 175 |
-
|
| 176 |
-
**Verify it worked:**
|
| 177 |
-
- Ask Claude: "Show me all orders"
|
| 178 |
-
- Should see only the order you just created
|
| 179 |
-
- The order should have your user_id in the database
|
| 180 |
-
|
| 181 |
-
---
|
| 182 |
-
|
| 183 |
-
## ⏱️ TIME TO COMPLETE REMAINING WORK
|
| 184 |
-
|
| 185 |
-
- **Remaining handlers:** 21 × 2 min = 42 minutes
|
| 186 |
-
- **Remaining MCP tools:** 26 × 2 min = 52 minutes
|
| 187 |
-
- **Total:** ~1.5 hours
|
| 188 |
-
|
| 189 |
-
---
|
| 190 |
-
|
| 191 |
-
## 📋 NEXT STEPS
|
| 192 |
-
|
| 193 |
-
### Option A: Complete Remaining Work Manually
|
| 194 |
-
Follow the pattern from completed examples:
|
| 195 |
-
- **Handler example:** `handle_create_order` (line 1331, chat/tools.py)
|
| 196 |
-
- **Tool example:** `create_order` (line 405, server.py)
|
| 197 |
-
|
| 198 |
-
### Option B: Test Current Implementation First
|
| 199 |
-
1. Test the working tools
|
| 200 |
-
2. Verify authentication flow
|
| 201 |
-
3. Confirm data isolation
|
| 202 |
-
4. Then decide if you need all 27 tools protected immediately
|
| 203 |
-
|
| 204 |
-
### Option C: Phased Rollout
|
| 205 |
-
1. **Phase 1 (Current):** Core CRUD protected
|
| 206 |
-
2. **Phase 2:** Add update/delete operations
|
| 207 |
-
3. **Phase 3:** Add assignment operations
|
| 208 |
-
4. **Phase 4:** Add bulk operations
|
| 209 |
-
|
| 210 |
-
---
|
| 211 |
-
|
| 212 |
-
## 💡 RECOMMENDATION
|
| 213 |
-
|
| 214 |
-
**Test what we have first!**
|
| 215 |
-
|
| 216 |
-
The core functionality is working:
|
| 217 |
-
- ✅ User authentication
|
| 218 |
-
- ✅ Order creation with user_id
|
| 219 |
-
- ✅ Order fetching filtered by user_id
|
| 220 |
-
- ✅ Driver creation with user_id
|
| 221 |
-
- ✅ Driver fetching filtered by user_id
|
| 222 |
-
|
| 223 |
-
This is enough to:
|
| 224 |
-
1. Verify the authentication flow works
|
| 225 |
-
2. Confirm data isolation works
|
| 226 |
-
3. Test with multiple users
|
| 227 |
-
4. Identify any issues before completing remaining work
|
| 228 |
-
|
| 229 |
-
**Then** we can complete the remaining 47 functions knowing the foundation is solid.
|
| 230 |
-
|
| 231 |
-
---
|
| 232 |
-
|
| 233 |
-
## 🔧 Files Created
|
| 234 |
-
|
| 235 |
-
1. `database/migrations/007_add_user_id.py` - Database migration
|
| 236 |
-
2. `database/user_context.py` - Authentication module
|
| 237 |
-
3. `IMPLEMENTATION_PLAN.md` - Original detailed plan
|
| 238 |
-
4. `AUTHENTICATION_COMPLETION_GUIDE.md` - Step-by-step guide
|
| 239 |
-
5. `IMPLEMENTATION_STATUS.md` - This file
|
| 240 |
-
6. `apply_auth_pattern.py` - Helper script (not yet used)
|
| 241 |
-
|
| 242 |
-
---
|
| 243 |
-
|
| 244 |
-
## 📊 Summary
|
| 245 |
-
|
| 246 |
-
**Completion: 25% Complete, 75% Remaining**
|
| 247 |
-
- Infrastructure: 100% ✅
|
| 248 |
-
- Core handlers: 6/27 (22%) ✅
|
| 249 |
-
- MCP tools: 1/27 (4%) ✅
|
| 250 |
-
- **Status:** Ready for initial testing
|
| 251 |
-
|
| 252 |
-
**Estimated time to 100%:** 1.5 hours of systematic updates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MCP_TOOLS_SUMMARY.md
DELETED
|
@@ -1,107 +0,0 @@
|
|
| 1 |
-
# FleetMind MCP Tools - Quick Reference
|
| 2 |
-
|
| 3 |
-
## Geocoding & Routing (3 tools)
|
| 4 |
-
|
| 5 |
-
1. **`geocode_address`** - Convert address to GPS coordinates
|
| 6 |
-
2. **`calculate_route`** - Calculate route with vehicle-specific optimization (motorcycle/car/bicycle), toll avoidance, traffic data
|
| 7 |
-
3. **`calculate_intelligent_route`** - Advanced routing with weather + traffic + vehicle type analysis
|
| 8 |
-
|
| 9 |
-
## Order Management (8 tools)
|
| 10 |
-
|
| 11 |
-
4. **`create_order`** - Create new delivery order with **MANDATORY expected_delivery_time** and SLA tracking
|
| 12 |
-
5. **`count_orders`** - Count orders by status (pending/assigned/in_transit/delivered)
|
| 13 |
-
6. **`fetch_orders`** - Get list of orders with filters and pagination
|
| 14 |
-
7. **`get_order_details`** - Get full details of specific order by ID including timing and SLA data
|
| 15 |
-
8. **`search_orders`** - Search orders by customer name, address, or order ID
|
| 16 |
-
9. **`get_incomplete_orders`** - Get all pending/assigned/in_transit orders
|
| 17 |
-
10. **`update_order`** - Update order status, driver, location, notes (with assignment cascading)
|
| 18 |
-
11. **`delete_order`** - Delete order (with active assignment checks)
|
| 19 |
-
|
| 20 |
-
## Driver Management (8 tools)
|
| 21 |
-
|
| 22 |
-
12. **`create_driver`** - Register new driver with name, phone, vehicle type
|
| 23 |
-
13. **`count_drivers`** - Count drivers by status (active/busy/offline)
|
| 24 |
-
14. **`fetch_drivers`** - Get list of drivers with filters and pagination
|
| 25 |
-
15. **`get_driver_details`** - Get full details of specific driver by ID
|
| 26 |
-
16. **`search_drivers`** - Search drivers by name, phone, or driver ID
|
| 27 |
-
17. **`get_available_drivers`** - Get all active drivers ready for assignment
|
| 28 |
-
18. **`update_driver`** - Update driver status, phone, vehicle type, location (with assignment validation)
|
| 29 |
-
19. **`delete_driver`** - Delete driver (with assignment safety checks)
|
| 30 |
-
|
| 31 |
-
## Assignment Management (8 tools)
|
| 32 |
-
|
| 33 |
-
20. **`create_assignment`** - Manually assign order to driver (validates status, calculates route, saves all data)
|
| 34 |
-
21. **`auto_assign_order`** - **AUTO ASSIGNMENT**: Automatically assign order to nearest driver meeting requirements (distance + validation based)
|
| 35 |
-
22. **`intelligent_assign_order`** - **AI ASSIGNMENT**: Use Google Gemini AI to intelligently select best driver based on all parameters with reasoning
|
| 36 |
-
23. **`get_assignment_details`** - Get assignment details by assignment ID, order ID, or driver ID
|
| 37 |
-
24. **`update_assignment`** - Update assignment status with cascading updates to orders/drivers
|
| 38 |
-
25. **`unassign_order`** - Unassign order from driver (reverts statuses, requires confirmation)
|
| 39 |
-
26. **`complete_delivery`** - Mark delivery complete and auto-update driver location to delivery address
|
| 40 |
-
27. **`fail_delivery`** - Mark delivery as failed with MANDATORY driver location and failure reason
|
| 41 |
-
|
| 42 |
-
## Bulk Operations (2 tools)
|
| 43 |
-
|
| 44 |
-
28. **`delete_all_orders`** - Bulk delete all orders (or by status filter, blocks if active assignments exist)
|
| 45 |
-
29. **`delete_all_drivers`** - Bulk delete all drivers (or by status filter, blocks if assignments exist)
|
| 46 |
-
|
| 47 |
-
---
|
| 48 |
-
|
| 49 |
-
## Total: 29 MCP Tools
|
| 50 |
-
|
| 51 |
-
**Routing Tools:** 3 (with Google Routes API integration)
|
| 52 |
-
**Order Tools:** 8 (full CRUD + search + cascading)
|
| 53 |
-
**Driver Tools:** 8 (full CRUD + search + cascading)
|
| 54 |
-
**Assignment Tools:** 8 (manual + auto + intelligent AI assignment + lifecycle management)
|
| 55 |
-
**Bulk Operations:** 2 (efficient mass deletions with safety checks)
|
| 56 |
-
|
| 57 |
-
### Key Features:
|
| 58 |
-
- ✅ Real-time traffic & weather-aware routing
|
| 59 |
-
- ✅ Vehicle-specific optimization (motorcycle/bicycle/car/van/truck)
|
| 60 |
-
- ✅ Toll detection & avoidance
|
| 61 |
-
- ✅ Complete fleet management (orders + drivers + assignments)
|
| 62 |
-
- ✅ **Three assignment methods: Manual, Auto (distance-based), and Intelligent (Gemini 2.0 AI)**
|
| 63 |
-
- ✅ **Auto assignment: nearest driver with capacity & skill validation**
|
| 64 |
-
- ✅ **Intelligent AI assignment: Gemini 2.0 Flash analyzes all parameters with detailed reasoning**
|
| 65 |
-
- ✅ Assignment system with automatic route calculation
|
| 66 |
-
- ✅ **Mandatory delivery deadline (expected_delivery_time) when creating orders**
|
| 67 |
-
- ✅ **Automatic SLA tracking with grace period**
|
| 68 |
-
- ✅ **Delivery performance status: on_time, late, very_late, failed_on_time, failed_late**
|
| 69 |
-
- ✅ **Automatic driver location updates on delivery completion**
|
| 70 |
-
- ✅ **Mandatory location + reason tracking for failed deliveries**
|
| 71 |
-
- ✅ **Structured failure reasons for analytics and reporting**
|
| 72 |
-
- ✅ Cascading status updates (order → assignment → driver)
|
| 73 |
-
- ✅ Safety checks preventing invalid deletions/updates
|
| 74 |
-
- ✅ PostgreSQL database with foreign key constraints
|
| 75 |
-
- ✅ Search & filtering capabilities
|
| 76 |
-
- ✅ Status tracking & validation
|
| 77 |
-
|
| 78 |
-
### Assignment System Capabilities:
|
| 79 |
-
- **Manual assignment** (`create_assignment`) - Manually assign order to specific driver
|
| 80 |
-
- **Auto assignment** (`auto_assign_order`) - Automatically assign to nearest driver meeting requirements:
|
| 81 |
-
- Selects nearest driver by real-time route distance
|
| 82 |
-
- Validates vehicle capacity (weight & volume)
|
| 83 |
-
- Validates driver skills (fragile handling, cold storage)
|
| 84 |
-
- Returns selection reason and distance info
|
| 85 |
-
- **Intelligent AI assignment** (`intelligent_assign_order`) - Gemini 2.0 Flash AI analyzes all parameters:
|
| 86 |
-
- Uses latest Gemini 2.0 Flash model (gemini-2.0-flash-exp)
|
| 87 |
-
- Evaluates order priority, fragility, time constraints, value
|
| 88 |
-
- Considers driver location, capacity, skills, vehicle type
|
| 89 |
-
- Analyzes real-time traffic, weather conditions
|
| 90 |
-
- Evaluates complex tradeoffs (speed vs safety, cost vs quality)
|
| 91 |
-
- Returns detailed AI reasoning and confidence score
|
| 92 |
-
- Requires GOOGLE_API_KEY environment variable
|
| 93 |
-
- **Automatic route calculation** from driver location to delivery address
|
| 94 |
-
- **Delivery completion** with automatic driver location update to delivery address
|
| 95 |
-
- **SLA & Timing Tracking**:
|
| 96 |
-
- Mandatory `expected_delivery_time` when creating orders
|
| 97 |
-
- Automatic comparison of actual vs expected delivery time
|
| 98 |
-
- Grace period support (default: 15 minutes)
|
| 99 |
-
- Performance statuses: `on_time`, `late` (within grace), `very_late` (SLA violation)
|
| 100 |
-
- `delivered_at` field automatically populated on completion/failure
|
| 101 |
-
- **Delivery failure handling** with mandatory GPS location and failure reason
|
| 102 |
-
- **Failure timing tracking**: `failed_on_time` vs `failed_late` status
|
| 103 |
-
- **Structured failure reasons**: customer_not_available, wrong_address, refused_delivery, damaged_goods, payment_issue, vehicle_breakdown, access_restricted, weather_conditions, other
|
| 104 |
-
- **Status management** with cascading updates across orders/drivers/assignments
|
| 105 |
-
- **Safety checks** preventing deletion of orders/drivers with active assignments
|
| 106 |
-
- **Assignment lifecycle**: active → in_progress → completed/failed/cancelled
|
| 107 |
-
- **Database integrity** via FK constraints (ON DELETE CASCADE/RESTRICT/SET NULL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MIGRATION_SUMMARY.md
DELETED
|
@@ -1,478 +0,0 @@
|
|
| 1 |
-
# FleetMind → FastMCP Migration Summary
|
| 2 |
-
|
| 3 |
-
**Date:** November 14, 2025
|
| 4 |
-
**Status:** ✅ **COMPLETE**
|
| 5 |
-
**Effort:** ~26 hours of planned work completed
|
| 6 |
-
|
| 7 |
-
---
|
| 8 |
-
|
| 9 |
-
## Executive Summary
|
| 10 |
-
|
| 11 |
-
Successfully transformed FleetMind from a Gradio web application to an industry-standard FastMCP server, achieving:
|
| 12 |
-
- **46% code reduction** (5,400 → 3,100 lines)
|
| 13 |
-
- **18 AI tools** fully operational
|
| 14 |
-
- **2 real-time resources** providing live data
|
| 15 |
-
- **100% business logic preserved** (database, geocoding unchanged)
|
| 16 |
-
- **Multi-client support** (Claude Desktop, Continue, Cline, custom apps)
|
| 17 |
-
|
| 18 |
-
---
|
| 19 |
-
|
| 20 |
-
## What Was Built
|
| 21 |
-
|
| 22 |
-
### Core MCP Server (`server.py` - 882 lines)
|
| 23 |
-
|
| 24 |
-
**Features:**
|
| 25 |
-
- FastMCP 2.13.0 framework integration
|
| 26 |
-
- Logging infrastructure
|
| 27 |
-
- Database connectivity validation
|
| 28 |
-
- Google Maps API integration
|
| 29 |
-
- 18 tool wrappers with type hints
|
| 30 |
-
- 2 resource providers
|
| 31 |
-
|
| 32 |
-
**Tools Implemented (18 total):**
|
| 33 |
-
|
| 34 |
-
#### Order Management (10 tools)
|
| 35 |
-
1. ✅ `geocode_address` - Address validation & geocoding
|
| 36 |
-
2. ✅ `calculate_route` - Route calculation with Google Maps Directions API
|
| 37 |
-
3. ✅ `create_order` - Create delivery orders
|
| 38 |
-
4. ✅ `count_orders` - Count with flexible filters
|
| 39 |
-
5. ✅ `fetch_orders` - Pagination & sorting
|
| 40 |
-
6. ✅ `get_order_details` - Complete order information
|
| 41 |
-
7. ✅ `search_orders` - Search by customer/ID
|
| 42 |
-
8. ✅ `get_incomplete_orders` - Active deliveries shortcut
|
| 43 |
-
9. ✅ `update_order` - Update with auto-geocoding
|
| 44 |
-
10. ✅ `delete_order` - Permanent deletion with confirmation
|
| 45 |
-
|
| 46 |
-
#### Driver Management (8 tools)
|
| 47 |
-
11. ✅ `create_driver` - Driver onboarding
|
| 48 |
-
12. ✅ `count_drivers` - Count with status/vehicle filters
|
| 49 |
-
13. ✅ `fetch_drivers` - Pagination & sorting
|
| 50 |
-
14. ✅ `get_driver_details` - With reverse-geocoded location
|
| 51 |
-
15. ✅ `search_drivers` - Search by name/plate/ID
|
| 52 |
-
16. ✅ `get_available_drivers` - Available drivers shortcut
|
| 53 |
-
17. ✅ `update_driver` - Update with location tracking
|
| 54 |
-
18. ✅ `delete_driver` - Permanent deletion with confirmation
|
| 55 |
-
|
| 56 |
-
**Resources Implemented (2 total):**
|
| 57 |
-
1. ✅ `orders://all` - Last 30 days, max 1000 orders
|
| 58 |
-
2. ✅ `drivers://all` - All drivers with current locations
|
| 59 |
-
|
| 60 |
-
**Prompts:** Planned but deferred (FastMCP API pending confirmation)
|
| 61 |
-
|
| 62 |
-
---
|
| 63 |
-
|
| 64 |
-
## Architecture Comparison
|
| 65 |
-
|
| 66 |
-
### Before (Gradio System)
|
| 67 |
-
```
|
| 68 |
-
┌─────────────────────────────────────┐
|
| 69 |
-
│ Gradio Web UI (ui/app.py) │ 1,128 lines
|
| 70 |
-
└─────────────────────────────────────┘
|
| 71 |
-
↓
|
| 72 |
-
┌─────────────────────────────────────┐
|
| 73 |
-
│ ChatEngine (chat/chat_engine.py) │ 109 lines
|
| 74 |
-
└─────────────────────────────────────┘
|
| 75 |
-
↓
|
| 76 |
-
┌──────────────────┬──────────────────┐
|
| 77 |
-
│ GeminiProvider │ ClaudeProvider │ 1,358 lines total
|
| 78 |
-
│ (984 lines) │ (374 lines) │
|
| 79 |
-
└──────────────────┴──────────────────┘
|
| 80 |
-
↓
|
| 81 |
-
┌─────────────────────────────────────┐
|
| 82 |
-
│ Tools (chat/tools.py) │ 2,099 lines
|
| 83 |
-
└─────────────────────────────────────┘
|
| 84 |
-
↓
|
| 85 |
-
┌──────────────┬──────────────────────┐
|
| 86 |
-
│ Geocoding │ Database (PostgreSQL)│ 455 lines
|
| 87 |
-
│ (234 lines) │ (221 lines) │
|
| 88 |
-
└──────────────┴──────────────────────┘
|
| 89 |
-
```
|
| 90 |
-
|
| 91 |
-
**Total:** ~5,400 lines of code
|
| 92 |
-
|
| 93 |
-
### After (MCP System)
|
| 94 |
-
```
|
| 95 |
-
┌──────────────────────────────────────┐
|
| 96 |
-
│ Any MCP Client (Claude Desktop, │
|
| 97 |
-
│ Continue, Cline, Custom Apps) │
|
| 98 |
-
└──────────────────────────────────────┘
|
| 99 |
-
↓
|
| 100 |
-
MCP Protocol
|
| 101 |
-
↓
|
| 102 |
-
┌──────────────────────────────────────┐
|
| 103 |
-
│ FleetMind MCP Server (server.py) │ 882 lines
|
| 104 |
-
│ - 18 tools │
|
| 105 |
-
│ - 2 resources │
|
| 106 |
-
│ - Logging & validation │
|
| 107 |
-
└──────────────────────────────────────┘
|
| 108 |
-
↓
|
| 109 |
-
┌──────────────┬───────────────────────┐
|
| 110 |
-
│ Tools │ Geocoding │ Database│ 2,554 lines
|
| 111 |
-
│ (2,099) │ (234) │ (221) │
|
| 112 |
-
└──────────────┴─────────────┴─────────┘
|
| 113 |
-
```
|
| 114 |
-
|
| 115 |
-
**Total:** ~3,100 lines of code (-46%)
|
| 116 |
-
|
| 117 |
-
---
|
| 118 |
-
|
| 119 |
-
## Files Created
|
| 120 |
-
|
| 121 |
-
### New Files
|
| 122 |
-
1. ✅ `server.py` (882 lines) - Main MCP server
|
| 123 |
-
2. ✅ `pyproject.toml` - Package configuration
|
| 124 |
-
3. ✅ `mcp_config.json` - MCP metadata
|
| 125 |
-
4. ✅ `README_MCP.md` - Comprehensive documentation
|
| 126 |
-
5. ✅ `MIGRATION_SUMMARY.md` (this file)
|
| 127 |
-
6. ✅ `logs/.gitkeep` - Logs directory
|
| 128 |
-
7. ✅ `archive/` - Archived old code
|
| 129 |
-
|
| 130 |
-
### Modified Files
|
| 131 |
-
1. ✅ `requirements.txt` - Updated dependencies (removed Gradio, Anthropic, Gemini)
|
| 132 |
-
2. ✅ `.env` - Compatible (no changes needed)
|
| 133 |
-
|
| 134 |
-
### Preserved Files (Unchanged)
|
| 135 |
-
1. ✅ `chat/tools.py` - All 18 tool handlers
|
| 136 |
-
2. ✅ `chat/geocoding.py` - Geocoding service
|
| 137 |
-
3. ✅ `database/connection.py` - Database layer
|
| 138 |
-
4. ✅ `database/schema.py` - Schema definitions
|
| 139 |
-
5. ✅ `.env` - Environment configuration
|
| 140 |
-
|
| 141 |
-
### Files for Archiving (Phase 8)
|
| 142 |
-
1. `ui/app.py` (1,128 lines) - Gradio interface
|
| 143 |
-
2. `chat/chat_engine.py` (109 lines) - Provider router
|
| 144 |
-
3. `chat/providers/gemini_provider.py` (984 lines) - Gemini integration
|
| 145 |
-
4. `chat/providers/claude_provider.py` (374 lines) - Claude integration
|
| 146 |
-
5. `chat/conversation.py` (86 lines) - Session management
|
| 147 |
-
6. `app.py` (8 lines) - Gradio entry point
|
| 148 |
-
|
| 149 |
-
---
|
| 150 |
-
|
| 151 |
-
## Testing Results
|
| 152 |
-
|
| 153 |
-
### ✅ Server Import Test
|
| 154 |
-
```bash
|
| 155 |
-
$ python -c "import server; print('Success')"
|
| 156 |
-
INFO:server:Initializing FleetMind MCP Server...
|
| 157 |
-
INFO:server:Geocoding Service: ✅ Google Maps API connected
|
| 158 |
-
INFO:server:Database: Connected to PostgreSQL
|
| 159 |
-
Success
|
| 160 |
-
```
|
| 161 |
-
|
| 162 |
-
### ✅ Database Connectivity
|
| 163 |
-
- Connection pool: Working
|
| 164 |
-
- PostgreSQL version: 17.5
|
| 165 |
-
- Database: fleetmind@Neon
|
| 166 |
-
- Region: ap-southeast-1
|
| 167 |
-
|
| 168 |
-
### ✅ Geocoding Service
|
| 169 |
-
- Google Maps API: Connected
|
| 170 |
-
- Quota: 60 queries (per time window)
|
| 171 |
-
- Mock fallback: Available
|
| 172 |
-
|
| 173 |
-
### ✅ Tool Handlers
|
| 174 |
-
- All 18 handlers verified in `chat/tools.py`
|
| 175 |
-
- Import successful
|
| 176 |
-
- Database queries tested
|
| 177 |
-
|
| 178 |
-
---
|
| 179 |
-
|
| 180 |
-
## Dependencies Comparison
|
| 181 |
-
|
| 182 |
-
### Before (Gradio System)
|
| 183 |
-
```
|
| 184 |
-
gradio==5.49.1
|
| 185 |
-
anthropic>=0.40.0
|
| 186 |
-
google-generativeai>=0.3.0
|
| 187 |
-
pandas>=2.2.0
|
| 188 |
-
faker>=23.0.0
|
| 189 |
-
psycopg2-binary>=2.9.9
|
| 190 |
-
requests>=2.31.0
|
| 191 |
-
httpx>=0.27.1
|
| 192 |
-
googlemaps>=4.10.0
|
| 193 |
-
python-dotenv>=1.0.0
|
| 194 |
-
pydantic==2.8.2
|
| 195 |
-
fastmcp>=0.3.0 # (not used)
|
| 196 |
-
```
|
| 197 |
-
|
| 198 |
-
### After (MCP System)
|
| 199 |
-
```
|
| 200 |
-
fastmcp>=0.3.0 # ← Now actively used
|
| 201 |
-
pydantic>=2.8.2
|
| 202 |
-
psycopg2-binary>=2.9.9
|
| 203 |
-
googlemaps>=4.10.0
|
| 204 |
-
python-dotenv>=1.0.0
|
| 205 |
-
pytest>=8.0.0 # Dev dependency
|
| 206 |
-
pytest-asyncio>=0.23.0
|
| 207 |
-
mypy>=1.8.0
|
| 208 |
-
black>=24.0.0
|
| 209 |
-
ruff>=0.1.0
|
| 210 |
-
```
|
| 211 |
-
|
| 212 |
-
**Removed:**
|
| 213 |
-
- gradio (web UI framework)
|
| 214 |
-
- anthropic (Claude API client)
|
| 215 |
-
- google-generativeai (Gemini API client)
|
| 216 |
-
- pandas (data manipulation - was only used in UI)
|
| 217 |
-
- faker (test data - moved to dev dependencies)
|
| 218 |
-
|
| 219 |
-
---
|
| 220 |
-
|
| 221 |
-
## Configuration Changes
|
| 222 |
-
|
| 223 |
-
### Environment Variables
|
| 224 |
-
|
| 225 |
-
**Unchanged:**
|
| 226 |
-
- ✅ `DB_HOST` - PostgreSQL host
|
| 227 |
-
- ✅ `DB_PORT` - PostgreSQL port
|
| 228 |
-
- ✅ `DB_NAME` - Database name
|
| 229 |
-
- ✅ `DB_USER` - Database user
|
| 230 |
-
- ✅ `DB_PASSWORD` - Database password
|
| 231 |
-
- ✅ `GOOGLE_MAPS_API_KEY` - Google Maps API key
|
| 232 |
-
|
| 233 |
-
**Removed (no longer needed):**
|
| 234 |
-
- ❌ `AI_PROVIDER` - Client handles AI provider selection
|
| 235 |
-
- ❌ `ANTHROPIC_API_KEY` - Not used in MCP server
|
| 236 |
-
- ❌ `GOOGLE_API_KEY` (Gemini) - Not used in MCP server
|
| 237 |
-
- ❌ `GRADIO_SERVER_PORT` - No Gradio UI
|
| 238 |
-
- ❌ `GRADIO_SHARE` - No Gradio UI
|
| 239 |
-
|
| 240 |
-
**Added (optional):**
|
| 241 |
-
- ➕ `MCP_SERVER_PORT` (optional) - For future HTTP/SSE mode
|
| 242 |
-
- ➕ `LOG_LEVEL` (optional) - Logging verbosity
|
| 243 |
-
- ➕ `LOG_FILE` (optional) - Log file path
|
| 244 |
-
|
| 245 |
-
---
|
| 246 |
-
|
| 247 |
-
## Database Schema
|
| 248 |
-
|
| 249 |
-
**Status:** ✅ **100% Preserved** - No changes required
|
| 250 |
-
|
| 251 |
-
All existing tables, indexes, constraints, and triggers remain unchanged:
|
| 252 |
-
- `orders` table (26 columns)
|
| 253 |
-
- `drivers` table (15 columns)
|
| 254 |
-
- `assignments` table
|
| 255 |
-
- `exceptions` table
|
| 256 |
-
- `agent_decisions` table
|
| 257 |
-
- `metrics` table
|
| 258 |
-
|
| 259 |
-
**Migration Required:** ❌ None
|
| 260 |
-
|
| 261 |
-
---
|
| 262 |
-
|
| 263 |
-
## Performance Improvements
|
| 264 |
-
|
| 265 |
-
### Code Metrics
|
| 266 |
-
| Metric | Before | After | Change |
|
| 267 |
-
|--------|--------|-------|--------|
|
| 268 |
-
| Total Lines | 5,400 | 3,100 | -46% |
|
| 269 |
-
| Files | 19 | 12 | -37% |
|
| 270 |
-
| Dependencies | 12 | 10 | -17% |
|
| 271 |
-
| Tools | 18 | 18 | 0% |
|
| 272 |
-
| Features | All | All | 0% |
|
| 273 |
-
|
| 274 |
-
### Deployment Benefits
|
| 275 |
-
- **Startup Time:** Faster (no Gradio UI initialization)
|
| 276 |
-
- **Memory Footprint:** Lower (no web framework overhead)
|
| 277 |
-
- **Scalability:** Better (stateless MCP protocol)
|
| 278 |
-
- **Testing:** Easier (isolated tools)
|
| 279 |
-
|
| 280 |
-
---
|
| 281 |
-
|
| 282 |
-
## Client Integration
|
| 283 |
-
|
| 284 |
-
### Claude Desktop
|
| 285 |
-
|
| 286 |
-
**Setup:**
|
| 287 |
-
1. Install Claude Desktop
|
| 288 |
-
2. Edit `claude_desktop_config.json`
|
| 289 |
-
3. Add FleetMind server configuration
|
| 290 |
-
4. Restart Claude Desktop
|
| 291 |
-
|
| 292 |
-
**Example:**
|
| 293 |
-
```json
|
| 294 |
-
{
|
| 295 |
-
"mcpServers": {
|
| 296 |
-
"fleetmind": {
|
| 297 |
-
"command": "python",
|
| 298 |
-
"args": ["F:\\github-fleetmind-team\\server.py"]
|
| 299 |
-
}
|
| 300 |
-
}
|
| 301 |
-
}
|
| 302 |
-
```
|
| 303 |
-
|
| 304 |
-
### Continue.dev (VS Code)
|
| 305 |
-
|
| 306 |
-
**Setup:**
|
| 307 |
-
1. Install Continue extension
|
| 308 |
-
2. Add FleetMind to MCP servers
|
| 309 |
-
3. Reload VS Code
|
| 310 |
-
|
| 311 |
-
### Cline (VS Code)
|
| 312 |
-
|
| 313 |
-
**Setup:**
|
| 314 |
-
1. Install Cline extension
|
| 315 |
-
2. Configure MCP server
|
| 316 |
-
3. Start using tools
|
| 317 |
-
|
| 318 |
-
### Custom Applications
|
| 319 |
-
|
| 320 |
-
**Any application supporting MCP protocol can integrate!**
|
| 321 |
-
|
| 322 |
-
---
|
| 323 |
-
|
| 324 |
-
## Known Issues & Limitations
|
| 325 |
-
|
| 326 |
-
### 1. Prompts Deferred
|
| 327 |
-
**Issue:** FastMCP prompt API changed in v2.13.0
|
| 328 |
-
**Status:** Prompts commented out, tools fully functional
|
| 329 |
-
**Impact:** Low - prompts are optional, tools work perfectly
|
| 330 |
-
**Resolution:** Will add once API confirmed
|
| 331 |
-
|
| 332 |
-
### 2. Dependency Conflicts (Expected)
|
| 333 |
-
**Issue:** Some packages have version conflicts with Gradio
|
| 334 |
-
**Status:** Warnings only, no functional impact
|
| 335 |
-
**Impact:** None - Gradio being removed
|
| 336 |
-
**Resolution:** Clean install recommended
|
| 337 |
-
|
| 338 |
-
### 3. Windows Path Issues
|
| 339 |
-
**Issue:** Windows pip sometimes has permission errors
|
| 340 |
-
**Status:** Resolved using `--user` flag
|
| 341 |
-
**Impact:** Installation only
|
| 342 |
-
**Resolution:** Use `pip install --user`
|
| 343 |
-
|
| 344 |
-
---
|
| 345 |
-
|
| 346 |
-
## Next Steps
|
| 347 |
-
|
| 348 |
-
### Immediate (Post-Migration)
|
| 349 |
-
1. ✅ Test server with Claude Desktop
|
| 350 |
-
2. ✅ Create sample orders/drivers
|
| 351 |
-
3. ✅ Verify all 18 tools work
|
| 352 |
-
4. ✅ Test resources load correctly
|
| 353 |
-
5. ✅ Archive old code to `archive/`
|
| 354 |
-
|
| 355 |
-
### Short-Term (This Week)
|
| 356 |
-
1. Add comprehensive unit tests
|
| 357 |
-
2. Add integration tests
|
| 358 |
-
3. Set up CI/CD pipeline
|
| 359 |
-
4. Publish to GitHub
|
| 360 |
-
5. Create video tutorial
|
| 361 |
-
|
| 362 |
-
### Medium-Term (This Month)
|
| 363 |
-
1. Add prompt templates (once API confirmed)
|
| 364 |
-
2. Add assignment optimization algorithm
|
| 365 |
-
3. Add route optimization for multi-stop deliveries
|
| 366 |
-
4. Create mobile app MCP client
|
| 367 |
-
5. Add real-time tracking via WebSocket
|
| 368 |
-
|
| 369 |
-
### Long-Term (This Quarter)
|
| 370 |
-
1. Add analytics dashboard
|
| 371 |
-
2. Add driver app integration
|
| 372 |
-
3. Add customer tracking portal
|
| 373 |
-
4. Scale to handle 10,000+ orders/day
|
| 374 |
-
5. Add machine learning for route prediction
|
| 375 |
-
|
| 376 |
-
---
|
| 377 |
-
|
| 378 |
-
## Migration Checklist
|
| 379 |
-
|
| 380 |
-
### Pre-Migration
|
| 381 |
-
- [x] Backup database (`pg_dump`)
|
| 382 |
-
- [x] Document current architecture
|
| 383 |
-
- [x] Test all existing features
|
| 384 |
-
- [x] Inventory dependencies
|
| 385 |
-
|
| 386 |
-
### Migration
|
| 387 |
-
- [x] Create `server.py` with FastMCP
|
| 388 |
-
- [x] Convert all 18 tools
|
| 389 |
-
- [x] Add 2 resources
|
| 390 |
-
- [x] Update `requirements.txt`
|
| 391 |
-
- [x] Create configuration files
|
| 392 |
-
- [x] Test server imports
|
| 393 |
-
- [x] Verify database connectivity
|
| 394 |
-
- [x] Test geocoding service
|
| 395 |
-
|
| 396 |
-
### Post-Migration
|
| 397 |
-
- [x] Create comprehensive documentation
|
| 398 |
-
- [x] Update README
|
| 399 |
-
- [x] Create migration summary
|
| 400 |
-
- [ ] Archive old code
|
| 401 |
-
- [ ] Test with Claude Desktop
|
| 402 |
-
- [ ] Create demo video
|
| 403 |
-
- [ ] Publish to GitHub
|
| 404 |
-
|
| 405 |
-
---
|
| 406 |
-
|
| 407 |
-
## Success Metrics
|
| 408 |
-
|
| 409 |
-
### Code Quality
|
| 410 |
-
- ✅ 46% code reduction achieved
|
| 411 |
-
- ✅ Type hints added to all tools
|
| 412 |
-
- ✅ Logging infrastructure implemented
|
| 413 |
-
- ✅ Error handling preserved
|
| 414 |
-
|
| 415 |
-
### Functionality
|
| 416 |
-
- ✅ All 18 tools working
|
| 417 |
-
- ✅ 2 resources providing live data
|
| 418 |
-
- ✅ Database operations unchanged
|
| 419 |
-
- ✅ Geocoding fully functional
|
| 420 |
-
|
| 421 |
-
### Architecture
|
| 422 |
-
- ✅ Industry-standard MCP protocol
|
| 423 |
-
- ✅ Multi-client support
|
| 424 |
-
- ✅ Stateless design
|
| 425 |
-
- ✅ Scalable infrastructure
|
| 426 |
-
|
| 427 |
-
### Documentation
|
| 428 |
-
- ✅ Comprehensive README_MCP.md
|
| 429 |
-
- ✅ API reference for all tools
|
| 430 |
-
- ✅ Usage examples
|
| 431 |
-
- ✅ Troubleshooting guide
|
| 432 |
-
- ✅ Migration summary
|
| 433 |
-
|
| 434 |
-
---
|
| 435 |
-
|
| 436 |
-
## Lessons Learned
|
| 437 |
-
|
| 438 |
-
### What Went Well
|
| 439 |
-
1. **Preserved Business Logic:** All tool handlers worked unchanged
|
| 440 |
-
2. **Clean Separation:** UI/AI provider code easily removed
|
| 441 |
-
3. **FastMCP Framework:** Excellent developer experience
|
| 442 |
-
4. **Database Compatibility:** Zero schema changes needed
|
| 443 |
-
5. **Testing:** Incremental validation caught issues early
|
| 444 |
-
|
| 445 |
-
### Challenges Faced
|
| 446 |
-
1. **FastMCP API Changes:** Prompt API changed in v2.13.0
|
| 447 |
-
2. **Windows Pip Issues:** Permission errors resolved with `--user`
|
| 448 |
-
3. **Dependency Conflicts:** Expected with Gradio removal
|
| 449 |
-
4. **Documentation:** Needed comprehensive examples for users
|
| 450 |
-
|
| 451 |
-
### Best Practices Applied
|
| 452 |
-
1. **Incremental Migration:** Completed in 8 phases
|
| 453 |
-
2. **Test-Driven:** Tested each phase before proceeding
|
| 454 |
-
3. **Documentation-First:** Created README before cleanup
|
| 455 |
-
4. **Version Control:** Each phase could be committed separately
|
| 456 |
-
5. **Backwards Compatibility:** .env file unchanged
|
| 457 |
-
|
| 458 |
-
---
|
| 459 |
-
|
| 460 |
-
## Conclusion
|
| 461 |
-
|
| 462 |
-
The FleetMind → FastMCP migration was **100% successful**, achieving all objectives:
|
| 463 |
-
|
| 464 |
-
✅ **Functionality:** All 18 tools operational
|
| 465 |
-
✅ **Architecture:** Industry-standard MCP protocol
|
| 466 |
-
✅ **Code Quality:** 46% reduction while preserving features
|
| 467 |
-
✅ **Multi-Client:** Works with Claude Desktop, Continue, Cline
|
| 468 |
-
✅ **Database:** Zero changes required
|
| 469 |
-
✅ **Documentation:** Comprehensive guides created
|
| 470 |
-
|
| 471 |
-
**FleetMind is now a production-ready MCP server compatible with any MCP client.**
|
| 472 |
-
|
| 473 |
-
---
|
| 474 |
-
|
| 475 |
-
**Migration Completed By:** Claude Code (Sonnet 4.5)
|
| 476 |
-
**Date:** November 14, 2025
|
| 477 |
-
**Total Effort:** 26 hours (as planned)
|
| 478 |
-
**Status:** ✅ **PRODUCTION READY**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PROXY_SETUP.md
DELETED
|
@@ -1,202 +0,0 @@
|
|
| 1 |
-
# FleetMind MCP - Multi-Tenant Authentication Proxy
|
| 2 |
-
|
| 3 |
-
## Overview
|
| 4 |
-
|
| 5 |
-
This setup enables **true multi-tenant authentication** for FleetMind MCP Server by using an HTTP proxy that captures API keys from client connections and injects them into server requests.
|
| 6 |
-
|
| 7 |
-
## Architecture
|
| 8 |
-
|
| 9 |
-
```
|
| 10 |
-
Claude Desktop → Auth Proxy (port 7860) → FastMCP Server (port 7861)
|
| 11 |
-
↓ Captures api_key
|
| 12 |
-
↓ Stores session mapping
|
| 13 |
-
↓ Injects into requests
|
| 14 |
-
```
|
| 15 |
-
|
| 16 |
-
## Components
|
| 17 |
-
|
| 18 |
-
### 1. **proxy.py** - Authentication Proxy
|
| 19 |
-
- Listens on port **7860** (public-facing)
|
| 20 |
-
- Captures `api_key` from `/sse?api_key=xxx` connections
|
| 21 |
-
- Maps `session_id` to `api_key`
|
| 22 |
-
- Injects `api_key` into all `/messages/?session_id=xxx` requests
|
| 23 |
-
- Forwards all requests to FastMCP on port 7861
|
| 24 |
-
|
| 25 |
-
### 2. **app.py** - FastMCP Server
|
| 26 |
-
- Runs on port **7861** (internal only)
|
| 27 |
-
- Receives requests from proxy with API keys injected
|
| 28 |
-
- Handles all MCP protocol operations
|
| 29 |
-
- Authenticates users using injected API keys
|
| 30 |
-
|
| 31 |
-
### 3. **start_with_proxy.py** - Unified Launcher
|
| 32 |
-
- Starts both FastMCP and proxy together
|
| 33 |
-
- Handles graceful shutdown
|
| 34 |
-
- Monitors both processes
|
| 35 |
-
|
| 36 |
-
## Quick Start
|
| 37 |
-
|
| 38 |
-
### Local Development
|
| 39 |
-
|
| 40 |
-
1. **Start the server with proxy:**
|
| 41 |
-
```bash
|
| 42 |
-
cd fleetmind-mcp
|
| 43 |
-
python start_with_proxy.py
|
| 44 |
-
```
|
| 45 |
-
|
| 46 |
-
2. **Update Claude Desktop config:**
|
| 47 |
-
```json
|
| 48 |
-
{
|
| 49 |
-
"mcpServers": {
|
| 50 |
-
"fleetmind_local": {
|
| 51 |
-
"command": "npx",
|
| 52 |
-
"args": [
|
| 53 |
-
"mcp-remote",
|
| 54 |
-
"http://localhost:7860/sse?api_key=fm_WGJMC9LO5KqZmqrcswel-CJFXFWLiUqJo21ebm5iKgk"
|
| 55 |
-
]
|
| 56 |
-
}
|
| 57 |
-
}
|
| 58 |
-
}
|
| 59 |
-
```
|
| 60 |
-
|
| 61 |
-
3. **Restart Claude Desktop**
|
| 62 |
-
|
| 63 |
-
4. **Test:**
|
| 64 |
-
- Connect from Claude Desktop
|
| 65 |
-
- Run any command (e.g., "count orders")
|
| 66 |
-
- Check console for: `🔑 Captured API key from SSE connection`
|
| 67 |
-
|
| 68 |
-
## How It Works
|
| 69 |
-
|
| 70 |
-
### Step 1: Initial Connection
|
| 71 |
-
```
|
| 72 |
-
Client → GET /sse?api_key=fm_xxx
|
| 73 |
-
Proxy captures api_key, stores as '_pending_api_key'
|
| 74 |
-
Proxy forwards to FastMCP → GET /sse?api_key=fm_xxx
|
| 75 |
-
```
|
| 76 |
-
|
| 77 |
-
### Step 2: Session Linking
|
| 78 |
-
```
|
| 79 |
-
Client → POST /messages/?session_id=abc123
|
| 80 |
-
Proxy sees session_id
|
| 81 |
-
Proxy links session_id → api_key from pending
|
| 82 |
-
Proxy stores mapping: {abc123: fm_xxx}
|
| 83 |
-
```
|
| 84 |
-
|
| 85 |
-
### Step 3: Tool Calls
|
| 86 |
-
```
|
| 87 |
-
Client → POST /messages/?session_id=abc123
|
| 88 |
-
Proxy looks up api_key for session abc123
|
| 89 |
-
Proxy injects: /messages/?session_id=abc123&api_key=fm_xxx
|
| 90 |
-
FastMCP receives request with API key
|
| 91 |
-
FastMCP authenticates and executes tool
|
| 92 |
-
```
|
| 93 |
-
|
| 94 |
-
## Multi-Tenant Support
|
| 95 |
-
|
| 96 |
-
Each client connection gets its own session with its own API key:
|
| 97 |
-
|
| 98 |
-
```
|
| 99 |
-
User A → session_1 → api_key_A
|
| 100 |
-
User B → session_2 → api_key_B
|
| 101 |
-
User C → session_3 → api_key_C
|
| 102 |
-
```
|
| 103 |
-
|
| 104 |
-
All requests are isolated and authenticated independently!
|
| 105 |
-
|
| 106 |
-
## Configuration
|
| 107 |
-
|
| 108 |
-
### Environment Variables
|
| 109 |
-
|
| 110 |
-
**For proxy.py:**
|
| 111 |
-
- `PROXY_PORT` - Proxy listening port (default: 7860)
|
| 112 |
-
- `FASTMCP_PORT` - FastMCP internal port (default: 7861)
|
| 113 |
-
- `FASTMCP_HOST` - FastMCP host (default: localhost)
|
| 114 |
-
|
| 115 |
-
**For app.py:**
|
| 116 |
-
- `PORT` - FastMCP port (default: 7861)
|
| 117 |
-
- `HOST` - FastMCP host (default: 0.0.0.0)
|
| 118 |
-
|
| 119 |
-
### Claude Desktop Config
|
| 120 |
-
|
| 121 |
-
**With API key in URL** (recommended):
|
| 122 |
-
```json
|
| 123 |
-
{
|
| 124 |
-
"mcpServers": {
|
| 125 |
-
"fleetmind": {
|
| 126 |
-
"command": "npx",
|
| 127 |
-
"args": [
|
| 128 |
-
"mcp-remote",
|
| 129 |
-
"http://localhost:7860/sse?api_key=YOUR_API_KEY_HERE"
|
| 130 |
-
]
|
| 131 |
-
}
|
| 132 |
-
}
|
| 133 |
-
}
|
| 134 |
-
```
|
| 135 |
-
|
| 136 |
-
## Deployment to Hugging Face
|
| 137 |
-
|
| 138 |
-
### Option 1: Single Script (Recommended)
|
| 139 |
-
|
| 140 |
-
1. Create `app_with_proxy.py` that runs both servers
|
| 141 |
-
2. Update `Dockerfile` to install `aiohttp`
|
| 142 |
-
3. Deploy to HuggingFace Space
|
| 143 |
-
|
| 144 |
-
### Option 2: Process Manager
|
| 145 |
-
|
| 146 |
-
1. Use `start_with_proxy.py` as entry point
|
| 147 |
-
2. Update `Dockerfile`:
|
| 148 |
-
```dockerfile
|
| 149 |
-
CMD ["python", "start_with_proxy.py"]
|
| 150 |
-
```
|
| 151 |
-
|
| 152 |
-
### Option 3: Supervisor
|
| 153 |
-
|
| 154 |
-
1. Install supervisor
|
| 155 |
-
2. Configure both processes
|
| 156 |
-
3. Use supervisor as entry point
|
| 157 |
-
|
| 158 |
-
## Troubleshooting
|
| 159 |
-
|
| 160 |
-
### Proxy not starting
|
| 161 |
-
- Check port 7860 is available: `netstat -an | findstr 7860`
|
| 162 |
-
- Check Python version: `python --version` (need 3.7+)
|
| 163 |
-
- Install aiohttp: `pip install aiohttp`
|
| 164 |
-
|
| 165 |
-
### FastMCP not starting
|
| 166 |
-
- Check port 7861 is available
|
| 167 |
-
- Check database connection
|
| 168 |
-
- Check environment variables in `.env`
|
| 169 |
-
|
| 170 |
-
### API key not captured
|
| 171 |
-
- Check logs for "🔑 Captured API key from SSE connection"
|
| 172 |
-
- Verify API key is in URL: `/sse?api_key=xxx`
|
| 173 |
-
- Check proxy is receiving requests
|
| 174 |
-
|
| 175 |
-
### Authentication failing
|
| 176 |
-
- Check session linking: "✅ Linked session ... to API key"
|
| 177 |
-
- Verify API key is valid in database
|
| 178 |
-
- Check FastMCP logs for authentication errors
|
| 179 |
-
|
| 180 |
-
## Advantages
|
| 181 |
-
|
| 182 |
-
✅ **True multi-tenant** - Each user has their own API key
|
| 183 |
-
✅ **No FastMCP changes** - Works with existing FastMCP code
|
| 184 |
-
✅ **Simple** - Only 200 lines of proxy code
|
| 185 |
-
✅ **Reliable** - Standard HTTP proxy pattern
|
| 186 |
-
✅ **Scalable** - Can add Redis for session storage
|
| 187 |
-
✅ **Deployable** - Works on HuggingFace, AWS, anywhere
|
| 188 |
-
|
| 189 |
-
## Future Improvements
|
| 190 |
-
|
| 191 |
-
- [ ] Add Redis for distributed session storage
|
| 192 |
-
- [ ] Add session expiration (TTL)
|
| 193 |
-
- [ ] Add request logging
|
| 194 |
-
- [ ] Add metrics/monitoring
|
| 195 |
-
- [ ] Add rate limiting per API key
|
| 196 |
-
- [ ] Add WebSocket support
|
| 197 |
-
|
| 198 |
-
## Support
|
| 199 |
-
|
| 200 |
-
For issues or questions:
|
| 201 |
-
- GitHub: https://github.com/mashrur-rahman-fahim/fleetmind-mcp
|
| 202 |
-
- HuggingFace: https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QUICK_START.md
DELETED
|
@@ -1,138 +0,0 @@
|
|
| 1 |
-
# GitHub Actions Setup - Quick Start
|
| 2 |
-
|
| 3 |
-
## ⚡ Complete These 4 Steps (15 minutes)
|
| 4 |
-
|
| 5 |
-
### STEP 1: Create GitHub Repository (2 min)
|
| 6 |
-
|
| 7 |
-
1. Go to: https://github.com/new
|
| 8 |
-
2. Name: `fleetmind-mcp`
|
| 9 |
-
3. Visibility: Public
|
| 10 |
-
4. **DON'T** initialize with README
|
| 11 |
-
5. Click "Create repository"
|
| 12 |
-
6. **SAVE YOUR USERNAME!**
|
| 13 |
-
|
| 14 |
-
---
|
| 15 |
-
|
| 16 |
-
### STEP 2: Get Hugging Face Token (2 min)
|
| 17 |
-
|
| 18 |
-
1. Go to: https://huggingface.co/settings/tokens
|
| 19 |
-
2. Click "Create new token"
|
| 20 |
-
3. Name: `GitHub Actions Sync`
|
| 21 |
-
4. Type: **Write**
|
| 22 |
-
5. Click "Generate token"
|
| 23 |
-
6. **COPY THE TOKEN** (starts with `hf_...`)
|
| 24 |
-
|
| 25 |
-
---
|
| 26 |
-
|
| 27 |
-
### STEP 3: Push to GitHub (3 min)
|
| 28 |
-
|
| 29 |
-
**Open PowerShell and run:**
|
| 30 |
-
|
| 31 |
-
```powershell
|
| 32 |
-
# Navigate to repo
|
| 33 |
-
cd F:\github-fleetmind-team
|
| 34 |
-
|
| 35 |
-
# Add and commit
|
| 36 |
-
git add .
|
| 37 |
-
git commit -m "Initial commit with GitHub Actions"
|
| 38 |
-
|
| 39 |
-
# Add GitHub remote (REPLACE YOUR-USERNAME!)
|
| 40 |
-
git remote add origin https://github.com/YOUR-USERNAME/fleetmind-mcp.git
|
| 41 |
-
|
| 42 |
-
# Push to GitHub
|
| 43 |
-
git branch -M main
|
| 44 |
-
git push -u origin main
|
| 45 |
-
```
|
| 46 |
-
|
| 47 |
-
**You'll be asked for credentials:**
|
| 48 |
-
- Username: Your GitHub username
|
| 49 |
-
- Password: Your GitHub Personal Access Token
|
| 50 |
-
- Get it at: https://github.com/settings/tokens
|
| 51 |
-
- Or use GitHub Desktop/CLI for easier auth
|
| 52 |
-
|
| 53 |
-
---
|
| 54 |
-
|
| 55 |
-
### STEP 4: Add HF_TOKEN to GitHub (3 min)
|
| 56 |
-
|
| 57 |
-
1. **Go to:** `https://github.com/YOUR-USERNAME/fleetmind-mcp/settings/secrets/actions`
|
| 58 |
-
|
| 59 |
-
2. **Click:** "New repository secret"
|
| 60 |
-
|
| 61 |
-
3. **Fill in:**
|
| 62 |
-
- Name: `HF_TOKEN`
|
| 63 |
-
- Secret: Paste your Hugging Face token from Step 2
|
| 64 |
-
|
| 65 |
-
4. **Click:** "Add secret"
|
| 66 |
-
|
| 67 |
-
---
|
| 68 |
-
|
| 69 |
-
## ✅ Test It Works (5 min)
|
| 70 |
-
|
| 71 |
-
```powershell
|
| 72 |
-
cd F:\github-fleetmind-team
|
| 73 |
-
|
| 74 |
-
# Make a small change
|
| 75 |
-
echo "`n## Auto-synced from GitHub" >> README.md
|
| 76 |
-
|
| 77 |
-
# Commit and push
|
| 78 |
-
git add README.md
|
| 79 |
-
git commit -m "Test: Auto-sync"
|
| 80 |
-
git push origin main
|
| 81 |
-
```
|
| 82 |
-
|
| 83 |
-
**Check:**
|
| 84 |
-
1. GitHub Actions: `https://github.com/YOUR-USERNAME/fleetmind-mcp/actions`
|
| 85 |
-
- Should show green checkmark ✅
|
| 86 |
-
2. HF Space: https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai
|
| 87 |
-
- Should have new commit
|
| 88 |
-
|
| 89 |
-
---
|
| 90 |
-
|
| 91 |
-
## 🎉 Done! Now Your Team Can Collaborate
|
| 92 |
-
|
| 93 |
-
**Team members:**
|
| 94 |
-
|
| 95 |
-
```bash
|
| 96 |
-
# Clone GitHub repo
|
| 97 |
-
git clone https://github.com/YOUR-USERNAME/fleetmind-mcp.git
|
| 98 |
-
cd fleetmind-mcp
|
| 99 |
-
|
| 100 |
-
# Make changes
|
| 101 |
-
# ... edit files ...
|
| 102 |
-
|
| 103 |
-
# Push to GitHub (auto-syncs to HF Space!)
|
| 104 |
-
git add .
|
| 105 |
-
git commit -m "Add feature"
|
| 106 |
-
git push
|
| 107 |
-
```
|
| 108 |
-
|
| 109 |
-
**Every push to GitHub automatically updates HF Space!** ✨
|
| 110 |
-
|
| 111 |
-
---
|
| 112 |
-
|
| 113 |
-
## 📋 Add Team Members to GitHub
|
| 114 |
-
|
| 115 |
-
1. Go to: `https://github.com/YOUR-USERNAME/fleetmind-mcp/settings/access`
|
| 116 |
-
2. Click "Add people"
|
| 117 |
-
3. Enter their GitHub username
|
| 118 |
-
4. Role: **Write**
|
| 119 |
-
5. Click "Add"
|
| 120 |
-
|
| 121 |
-
---
|
| 122 |
-
|
| 123 |
-
## 🆘 Quick Troubleshooting
|
| 124 |
-
|
| 125 |
-
**GitHub Actions fails?**
|
| 126 |
-
→ Check HF_TOKEN is set correctly in GitHub Secrets
|
| 127 |
-
|
| 128 |
-
**Can't push to GitHub?**
|
| 129 |
-
→ Create GitHub Personal Access Token: https://github.com/settings/tokens
|
| 130 |
-
|
| 131 |
-
**Files too large?**
|
| 132 |
-
→ Use Git LFS (see full guide)
|
| 133 |
-
|
| 134 |
-
---
|
| 135 |
-
|
| 136 |
-
**Full documentation:** See `GITHUB_ACTIONS_SETUP.md`
|
| 137 |
-
|
| 138 |
-
**Ready to build!** 🚀
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README_MCP.md
DELETED
|
@@ -1,497 +0,0 @@
|
|
| 1 |
-
# FleetMind MCP Server
|
| 2 |
-
|
| 3 |
-
**Industry-standard Model Context Protocol server for AI-powered delivery dispatch management**
|
| 4 |
-
|
| 5 |
-
[](https://github.com/jlowin/fastmcp)
|
| 6 |
-
[](https://www.python.org/)
|
| 7 |
-
[](LICENSE)
|
| 8 |
-
|
| 9 |
-
---
|
| 10 |
-
|
| 11 |
-
## Overview
|
| 12 |
-
|
| 13 |
-
FleetMind MCP Server provides 18 AI tools and 2 real-time resources for managing delivery dispatch operations through any MCP-compatible client (Claude Desktop, Continue, Cline, etc.).
|
| 14 |
-
|
| 15 |
-
**What is MCP?**
|
| 16 |
-
The Model Context Protocol (MCP) is an open standard that enables AI assistants to securely connect to external data sources and tools. Think of it as a universal API for AI agents.
|
| 17 |
-
|
| 18 |
-
---
|
| 19 |
-
|
| 20 |
-
## Quick Start
|
| 21 |
-
|
| 22 |
-
### 1. Installation
|
| 23 |
-
|
| 24 |
-
```bash
|
| 25 |
-
# Clone the repository
|
| 26 |
-
git clone https://github.com/your-org/fleetmind-mcp.git
|
| 27 |
-
cd fleetmind-mcp
|
| 28 |
-
|
| 29 |
-
# Install dependencies
|
| 30 |
-
pip install -r requirements.txt
|
| 31 |
-
|
| 32 |
-
# Configure environment variables
|
| 33 |
-
cp .env.example .env
|
| 34 |
-
# Edit .env with your credentials
|
| 35 |
-
```
|
| 36 |
-
|
| 37 |
-
### 2. Configure Environment
|
| 38 |
-
|
| 39 |
-
Edit `.env` file:
|
| 40 |
-
|
| 41 |
-
```ini
|
| 42 |
-
# Database (required)
|
| 43 |
-
DB_HOST=your-postgres-host.com
|
| 44 |
-
DB_PORT=5432
|
| 45 |
-
DB_NAME=fleetmind
|
| 46 |
-
DB_USER=your_db_user
|
| 47 |
-
DB_PASSWORD=your_db_password
|
| 48 |
-
|
| 49 |
-
# Google Maps API (required for geocoding)
|
| 50 |
-
GOOGLE_MAPS_API_KEY=your_google_maps_key
|
| 51 |
-
```
|
| 52 |
-
|
| 53 |
-
### 3. Test the Server
|
| 54 |
-
|
| 55 |
-
```bash
|
| 56 |
-
# Test server imports and database connectivity
|
| 57 |
-
python -c "import server; print('FleetMind MCP Server ready!')"
|
| 58 |
-
```
|
| 59 |
-
|
| 60 |
-
### 4. Run with Claude Desktop
|
| 61 |
-
|
| 62 |
-
Add to your Claude Desktop config (`claude_desktop_config.json`):
|
| 63 |
-
|
| 64 |
-
```json
|
| 65 |
-
{
|
| 66 |
-
"mcpServers": {
|
| 67 |
-
"fleetmind": {
|
| 68 |
-
"command": "python",
|
| 69 |
-
"args": ["F:\\path\\to\\fleetmind-mcp\\server.py"],
|
| 70 |
-
"env": {
|
| 71 |
-
"GOOGLE_MAPS_API_KEY": "your_api_key",
|
| 72 |
-
"DB_HOST": "your-host.com",
|
| 73 |
-
"DB_NAME": "fleetmind",
|
| 74 |
-
"DB_USER": "your_user",
|
| 75 |
-
"DB_PASSWORD": "your_password"
|
| 76 |
-
}
|
| 77 |
-
}
|
| 78 |
-
}
|
| 79 |
-
}
|
| 80 |
-
```
|
| 81 |
-
|
| 82 |
-
Restart Claude Desktop. You'll now see FleetMind tools available!
|
| 83 |
-
|
| 84 |
-
---
|
| 85 |
-
|
| 86 |
-
## Architecture
|
| 87 |
-
|
| 88 |
-
### **Before (Gradio UI):**
|
| 89 |
-
```
|
| 90 |
-
User → Gradio Web UI → ChatEngine → Gemini/Claude API → Tools → Database
|
| 91 |
-
```
|
| 92 |
-
|
| 93 |
-
### **After (MCP Protocol):**
|
| 94 |
-
```
|
| 95 |
-
User → Claude Desktop (or any MCP client) → MCP Protocol → FleetMind Server → Tools → Database
|
| 96 |
-
└→ Continue.dev ────────────────────────┘
|
| 97 |
-
└→ Cline ───────────────────────────────┘
|
| 98 |
-
└→ Custom Apps ──────────────────────────┘
|
| 99 |
-
```
|
| 100 |
-
|
| 101 |
-
**Benefits:**
|
| 102 |
-
- ✅ Use from multiple clients (Claude Desktop, VS Code, mobile apps)
|
| 103 |
-
- ✅ 46% less code (no UI, no provider abstractions)
|
| 104 |
-
- ✅ Industry-standard protocol (MCP)
|
| 105 |
-
- ✅ Better testing (isolated tools)
|
| 106 |
-
- ✅ Scalable architecture
|
| 107 |
-
|
| 108 |
-
---
|
| 109 |
-
|
| 110 |
-
## Features
|
| 111 |
-
|
| 112 |
-
### **18 AI Tools**
|
| 113 |
-
|
| 114 |
-
#### Order Management (10 tools)
|
| 115 |
-
- `geocode_address` - Convert addresses to GPS coordinates
|
| 116 |
-
- `calculate_route` - Find shortest route between locations
|
| 117 |
-
- `create_order` - Create new delivery orders
|
| 118 |
-
- `count_orders` - Count orders with filters
|
| 119 |
-
- `fetch_orders` - Retrieve orders with pagination
|
| 120 |
-
- `get_order_details` - Get complete order information
|
| 121 |
-
- `search_orders` - Search by customer/ID
|
| 122 |
-
- `get_incomplete_orders` - List active deliveries
|
| 123 |
-
- `update_order` - Update order details (auto-geocoding)
|
| 124 |
-
- `delete_order` - Permanently remove orders
|
| 125 |
-
|
| 126 |
-
#### Driver Management (8 tools)
|
| 127 |
-
- `create_driver` - Onboard new drivers
|
| 128 |
-
- `count_drivers` - Count drivers with filters
|
| 129 |
-
- `fetch_drivers` - Retrieve drivers with pagination
|
| 130 |
-
- `get_driver_details` - Get driver info + reverse-geocoded location
|
| 131 |
-
- `search_drivers` - Search by name/plate/ID
|
| 132 |
-
- `get_available_drivers` - List drivers ready for dispatch
|
| 133 |
-
- `update_driver` - Update driver information
|
| 134 |
-
- `delete_driver` - Remove drivers from fleet
|
| 135 |
-
|
| 136 |
-
### **2 Real-Time Resources**
|
| 137 |
-
|
| 138 |
-
- `orders://all` - Live orders dataset (last 30 days, max 1000)
|
| 139 |
-
- `drivers://all` - Live drivers dataset with locations
|
| 140 |
-
|
| 141 |
-
Resources provide AI assistants with contextual data for smarter responses.
|
| 142 |
-
|
| 143 |
-
---
|
| 144 |
-
|
| 145 |
-
## Usage Examples
|
| 146 |
-
|
| 147 |
-
### Example 1: Create an Order
|
| 148 |
-
|
| 149 |
-
**User (in Claude Desktop):**
|
| 150 |
-
"Create an urgent delivery order for Sarah Johnson at 456 Oak Ave, San Francisco CA. Phone: 555-1234."
|
| 151 |
-
|
| 152 |
-
**Claude automatically:**
|
| 153 |
-
1. Calls `geocode_address("456 Oak Ave, San Francisco CA")`
|
| 154 |
-
2. Gets coordinates: `(37.7749, -122.4194)`
|
| 155 |
-
3. Calls `create_order(customer_name="Sarah Johnson", delivery_address="456 Oak Ave, SF CA 94103", delivery_lat=37.7749, delivery_lng=-122.4194, customer_phone="555-1234", priority="urgent")`
|
| 156 |
-
4. Returns: `"Order ORD-20251114163800 created successfully!"`
|
| 157 |
-
|
| 158 |
-
### Example 2: Assign Driver
|
| 159 |
-
|
| 160 |
-
**User:**
|
| 161 |
-
"Assign order ORD-20251114163800 to the nearest available driver"
|
| 162 |
-
|
| 163 |
-
**Claude automatically:**
|
| 164 |
-
1. Calls `get_order_details("ORD-20251114163800")` → Gets delivery location
|
| 165 |
-
2. Calls `get_available_drivers(limit=10)` → Lists available drivers
|
| 166 |
-
3. Calls `calculate_route()` for each driver → Finds nearest
|
| 167 |
-
4. Calls `update_order(order_id="ORD-20251114163800", assigned_driver_id="DRV-...", status="assigned")`
|
| 168 |
-
5. Returns: `"Order assigned to John Smith (DRV-20251110120000), 5.2 km away, ETA 12 mins"`
|
| 169 |
-
|
| 170 |
-
### Example 3: Track Orders
|
| 171 |
-
|
| 172 |
-
**User:**
|
| 173 |
-
"Show me all urgent orders that haven't been delivered yet"
|
| 174 |
-
|
| 175 |
-
**Claude automatically:**
|
| 176 |
-
1. Calls `fetch_orders(status="pending", priority="urgent")` OR
|
| 177 |
-
2. Calls `fetch_orders(status="in_transit", priority="urgent")`
|
| 178 |
-
3. Returns formatted list with customer names, addresses, and deadlines
|
| 179 |
-
|
| 180 |
-
---
|
| 181 |
-
|
| 182 |
-
## API Reference
|
| 183 |
-
|
| 184 |
-
### Tool: `create_order`
|
| 185 |
-
|
| 186 |
-
Create a new delivery order.
|
| 187 |
-
|
| 188 |
-
**Parameters:**
|
| 189 |
-
- `customer_name` (string, required): Full name
|
| 190 |
-
- `delivery_address` (string, required): Complete address
|
| 191 |
-
- `delivery_lat` (float, required): Latitude from geocoding
|
| 192 |
-
- `delivery_lng` (float, required): Longitude from geocoding
|
| 193 |
-
- `customer_phone` (string, optional): Phone number
|
| 194 |
-
- `customer_email` (string, optional): Email address
|
| 195 |
-
- `priority` (enum, optional): `standard` | `express` | `urgent` (default: `standard`)
|
| 196 |
-
- `weight_kg` (float, optional): Package weight (default: 5.0)
|
| 197 |
-
- `special_instructions` (string, optional): Delivery notes
|
| 198 |
-
- `time_window_end` (string, optional): Deadline in ISO format (default: +6 hours)
|
| 199 |
-
|
| 200 |
-
**Returns:**
|
| 201 |
-
```json
|
| 202 |
-
{
|
| 203 |
-
"success": true,
|
| 204 |
-
"order_id": "ORD-20251114163800",
|
| 205 |
-
"status": "pending",
|
| 206 |
-
"customer": "Sarah Johnson",
|
| 207 |
-
"address": "456 Oak Ave, San Francisco CA 94103",
|
| 208 |
-
"deadline": "2025-11-14T22:38:00",
|
| 209 |
-
"priority": "urgent",
|
| 210 |
-
"message": "Order created successfully!"
|
| 211 |
-
}
|
| 212 |
-
```
|
| 213 |
-
|
| 214 |
-
### Tool: `calculate_route`
|
| 215 |
-
|
| 216 |
-
Calculate shortest route between two locations.
|
| 217 |
-
|
| 218 |
-
**Parameters:**
|
| 219 |
-
- `origin` (string, required): Starting location (address or "lat,lng")
|
| 220 |
-
- `destination` (string, required): Ending location (address or "lat,lng")
|
| 221 |
-
- `mode` (enum, optional): `driving` | `walking` | `bicycling` | `transit` (default: `driving`)
|
| 222 |
-
- `alternatives` (boolean, optional): Return multiple routes (default: false)
|
| 223 |
-
- `include_steps` (boolean, optional): Include turn-by-turn directions (default: false)
|
| 224 |
-
|
| 225 |
-
**Returns:**
|
| 226 |
-
```json
|
| 227 |
-
{
|
| 228 |
-
"success": true,
|
| 229 |
-
"origin": "San Francisco City Hall, CA 94102, USA",
|
| 230 |
-
"destination": "Oakland Airport, CA 94621, USA",
|
| 231 |
-
"distance": {"meters": 25400, "text": "25.4 km"},
|
| 232 |
-
"duration": {"seconds": 1680, "text": "28 mins"},
|
| 233 |
-
"mode": "driving",
|
| 234 |
-
"route_summary": "I-880 N",
|
| 235 |
-
"confidence": "high (Google Maps API)"
|
| 236 |
-
}
|
| 237 |
-
```
|
| 238 |
-
|
| 239 |
-
### Resource: `orders://all`
|
| 240 |
-
|
| 241 |
-
Real-time orders dataset for AI context.
|
| 242 |
-
|
| 243 |
-
**Contains:** All orders from last 30 days (max 1000)
|
| 244 |
-
|
| 245 |
-
**Fields:** order_id, customer_name, delivery_address, status, priority, created_at, assigned_driver_id
|
| 246 |
-
|
| 247 |
-
**Usage:** AI automatically references this when answering questions like "How many pending orders?" or "What's the oldest unassigned order?"
|
| 248 |
-
|
| 249 |
-
### Resource: `drivers://all`
|
| 250 |
-
|
| 251 |
-
Real-time drivers dataset with current locations.
|
| 252 |
-
|
| 253 |
-
**Contains:** All drivers sorted alphabetically
|
| 254 |
-
|
| 255 |
-
**Fields:** driver_id, name, status, vehicle_type, vehicle_plate, current_lat, current_lng, last_location_update
|
| 256 |
-
|
| 257 |
-
**Usage:** AI automatically references this for questions like "How many active drivers?" or "Which driver is closest to downtown?"
|
| 258 |
-
|
| 259 |
-
---
|
| 260 |
-
|
| 261 |
-
## Database Schema
|
| 262 |
-
|
| 263 |
-
### `orders` table (26 columns)
|
| 264 |
-
|
| 265 |
-
```sql
|
| 266 |
-
CREATE TABLE orders (
|
| 267 |
-
order_id VARCHAR(50) PRIMARY KEY,
|
| 268 |
-
customer_name VARCHAR(255) NOT NULL,
|
| 269 |
-
customer_phone VARCHAR(20),
|
| 270 |
-
customer_email VARCHAR(255),
|
| 271 |
-
delivery_address TEXT NOT NULL,
|
| 272 |
-
delivery_lat DECIMAL(10,8),
|
| 273 |
-
delivery_lng DECIMAL(11,8),
|
| 274 |
-
status VARCHAR(20) CHECK (status IN ('pending','assigned','in_transit','delivered','failed','cancelled')),
|
| 275 |
-
priority VARCHAR(20) CHECK (priority IN ('standard','express','urgent')),
|
| 276 |
-
time_window_end TIMESTAMP,
|
| 277 |
-
assigned_driver_id VARCHAR(50),
|
| 278 |
-
payment_status VARCHAR(20) CHECK (payment_status IN ('pending','paid','cod')),
|
| 279 |
-
weight_kg DECIMAL(10,2),
|
| 280 |
-
special_instructions TEXT,
|
| 281 |
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 282 |
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 283 |
-
-- ... additional fields
|
| 284 |
-
);
|
| 285 |
-
```
|
| 286 |
-
|
| 287 |
-
### `drivers` table (15 columns)
|
| 288 |
-
|
| 289 |
-
```sql
|
| 290 |
-
CREATE TABLE drivers (
|
| 291 |
-
driver_id VARCHAR(50) PRIMARY KEY,
|
| 292 |
-
name VARCHAR(255) NOT NULL,
|
| 293 |
-
phone VARCHAR(20),
|
| 294 |
-
email VARCHAR(255),
|
| 295 |
-
status VARCHAR(20) CHECK (status IN ('active','busy','offline','unavailable')),
|
| 296 |
-
vehicle_type VARCHAR(50),
|
| 297 |
-
vehicle_plate VARCHAR(20),
|
| 298 |
-
capacity_kg DECIMAL(10,2),
|
| 299 |
-
skills JSONB,
|
| 300 |
-
current_lat DECIMAL(10,8),
|
| 301 |
-
current_lng DECIMAL(11,8),
|
| 302 |
-
last_location_update TIMESTAMP,
|
| 303 |
-
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 304 |
-
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 305 |
-
);
|
| 306 |
-
```
|
| 307 |
-
|
| 308 |
-
---
|
| 309 |
-
|
| 310 |
-
## Development
|
| 311 |
-
|
| 312 |
-
### Project Structure
|
| 313 |
-
|
| 314 |
-
```
|
| 315 |
-
fleetmind-mcp/
|
| 316 |
-
├── server.py # Main MCP server (882 lines)
|
| 317 |
-
├── pyproject.toml # Package configuration
|
| 318 |
-
├── mcp_config.json # MCP metadata
|
| 319 |
-
├── requirements.txt # Dependencies
|
| 320 |
-
├── .env # Environment variables
|
| 321 |
-
│
|
| 322 |
-
├── chat/
|
| 323 |
-
│ ├── tools.py # 18 tool handlers (2099 lines)
|
| 324 |
-
│ └── geocoding.py # Geocoding service (429 lines)
|
| 325 |
-
│
|
| 326 |
-
├── database/
|
| 327 |
-
│ ├── connection.py # Database layer (221 lines)
|
| 328 |
-
│ └── schema.py # Schema definitions (213 lines)
|
| 329 |
-
│
|
| 330 |
-
├── logs/ # Server logs
|
| 331 |
-
└── docs/ # Documentation
|
| 332 |
-
```
|
| 333 |
-
|
| 334 |
-
### Running Tests
|
| 335 |
-
|
| 336 |
-
```bash
|
| 337 |
-
# Install test dependencies
|
| 338 |
-
pip install pytest pytest-asyncio
|
| 339 |
-
|
| 340 |
-
# Run tests
|
| 341 |
-
pytest tests/
|
| 342 |
-
```
|
| 343 |
-
|
| 344 |
-
### Testing with MCP Inspector
|
| 345 |
-
|
| 346 |
-
```bash
|
| 347 |
-
# Official MCP protocol testing tool
|
| 348 |
-
npx @modelcontextprotocol/inspector python server.py
|
| 349 |
-
```
|
| 350 |
-
|
| 351 |
-
---
|
| 352 |
-
|
| 353 |
-
## Deployment
|
| 354 |
-
|
| 355 |
-
### Option 1: Local Development
|
| 356 |
-
|
| 357 |
-
```bash
|
| 358 |
-
python server.py
|
| 359 |
-
```
|
| 360 |
-
|
| 361 |
-
### Option 2: Docker Container
|
| 362 |
-
|
| 363 |
-
```dockerfile
|
| 364 |
-
FROM python:3.11-slim
|
| 365 |
-
WORKDIR /app
|
| 366 |
-
COPY requirements.txt .
|
| 367 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 368 |
-
COPY . .
|
| 369 |
-
CMD ["python", "server.py"]
|
| 370 |
-
```
|
| 371 |
-
|
| 372 |
-
```bash
|
| 373 |
-
docker build -t fleetmind-mcp .
|
| 374 |
-
docker run -d --env-file .env fleetmind-mcp
|
| 375 |
-
```
|
| 376 |
-
|
| 377 |
-
### Option 3: Production Server
|
| 378 |
-
|
| 379 |
-
For production, use a process manager like `supervisord` or `systemd`:
|
| 380 |
-
|
| 381 |
-
```ini
|
| 382 |
-
# /etc/systemd/system/fleetmind-mcp.service
|
| 383 |
-
[Unit]
|
| 384 |
-
Description=FleetMind MCP Server
|
| 385 |
-
After=network.target
|
| 386 |
-
|
| 387 |
-
[Service]
|
| 388 |
-
Type=simple
|
| 389 |
-
User=fleetmind
|
| 390 |
-
WorkingDirectory=/opt/fleetmind-mcp
|
| 391 |
-
Environment="PATH=/opt/fleetmind-mcp/venv/bin"
|
| 392 |
-
EnvironmentFile=/opt/fleetmind-mcp/.env
|
| 393 |
-
ExecStart=/opt/fleetmind-mcp/venv/bin/python server.py
|
| 394 |
-
Restart=always
|
| 395 |
-
|
| 396 |
-
[Install]
|
| 397 |
-
WantedBy=multi-user.target
|
| 398 |
-
```
|
| 399 |
-
|
| 400 |
-
---
|
| 401 |
-
|
| 402 |
-
## Troubleshooting
|
| 403 |
-
|
| 404 |
-
### Error: "Cannot import name 'UserMessage'"
|
| 405 |
-
|
| 406 |
-
**Solution:** Prompts are currently disabled pending FastMCP API confirmation. Tools and resources work perfectly.
|
| 407 |
-
|
| 408 |
-
### Error: "Database connection failed"
|
| 409 |
-
|
| 410 |
-
**Check:**
|
| 411 |
-
1. `.env` file has correct credentials
|
| 412 |
-
2. PostgreSQL server is running
|
| 413 |
-
3. Database `fleetmind` exists
|
| 414 |
-
4. Network allows connection (check firewall/security groups)
|
| 415 |
-
|
| 416 |
-
### Error: "Geocoding failed"
|
| 417 |
-
|
| 418 |
-
**Check:**
|
| 419 |
-
1. `GOOGLE_MAPS_API_KEY` is set in `.env`
|
| 420 |
-
2. API key has Geocoding API enabled
|
| 421 |
-
3. API key has sufficient quota
|
| 422 |
-
|
| 423 |
-
**Fallback:** Server automatically uses mock geocoding if API unavailable.
|
| 424 |
-
|
| 425 |
-
---
|
| 426 |
-
|
| 427 |
-
## Migration from Gradio UI
|
| 428 |
-
|
| 429 |
-
### What Changed?
|
| 430 |
-
|
| 431 |
-
| Component | Gradio Version | MCP Version |
|
| 432 |
-
|-----------|----------------|-------------|
|
| 433 |
-
| UI | Gradio web interface | Any MCP client |
|
| 434 |
-
| AI Provider | Gemini/Claude via API | Client handles AI |
|
| 435 |
-
| Tool Execution | chat/tools.py handlers | Same handlers |
|
| 436 |
-
| Database | PostgreSQL/Neon | Same database |
|
| 437 |
-
| Geocoding | Google Maps API | Same API |
|
| 438 |
-
|
| 439 |
-
### What Stayed the Same?
|
| 440 |
-
|
| 441 |
-
- ✅ All 18 tool handlers (unchanged)
|
| 442 |
-
- ✅ Database schema (identical)
|
| 443 |
-
- ✅ Geocoding logic (same)
|
| 444 |
-
- ✅ Business logic (preserved)
|
| 445 |
-
- ✅ .env configuration (compatible)
|
| 446 |
-
|
| 447 |
-
### Migration Steps
|
| 448 |
-
|
| 449 |
-
1. **Backup your data:** `pg_dump fleetmind > backup.sql`
|
| 450 |
-
2. **Install MCP dependencies:** `pip install -r requirements.txt`
|
| 451 |
-
3. **Test server:** `python -c "import server"`
|
| 452 |
-
4. **Configure Claude Desktop:** Add server to `claude_desktop_config.json`
|
| 453 |
-
5. **Test with Claude:** Create a test order
|
| 454 |
-
6. **Archive old code:** Move `ui/`, `chat/providers/`, `chat/chat_engine.py` to `archive/`
|
| 455 |
-
|
| 456 |
-
---
|
| 457 |
-
|
| 458 |
-
## Contributing
|
| 459 |
-
|
| 460 |
-
We welcome contributions! Please:
|
| 461 |
-
|
| 462 |
-
1. Fork the repository
|
| 463 |
-
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
| 464 |
-
3. Commit your changes (`git commit -m 'Add amazing feature'`)
|
| 465 |
-
4. Push to the branch (`git push origin feature/amazing-feature`)
|
| 466 |
-
5. Open a Pull Request
|
| 467 |
-
|
| 468 |
-
---
|
| 469 |
-
|
| 470 |
-
## License
|
| 471 |
-
|
| 472 |
-
MIT License - see [LICENSE](LICENSE) file for details.
|
| 473 |
-
|
| 474 |
-
---
|
| 475 |
-
|
| 476 |
-
## Support
|
| 477 |
-
|
| 478 |
-
- **Issues:** https://github.com/your-org/fleetmind-mcp/issues
|
| 479 |
-
- **Documentation:** https://docs.fleetmind.com
|
| 480 |
-
- **Discord:** https://discord.gg/fleetmind
|
| 481 |
-
|
| 482 |
-
---
|
| 483 |
-
|
| 484 |
-
## Roadmap
|
| 485 |
-
|
| 486 |
-
- [x] Convert all 18 tools to MCP format
|
| 487 |
-
- [x] Add 2 real-time resources (orders, drivers)
|
| 488 |
-
- [ ] Add prompt templates (pending FastMCP API confirmation)
|
| 489 |
-
- [ ] Add assignment optimization algorithm
|
| 490 |
-
- [ ] Add route optimization for multi-stop deliveries
|
| 491 |
-
- [ ] Add real-time driver tracking via WebSocket
|
| 492 |
-
- [ ] Add analytics dashboard
|
| 493 |
-
- [] Mobile app MCP client
|
| 494 |
-
|
| 495 |
-
---
|
| 496 |
-
|
| 497 |
-
**Built with ❤️ using [FastMCP](https://github.com/jlowin/fastmcp)**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ROUTES_API_IMPLEMENTATION.md
DELETED
|
@@ -1,305 +0,0 @@
|
|
| 1 |
-
# Google Routes API Implementation - Complete
|
| 2 |
-
|
| 3 |
-
## Summary
|
| 4 |
-
|
| 5 |
-
Successfully integrated Google Routes API to replace mock routing calculations with real-time traffic data from Google Maps.
|
| 6 |
-
|
| 7 |
-
## What Was Implemented
|
| 8 |
-
|
| 9 |
-
### 1. Routes API Integration (`chat/tools.py`)
|
| 10 |
-
|
| 11 |
-
Added two new functions:
|
| 12 |
-
|
| 13 |
-
#### `_location_to_latlng()` (lines 798-824)
|
| 14 |
-
Helper function to convert addresses or coordinates to lat/lng format required by Routes API.
|
| 15 |
-
- Detects if input is already "lat,lng" format
|
| 16 |
-
- Otherwise geocodes the address using existing geocoding service
|
| 17 |
-
- Returns `{"latitude": float, "longitude": float}` dict
|
| 18 |
-
|
| 19 |
-
#### `_calculate_route_routes_api()` (lines 827-1004)
|
| 20 |
-
Complete Routes API implementation:
|
| 21 |
-
- Makes POST requests to `https://routes.googleapis.com/directions/v2:computeRoutes`
|
| 22 |
-
- Uses existing `GOOGLE_MAPS_API_KEY` from environment
|
| 23 |
-
- Sets proper headers (`X-Goog-Api-Key`, `X-Goog-FieldMask`)
|
| 24 |
-
- Requests traffic-aware routing for driving mode
|
| 25 |
-
- Supports all travel modes: DRIVE, WALK, BICYCLE, TRANSIT
|
| 26 |
-
- Returns alternative routes if requested
|
| 27 |
-
- Includes turn-by-turn directions if requested
|
| 28 |
-
- Returns data in same format as existing functions (backward compatible)
|
| 29 |
-
|
| 30 |
-
### 2. Triple Fallback Logic (`chat/tools.py` lines 680-698)
|
| 31 |
-
|
| 32 |
-
Updated `handle_calculate_route()` with intelligent fallback chain:
|
| 33 |
-
```
|
| 34 |
-
1. Try Routes API (recommended, most accurate)
|
| 35 |
-
↓ (if fails)
|
| 36 |
-
2. Try Directions API (legacy fallback)
|
| 37 |
-
↓ (if fails)
|
| 38 |
-
3. Use Mock calculation (offline mode)
|
| 39 |
-
```
|
| 40 |
-
|
| 41 |
-
Each fallback is logged clearly so you know which API is being used.
|
| 42 |
-
|
| 43 |
-
### 3. Documentation Update (`.env.example`)
|
| 44 |
-
|
| 45 |
-
Updated comments to explain:
|
| 46 |
-
- Routes API is recommended (most accurate)
|
| 47 |
-
- Directions API is legacy fallback
|
| 48 |
-
- System tries Routes API first automatically
|
| 49 |
-
- All three APIs need to be enabled in Google Cloud Console
|
| 50 |
-
|
| 51 |
-
## Test Results
|
| 52 |
-
|
| 53 |
-
### Dhaka Route Test (Successful!)
|
| 54 |
-
|
| 55 |
-
**Route:** Ahsanullah University → Tejgaon College
|
| 56 |
-
|
| 57 |
-
| Method | Distance | Time | Notes |
|
| 58 |
-
|--------|----------|------|-------|
|
| 59 |
-
| **Routes API** | **5.0 km** | **13 minutes** | ✅ Real-time traffic data |
|
| 60 |
-
| Mock Algorithm | 2.5 km | 57 minutes | ❌ 2x over-estimate |
|
| 61 |
-
| Real-World Avg | ~2.5 km | 31 minutes | Based on 4.8 km/h city average |
|
| 62 |
-
|
| 63 |
-
**Key Findings:**
|
| 64 |
-
|
| 65 |
-
1. **Routes API Working!**
|
| 66 |
-
- Successfully connected to Google Routes API
|
| 67 |
-
- Confidence: "high (Routes API with real-time traffic)"
|
| 68 |
-
- Route via: "Shaheed Tajuddin Ahmed Ave"
|
| 69 |
-
|
| 70 |
-
2. **Distance Difference Explained:**
|
| 71 |
-
- Straight-line: 2.5 km
|
| 72 |
-
- Actual road route: **5.0 km** (real roads with turns, one-ways)
|
| 73 |
-
- This is realistic - city roads are never straight lines!
|
| 74 |
-
|
| 75 |
-
3. **Time Estimate Analysis:**
|
| 76 |
-
- Routes API: 13 minutes for 5 km = 22.7 km/h average
|
| 77 |
-
- This is faster than 4.8 km/h city-wide average because:
|
| 78 |
-
- Google routes through less congested roads
|
| 79 |
-
- May be using highways/main roads
|
| 80 |
-
- Real-time traffic might be lighter than worst-case
|
| 81 |
-
- Current traffic conditions vs statistical average
|
| 82 |
-
|
| 83 |
-
4. **Improvement Over Mock:**
|
| 84 |
-
- Mock estimated: 57 minutes (way too conservative)
|
| 85 |
-
- Routes API: 13 minutes (real data)
|
| 86 |
-
- **4.4x more accurate!**
|
| 87 |
-
|
| 88 |
-
## How It Works
|
| 89 |
-
|
| 90 |
-
### API Flow
|
| 91 |
-
|
| 92 |
-
```
|
| 93 |
-
User requests route
|
| 94 |
-
↓
|
| 95 |
-
handle_calculate_route()
|
| 96 |
-
↓
|
| 97 |
-
Check if API key exists?
|
| 98 |
-
├─ NO → Use mock calculation
|
| 99 |
-
└─ YES → Try Routes API
|
| 100 |
-
↓
|
| 101 |
-
Routes API succeeds?
|
| 102 |
-
├─ YES → Return real traffic data ✅
|
| 103 |
-
└─ NO → Try Directions API (legacy)
|
| 104 |
-
↓
|
| 105 |
-
Directions API succeeds?
|
| 106 |
-
├─ YES → Return traffic data
|
| 107 |
-
└─ NO → Use mock calculation
|
| 108 |
-
```
|
| 109 |
-
|
| 110 |
-
### Routes API Request Format
|
| 111 |
-
|
| 112 |
-
```json
|
| 113 |
-
POST https://routes.googleapis.com/directions/v2:computeRoutes
|
| 114 |
-
|
| 115 |
-
Headers:
|
| 116 |
-
Content-Type: application/json
|
| 117 |
-
X-Goog-Api-Key: {YOUR_API_KEY}
|
| 118 |
-
X-Goog-FieldMask: routes.duration,routes.distanceMeters,...
|
| 119 |
-
|
| 120 |
-
Body:
|
| 121 |
-
{
|
| 122 |
-
"origin": {"location": {"latLng": {"latitude": 23.76, "longitude": 90.38}}},
|
| 123 |
-
"destination": {"location": {"latLng": {"latitude": 23.75, "longitude": 90.39}}},
|
| 124 |
-
"travelMode": "DRIVE",
|
| 125 |
-
"routingPreference": "TRAFFIC_AWARE",
|
| 126 |
-
"computeAlternativeRoutes": true
|
| 127 |
-
}
|
| 128 |
-
```
|
| 129 |
-
|
| 130 |
-
### Response Mapping
|
| 131 |
-
|
| 132 |
-
Routes API returns different format than Directions API. We map it to maintain compatibility:
|
| 133 |
-
|
| 134 |
-
| Routes API Field | Our Format |
|
| 135 |
-
|------------------|------------|
|
| 136 |
-
| `distanceMeters` | `distance.meters` + `distance.text` |
|
| 137 |
-
| `duration` ("795s") | `duration.seconds` + `duration.text` |
|
| 138 |
-
| `description` | `route_summary` |
|
| 139 |
-
| `legs[].steps[]` | `steps[]` (if requested) |
|
| 140 |
-
| `routes[1:]` | `alternatives[]` (if requested) |
|
| 141 |
-
|
| 142 |
-
## Benefits Achieved
|
| 143 |
-
|
| 144 |
-
### 1. Accuracy
|
| 145 |
-
- ✅ Real-time traffic data from Google
|
| 146 |
-
- ✅ Actual road distances (not straight-line estimates)
|
| 147 |
-
- ✅ Traffic-aware routing (considers current conditions)
|
| 148 |
-
- ✅ 4.4x more accurate than mock algorithm
|
| 149 |
-
|
| 150 |
-
### 2. Features
|
| 151 |
-
- ✅ Alternative routes available
|
| 152 |
-
- ✅ Turn-by-turn directions available
|
| 153 |
-
- ✅ Works with all travel modes (car, motorcycle, bicycle, walk, transit)
|
| 154 |
-
- ✅ City-specific routing (Dhaka, San Francisco, etc.)
|
| 155 |
-
|
| 156 |
-
### 3. Reliability
|
| 157 |
-
- ✅ Triple fallback chain ensures always-working system
|
| 158 |
-
- ✅ No breaking changes - existing code works unchanged
|
| 159 |
-
- ✅ Clear logging shows which API is being used
|
| 160 |
-
- ✅ Graceful degradation if APIs are unavailable
|
| 161 |
-
|
| 162 |
-
### 4. Future-Proof
|
| 163 |
-
- ✅ Uses Google's recommended Routes API
|
| 164 |
-
- ✅ Access to new features (tolls, eco-routes, etc.)
|
| 165 |
-
- ✅ Better performance and accuracy over time
|
| 166 |
-
- ✅ Still supports legacy Directions API
|
| 167 |
-
|
| 168 |
-
## Code Changes Summary
|
| 169 |
-
|
| 170 |
-
### Files Modified
|
| 171 |
-
|
| 172 |
-
1. **`chat/tools.py`**
|
| 173 |
-
- Added `_location_to_latlng()` helper (27 lines)
|
| 174 |
-
- Added `_calculate_route_routes_api()` function (178 lines)
|
| 175 |
-
- Updated `handle_calculate_route()` fallback logic (18 lines)
|
| 176 |
-
- Total: ~220 lines added
|
| 177 |
-
|
| 178 |
-
2. **`.env.example`**
|
| 179 |
-
- Updated Google Maps API documentation (5 lines)
|
| 180 |
-
|
| 181 |
-
### Files Not Changed
|
| 182 |
-
|
| 183 |
-
- ✅ `chat/geocoding.py` - No changes needed
|
| 184 |
-
- ✅ `chat/route_optimizer.py` - Works transparently
|
| 185 |
-
- ✅ `chat/weather.py` - No changes needed
|
| 186 |
-
- ✅ `server.py` - No changes needed
|
| 187 |
-
- ✅ `requirements.txt` - No new dependencies (uses existing `requests`)
|
| 188 |
-
|
| 189 |
-
## Usage Examples
|
| 190 |
-
|
| 191 |
-
### Basic Route Calculation
|
| 192 |
-
|
| 193 |
-
```python
|
| 194 |
-
from chat.tools import handle_calculate_route
|
| 195 |
-
|
| 196 |
-
result = handle_calculate_route({
|
| 197 |
-
"origin": "Ahsanullah University, Dhaka",
|
| 198 |
-
"destination": "Tejgaon College, Dhaka",
|
| 199 |
-
"vehicle_type": "car"
|
| 200 |
-
})
|
| 201 |
-
|
| 202 |
-
print(f"Distance: {result['distance']['text']}")
|
| 203 |
-
print(f"Duration: {result['duration_in_traffic']['text']}")
|
| 204 |
-
print(f"Confidence: {result['confidence']}")
|
| 205 |
-
# Output:
|
| 206 |
-
# Distance: 5.0 km
|
| 207 |
-
# Duration: 13 mins
|
| 208 |
-
# Confidence: high (Routes API with real-time traffic)
|
| 209 |
-
```
|
| 210 |
-
|
| 211 |
-
### With Alternative Routes
|
| 212 |
-
|
| 213 |
-
```python
|
| 214 |
-
result = handle_calculate_route({
|
| 215 |
-
"origin": "Start Address",
|
| 216 |
-
"destination": "End Address",
|
| 217 |
-
"alternatives": True
|
| 218 |
-
})
|
| 219 |
-
|
| 220 |
-
for i, alt in enumerate(result.get('alternatives', []), 1):
|
| 221 |
-
print(f"Route {i}: {alt['duration']} via {alt['route_summary']}")
|
| 222 |
-
```
|
| 223 |
-
|
| 224 |
-
### With Turn-by-Turn Directions
|
| 225 |
-
|
| 226 |
-
```python
|
| 227 |
-
result = handle_calculate_route({
|
| 228 |
-
"origin": "Start",
|
| 229 |
-
"destination": "End",
|
| 230 |
-
"include_steps": True
|
| 231 |
-
})
|
| 232 |
-
|
| 233 |
-
for step in result.get('steps', []):
|
| 234 |
-
print(f"- {step['instruction']} ({step['distance']}m)")
|
| 235 |
-
```
|
| 236 |
-
|
| 237 |
-
## Verification Logs
|
| 238 |
-
|
| 239 |
-
From successful test run:
|
| 240 |
-
|
| 241 |
-
```
|
| 242 |
-
INFO:chat.tools:Attempting Routes API (recommended)
|
| 243 |
-
INFO:chat.tools:Calling Routes API: Ahsanullah University... → Tejgaon College... (mode: DRIVE)
|
| 244 |
-
INFO:chat.tools:Routes API: 5.0 km, 13 mins
|
| 245 |
-
```
|
| 246 |
-
|
| 247 |
-
This confirms:
|
| 248 |
-
- ✅ Routes API is being called
|
| 249 |
-
- ✅ Request is successful
|
| 250 |
-
- ✅ Real-time traffic data is returned
|
| 251 |
-
- ✅ Results are accurate
|
| 252 |
-
|
| 253 |
-
## Next Steps & Recommendations
|
| 254 |
-
|
| 255 |
-
### Immediate
|
| 256 |
-
1. ✅ **COMPLETE** - Routes API is fully integrated and working
|
| 257 |
-
2. ✅ **COMPLETE** - Tested with real Dhaka route
|
| 258 |
-
3. ✅ **COMPLETE** - Fallback logic implemented
|
| 259 |
-
|
| 260 |
-
### Optional Enhancements (Future)
|
| 261 |
-
|
| 262 |
-
1. **Add More City Profiles**
|
| 263 |
-
- Could add Mumbai, Jakarta, Manila (other highly congested cities)
|
| 264 |
-
- Routes API handles this automatically with real-time data
|
| 265 |
-
|
| 266 |
-
2. **Cache Recent Routes**
|
| 267 |
-
- Cache responses for 5-10 minutes to reduce API calls
|
| 268 |
-
- Good for repeated queries to same route
|
| 269 |
-
|
| 270 |
-
3. **Add Toll Information**
|
| 271 |
-
- Routes API supports toll data
|
| 272 |
-
- Add `tolls.tollPasses` to field mask
|
| 273 |
-
|
| 274 |
-
4. **Add Eco-Friendly Routes**
|
| 275 |
-
- Routes API has `routingPreference: FUEL_EFFICIENT`
|
| 276 |
-
- Could offer as alternative route option
|
| 277 |
-
|
| 278 |
-
5. **Monitor API Usage**
|
| 279 |
-
- Log API calls for billing tracking
|
| 280 |
-
- Alert if approaching quota limits
|
| 281 |
-
|
| 282 |
-
## Conclusion
|
| 283 |
-
|
| 284 |
-
The Google Routes API integration is **complete and working perfectly**!
|
| 285 |
-
|
| 286 |
-
**Before:**
|
| 287 |
-
- Mock algorithm estimated 57 minutes for 2.5 km (2x too slow)
|
| 288 |
-
- No real traffic data
|
| 289 |
-
- Unrealistic urban traffic modeling
|
| 290 |
-
|
| 291 |
-
**After:**
|
| 292 |
-
- Routes API provides real-time data: 13 minutes for actual 5 km route
|
| 293 |
-
- Real road distances and traffic conditions
|
| 294 |
-
- 4.4x more accurate estimates
|
| 295 |
-
- Alternative routes available
|
| 296 |
-
- Turn-by-turn directions available
|
| 297 |
-
|
| 298 |
-
The system now provides **production-ready, accurate routing** for FleetMind dispatch operations using real Google Maps data with intelligent fallback handling.
|
| 299 |
-
|
| 300 |
-
---
|
| 301 |
-
|
| 302 |
-
**Implementation Date:** 2025-11-15
|
| 303 |
-
**Status:** ✅ Complete and Tested
|
| 304 |
-
**API:** Google Routes API v2
|
| 305 |
-
**Backward Compatible:** Yes (triple fallback to Directions API and Mock)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEAM_COLLABORATION_GUIDE.md
DELETED
|
@@ -1,305 +0,0 @@
|
|
| 1 |
-
# Team Collaboration Guide - MCP 1st Birthday Hackathon
|
| 2 |
-
|
| 3 |
-
## Overview
|
| 4 |
-
|
| 5 |
-
This guide explains how to add team partners to your FleetMind MCP hackathon submission on Hugging Face Spaces.
|
| 6 |
-
|
| 7 |
-
**Hackathon Details:**
|
| 8 |
-
- **Team Size:** 2-5 members allowed
|
| 9 |
-
- **Your Space:** https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai
|
| 10 |
-
- **Submission Deadline:** November 30, 2025 (11:59 PM UTC)
|
| 11 |
-
- **Track:** MCP in Action (Track 01)
|
| 12 |
-
|
| 13 |
-
---
|
| 14 |
-
|
| 15 |
-
## Method 1: Add Team Members via README Documentation (EASIEST)
|
| 16 |
-
|
| 17 |
-
For hackathon submission purposes, you MUST document your team in the Space's README.md file.
|
| 18 |
-
|
| 19 |
-
### Steps:
|
| 20 |
-
|
| 21 |
-
1. **Edit the Team Section in README.md**
|
| 22 |
-
|
| 23 |
-
The Team section is already added to your README. Update it with your actual team information:
|
| 24 |
-
|
| 25 |
-
```markdown
|
| 26 |
-
## 👥 Team
|
| 27 |
-
|
| 28 |
-
**Team Name:** FleetMind AI Team
|
| 29 |
-
|
| 30 |
-
**Team Members:**
|
| 31 |
-
- **John Doe** - [@johndoe](https://huggingface.co/johndoe) - Lead Developer & AI Architect
|
| 32 |
-
- **Jane Smith** - [@janesmith](https://huggingface.co/janesmith) - Database Engineer
|
| 33 |
-
- **Alex Chen** - [@alexchen](https://huggingface.co/alexchen) - UI/UX Developer
|
| 34 |
-
```
|
| 35 |
-
|
| 36 |
-
2. **Replace Placeholders:**
|
| 37 |
-
- `[Your Team Name]` → Your actual team name
|
| 38 |
-
- `[Your Name]` → Team member's real name
|
| 39 |
-
- `@your-hf-username` → Their Hugging Face username
|
| 40 |
-
- `[Role]` → Their role in the project
|
| 41 |
-
|
| 42 |
-
3. **Commit and Push:**
|
| 43 |
-
|
| 44 |
-
```bash
|
| 45 |
-
cd F:\fleetmind-mcp\fleetmind-dispatch-ai
|
| 46 |
-
git add README.md
|
| 47 |
-
git commit -m "Update team member information"
|
| 48 |
-
git push
|
| 49 |
-
```
|
| 50 |
-
|
| 51 |
-
---
|
| 52 |
-
|
| 53 |
-
## Method 2: Grant Git Access to Team Partners (TECHNICAL COLLABORATION)
|
| 54 |
-
|
| 55 |
-
If your team partners need to push code directly to the Space, they need Git access.
|
| 56 |
-
|
| 57 |
-
### Option A: Via Organization Membership
|
| 58 |
-
|
| 59 |
-
Since your Space is owned by the **MCP-1st-Birthday organization**, team members can:
|
| 60 |
-
|
| 61 |
-
1. **Join the Organization:**
|
| 62 |
-
- Go to https://huggingface.co/MCP-1st-Birthday
|
| 63 |
-
- Click **"Request to join this org"** (top right)
|
| 64 |
-
- Fill out the registration form
|
| 65 |
-
- Wait for admin approval
|
| 66 |
-
|
| 67 |
-
2. **Verify Access:**
|
| 68 |
-
- Once approved, they'll automatically have access based on organization permissions
|
| 69 |
-
- Organization members with "write" or "contributor" roles can collaborate
|
| 70 |
-
|
| 71 |
-
### Option B: Direct Collaborator Access
|
| 72 |
-
|
| 73 |
-
If you have admin rights to your Space:
|
| 74 |
-
|
| 75 |
-
1. **Go to Space Settings:**
|
| 76 |
-
- Visit: https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai/settings
|
| 77 |
-
- Look for "Collaborators" or "Access" section
|
| 78 |
-
|
| 79 |
-
2. **Add Collaborators by Username:**
|
| 80 |
-
- Enter their Hugging Face username
|
| 81 |
-
- Set their permission level (read/write/admin)
|
| 82 |
-
- Send invitation
|
| 83 |
-
|
| 84 |
-
---
|
| 85 |
-
|
| 86 |
-
## Method 3: Collaborate via Pull Requests (SAFEST)
|
| 87 |
-
|
| 88 |
-
Team members can contribute without direct write access using Pull Requests.
|
| 89 |
-
|
| 90 |
-
### Steps:
|
| 91 |
-
|
| 92 |
-
1. **Team Partner Forks/Duplicates the Space:**
|
| 93 |
-
- They go to your Space page
|
| 94 |
-
- Click the three dots (top right) → "Duplicate this Space"
|
| 95 |
-
- Make changes in their forked version
|
| 96 |
-
|
| 97 |
-
2. **Create Pull Request:**
|
| 98 |
-
- After making changes, they create a Pull Request
|
| 99 |
-
- You review and merge their changes
|
| 100 |
-
|
| 101 |
-
3. **Enable Pull Requests:**
|
| 102 |
-
- Go to Space Settings
|
| 103 |
-
- Ensure "Pull Requests" are enabled
|
| 104 |
-
|
| 105 |
-
---
|
| 106 |
-
|
| 107 |
-
## Method 4: Share Git Credentials (NOT RECOMMENDED)
|
| 108 |
-
|
| 109 |
-
While technically possible, sharing your Git credentials is NOT recommended for security reasons. Use Methods 1-3 instead.
|
| 110 |
-
|
| 111 |
-
---
|
| 112 |
-
|
| 113 |
-
## Technical Setup for Team Partners
|
| 114 |
-
|
| 115 |
-
Once your team partners have access, they need to set up their local environment:
|
| 116 |
-
|
| 117 |
-
### 1. Clone the Space
|
| 118 |
-
|
| 119 |
-
```bash
|
| 120 |
-
# Navigate to desired directory
|
| 121 |
-
cd F:\
|
| 122 |
-
|
| 123 |
-
# Clone the Space
|
| 124 |
-
git clone https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai
|
| 125 |
-
|
| 126 |
-
# Enter directory
|
| 127 |
-
cd fleetmind-dispatch-ai
|
| 128 |
-
```
|
| 129 |
-
|
| 130 |
-
### 2. Authenticate with Hugging Face
|
| 131 |
-
|
| 132 |
-
They need a Hugging Face access token:
|
| 133 |
-
|
| 134 |
-
1. **Get Token:**
|
| 135 |
-
- Go to https://huggingface.co/settings/tokens
|
| 136 |
-
- Click "New token"
|
| 137 |
-
- Create a token with "write" permissions
|
| 138 |
-
|
| 139 |
-
2. **Login via CLI:**
|
| 140 |
-
```bash
|
| 141 |
-
# Install Hugging Face CLI
|
| 142 |
-
pip install huggingface_hub
|
| 143 |
-
|
| 144 |
-
# Login (they'll be prompted for token)
|
| 145 |
-
huggingface-cli login
|
| 146 |
-
```
|
| 147 |
-
|
| 148 |
-
3. **Or Configure Git Credentials:**
|
| 149 |
-
```bash
|
| 150 |
-
git config credential.helper store
|
| 151 |
-
```
|
| 152 |
-
|
| 153 |
-
When they push for the first time, Git will ask for:
|
| 154 |
-
- Username: Their HF username
|
| 155 |
-
- Password: Their HF access token (NOT their account password)
|
| 156 |
-
|
| 157 |
-
### 3. Make Changes and Push
|
| 158 |
-
|
| 159 |
-
```bash
|
| 160 |
-
# Make changes to files
|
| 161 |
-
# ...
|
| 162 |
-
|
| 163 |
-
# Stage changes
|
| 164 |
-
git add .
|
| 165 |
-
|
| 166 |
-
# Commit
|
| 167 |
-
git commit -m "Add feature X"
|
| 168 |
-
|
| 169 |
-
# Push to Space
|
| 170 |
-
git push
|
| 171 |
-
```
|
| 172 |
-
|
| 173 |
-
---
|
| 174 |
-
|
| 175 |
-
## Hugging Face Spaces Access Levels
|
| 176 |
-
|
| 177 |
-
Understanding permission levels helps you decide what access to grant:
|
| 178 |
-
|
| 179 |
-
| Role | Can View | Can Clone | Can Push | Can Manage Settings |
|
| 180 |
-
|------|----------|-----------|----------|---------------------|
|
| 181 |
-
| **Public** | ✅ | ✅ | ❌ | ❌ |
|
| 182 |
-
| **Read** | ✅ | ✅ | ❌ | ❌ |
|
| 183 |
-
| **Contributor** | ✅ | ✅ | Via PR only | ❌ |
|
| 184 |
-
| **Write** | ✅ | ✅ | ✅ | ❌ |
|
| 185 |
-
| **Admin** | ✅ | ✅ | ✅ | ✅ |
|
| 186 |
-
|
| 187 |
-
---
|
| 188 |
-
|
| 189 |
-
## Hackathon Submission Requirements
|
| 190 |
-
|
| 191 |
-
For your team submission to be valid, ensure:
|
| 192 |
-
|
| 193 |
-
### Required in README.md:
|
| 194 |
-
- ✅ **Team section** with all member names and HF usernames
|
| 195 |
-
- ✅ **Track tag:** `mcp-in-action-track-01` (already added)
|
| 196 |
-
- ✅ **Demo video link** (1-5 minutes) - TODO
|
| 197 |
-
- ✅ **Social media post link** - TODO
|
| 198 |
-
|
| 199 |
-
### Required in Space:
|
| 200 |
-
- ✅ Published as a Space in MCP-1st-Birthday organization
|
| 201 |
-
- ✅ App.py entry point (already created)
|
| 202 |
-
- ✅ Working Gradio interface
|
| 203 |
-
- ✅ All code created during hackathon period (Nov 14-30, 2025)
|
| 204 |
-
|
| 205 |
-
---
|
| 206 |
-
|
| 207 |
-
## Troubleshooting
|
| 208 |
-
|
| 209 |
-
### "Permission denied" when team partner tries to push
|
| 210 |
-
|
| 211 |
-
**Solution:**
|
| 212 |
-
1. Verify they're added as collaborators with write access
|
| 213 |
-
2. Check they're using the correct HF access token (not account password)
|
| 214 |
-
3. Ensure token has "write" permissions
|
| 215 |
-
|
| 216 |
-
### "Repository not found" error
|
| 217 |
-
|
| 218 |
-
**Solution:**
|
| 219 |
-
1. Verify the Space URL is correct
|
| 220 |
-
2. Check they have at least "read" access
|
| 221 |
-
3. Ensure they're logged in: `huggingface-cli whoami`
|
| 222 |
-
|
| 223 |
-
### Team member can't see the Space
|
| 224 |
-
|
| 225 |
-
**Solution:**
|
| 226 |
-
1. If Space is private, add them as collaborators
|
| 227 |
-
2. If Space is public (recommended for hackathon), they should see it
|
| 228 |
-
3. Check organization membership status
|
| 229 |
-
|
| 230 |
-
---
|
| 231 |
-
|
| 232 |
-
## Best Practices for Team Collaboration
|
| 233 |
-
|
| 234 |
-
1. **Communication:**
|
| 235 |
-
- Use Discord channel: agents-mcp-hackathon-winter25
|
| 236 |
-
- Create a team group chat
|
| 237 |
-
- Document decisions in README
|
| 238 |
-
|
| 239 |
-
2. **Code Management:**
|
| 240 |
-
- Pull before making changes: `git pull`
|
| 241 |
-
- Commit frequently with clear messages
|
| 242 |
-
- Test locally before pushing
|
| 243 |
-
|
| 244 |
-
3. **Task Distribution:**
|
| 245 |
-
- Assign specific files/features to team members
|
| 246 |
-
- Avoid editing the same files simultaneously
|
| 247 |
-
- Use TODO comments in code
|
| 248 |
-
|
| 249 |
-
4. **Version Control:**
|
| 250 |
-
- Create branches for major features (optional)
|
| 251 |
-
- Use descriptive commit messages
|
| 252 |
-
- Review each other's code
|
| 253 |
-
|
| 254 |
-
---
|
| 255 |
-
|
| 256 |
-
## Quick Reference Commands
|
| 257 |
-
|
| 258 |
-
```bash
|
| 259 |
-
# Clone the Space
|
| 260 |
-
git clone https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai
|
| 261 |
-
|
| 262 |
-
# Check current status
|
| 263 |
-
git status
|
| 264 |
-
|
| 265 |
-
# Pull latest changes
|
| 266 |
-
git pull
|
| 267 |
-
|
| 268 |
-
# Add all changes
|
| 269 |
-
git add .
|
| 270 |
-
|
| 271 |
-
# Commit with message
|
| 272 |
-
git commit -m "Description of changes"
|
| 273 |
-
|
| 274 |
-
# Push to Space
|
| 275 |
-
git push
|
| 276 |
-
|
| 277 |
-
# Check who you're logged in as
|
| 278 |
-
huggingface-cli whoami
|
| 279 |
-
|
| 280 |
-
# Login to HF
|
| 281 |
-
huggingface-cli login
|
| 282 |
-
```
|
| 283 |
-
|
| 284 |
-
---
|
| 285 |
-
|
| 286 |
-
## Support & Resources
|
| 287 |
-
|
| 288 |
-
- **Hackathon Discord:** agents-mcp-hackathon-winter25 channel
|
| 289 |
-
- **Office Hours:** November 17-28 with Gradio team
|
| 290 |
-
- **HF Documentation:** https://huggingface.co/docs/hub/spaces
|
| 291 |
-
- **Git Documentation:** https://git-scm.com/doc
|
| 292 |
-
|
| 293 |
-
---
|
| 294 |
-
|
| 295 |
-
## Timeline Reminder
|
| 296 |
-
|
| 297 |
-
- **Start Date:** November 14, 2025
|
| 298 |
-
- **Submission Deadline:** November 30, 2025 (11:59 PM UTC)
|
| 299 |
-
- **Days Remaining:** Check dashboard regularly
|
| 300 |
-
|
| 301 |
-
Make sure all team members are added to README.md before the deadline!
|
| 302 |
-
|
| 303 |
-
---
|
| 304 |
-
|
| 305 |
-
**Good luck with your hackathon submission! 🚀**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TECHNICAL_COMPARISON.md
DELETED
|
@@ -1,779 +0,0 @@
|
|
| 1 |
-
```
|
| 2 |
-
╔══════════════════════════════════════════════════════════════════════════════╗
|
| 3 |
-
║ ║
|
| 4 |
-
║ "AI will never beat the human brain" ║
|
| 5 |
-
║ ║
|
| 6 |
-
║ Innovation requires human creativity, ║
|
| 7 |
-
║ intuition, and the wisdom of experience. ║
|
| 8 |
-
║ ║
|
| 9 |
-
╚══════════════════════════════════════════════════════════════════════════════╝
|
| 10 |
-
```
|
| 11 |
-
|
| 12 |
-
---
|
| 13 |
-
|
| 14 |
-
# Multi-Tenant Authentication for FastMCP Server
|
| 15 |
-
## Technical Comparison: Middleware vs Proxy Approach
|
| 16 |
-
|
| 17 |
-
**Document Version:** 1.0
|
| 18 |
-
**Date:** November 25, 2024
|
| 19 |
-
**Project:** FleetMind MCP Server
|
| 20 |
-
**Author:** Development Team
|
| 21 |
-
**Status:** Implementation Complete
|
| 22 |
-
|
| 23 |
-
---
|
| 24 |
-
|
| 25 |
-
## Executive Summary
|
| 26 |
-
|
| 27 |
-
This document compares two approaches for implementing multi-tenant API key authentication in a FastMCP-based Model Context Protocol (MCP) server deployed on HuggingFace Spaces. The initial middleware-based approach faced fundamental compatibility issues with FastMCP 2.13.1, leading to the development of a successful HTTP proxy solution.
|
| 28 |
-
|
| 29 |
-
**Key Finding:** The proxy-based architecture successfully enables multi-tenant authentication by intercepting and augmenting HTTP requests before they reach the FastMCP server, without requiring framework modifications.
|
| 30 |
-
|
| 31 |
-
---
|
| 32 |
-
|
| 33 |
-
## 1. Problem Statement
|
| 34 |
-
|
| 35 |
-
### Business Requirement
|
| 36 |
-
Enable multiple users to connect to a shared FleetMind MCP server deployment, each authenticated with their own API key for data isolation and access control.
|
| 37 |
-
|
| 38 |
-
### Technical Challenge
|
| 39 |
-
The Model Context Protocol (MCP) uses Server-Sent Events (SSE) for initial connection establishment, where the client provides an API key in the connection URL:
|
| 40 |
-
|
| 41 |
-
```
|
| 42 |
-
GET /sse?api_key=fm_USER_KEY_HERE
|
| 43 |
-
```
|
| 44 |
-
|
| 45 |
-
However, subsequent tool execution requests arrive without the API key:
|
| 46 |
-
|
| 47 |
-
```
|
| 48 |
-
POST /messages/?session_id=abc123
|
| 49 |
-
```
|
| 50 |
-
|
| 51 |
-
**Challenge:** How to link the initial API key to the ongoing session for authentication of tool calls?
|
| 52 |
-
|
| 53 |
-
### Constraints
|
| 54 |
-
1. FastMCP framework (v2.13.1) in use
|
| 55 |
-
2. Deployment target: HuggingFace Spaces (Docker)
|
| 56 |
-
3. Must support concurrent users with different API keys
|
| 57 |
-
4. Minimal code changes preferred
|
| 58 |
-
5. Production-ready reliability required
|
| 59 |
-
|
| 60 |
-
---
|
| 61 |
-
|
| 62 |
-
## 2. Approach 1: Middleware-Based Solution (FAILED)
|
| 63 |
-
|
| 64 |
-
### 2.1 Concept
|
| 65 |
-
|
| 66 |
-
Implement a Starlette middleware that intercepts HTTP requests inside the FastMCP application to capture and inject API keys.
|
| 67 |
-
|
| 68 |
-
### 2.2 Architecture
|
| 69 |
-
|
| 70 |
-
```
|
| 71 |
-
┌─────────────────────────────────────────────┐
|
| 72 |
-
│ Claude Desktop Client │
|
| 73 |
-
└────────────────┬────────────────────────────┘
|
| 74 |
-
│
|
| 75 |
-
│ 1. GET /sse?api_key=xxx
|
| 76 |
-
▼
|
| 77 |
-
┌─────────────────────────────────────────────┐
|
| 78 |
-
│ FastMCP Server (Port 7860) │
|
| 79 |
-
│ ┌───────────────────────────────────────┐ │
|
| 80 |
-
│ │ ApiKeyCaptureMiddleware (TRIED) │ │
|
| 81 |
-
│ │ - Capture api_key from /sse │ │
|
| 82 |
-
│ │ - Store in _session_auth_store │ │
|
| 83 |
-
│ │ - Link to session_id │ │
|
| 84 |
-
│ └───────────────────────────────────────┘ │
|
| 85 |
-
│ ┌───────────────────────────────────────┐ │
|
| 86 |
-
│ │ FastMCP Request Handler │ │
|
| 87 |
-
│ └───────────────────────────────────────┘ │
|
| 88 |
-
└─────────────────────────────────────────────┘
|
| 89 |
-
```
|
| 90 |
-
|
| 91 |
-
### 2.3 Implementation Attempt
|
| 92 |
-
|
| 93 |
-
```python
|
| 94 |
-
from starlette.middleware.base import BaseHTTPMiddleware
|
| 95 |
-
|
| 96 |
-
class ApiKeyCaptureMiddleware(BaseHTTPMiddleware):
|
| 97 |
-
"""Captures API key from SSE and links it to session_id"""
|
| 98 |
-
|
| 99 |
-
async def dispatch(self, request: Request, call_next):
|
| 100 |
-
api_key = request.query_params.get('api_key')
|
| 101 |
-
session_id = request.query_params.get('session_id')
|
| 102 |
-
path = request.url.path
|
| 103 |
-
|
| 104 |
-
# SSE connection with api_key - store for later linking
|
| 105 |
-
if api_key and path == '/sse':
|
| 106 |
-
_session_auth_store['_pending_api_key'] = api_key
|
| 107 |
-
logger.info(f"Captured API key: {api_key[:15]}...")
|
| 108 |
-
|
| 109 |
-
# Messages request - link session to api_key
|
| 110 |
-
if session_id and path.startswith('/messages'):
|
| 111 |
-
if '_pending_api_key' in _session_auth_store:
|
| 112 |
-
pending = _session_auth_store.pop('_pending_api_key')
|
| 113 |
-
store_session_api_key(session_id, pending)
|
| 114 |
-
|
| 115 |
-
return await call_next(request)
|
| 116 |
-
|
| 117 |
-
# Attempted integration
|
| 118 |
-
custom_middleware = [Middleware(ApiKeyCaptureMiddleware)]
|
| 119 |
-
sse_app = mcp.sse_app(custom_middleware=custom_middleware)
|
| 120 |
-
```
|
| 121 |
-
|
| 122 |
-
### 2.4 Critical Failure Points
|
| 123 |
-
|
| 124 |
-
#### Issue 1: Framework Incompatibility
|
| 125 |
-
```python
|
| 126 |
-
TypeError: FastMCP.sse_app() got an unexpected keyword argument 'custom_middleware'
|
| 127 |
-
```
|
| 128 |
-
|
| 129 |
-
**Root Cause:** FastMCP 2.13.1 does not expose middleware injection points in its public API. The `sse_app()` method does not accept middleware parameters.
|
| 130 |
-
|
| 131 |
-
#### Issue 2: ASGI App Access
|
| 132 |
-
Attempted workarounds:
|
| 133 |
-
- `mcp._sse_app` - Private attribute, not guaranteed stable
|
| 134 |
-
- `mcp.get_asgi_app()` - Method does not exist
|
| 135 |
-
- Monkey-patching internal structures - Fragile and unmaintainable
|
| 136 |
-
|
| 137 |
-
#### Issue 3: Request Interception Timing
|
| 138 |
-
Even if middleware could be added, SSE connection handling occurs deep within MCP's protocol layer, potentially before middleware execution.
|
| 139 |
-
|
| 140 |
-
### 2.5 Why It Failed
|
| 141 |
-
|
| 142 |
-
| Aspect | Problem |
|
| 143 |
-
|--------|---------|
|
| 144 |
-
| **Framework Support** | FastMCP doesn't support custom middleware injection |
|
| 145 |
-
| **API Surface** | No documented way to intercept requests at the right layer |
|
| 146 |
-
| **Maintainability** | Would require framework modifications or fragile hacks |
|
| 147 |
-
| **Reliability** | Internal API changes could break the implementation |
|
| 148 |
-
| **Deployment** | Cannot guarantee compatibility across FastMCP versions |
|
| 149 |
-
|
| 150 |
-
### 2.6 Lessons Learned
|
| 151 |
-
|
| 152 |
-
1. **Framework limitations are real constraints** - Working around them leads to technical debt
|
| 153 |
-
2. **Public API matters** - Relying on private/undocumented features is risky
|
| 154 |
-
3. **Layer selection is critical** - Middleware operates at the wrong abstraction level for this use case
|
| 155 |
-
|
| 156 |
-
---
|
| 157 |
-
|
| 158 |
-
## 3. Approach 2: HTTP Proxy Solution (SUCCESS)
|
| 159 |
-
|
| 160 |
-
### 3.1 Concept
|
| 161 |
-
|
| 162 |
-
Deploy a lightweight HTTP proxy in front of FastMCP that intercepts, captures, and augments requests before forwarding them to the MCP server.
|
| 163 |
-
|
| 164 |
-
### 3.2 Architecture
|
| 165 |
-
|
| 166 |
-
```
|
| 167 |
-
┌──────────────────────────────────────────────────────────────┐
|
| 168 |
-
│ Claude Desktop Client │
|
| 169 |
-
└────────────────────────┬─────────────────────────────────────┘
|
| 170 |
-
│
|
| 171 |
-
│ 1. GET /sse?api_key=fm_USER_KEY
|
| 172 |
-
▼
|
| 173 |
-
┌──────────────────────────────────────────────────────────────┐
|
| 174 |
-
│ Authentication Proxy (Port 7860) │
|
| 175 |
-
│ ┌────────────────────────────────────────────────────────┐ │
|
| 176 |
-
│ │ Step 1: Capture API Key │ │
|
| 177 |
-
│ │ - Extract api_key from /sse URL │ │
|
| 178 |
-
│ │ - Store: {'_pending_api_key': 'fm_USER_KEY'} │ │
|
| 179 |
-
│ └────────────────────────────────────────────────────────┘ │
|
| 180 |
-
│ ┌────────────────────────────────────────────────────────┐ │
|
| 181 |
-
│ │ Step 2: Link Session │ │
|
| 182 |
-
│ │ - On /messages/?session_id=abc123 │ │
|
| 183 |
-
│ │ - Link: {'abc123': 'fm_USER_KEY'} │ │
|
| 184 |
-
│ └────────────────────────────────────────────────────────┘ │
|
| 185 |
-
│ ┌────────────────────────────────────────────────────────┐ │
|
| 186 |
-
│ │ Step 3: Inject API Key │ │
|
| 187 |
-
│ │ - Add api_key to query params │ │
|
| 188 |
-
│ │ - Forward: /messages/?session_id=abc&api_key=fm_xxx │ │
|
| 189 |
-
│ └────────────────────────────────────────────────────────┘ ��
|
| 190 |
-
└────────────────────────┬─────────────────────────────────────┘
|
| 191 |
-
│
|
| 192 |
-
│ 2. Forward with API key injected
|
| 193 |
-
▼
|
| 194 |
-
┌──────────────────────────────────────────────────────────────┐
|
| 195 |
-
│ FastMCP Server (Port 7861 - Internal) │
|
| 196 |
-
│ ┌────────────────────────────────────────────────────────┐ │
|
| 197 |
-
│ │ Standard FastMCP Request Handler │ │
|
| 198 |
-
│ │ - Receives requests WITH api_key parameter │ │
|
| 199 |
-
│ │ - Authenticates using provided API key │ │
|
| 200 |
-
│ │ - Executes tool with proper user context │ │
|
| 201 |
-
│ └────────────────────────────────────────────────────────┘ │
|
| 202 |
-
└──────────────────────────────────────────────────────────────┘
|
| 203 |
-
```
|
| 204 |
-
|
| 205 |
-
### 3.3 Implementation
|
| 206 |
-
|
| 207 |
-
#### 3.3.1 Proxy Server (proxy.py)
|
| 208 |
-
|
| 209 |
-
```python
|
| 210 |
-
import asyncio
|
| 211 |
-
from aiohttp import web, ClientSession, ClientTimeout
|
| 212 |
-
from urllib.parse import urlencode
|
| 213 |
-
|
| 214 |
-
# Configuration
|
| 215 |
-
PROXY_PORT = int(os.getenv("PORT", 7860)) # Public port
|
| 216 |
-
FASTMCP_PORT = 7861 # Internal FastMCP port
|
| 217 |
-
FASTMCP_HOST = "localhost"
|
| 218 |
-
|
| 219 |
-
# Session storage: session_id -> api_key
|
| 220 |
-
session_api_keys = {}
|
| 221 |
-
|
| 222 |
-
async def proxy_handler(request):
|
| 223 |
-
"""Main proxy handler - captures API keys and forwards requests"""
|
| 224 |
-
path = request.path
|
| 225 |
-
query_params = dict(request.query)
|
| 226 |
-
|
| 227 |
-
api_key = query_params.get('api_key')
|
| 228 |
-
session_id = query_params.get('session_id')
|
| 229 |
-
|
| 230 |
-
# STEP 1: Capture API key from initial SSE connection
|
| 231 |
-
if api_key and path == '/sse':
|
| 232 |
-
logger.info(f"[AUTH] Captured API key: {api_key[:20]}...")
|
| 233 |
-
session_api_keys['_pending_api_key'] = api_key
|
| 234 |
-
|
| 235 |
-
# STEP 2: Link session_id to API key
|
| 236 |
-
if session_id and path.startswith('/messages'):
|
| 237 |
-
if session_id not in session_api_keys:
|
| 238 |
-
if '_pending_api_key' in session_api_keys:
|
| 239 |
-
api_key_to_store = session_api_keys['_pending_api_key']
|
| 240 |
-
session_api_keys[session_id] = api_key_to_store
|
| 241 |
-
logger.info(f"[AUTH] Linked session {session_id[:12]}...")
|
| 242 |
-
|
| 243 |
-
# STEP 3: Inject API key into request
|
| 244 |
-
stored_api_key = session_api_keys.get(session_id)
|
| 245 |
-
if stored_api_key:
|
| 246 |
-
query_params['api_key'] = stored_api_key
|
| 247 |
-
|
| 248 |
-
# Build target URL and forward
|
| 249 |
-
query_string = urlencode(query_params) if query_params else ""
|
| 250 |
-
target_url = f"http://{FASTMCP_HOST}:{FASTMCP_PORT}{path}"
|
| 251 |
-
if query_string:
|
| 252 |
-
target_url += f"?{query_string}"
|
| 253 |
-
|
| 254 |
-
# Forward to FastMCP with proper SSE streaming support
|
| 255 |
-
async with ClientSession(timeout=ClientTimeout(total=300)) as session:
|
| 256 |
-
if request.method == 'GET':
|
| 257 |
-
async with session.get(target_url, headers=headers) as resp:
|
| 258 |
-
if 'text/event-stream' in resp.content_type:
|
| 259 |
-
# Stream SSE responses
|
| 260 |
-
response = web.StreamResponse(
|
| 261 |
-
status=resp.status,
|
| 262 |
-
headers=dict(resp.headers)
|
| 263 |
-
)
|
| 264 |
-
await response.prepare(request)
|
| 265 |
-
async for chunk in resp.content.iter_any():
|
| 266 |
-
await response.write(chunk)
|
| 267 |
-
await response.write_eof()
|
| 268 |
-
return response
|
| 269 |
-
else:
|
| 270 |
-
# Regular HTTP responses
|
| 271 |
-
body = await resp.read()
|
| 272 |
-
return web.Response(body=body, status=resp.status)
|
| 273 |
-
# ... POST and other methods handled similarly
|
| 274 |
-
```
|
| 275 |
-
|
| 276 |
-
#### 3.3.2 Unified Launcher (start_with_proxy.py)
|
| 277 |
-
|
| 278 |
-
```python
|
| 279 |
-
import subprocess
|
| 280 |
-
import signal
|
| 281 |
-
|
| 282 |
-
def main():
|
| 283 |
-
# Start FastMCP on internal port 7861
|
| 284 |
-
fastmcp_process = subprocess.Popen([python_exe, "app.py"])
|
| 285 |
-
time.sleep(3) # Allow FastMCP to initialize
|
| 286 |
-
|
| 287 |
-
# Start proxy on public port 7860
|
| 288 |
-
proxy_process = subprocess.Popen([python_exe, "proxy.py"])
|
| 289 |
-
|
| 290 |
-
# Monitor both processes
|
| 291 |
-
while True:
|
| 292 |
-
if fastmcp_process.poll() is not None:
|
| 293 |
-
logger.error("FastMCP stopped unexpectedly")
|
| 294 |
-
proxy_process.terminate()
|
| 295 |
-
sys.exit(1)
|
| 296 |
-
if proxy_process.poll() is not None:
|
| 297 |
-
logger.error("Proxy stopped unexpectedly")
|
| 298 |
-
fastmcp_process.terminate()
|
| 299 |
-
sys.exit(1)
|
| 300 |
-
time.sleep(1)
|
| 301 |
-
```
|
| 302 |
-
|
| 303 |
-
#### 3.3.3 FastMCP Server (app.py)
|
| 304 |
-
|
| 305 |
-
```python
|
| 306 |
-
# Simplified - no middleware needed
|
| 307 |
-
HF_SPACE_PORT = int(os.getenv("PORT", 7861)) # Internal port
|
| 308 |
-
HF_SPACE_HOST = os.getenv("HOST", "0.0.0.0")
|
| 309 |
-
|
| 310 |
-
logger.info("[Auth] Using proxy-based authentication")
|
| 311 |
-
|
| 312 |
-
# Standard FastMCP execution
|
| 313 |
-
mcp.run(
|
| 314 |
-
transport="sse",
|
| 315 |
-
host=HF_SPACE_HOST,
|
| 316 |
-
port=HF_SPACE_PORT
|
| 317 |
-
)
|
| 318 |
-
```
|
| 319 |
-
|
| 320 |
-
### 3.4 Session Isolation Example
|
| 321 |
-
|
| 322 |
-
```python
|
| 323 |
-
# Three users connect simultaneously:
|
| 324 |
-
|
| 325 |
-
# User A
|
| 326 |
-
GET /sse?api_key=fm_AAA
|
| 327 |
-
→ session_api_keys = {'_pending_api_key': 'fm_AAA'}
|
| 328 |
-
|
| 329 |
-
POST /messages/?session_id=session_123
|
| 330 |
-
→ session_api_keys = {'session_123': 'fm_AAA', '_pending_api_key': 'fm_AAA'}
|
| 331 |
-
→ Forwarded: /messages/?session_id=session_123&api_key=fm_AAA
|
| 332 |
-
|
| 333 |
-
# User B (while User A active)
|
| 334 |
-
GET /sse?api_key=fm_BBB
|
| 335 |
-
→ session_api_keys = {'session_123': 'fm_AAA', '_pending_api_key': 'fm_BBB'}
|
| 336 |
-
|
| 337 |
-
POST /messages/?session_id=session_456
|
| 338 |
-
→ session_api_keys = {'session_123': 'fm_AAA', 'session_456': 'fm_BBB'}
|
| 339 |
-
→ Forwarded: /messages/?session_id=session_456&api_key=fm_BBB
|
| 340 |
-
|
| 341 |
-
# User C (both A and B still active)
|
| 342 |
-
GET /sse?api_key=fm_CCC
|
| 343 |
-
→ session_api_keys = {'session_123': 'fm_AAA', 'session_456': 'fm_BBB', '_pending_api_key': 'fm_CCC'}
|
| 344 |
-
|
| 345 |
-
POST /messages/?session_id=session_789
|
| 346 |
-
→ session_api_keys = {'session_123': 'fm_AAA', 'session_456': 'fm_BBB', 'session_789': 'fm_CCC'}
|
| 347 |
-
→ Forwarded: /messages/?session_id=session_789&api_key=fm_CCC
|
| 348 |
-
|
| 349 |
-
# Each user's requests are properly isolated:
|
| 350 |
-
User A → session_123 → fm_AAA
|
| 351 |
-
User B → session_456 → fm_BBB
|
| 352 |
-
User C → session_789 → fm_CCC
|
| 353 |
-
```
|
| 354 |
-
|
| 355 |
-
### 3.5 Why It Succeeds
|
| 356 |
-
|
| 357 |
-
| Aspect | Solution |
|
| 358 |
-
|--------|----------|
|
| 359 |
-
| **Framework Independence** | Proxy operates outside FastMCP - no framework modifications needed |
|
| 360 |
-
| **Clean Separation** | Authentication layer completely decoupled from application logic |
|
| 361 |
-
| **Request Control** | Full access to HTTP layer before FastMCP sees requests |
|
| 362 |
-
| **SSE Compatibility** | Proper streaming support for Server-Sent Events |
|
| 363 |
-
| **Maintainability** | Standard HTTP proxy pattern - well-understood and debuggable |
|
| 364 |
-
| **Scalability** | Can be enhanced with Redis for distributed session storage |
|
| 365 |
-
| **Deployment** | Single entry point works locally and on HuggingFace |
|
| 366 |
-
|
| 367 |
-
---
|
| 368 |
-
|
| 369 |
-
## 4. Technical Comparison
|
| 370 |
-
|
| 371 |
-
### 4.1 Architecture Comparison
|
| 372 |
-
|
| 373 |
-
| Dimension | Middleware Approach | Proxy Approach |
|
| 374 |
-
|-----------|---------------------|----------------|
|
| 375 |
-
| **Integration Point** | Inside FastMCP application | Before FastMCP application |
|
| 376 |
-
| **Coupling** | Tightly coupled to FastMCP internals | Loosely coupled via HTTP |
|
| 377 |
-
| **Framework Dependency** | Requires FastMCP middleware support | Framework-agnostic |
|
| 378 |
-
| **Code Location** | Mixed with application code | Separate component |
|
| 379 |
-
| **Testing** | Requires FastMCP test environment | Can test independently |
|
| 380 |
-
| **Deployment Complexity** | Single process | Two processes (managed) |
|
| 381 |
-
|
| 382 |
-
### 4.2 Operational Comparison
|
| 383 |
-
|
| 384 |
-
| Aspect | Middleware | Proxy |
|
| 385 |
-
|--------|-----------|-------|
|
| 386 |
-
| **Lines of Code** | ~50 (but doesn't work) | ~200 (working solution) |
|
| 387 |
-
| **Dependencies** | Starlette middleware APIs | aiohttp (standard library) |
|
| 388 |
-
| **Memory Overhead** | None (same process) | ~20-30 MB (separate process) |
|
| 389 |
-
| **Latency** | 0ms (in-process) | <1ms (localhost HTTP) |
|
| 390 |
-
| **Debugging** | Difficult (framework internals) | Easy (standard HTTP logs) |
|
| 391 |
-
| **Monitoring** | Limited visibility | Full HTTP request/response logs |
|
| 392 |
-
| **Failure Isolation** | Affects entire application | Proxy failure = graceful degradation |
|
| 393 |
-
|
| 394 |
-
### 4.3 Security Comparison
|
| 395 |
-
|
| 396 |
-
| Security Aspect | Middleware | Proxy |
|
| 397 |
-
|-----------------|-----------|-------|
|
| 398 |
-
| **API Key Exposure** | In-memory within FastMCP | In-memory in separate process |
|
| 399 |
-
| **Session Storage** | Shared application memory | Isolated proxy memory |
|
| 400 |
-
| **Request Logging** | Mixed with application logs | Dedicated authentication logs |
|
| 401 |
-
| **Attack Surface** | Same as application | Additional HTTP endpoint (7860) |
|
| 402 |
-
| **Rate Limiting** | N/A (not implemented) | Can add per-session rate limiting |
|
| 403 |
-
| **Audit Trail** | Limited | Complete request/response audit |
|
| 404 |
-
|
| 405 |
-
### 4.4 Scalability Comparison
|
| 406 |
-
|
| 407 |
-
| Scenario | Middleware | Proxy |
|
| 408 |
-
|----------|-----------|-------|
|
| 409 |
-
| **10 concurrent users** | N/A (doesn't work) | Excellent |
|
| 410 |
-
| **100 concurrent users** | N/A | Good (in-memory storage) |
|
| 411 |
-
| **1000+ concurrent users** | N/A | Requires Redis for session store |
|
| 412 |
-
| **Multi-instance deployment** | N/A | Requires distributed session store |
|
| 413 |
-
| **Horizontal scaling** | N/A | Possible with Redis backend |
|
| 414 |
-
|
| 415 |
-
---
|
| 416 |
-
|
| 417 |
-
## 5. Implementation Details
|
| 418 |
-
|
| 419 |
-
### 5.1 Key Technical Decisions
|
| 420 |
-
|
| 421 |
-
#### 5.1.1 Port Configuration
|
| 422 |
-
- **Public Port (7860)**: Proxy listens here (HuggingFace standard)
|
| 423 |
-
- **Internal Port (7861)**: FastMCP listens here (localhost only)
|
| 424 |
-
- **Rationale**: Clear separation of external and internal services
|
| 425 |
-
|
| 426 |
-
#### 5.1.2 Session Storage
|
| 427 |
-
- **Current**: In-memory Python dictionary
|
| 428 |
-
- **Production Enhancement**: Redis for distributed sessions
|
| 429 |
-
- **Trade-off**: Simplicity vs. scalability
|
| 430 |
-
|
| 431 |
-
#### 5.1.3 SSE Streaming
|
| 432 |
-
- **Challenge**: Proxy must not buffer SSE responses
|
| 433 |
-
- **Solution**: `web.StreamResponse` with `iter_any()` for chunk forwarding
|
| 434 |
-
- **Result**: Real-time event delivery maintained
|
| 435 |
-
|
| 436 |
-
#### 5.1.4 Error Handling
|
| 437 |
-
```python
|
| 438 |
-
try:
|
| 439 |
-
# Proxy request forwarding
|
| 440 |
-
async with session.get(target_url) as resp:
|
| 441 |
-
return await forward_response(resp)
|
| 442 |
-
except Exception as e:
|
| 443 |
-
logger.error(f"[ERROR] Proxy error: {e}")
|
| 444 |
-
return web.Response(text=f"Proxy error: {str(e)}", status=502)
|
| 445 |
-
```
|
| 446 |
-
|
| 447 |
-
### 5.2 Deployment Configuration
|
| 448 |
-
|
| 449 |
-
#### 5.2.1 Local Development
|
| 450 |
-
```bash
|
| 451 |
-
python start_with_proxy.py
|
| 452 |
-
```
|
| 453 |
-
|
| 454 |
-
#### 5.2.2 HuggingFace Spaces
|
| 455 |
-
```dockerfile
|
| 456 |
-
# Dockerfile
|
| 457 |
-
FROM python:3.11-slim
|
| 458 |
-
COPY requirements.txt .
|
| 459 |
-
RUN pip install -r requirements.txt
|
| 460 |
-
COPY . /app
|
| 461 |
-
EXPOSE 7860
|
| 462 |
-
CMD ["python", "start_with_proxy.py"]
|
| 463 |
-
```
|
| 464 |
-
|
| 465 |
-
#### 5.2.3 Environment Variables
|
| 466 |
-
```bash
|
| 467 |
-
# Proxy reads from HuggingFace
|
| 468 |
-
PORT=7860 # Automatically set by HuggingFace
|
| 469 |
-
|
| 470 |
-
# FastMCP configuration
|
| 471 |
-
DATABASE_URL=postgresql://...
|
| 472 |
-
GOOGLE_MAPS_API_KEY=...
|
| 473 |
-
```
|
| 474 |
-
|
| 475 |
-
---
|
| 476 |
-
|
| 477 |
-
## 6. Benefits & Trade-offs
|
| 478 |
-
|
| 479 |
-
### 6.1 Advantages of Proxy Approach
|
| 480 |
-
|
| 481 |
-
1. **Framework Independence**
|
| 482 |
-
- Works with any FastMCP version
|
| 483 |
-
- No dependency on internal APIs
|
| 484 |
-
- Future-proof against framework changes
|
| 485 |
-
|
| 486 |
-
2. **Clear Separation of Concerns**
|
| 487 |
-
- Authentication logic isolated from business logic
|
| 488 |
-
- Easier to test, debug, and maintain
|
| 489 |
-
- Can be reused with other MCP servers
|
| 490 |
-
|
| 491 |
-
3. **Enhanced Observability**
|
| 492 |
-
- Dedicated authentication logs
|
| 493 |
-
- HTTP-level monitoring
|
| 494 |
-
- Clear audit trail for security compliance
|
| 495 |
-
|
| 496 |
-
4. **Deployment Flexibility**
|
| 497 |
-
- Can run proxy and FastMCP on same host
|
| 498 |
-
- Can split across hosts if needed
|
| 499 |
-
- Easy to add load balancing later
|
| 500 |
-
|
| 501 |
-
5. **Extensibility**
|
| 502 |
-
- Easy to add features: rate limiting, request filtering, logging
|
| 503 |
-
- Can enhance with Redis for distributed sessions
|
| 504 |
-
- Can add caching layer if needed
|
| 505 |
-
|
| 506 |
-
### 6.2 Trade-offs
|
| 507 |
-
|
| 508 |
-
1. **Increased Complexity**
|
| 509 |
-
- **Impact**: Two processes to manage instead of one
|
| 510 |
-
- **Mitigation**: `start_with_proxy.py` handles process management
|
| 511 |
-
- **Severity**: Low - standard pattern
|
| 512 |
-
|
| 513 |
-
2. **Additional Latency**
|
| 514 |
-
- **Impact**: Extra localhost HTTP round-trip (~0.5-1ms)
|
| 515 |
-
- **Mitigation**: Negligible for typical MCP operations
|
| 516 |
-
- **Severity**: Very Low - not noticeable to users
|
| 517 |
-
|
| 518 |
-
3. **Memory Overhead**
|
| 519 |
-
- **Impact**: ~20-30 MB for proxy process
|
| 520 |
-
- **Mitigation**: Minimal compared to FastMCP and database
|
| 521 |
-
- **Severity**: Very Low - acceptable for all deployment targets
|
| 522 |
-
|
| 523 |
-
4. **Session Storage Limitations**
|
| 524 |
-
- **Impact**: In-memory storage lost on restart
|
| 525 |
-
- **Mitigation**: Sessions reconnect automatically with new API key
|
| 526 |
-
- **Enhancement**: Can add Redis for persistence if needed
|
| 527 |
-
- **Severity**: Low - acceptable for current scale
|
| 528 |
-
|
| 529 |
-
### 6.3 Risk Assessment
|
| 530 |
-
|
| 531 |
-
| Risk | Probability | Impact | Mitigation |
|
| 532 |
-
|------|-------------|--------|------------|
|
| 533 |
-
| Proxy crash | Low | Medium | Process monitor restarts automatically |
|
| 534 |
-
| FastMCP crash | Low | High | Proxy returns 502, client retries |
|
| 535 |
-
| Session store full | Very Low | Low | Implement TTL-based cleanup |
|
| 536 |
-
| Port conflict | Low | High | Configuration validation on startup |
|
| 537 |
-
| SSE streaming issue | Very Low | High | Extensive testing, fallback handling |
|
| 538 |
-
|
| 539 |
-
---
|
| 540 |
-
|
| 541 |
-
## 7. Performance Analysis
|
| 542 |
-
|
| 543 |
-
### 7.1 Latency Measurements
|
| 544 |
-
|
| 545 |
-
| Operation | Direct (Theoretical) | With Proxy | Overhead |
|
| 546 |
-
|-----------|---------------------|------------|----------|
|
| 547 |
-
| SSE Connection | ~50ms | ~51ms | +1ms |
|
| 548 |
-
| Tool Execution | ~200ms | ~201ms | +1ms |
|
| 549 |
-
| Database Query | ~20ms | ~20ms | 0ms |
|
| 550 |
-
|
| 551 |
-
**Conclusion**: Proxy overhead is negligible (<1% of total request time)
|
| 552 |
-
|
| 553 |
-
### 7.2 Throughput
|
| 554 |
-
|
| 555 |
-
| Concurrent Users | Requests/sec | CPU Usage | Memory |
|
| 556 |
-
|------------------|--------------|-----------|--------|
|
| 557 |
-
| 1 | 100 | 5% | 150 MB |
|
| 558 |
-
| 10 | 800 | 20% | 180 MB |
|
| 559 |
-
| 50 | 2000 | 45% | 280 MB |
|
| 560 |
-
| 100 | 3500 | 75% | 450 MB |
|
| 561 |
-
|
| 562 |
-
**Conclusion**: Handles 100+ concurrent users on standard HuggingFace Space resources
|
| 563 |
-
|
| 564 |
-
### 7.3 Resource Usage
|
| 565 |
-
|
| 566 |
-
```
|
| 567 |
-
Process CPU Memory Threads
|
| 568 |
-
-----------------------------------------
|
| 569 |
-
proxy.py 8% 35 MB 4
|
| 570 |
-
app.py 12% 120 MB 8
|
| 571 |
-
postgresql 15% 200 MB 12
|
| 572 |
-
-----------------------------------------
|
| 573 |
-
Total 35% 355 MB 24
|
| 574 |
-
```
|
| 575 |
-
|
| 576 |
-
**Conclusion**: Well within HuggingFace Spaces resource limits
|
| 577 |
-
|
| 578 |
-
---
|
| 579 |
-
|
| 580 |
-
## 8. Security Considerations
|
| 581 |
-
|
| 582 |
-
### 8.1 API Key Handling
|
| 583 |
-
|
| 584 |
-
1. **Transmission**: API keys transmitted via HTTPS (enforced by HuggingFace)
|
| 585 |
-
2. **Storage**: In-memory only, never persisted to disk by proxy
|
| 586 |
-
3. **Logging**: Only first 20 characters logged for debugging
|
| 587 |
-
4. **Lifetime**: Cleared when session ends or server restarts
|
| 588 |
-
|
| 589 |
-
### 8.2 Session Security
|
| 590 |
-
|
| 591 |
-
1. **Session IDs**: Generated by FastMCP, cryptographically random
|
| 592 |
-
2. **Session Hijacking**: Mitigated by short-lived sessions
|
| 593 |
-
3. **Cross-User Isolation**: Each session mapped to unique API key
|
| 594 |
-
4. **Session Expiry**: Automatic cleanup on disconnect
|
| 595 |
-
|
| 596 |
-
### 8.3 Threat Model
|
| 597 |
-
|
| 598 |
-
| Threat | Risk Level | Mitigation |
|
| 599 |
-
|--------|------------|------------|
|
| 600 |
-
| API key interception | Low | HTTPS encryption |
|
| 601 |
-
| Session hijacking | Low | Random session IDs, short lifetime |
|
| 602 |
-
| Proxy bypass | Medium | FastMCP on localhost only (7861) |
|
| 603 |
-
| DoS attack | Medium | Rate limiting (can be added) |
|
| 604 |
-
| Memory inspection | Low | Serverless environment isolation |
|
| 605 |
-
|
| 606 |
-
---
|
| 607 |
-
|
| 608 |
-
## 9. Future Enhancements
|
| 609 |
-
|
| 610 |
-
### 9.1 Distributed Session Storage (Redis)
|
| 611 |
-
|
| 612 |
-
**Current State**: In-memory dictionary
|
| 613 |
-
**Enhancement**: Redis backend for session persistence
|
| 614 |
-
|
| 615 |
-
```python
|
| 616 |
-
import redis
|
| 617 |
-
r = redis.Redis(host='redis', port=6379)
|
| 618 |
-
|
| 619 |
-
# Store session with 1-hour expiry
|
| 620 |
-
r.setex(f'session:{session_id}', 3600, api_key)
|
| 621 |
-
|
| 622 |
-
# Retrieve session
|
| 623 |
-
api_key = r.get(f'session:{session_id}')
|
| 624 |
-
```
|
| 625 |
-
|
| 626 |
-
**Benefits**:
|
| 627 |
-
- Sessions survive proxy restarts
|
| 628 |
-
- Enables horizontal scaling with multiple proxy instances
|
| 629 |
-
- Automatic expiry with TTL
|
| 630 |
-
|
| 631 |
-
### 9.2 Rate Limiting
|
| 632 |
-
|
| 633 |
-
```python
|
| 634 |
-
from aiohttp_limiter import Limiter
|
| 635 |
-
|
| 636 |
-
limiter = Limiter(store=redis_store)
|
| 637 |
-
|
| 638 |
-
@limiter.limit("100/minute")
|
| 639 |
-
async def proxy_handler(request):
|
| 640 |
-
# Limit to 100 requests per minute per API key
|
| 641 |
-
...
|
| 642 |
-
```
|
| 643 |
-
|
| 644 |
-
### 9.3 Request Caching
|
| 645 |
-
|
| 646 |
-
```python
|
| 647 |
-
# Cache GET requests for tool results
|
| 648 |
-
if request.method == 'GET' and cache_key in cache:
|
| 649 |
-
return cached_response
|
| 650 |
-
```
|
| 651 |
-
|
| 652 |
-
### 9.4 Advanced Monitoring
|
| 653 |
-
|
| 654 |
-
```python
|
| 655 |
-
# Prometheus metrics
|
| 656 |
-
from prometheus_client import Counter, Histogram
|
| 657 |
-
|
| 658 |
-
requests_total = Counter('proxy_requests_total', 'Total requests')
|
| 659 |
-
request_duration = Histogram('proxy_request_duration_seconds', 'Request duration')
|
| 660 |
-
```
|
| 661 |
-
|
| 662 |
-
---
|
| 663 |
-
|
| 664 |
-
## 10. Conclusion
|
| 665 |
-
|
| 666 |
-
### 10.1 Summary
|
| 667 |
-
|
| 668 |
-
The proxy-based authentication solution successfully addresses all requirements for multi-tenant API key authentication in the FleetMind MCP Server:
|
| 669 |
-
|
| 670 |
-
✅ **Functional**: Correctly captures, stores, and injects API keys
|
| 671 |
-
✅ **Reliable**: Stable operation with multiple concurrent users
|
| 672 |
-
✅ **Maintainable**: Clean separation of concerns, standard patterns
|
| 673 |
-
✅ **Scalable**: Handles 100+ concurrent users, extensible with Redis
|
| 674 |
-
✅ **Deployable**: Works on HuggingFace Spaces with zero modifications
|
| 675 |
-
✅ **Secure**: Proper isolation and API key handling
|
| 676 |
-
|
| 677 |
-
### 10.2 Recommendation
|
| 678 |
-
|
| 679 |
-
**Adopt the proxy-based approach** for production deployment.
|
| 680 |
-
|
| 681 |
-
**Rationale**:
|
| 682 |
-
1. Proven working solution (vs. middleware approach that cannot work)
|
| 683 |
-
2. Minimal overhead (~1ms per request)
|
| 684 |
-
3. Standard architectural pattern (reverse proxy)
|
| 685 |
-
4. Future-proof against FastMCP framework changes
|
| 686 |
-
5. Enhanced observability and debugging capabilities
|
| 687 |
-
6. Clear path for scaling (Redis integration)
|
| 688 |
-
|
| 689 |
-
### 10.3 Next Steps
|
| 690 |
-
|
| 691 |
-
1. **Immediate** (Week 1):
|
| 692 |
-
- Deploy proxy solution to production
|
| 693 |
-
- Monitor performance metrics
|
| 694 |
-
- Validate multi-tenant isolation
|
| 695 |
-
|
| 696 |
-
2. **Short-term** (Month 1):
|
| 697 |
-
- Add comprehensive logging
|
| 698 |
-
- Implement session TTL cleanup
|
| 699 |
-
- Set up monitoring alerts
|
| 700 |
-
|
| 701 |
-
3. **Medium-term** (Quarter 1):
|
| 702 |
-
- Integrate Redis for session persistence
|
| 703 |
-
- Add rate limiting per API key
|
| 704 |
-
- Implement request caching
|
| 705 |
-
|
| 706 |
-
4. **Long-term** (Quarter 2+):
|
| 707 |
-
- Multi-region deployment
|
| 708 |
-
- Advanced monitoring dashboard
|
| 709 |
-
- Performance optimization based on production metrics
|
| 710 |
-
|
| 711 |
-
---
|
| 712 |
-
|
| 713 |
-
## 11. References
|
| 714 |
-
|
| 715 |
-
### 11.1 Technical Documentation
|
| 716 |
-
|
| 717 |
-
- FastMCP Framework: https://docs.fastmcp.com
|
| 718 |
-
- Model Context Protocol: https://modelcontextprotocol.io
|
| 719 |
-
- aiohttp Documentation: https://docs.aiohttp.org
|
| 720 |
-
- HuggingFace Spaces: https://huggingface.co/docs/hub/spaces
|
| 721 |
-
|
| 722 |
-
### 11.2 Related Files
|
| 723 |
-
|
| 724 |
-
- `proxy.py` - Authentication proxy implementation
|
| 725 |
-
- `app.py` - FastMCP server (simplified, no middleware)
|
| 726 |
-
- `start_with_proxy.py` - Unified launcher
|
| 727 |
-
- `PROXY_SETUP.md` - Setup and deployment guide
|
| 728 |
-
- `HUGGINGFACE_DEPLOY.md` - HuggingFace-specific instructions
|
| 729 |
-
|
| 730 |
-
### 11.3 System Architecture
|
| 731 |
-
|
| 732 |
-
```
|
| 733 |
-
┌─────────────────────────────────────────────────────────────────┐
|
| 734 |
-
│ FleetMind MCP System │
|
| 735 |
-
│ │
|
| 736 |
-
│ ┌──────────────┐ ┌──────────────┐ ┌─────────────────┐ │
|
| 737 |
-
│ │ Claude │───▶│ Auth Proxy │───▶│ FastMCP Server │ │
|
| 738 |
-
│ │ Desktop │ │ (Port 7860) │ │ (Port 7861) │ │
|
| 739 |
-
│ └──────────────┘ └──────────────┘ └─────────────────┘ │
|
| 740 |
-
│ │ │ │
|
| 741 |
-
│ ▼ ▼ │
|
| 742 |
-
│ ┌─────────────┐ ┌──────────────┐ │
|
| 743 |
-
│ │ Session │ │ PostgreSQL │ │
|
| 744 |
-
│ │ Store │ │ Database │ │
|
| 745 |
-
│ └─────────────┘ └──────────────┘ │
|
| 746 |
-
└─────────────────────────────────────────────────────────────────┘
|
| 747 |
-
```
|
| 748 |
-
|
| 749 |
-
---
|
| 750 |
-
|
| 751 |
-
## Appendix A: Code Statistics
|
| 752 |
-
|
| 753 |
-
| Component | Lines of Code | Complexity | Test Coverage |
|
| 754 |
-
|-----------|---------------|------------|---------------|
|
| 755 |
-
| proxy.py | 206 | Low | Manual testing |
|
| 756 |
-
| app.py (modified) | -150 | Reduced | Existing |
|
| 757 |
-
| start_with_proxy.py | 165 | Low | Manual testing |
|
| 758 |
-
| **Total Added** | **371** | **Low** | **95%+** |
|
| 759 |
-
|
| 760 |
-
## Appendix B: Deployment Checklist
|
| 761 |
-
|
| 762 |
-
- [ ] Dockerfile updated to use `start_with_proxy.py`
|
| 763 |
-
- [ ] Environment variables configured on HuggingFace
|
| 764 |
-
- [ ] Database connection tested
|
| 765 |
-
- [ ] Proxy health check endpoint verified (`/health`)
|
| 766 |
-
- [ ] Claude Desktop config updated with proxy URL
|
| 767 |
-
- [ ] Multi-user testing completed
|
| 768 |
-
- [ ] Session isolation verified
|
| 769 |
-
- [ ] Error handling tested (proxy crash, FastMCP crash)
|
| 770 |
-
- [ ] Performance benchmarks documented
|
| 771 |
-
- [ ] Monitoring alerts configured
|
| 772 |
-
|
| 773 |
-
---
|
| 774 |
-
|
| 775 |
-
**Document Status**: Final
|
| 776 |
-
**Review Status**: Pending Senior Engineer Review
|
| 777 |
-
**Approval Required**: Yes
|
| 778 |
-
**Next Review Date**: After production deployment
|
| 779 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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with vehicle-specific routing features for motorcycles (TWO_WHEELER mode), bicycles, and cars with toll detection, fuel consumption, and traffic breakdown.
|
| 6 |
-
|
| 7 |
-
## Enhancements Implemented
|
| 8 |
-
|
| 9 |
-
### 1. Motorcycle-Specific Routing (TWO_WHEELER Mode)
|
| 10 |
-
|
| 11 |
-
**Implementation:**
|
| 12 |
-
- Updated `VEHICLE_TYPE_TO_MODE` mapping: motorcycle → TWO_WHEELER
|
| 13 |
-
- Routes API now uses proper TWO_WHEELER travel mode instead of generic "driving"
|
| 14 |
-
- Motorcycle routes are different from car routes (different roads, shortcuts)
|
| 15 |
-
|
| 16 |
-
**Test Results (Dhaka Route):**
|
| 17 |
-
- **Distance:** 4.4 km (vs 5.0 km for car - motorcycles can use different roads)
|
| 18 |
-
- **Time:** 17 minutes (15 min base + 1 min traffic delay)
|
| 19 |
-
- **Route:** via Moghbazar Rd/Shaheed Tajuddin Ahmed Ave
|
| 20 |
-
- **Alternatives:** 2 alternative routes provided
|
| 21 |
-
- **Traffic Data:** 17 traffic segments available
|
| 22 |
-
- **Beta Warning:** Displayed (TWO_WHEELER mode is in beta, billed at higher rate)
|
| 23 |
-
|
| 24 |
-
**Features:**
|
| 25 |
-
✅ Motorcycle-optimized routes
|
| 26 |
-
✅ Real-time traffic for motorcycles
|
| 27 |
-
✅ Alternative routes
|
| 28 |
-
✅ Beta status warning
|
| 29 |
-
|
| 30 |
-
### 2. Enhanced Car Routing (DRIVE Mode)
|
| 31 |
-
|
| 32 |
-
**New Features:**
|
| 33 |
-
- Duration WITH traffic vs WITHOUT traffic
|
| 34 |
-
- Traffic delay breakdown
|
| 35 |
-
- Toll road detection
|
| 36 |
-
- Fuel consumption estimates (when enabled)
|
| 37 |
-
- Route labels (DEFAULT_ROUTE, FUEL_EFFICIENT, etc.)
|
| 38 |
-
- Detailed traffic segments
|
| 39 |
-
|
| 40 |
-
**Test Results (Dhaka Route):**
|
| 41 |
-
|
| 42 |
-
**Test 2a: Car with Defaults**
|
| 43 |
-
- **Distance:** 5.0 km
|
| 44 |
-
- **Time:** 12 minutes (14 min base - 2 min traffic benefit!)
|
| 45 |
-
- **Route:** via Shaheed Tajuddin Ahmed Ave
|
| 46 |
-
- **Toll Info:** YES - Toll roads on route
|
| 47 |
-
- **Traffic Data:** 3 traffic segments
|
| 48 |
-
- **Traffic Delay:** No delay (faster route with traffic)
|
| 49 |
-
|
| 50 |
-
**Test 2b: Car Avoiding Tolls**
|
| 51 |
-
- **Distance:** 4.2 km (different route)
|
| 52 |
-
- **Time:** 18 minutes (15 min base + 3 min traffic delay)
|
| 53 |
-
- **Route:** via Kazi Nazrul Islam Ave (different!)
|
| 54 |
-
- **Toll Info:** NO - Toll roads avoided successfully
|
| 55 |
-
- **Traffic Data:** 15 traffic segments
|
| 56 |
-
- **Traffic Delay:** +3 minutes
|
| 57 |
-
|
| 58 |
-
**Key Insight:** avoid_tolls parameter works! Routes API chooses a completely different route when tolls are avoided.
|
| 59 |
-
|
| 60 |
-
### 3. Bicycle Routing (BICYCLE Mode)
|
| 61 |
-
|
| 62 |
-
**Implementation:**
|
| 63 |
-
- Proper BICYCLE mode support
|
| 64 |
-
- Fixed routing preference bug (can't set preference for BICYCLE/WALK modes)
|
| 65 |
-
|
| 66 |
-
**Test Results:**
|
| 67 |
-
- Routes API found no bicycle routes for this specific Dhaka area
|
| 68 |
-
- Gracefully fell back to mock calculation
|
| 69 |
-
- This is expected - not all areas have mapped bicycle infrastructure
|
| 70 |
-
|
| 71 |
-
### 4. New Tool Parameters
|
| 72 |
-
|
| 73 |
-
Added to `calculate_route` tool in `server.py`:
|
| 74 |
-
|
| 75 |
-
| Parameter | Type | Description | Applies To |
|
| 76 |
-
|-----------|------|-------------|------------|
|
| 77 |
-
| `vehicle_type` | string | motorcycle, bicycle, car, van, truck | All |
|
| 78 |
-
| `avoid_tolls` | boolean | Avoid toll roads | Car, Motorcycle |
|
| 79 |
-
| `avoid_highways` | boolean | Avoid highways | Car, Motorcycle |
|
| 80 |
-
| `avoid_ferries` | boolean | Avoid ferry routes | Car, Motorcycle |
|
| 81 |
-
| `emission_type` | string | GASOLINE, ELECTRIC, HYBRID, DIESEL | Car, Van, Truck |
|
| 82 |
-
| `request_fuel_efficient` | boolean | Request eco-friendly route | Car, Van, Truck |
|
| 83 |
-
|
| 84 |
-
### 5. Enhanced Response Data
|
| 85 |
-
|
| 86 |
-
**New Response Fields:**
|
| 87 |
-
|
| 88 |
-
```python
|
| 89 |
-
{
|
| 90 |
-
"duration": {seconds, text}, # WITHOUT traffic
|
| 91 |
-
"duration_in_traffic": {seconds, text}, # WITH traffic
|
| 92 |
-
"traffic_delay": {seconds, text}, # Difference
|
| 93 |
-
"vehicle_type": str, # Vehicle used
|
| 94 |
-
"route_labels": [str], # Route type labels
|
| 95 |
-
"toll_info": { # Toll detection
|
| 96 |
-
"has_tolls": bool,
|
| 97 |
-
"details": str
|
| 98 |
-
},
|
| 99 |
-
"fuel_consumption": { # DRIVE mode only
|
| 100 |
-
"liters": float,
|
| 101 |
-
"text": str
|
| 102 |
-
},
|
| 103 |
-
"traffic_data_available": bool, # Traffic segments available
|
| 104 |
-
"traffic_segments_count": int, # Number of segments
|
| 105 |
-
"warning": str # Beta warnings
|
| 106 |
-
}
|
| 107 |
-
```
|
| 108 |
-
|
| 109 |
-
## Code Changes Summary
|
| 110 |
-
|
| 111 |
-
### Files Modified
|
| 112 |
-
|
| 113 |
-
**1. `chat/tools.py` (~150 lines modified/added)**
|
| 114 |
-
- Updated `VEHICLE_TYPE_TO_MODE` mapping (line 666)
|
| 115 |
-
- Updated `handle_calculate_route()` to pass vehicle_type and tool_input (line 688)
|
| 116 |
-
- Updated `_calculate_route_routes_api()` signature (line 837)
|
| 117 |
-
- Added enhanced field masks (lines 879-895)
|
| 118 |
-
- Added route modifiers (vehicleInfo, avoid options) (lines 922-943)
|
| 119 |
-
- Added extraComputations (TRAFFIC_ON_POLYLINE, TOLLS, FUEL_CONSUMPTION) (lines 945-965)
|
| 120 |
-
- Fixed routing preference for BICYCLE/WALK modes (lines 921-923)
|
| 121 |
-
- Enhanced response parsing (static duration, tolls, fuel) (lines 991-1030)
|
| 122 |
-
- Enhanced response data structure (lines 1036-1094)
|
| 123 |
-
- Fixed logging bug (line 1136)
|
| 124 |
-
|
| 125 |
-
**2. `server.py` (~70 lines modified)**
|
| 126 |
-
- Updated `calculate_route` tool parameters (lines 156-224)
|
| 127 |
-
- Added all new parameters to function signature
|
| 128 |
-
- Updated documentation with enhanced return type
|
| 129 |
-
|
| 130 |
-
## API Features Utilized
|
| 131 |
-
|
| 132 |
-
### Routes API v2 Features Now Used:
|
| 133 |
-
|
| 134 |
-
1. **Travel Modes:**
|
| 135 |
-
- DRIVE (cars, vans, trucks)
|
| 136 |
-
- TWO_WHEELER (motorcycles) ✅ **NEW**
|
| 137 |
-
- BICYCLE (bicycles)
|
| 138 |
-
- WALK (pedestrians)
|
| 139 |
-
|
| 140 |
-
2. **Route Modifiers:**
|
| 141 |
-
- `vehicleInfo.emissionType` ✅ **NEW**
|
| 142 |
-
- `avoidTolls` ✅ **NEW**
|
| 143 |
-
- `avoidHighways` ✅ **NEW**
|
| 144 |
-
- `avoidFerries` ✅ **NEW**
|
| 145 |
-
|
| 146 |
-
3. **Extra Computations:**
|
| 147 |
-
- `TRAFFIC_ON_POLYLINE` ✅ **NEW**
|
| 148 |
-
- `TOLLS` ✅ **NEW**
|
| 149 |
-
- `FUEL_CONSUMPTION` ✅ **NEW**
|
| 150 |
-
|
| 151 |
-
4. **Enhanced Data:**
|
| 152 |
-
- `staticDuration` (without traffic) ✅ **NEW**
|
| 153 |
-
- `routeLabels` ✅ **NEW**
|
| 154 |
-
- `travelAdvisory.tollInfo` ✅ **NEW**
|
| 155 |
-
- `travelAdvisory.fuelConsumptionMicroliters` ✅ **NEW**
|
| 156 |
-
- `travelAdvisory.speedReadingIntervals` ✅ **NEW**
|
| 157 |
-
|
| 158 |
-
## Comparison: Before vs After
|
| 159 |
-
|
| 160 |
-
### Dhaka Route Test (Same Origin/Destination)
|
| 161 |
-
|
| 162 |
-
| Vehicle | Before (Mock) | After (Routes API) | Improvement |
|
| 163 |
-
|---------|---------------|-------------------|-------------|
|
| 164 |
-
| **Motorcycle** | 14 min (2.0 km) | 17 min (4.4 km) | ✅ Real TWO_WHEELER routing |
|
| 165 |
-
| **Car** | 57 min (2.5 km) | 12 min (5.0 km) | ✅ 4.75x more accurate |
|
| 166 |
-
| **Car (no tolls)** | N/A | 18 min (4.2 km) | ✅ New feature works! |
|
| 167 |
-
| **Bicycle** | 9 min (2.4 km) | No routes (mock) | ⚠️ Area has no bike paths |
|
| 168 |
-
|
| 169 |
-
### Key Insights
|
| 170 |
-
|
| 171 |
-
1. **Motorcycle routes are different:**
|
| 172 |
-
- 4.4 km vs 5.0 km for cars
|
| 173 |
-
- Different roads (Moghbazar Rd vs Shaheed Tajuddin Ahmed Ave)
|
| 174 |
-
- Motorcycles can navigate through different paths
|
| 175 |
-
|
| 176 |
-
2. **Toll avoidance works:**
|
| 177 |
-
- Default route: 5.0 km via Shaheed Tajuddin Ahmed Ave (with tolls)
|
| 178 |
-
- Avoid tolls route: 4.2 km via Kazi Nazrul Islam Ave (no tolls)
|
| 179 |
-
- Different roads, different times
|
| 180 |
-
|
| 181 |
-
3. **Traffic delay breakdown is insightful:**
|
| 182 |
-
- Some routes are FASTER with traffic (route optimization)
|
| 183 |
-
- Traffic delay can be negative (better route found)
|
| 184 |
-
- Clear separation of base time vs traffic impact
|
| 185 |
-
|
| 186 |
-
## Benefits Achieved
|
| 187 |
-
|
| 188 |
-
### 1. Accuracy
|
| 189 |
-
✅ **4.75x more accurate** than mock algorithm
|
| 190 |
-
✅ Real-time traffic data from Google
|
| 191 |
-
✅ Actual road distances (not estimates)
|
| 192 |
-
✅ Vehicle-specific route optimization
|
| 193 |
-
|
| 194 |
-
### 2. Features
|
| 195 |
-
✅ Motorcycle-specific routing (TWO_WHEELER mode)
|
| 196 |
-
✅ Toll road detection and avoidance
|
| 197 |
-
✅ Traffic breakdown (with vs without)
|
| 198 |
-
✅ Fuel consumption estimates (future enhancement)
|
| 199 |
-
✅ Alternative routes with labels
|
| 200 |
-
✅ Detailed traffic segments
|
| 201 |
-
|
| 202 |
-
### 3. Flexibility
|
| 203 |
-
✅ Avoid tolls, highways, ferries
|
| 204 |
-
✅ Multiple vehicle types supported
|
| 205 |
-
✅ Emission type configuration
|
| 206 |
-
✅ Eco-friendly route requests
|
| 207 |
-
|
| 208 |
-
### 4. Reliability
|
| 209 |
-
✅ Beta warnings for TWO_WHEELER/BICYCLE modes
|
| 210 |
-
✅ Graceful fallback if Routes API fails
|
| 211 |
-
✅ Works with real-time traffic conditions
|
| 212 |
-
✅ Handles "no routes" scenarios
|
| 213 |
-
|
| 214 |
-
## Usage Examples
|
| 215 |
-
|
| 216 |
-
### Example 1: Motorcycle Routing
|
| 217 |
-
|
| 218 |
-
```python
|
| 219 |
-
from chat.tools import handle_calculate_route
|
| 220 |
-
|
| 221 |
-
result = handle_calculate_route({
|
| 222 |
-
"origin": "Ahsanullah University, Dhaka",
|
| 223 |
-
"destination": "Tejgaon College, Dhaka",
|
| 224 |
-
"vehicle_type": "motorcycle",
|
| 225 |
-
"alternatives": True
|
| 226 |
-
})
|
| 227 |
-
|
| 228 |
-
print(f"Distance: {result['distance']['text']}") # 4.4 km
|
| 229 |
-
print(f"Duration: {result['duration_in_traffic']['text']}") # 17 mins
|
| 230 |
-
print(f"Mode: {result['mode']}") # TWO_WHEELER
|
| 231 |
-
print(f"Warning: {result.get('warning', 'None')}") # Beta warning
|
| 232 |
-
# Output:
|
| 233 |
-
# Distance: 4.4 km
|
| 234 |
-
# Duration: 17 mins
|
| 235 |
-
# Mode: TWO_WHEELER
|
| 236 |
-
# Warning: Motorcycle routing uses TWO_WHEELER mode (beta)...
|
| 237 |
-
```
|
| 238 |
-
|
| 239 |
-
### Example 2: Car Routing with Toll Avoidance
|
| 240 |
-
|
| 241 |
-
```python
|
| 242 |
-
result = handle_calculate_route({
|
| 243 |
-
"origin": "Ahsanullah University, Dhaka",
|
| 244 |
-
"destination": "Tejgaon College, Dhaka",
|
| 245 |
-
"vehicle_type": "car",
|
| 246 |
-
"avoid_tolls": True
|
| 247 |
-
})
|
| 248 |
-
|
| 249 |
-
print(f"Route: {result['route_summary']}") # Kazi Nazrul Islam Ave
|
| 250 |
-
print(f"Has Tolls: {result['toll_info']['has_tolls']}") # False
|
| 251 |
-
print(f"Traffic Delay: {result['traffic_delay']['text']}") # 3 mins
|
| 252 |
-
# Output:
|
| 253 |
-
# Route: Kazi Nazrul Islam Ave
|
| 254 |
-
# Has Tolls: False
|
| 255 |
-
# Traffic Delay: 3 mins
|
| 256 |
-
```
|
| 257 |
-
|
| 258 |
-
### Example 3: Car with Fuel Consumption
|
| 259 |
-
|
| 260 |
-
```python
|
| 261 |
-
result = handle_calculate_route({
|
| 262 |
-
"origin": "Start Address",
|
| 263 |
-
"destination": "End Address",
|
| 264 |
-
"vehicle_type": "car",
|
| 265 |
-
"emission_type": "ELECTRIC",
|
| 266 |
-
"request_fuel_efficient": True
|
| 267 |
-
})
|
| 268 |
-
|
| 269 |
-
if result.get('fuel_consumption'):
|
| 270 |
-
print(f"Fuel: {result['fuel_consumption']['text']}")
|
| 271 |
-
# Note: Fuel consumption data depends on route length and API response
|
| 272 |
-
```
|
| 273 |
-
|
| 274 |
-
## API Limitations
|
| 275 |
-
|
| 276 |
-
### What Works:
|
| 277 |
-
✅ Motorcycle-specific routing (TWO_WHEELER mode)
|
| 278 |
-
✅ Bicycle routing (where infrastructure exists)
|
| 279 |
-
✅ Car routing with enhancements
|
| 280 |
-
✅ Toll detection and avoidance
|
| 281 |
-
✅ Traffic breakdown
|
| 282 |
-
✅ Alternative routes
|
| 283 |
-
|
| 284 |
-
### What Doesn't Work:
|
| 285 |
-
❌ Truck-specific routing (weight/height restrictions)
|
| 286 |
-
❌ Differentiation between car/van/truck (all use DRIVE mode)
|
| 287 |
-
❌ Hazmat routing
|
| 288 |
-
❌ Commercial vehicle restrictions
|
| 289 |
-
❌ Bicycle routing in areas without mapped bike infrastructure
|
| 290 |
-
|
| 291 |
-
**Reason:** Google Routes API doesn't support truck-specific parameters or commercial vehicle restrictions. All 4-wheeled vehicles use the same DRIVE mode.
|
| 292 |
-
|
| 293 |
-
## Future Enhancements (Optional)
|
| 294 |
-
|
| 295 |
-
1. **Add More Cities to City Profiles**
|
| 296 |
-
- Not needed - Routes API handles all cities automatically with real data
|
| 297 |
-
|
| 298 |
-
2. **Cache Recent Routes**
|
| 299 |
-
- Cache responses for 5-10 minutes to reduce API calls
|
| 300 |
-
- Good for repeated queries
|
| 301 |
-
|
| 302 |
-
3. **Toll Cost Estimates**
|
| 303 |
-
- Parse actual toll cost from `tollInfo.estimatedPrice`
|
| 304 |
-
- Currently just detecting presence, not cost
|
| 305 |
-
|
| 306 |
-
4. **Fuel Consumption Tracking**
|
| 307 |
-
- Parse and display actual fuel consumption data
|
| 308 |
-
- Currently field is requested but may not always be returned
|
| 309 |
-
|
| 310 |
-
5. **Traffic Segment Visualization**
|
| 311 |
-
- Use `speedReadingIntervals` for color-coded traffic visualization
|
| 312 |
-
- Show congestion levels along route
|
| 313 |
-
|
| 314 |
-
## Conclusion
|
| 315 |
-
|
| 316 |
-
The vehicle-specific routing enhancement is **complete and working perfectly**!
|
| 317 |
-
|
| 318 |
-
**Before:**
|
| 319 |
-
- All vehicles used same "driving" mode
|
| 320 |
-
- No toll detection
|
| 321 |
-
- No traffic breakdown
|
| 322 |
-
- Mock algorithm (over-estimated by 4.75x)
|
| 323 |
-
|
| 324 |
-
**After:**
|
| 325 |
-
- Motorcycles use TWO_WHEELER mode (different routes)
|
| 326 |
-
- Toll detection and avoidance working
|
| 327 |
-
- Traffic breakdown (with vs without traffic)
|
| 328 |
-
- Real-time Routes API data (4.75x more accurate)
|
| 329 |
-
- Enhanced features: fuel consumption, route labels, traffic segments
|
| 330 |
-
|
| 331 |
-
**Test Verification:**
|
| 332 |
-
✅ Motorcycle routing: 4.4 km in 17 mins via different roads
|
| 333 |
-
✅ Car routing: 5.0 km in 12 mins with toll detection
|
| 334 |
-
✅ Toll avoidance: 4.2 km via different route (no tolls)
|
| 335 |
-
✅ Bicycle routing: Graceful fallback when no bike paths
|
| 336 |
-
✅ Beta warnings: Displayed for TWO_WHEELER mode
|
| 337 |
-
✅ Traffic data: Available with segment counts
|
| 338 |
-
|
| 339 |
-
The system now provides **production-ready, vehicle-specific routing** for FleetMind dispatch operations using real Google Maps Routes API data with intelligent vehicle optimization.
|
| 340 |
-
|
| 341 |
-
---
|
| 342 |
-
|
| 343 |
-
**Implementation Date:** 2025-11-15
|
| 344 |
-
**Status:** ✅ Complete and Tested
|
| 345 |
-
**API:** Google Routes API v2 with vehicle-specific features
|
| 346 |
-
**Vehicles Supported:** Motorcycle (TWO_WHEELER), Bicycle, Car, Van, Truck (DRIVE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
apply_auth_pattern.py
DELETED
|
@@ -1,140 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Automated script to apply authentication pattern to all remaining handlers and tools
|
| 3 |
-
Run this to complete the authentication implementation
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import re
|
| 7 |
-
|
| 8 |
-
# Pattern for handler functions that need user_id parameter
|
| 9 |
-
HANDLER_FUNCTIONS_TO_UPDATE = [
|
| 10 |
-
# Order handlers
|
| 11 |
-
'handle_get_order_details',
|
| 12 |
-
'handle_search_orders',
|
| 13 |
-
'handle_get_incomplete_orders',
|
| 14 |
-
'handle_update_order',
|
| 15 |
-
'handle_delete_order',
|
| 16 |
-
'handle_delete_all_orders',
|
| 17 |
-
|
| 18 |
-
# Driver handlers
|
| 19 |
-
'handle_count_drivers',
|
| 20 |
-
'handle_get_driver_details',
|
| 21 |
-
'handle_search_drivers',
|
| 22 |
-
'handle_get_available_drivers',
|
| 23 |
-
'handle_update_driver',
|
| 24 |
-
'handle_delete_driver',
|
| 25 |
-
'handle_delete_all_drivers',
|
| 26 |
-
|
| 27 |
-
# Assignment handlers
|
| 28 |
-
'handle_create_assignment',
|
| 29 |
-
'handle_auto_assign_order',
|
| 30 |
-
'handle_intelligent_assign_order',
|
| 31 |
-
'handle_get_assignment_details',
|
| 32 |
-
'handle_update_assignment',
|
| 33 |
-
'handle_unassign_order',
|
| 34 |
-
'handle_complete_delivery',
|
| 35 |
-
'handle_fail_delivery',
|
| 36 |
-
]
|
| 37 |
-
|
| 38 |
-
AUTH_CHECK_CODE = ''' # Authentication check
|
| 39 |
-
if not user_id:
|
| 40 |
-
return {
|
| 41 |
-
"success": False,
|
| 42 |
-
"error": "Authentication required. Please login first.",
|
| 43 |
-
"auth_required": True
|
| 44 |
-
}
|
| 45 |
-
'''
|
| 46 |
-
|
| 47 |
-
def update_handler_function(content: str, func_name: str) -> str:
|
| 48 |
-
"""Add user_id parameter and auth check to a handler function"""
|
| 49 |
-
|
| 50 |
-
# Pattern 1: Update function signature
|
| 51 |
-
pattern1 = rf'(def {func_name}\(tool_input: dict)\) -> dict:'
|
| 52 |
-
replacement1 = r'\1, user_id: str = None) -> dict:'
|
| 53 |
-
content = re.sub(pattern1, replacement1, content)
|
| 54 |
-
|
| 55 |
-
# Pattern 2: Add auth check after docstring
|
| 56 |
-
pattern2 = rf'(def {func_name}\(.*?\).*?""".*?""")\n(\s+)(#|[a-zA-Z])'
|
| 57 |
-
|
| 58 |
-
def add_auth_check(match):
|
| 59 |
-
return match.group(1) + '\n' + AUTH_CHECK_CODE + '\n' + match.group(2) + match.group(3)
|
| 60 |
-
|
| 61 |
-
content = re.sub(pattern2, add_auth_check, content, flags=re.DOTALL)
|
| 62 |
-
|
| 63 |
-
return content
|
| 64 |
-
|
| 65 |
-
def update_handler_queries(content: str, func_name: str) -> str:
|
| 66 |
-
"""Add user_id filtering to WHERE clauses in handler functions"""
|
| 67 |
-
|
| 68 |
-
# Find the function
|
| 69 |
-
func_pattern = rf'def {func_name}\(.*?\).*?(?=\ndef\s|\Z)'
|
| 70 |
-
func_match = re.search(func_pattern, content, re.DOTALL)
|
| 71 |
-
|
| 72 |
-
if not func_match:
|
| 73 |
-
return content
|
| 74 |
-
|
| 75 |
-
func_content = func_match.group(0)
|
| 76 |
-
original_func = func_content
|
| 77 |
-
|
| 78 |
-
# For SELECT queries: Add user_id filter
|
| 79 |
-
if 'SELECT' in func_content and 'WHERE' in func_content:
|
| 80 |
-
# Pattern: where_clauses = []
|
| 81 |
-
func_content = re.sub(
|
| 82 |
-
r'(\s+where_clauses = \[\])',
|
| 83 |
-
r'\1\n # IMPORTANT: Always filter by user_id FIRST\n where_clauses = ["user_id = %s"]',
|
| 84 |
-
func_content
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
# Pattern: params = []
|
| 88 |
-
func_content = re.sub(
|
| 89 |
-
r'(\s+params = \[\])',
|
| 90 |
-
r'\1\n params = [user_id]',
|
| 91 |
-
func_content
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
# For UPDATE/DELETE queries: Add user_id to WHERE
|
| 95 |
-
if ('UPDATE' in func_content or 'DELETE' in func_content) and 'WHERE' not in func_content:
|
| 96 |
-
# Add WHERE user_id = %s to UPDATE/DELETE queries
|
| 97 |
-
func_content = re.sub(
|
| 98 |
-
r'(DELETE FROM \w+)',
|
| 99 |
-
r'\1 WHERE user_id = %s',
|
| 100 |
-
func_content
|
| 101 |
-
)
|
| 102 |
-
func_content = re.sub(
|
| 103 |
-
r'(UPDATE \w+ SET.*?)(\s+""")',
|
| 104 |
-
r'\1 WHERE user_id = %s\2',
|
| 105 |
-
func_content,
|
| 106 |
-
flags=re.DOTALL
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
# Replace in main content
|
| 110 |
-
content = content.replace(original_func, func_content)
|
| 111 |
-
|
| 112 |
-
return content
|
| 113 |
-
|
| 114 |
-
def main():
|
| 115 |
-
print("Applying authentication pattern to all handler functions...")
|
| 116 |
-
|
| 117 |
-
# Read chat/tools.py
|
| 118 |
-
with open('chat/tools.py', 'r', encoding='utf-8') as f:
|
| 119 |
-
content = f.read()
|
| 120 |
-
|
| 121 |
-
updated_count = 0
|
| 122 |
-
|
| 123 |
-
for func_name in HANDLER_FUNCTIONS_TO_UPDATE:
|
| 124 |
-
if f'def {func_name}(tool_input: dict) -> dict:' in content:
|
| 125 |
-
print(f" Updating {func_name}...")
|
| 126 |
-
content = update_handler_function(content, func_name)
|
| 127 |
-
content = update_handler_queries(content, func_name)
|
| 128 |
-
updated_count += 1
|
| 129 |
-
else:
|
| 130 |
-
print(f" Skipping {func_name} (already updated or not found)")
|
| 131 |
-
|
| 132 |
-
# Write back
|
| 133 |
-
with open('chat/tools.py', 'w', encoding='utf-8') as f:
|
| 134 |
-
f.write(content)
|
| 135 |
-
|
| 136 |
-
print(f"\nCompleted! Updated {updated_count} handler functions.")
|
| 137 |
-
print("\nNext: Run 'python update_server_tools.py' to update server.py tools")
|
| 138 |
-
|
| 139 |
-
if __name__ == '__main__':
|
| 140 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cleanup_bad_user_ids.py
DELETED
|
@@ -1,124 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Cleanup script for records with incorrect user_id='user_id'
|
| 3 |
-
This fixes data created before the verify_api_key() bug was fixed
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
from database.connection import get_db_connection
|
| 7 |
-
from database.api_keys import list_api_keys
|
| 8 |
-
import sys
|
| 9 |
-
|
| 10 |
-
def check_bad_records():
|
| 11 |
-
"""Check how many records have user_id='user_id'"""
|
| 12 |
-
conn = get_db_connection()
|
| 13 |
-
cursor = conn.cursor()
|
| 14 |
-
|
| 15 |
-
print("\n=== Checking for records with incorrect user_id='user_id' ===\n")
|
| 16 |
-
|
| 17 |
-
tables = ['orders', 'drivers', 'assignments']
|
| 18 |
-
total_bad = 0
|
| 19 |
-
details = {}
|
| 20 |
-
|
| 21 |
-
for table in tables:
|
| 22 |
-
try:
|
| 23 |
-
cursor.execute(f"SELECT COUNT(*) as count FROM {table} WHERE user_id = %s", ('user_id',))
|
| 24 |
-
result = cursor.fetchone()
|
| 25 |
-
count = result['count'] if result else 0
|
| 26 |
-
|
| 27 |
-
if count > 0:
|
| 28 |
-
print(f" {table}: {count} records with user_id='user_id'")
|
| 29 |
-
total_bad += count
|
| 30 |
-
details[table] = count
|
| 31 |
-
else:
|
| 32 |
-
print(f" {table}: No bad records found")
|
| 33 |
-
details[table] = 0
|
| 34 |
-
except Exception as e:
|
| 35 |
-
print(f" {table}: Error checking - {e}")
|
| 36 |
-
details[table] = 0
|
| 37 |
-
|
| 38 |
-
cursor.close()
|
| 39 |
-
conn.close()
|
| 40 |
-
|
| 41 |
-
print(f"\nTotal bad records: {total_bad}\n")
|
| 42 |
-
return total_bad, details
|
| 43 |
-
|
| 44 |
-
def get_first_api_key_user():
|
| 45 |
-
"""Get the first API key user (likely the correct one)"""
|
| 46 |
-
try:
|
| 47 |
-
keys = list_api_keys()
|
| 48 |
-
if keys and len(keys) > 0:
|
| 49 |
-
return keys[0]['user_id']
|
| 50 |
-
except Exception as e:
|
| 51 |
-
print(f"Error fetching API keys: {e}")
|
| 52 |
-
return None
|
| 53 |
-
|
| 54 |
-
def cleanup_bad_records(correct_user_id: str):
|
| 55 |
-
"""
|
| 56 |
-
Update records with user_id='user_id' to the correct user_id
|
| 57 |
-
|
| 58 |
-
Args:
|
| 59 |
-
correct_user_id: The actual user_id to assign (e.g., 'user_75d23af433e0')
|
| 60 |
-
"""
|
| 61 |
-
conn = get_db_connection()
|
| 62 |
-
cursor = conn.cursor()
|
| 63 |
-
|
| 64 |
-
print(f"\n=== Updating records to user_id='{correct_user_id}' ===\n")
|
| 65 |
-
|
| 66 |
-
tables = ['orders', 'drivers', 'assignments']
|
| 67 |
-
total_updated = 0
|
| 68 |
-
|
| 69 |
-
for table in tables:
|
| 70 |
-
try:
|
| 71 |
-
cursor.execute(
|
| 72 |
-
f"UPDATE {table} SET user_id = %s WHERE user_id = %s",
|
| 73 |
-
(correct_user_id, 'user_id')
|
| 74 |
-
)
|
| 75 |
-
count = cursor.rowcount
|
| 76 |
-
|
| 77 |
-
if count > 0:
|
| 78 |
-
print(f" {table}: Updated {count} records")
|
| 79 |
-
total_updated += count
|
| 80 |
-
else:
|
| 81 |
-
print(f" {table}: No records to update")
|
| 82 |
-
except Exception as e:
|
| 83 |
-
print(f" {table}: Error updating - {e}")
|
| 84 |
-
conn.rollback()
|
| 85 |
-
cursor.close()
|
| 86 |
-
conn.close()
|
| 87 |
-
return 0
|
| 88 |
-
|
| 89 |
-
conn.commit()
|
| 90 |
-
cursor.close()
|
| 91 |
-
conn.close()
|
| 92 |
-
|
| 93 |
-
print(f"\nTotal records updated: {total_updated}\n")
|
| 94 |
-
return total_updated
|
| 95 |
-
|
| 96 |
-
if __name__ == "__main__":
|
| 97 |
-
print("=== FleetMind User ID Cleanup Utility ===")
|
| 98 |
-
|
| 99 |
-
# First, check how many bad records exist
|
| 100 |
-
bad_count, details = check_bad_records()
|
| 101 |
-
|
| 102 |
-
if bad_count == 0:
|
| 103 |
-
print("No cleanup needed - all records have correct user_id values!")
|
| 104 |
-
sys.exit(0)
|
| 105 |
-
|
| 106 |
-
# Get the correct user_id from API keys table
|
| 107 |
-
correct_user_id = get_first_api_key_user()
|
| 108 |
-
|
| 109 |
-
if not correct_user_id:
|
| 110 |
-
print("\nERROR: Could not determine correct user_id from API keys table")
|
| 111 |
-
print("Please run this script with the user_id as argument:")
|
| 112 |
-
print(" python cleanup_bad_user_ids.py <user_id>")
|
| 113 |
-
sys.exit(1)
|
| 114 |
-
|
| 115 |
-
print(f"\nFound API key user: {correct_user_id}")
|
| 116 |
-
|
| 117 |
-
# Auto-cleanup
|
| 118 |
-
print(f"\nProceeding with automatic cleanup...")
|
| 119 |
-
updated = cleanup_bad_records(correct_user_id)
|
| 120 |
-
|
| 121 |
-
if updated > 0:
|
| 122 |
-
print(f"\nSUCCESS: Cleanup complete! Updated {updated} records.")
|
| 123 |
-
else:
|
| 124 |
-
print("\nNo updates were made.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fix_user_id.py
DELETED
|
@@ -1,53 +0,0 @@
|
|
| 1 |
-
"""Quick fix for the bad user_id record"""
|
| 2 |
-
|
| 3 |
-
from database.connection import get_db_connection
|
| 4 |
-
|
| 5 |
-
def fix_bad_user_id():
|
| 6 |
-
conn = get_db_connection()
|
| 7 |
-
cursor = conn.cursor()
|
| 8 |
-
|
| 9 |
-
# Get the correct user_id from api_keys table
|
| 10 |
-
cursor.execute("SELECT user_id FROM api_keys WHERE is_active = true LIMIT 1")
|
| 11 |
-
result = cursor.fetchone()
|
| 12 |
-
|
| 13 |
-
if not result:
|
| 14 |
-
print("ERROR: No active API key found")
|
| 15 |
-
cursor.close()
|
| 16 |
-
conn.close()
|
| 17 |
-
return
|
| 18 |
-
|
| 19 |
-
correct_user_id = result['user_id']
|
| 20 |
-
print(f"Found active user: {correct_user_id}")
|
| 21 |
-
|
| 22 |
-
# Update orders with bad user_id
|
| 23 |
-
cursor.execute(
|
| 24 |
-
"UPDATE orders SET user_id = %s WHERE user_id = %s",
|
| 25 |
-
(correct_user_id, 'user_id')
|
| 26 |
-
)
|
| 27 |
-
orders_updated = cursor.rowcount
|
| 28 |
-
print(f"Updated {orders_updated} orders")
|
| 29 |
-
|
| 30 |
-
# Update drivers with bad user_id
|
| 31 |
-
cursor.execute(
|
| 32 |
-
"UPDATE drivers SET user_id = %s WHERE user_id = %s",
|
| 33 |
-
(correct_user_id, 'user_id')
|
| 34 |
-
)
|
| 35 |
-
drivers_updated = cursor.rowcount
|
| 36 |
-
print(f"Updated {drivers_updated} drivers")
|
| 37 |
-
|
| 38 |
-
# Update assignments with bad user_id
|
| 39 |
-
cursor.execute(
|
| 40 |
-
"UPDATE assignments SET user_id = %s WHERE user_id = %s",
|
| 41 |
-
(correct_user_id, 'user_id')
|
| 42 |
-
)
|
| 43 |
-
assignments_updated = cursor.rowcount
|
| 44 |
-
print(f"Updated {assignments_updated} assignments")
|
| 45 |
-
|
| 46 |
-
conn.commit()
|
| 47 |
-
cursor.close()
|
| 48 |
-
conn.close()
|
| 49 |
-
|
| 50 |
-
print(f"\nTotal records fixed: {orders_updated + drivers_updated + assignments_updated}")
|
| 51 |
-
|
| 52 |
-
if __name__ == "__main__":
|
| 53 |
-
fix_bad_user_id()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
flow.md
DELETED
|
@@ -1,828 +0,0 @@
|
|
| 1 |
-
● 🔄 Complete Function Flow
|
| 2 |
-
|
| 3 |
-
Input: "Create an order for John Doe at 123 Main St, San
|
| 4 |
-
Francisco"
|
| 5 |
-
|
| 6 |
-
Let me trace every single function call with this exact example!
|
| 7 |
-
|
| 8 |
-
---
|
| 9 |
-
📞 FUNCTION CALL SEQUENCE:
|
| 10 |
-
|
| 11 |
-
STEP 1: User Clicks Send Button
|
| 12 |
-
|
| 13 |
-
FILE: ui/app.py
|
| 14 |
-
LINE: 448-452
|
| 15 |
-
|
| 16 |
-
User clicks "Send" button in Gradio UI
|
| 17 |
-
↓
|
| 18 |
-
FUNCTION CALLED: send_message(message, conv_state)
|
| 19 |
-
message = "Create an order for John Doe at 123 Main St, San
|
| 20 |
-
Francisco"
|
| 21 |
-
conv_state = ConversationManager object
|
| 22 |
-
|
| 23 |
-
Function Code:
|
| 24 |
-
def send_message(message, conv_state):
|
| 25 |
-
"""Handle send button click"""
|
| 26 |
-
chat_history, tools, new_state = handle_chat_message(message,
|
| 27 |
-
conv_state)
|
| 28 |
-
# ↑
|
| 29 |
-
# CALLS THIS NEXT
|
| 30 |
-
return chat_history, tools, new_state, ""
|
| 31 |
-
|
| 32 |
-
---
|
| 33 |
-
STEP 2: handle_chat_message()
|
| 34 |
-
|
| 35 |
-
FILE: ui/app.py
|
| 36 |
-
LINE: 223-241
|
| 37 |
-
|
| 38 |
-
FUNCTION: handle_chat_message(message, conversation_state)
|
| 39 |
-
message = "Create an order for John Doe at 123 Main St, San
|
| 40 |
-
Francisco"
|
| 41 |
-
conversation_state = ConversationManager object
|
| 42 |
-
|
| 43 |
-
Function Code:
|
| 44 |
-
def handle_chat_message(message, conversation_state):
|
| 45 |
-
if not message.strip():
|
| 46 |
-
return ...
|
| 47 |
-
|
| 48 |
-
# Process message through chat engine
|
| 49 |
-
response, tool_calls = chat_engine.process_message(message,
|
| 50 |
-
conversation_state)
|
| 51 |
-
# ↑
|
| 52 |
-
# CALLS THIS NEXT
|
| 53 |
-
|
| 54 |
-
# Return updated UI
|
| 55 |
-
return conversation_state.get_formatted_history(),
|
| 56 |
-
conversation_state.get_tool_calls(), conversation_state
|
| 57 |
-
|
| 58 |
-
---
|
| 59 |
-
STEP 3: chat_engine.process_message()
|
| 60 |
-
|
| 61 |
-
FILE: chat/chat_engine.py
|
| 62 |
-
LINE: 58-73
|
| 63 |
-
|
| 64 |
-
FUNCTION: ChatEngine.process_message(user_message, conversation)
|
| 65 |
-
user_message = "Create an order for John Doe at 123 Main St,
|
| 66 |
-
San Francisco"
|
| 67 |
-
conversation = ConversationManager object
|
| 68 |
-
|
| 69 |
-
Function Code:
|
| 70 |
-
def process_message(self, user_message, conversation):
|
| 71 |
-
"""Process user message and return AI response"""
|
| 72 |
-
return self.provider.process_message(user_message,
|
| 73 |
-
conversation)
|
| 74 |
-
# ↑
|
| 75 |
-
# self.provider = GeminiProvider (from chat_engine.py:26)
|
| 76 |
-
# CALLS GeminiProvider.process_message() NEXT
|
| 77 |
-
|
| 78 |
-
---
|
| 79 |
-
STEP 4: GeminiProvider.process_message()
|
| 80 |
-
|
| 81 |
-
FILE: chat/providers/gemini_provider.py
|
| 82 |
-
LINE: 173-212
|
| 83 |
-
|
| 84 |
-
FUNCTION: GeminiProvider.process_message(user_message,
|
| 85 |
-
conversation)
|
| 86 |
-
user_message = "Create an order for John Doe at 123 Main St,
|
| 87 |
-
San Francisco"
|
| 88 |
-
conversation = ConversationManager object
|
| 89 |
-
|
| 90 |
-
Function Code:
|
| 91 |
-
def process_message(self, user_message, conversation):
|
| 92 |
-
"""Process user message with Gemini"""
|
| 93 |
-
if not self.api_available:
|
| 94 |
-
return self._handle_no_api(), []
|
| 95 |
-
|
| 96 |
-
# Lazy initialization on first use
|
| 97 |
-
self._ensure_initialized() # ← CALLS THIS if not initialized
|
| 98 |
-
|
| 99 |
-
if not self._initialized:
|
| 100 |
-
return "⚠️ Failed to initialize...", []
|
| 101 |
-
|
| 102 |
-
try:
|
| 103 |
-
# Build conversation history for Gemini
|
| 104 |
-
chat =
|
| 105 |
-
self.model.start_chat(history=self._convert_history(conversation))
|
| 106 |
-
# ↑
|
| 107 |
-
# CALLS
|
| 108 |
-
_convert_history()
|
| 109 |
-
|
| 110 |
-
# Send message and get response
|
| 111 |
-
response = chat.send_message(user_message,
|
| 112 |
-
safety_settings={...})
|
| 113 |
-
# ↑
|
| 114 |
-
# 🌐 API CALL TO GOOGLE GEMINI
|
| 115 |
-
# Sends: "Create an order for John Doe at 123
|
| 116 |
-
Main St, San Francisco"
|
| 117 |
-
|
| 118 |
-
# Add user message to conversation
|
| 119 |
-
conversation.add_message("user", user_message)
|
| 120 |
-
|
| 121 |
-
# Process response and handle function calls
|
| 122 |
-
return self._process_response(response, conversation,
|
| 123 |
-
chat)
|
| 124 |
-
# ↑
|
| 125 |
-
# CALLS THIS NEXT
|
| 126 |
-
|
| 127 |
-
---
|
| 128 |
-
STEP 5: Gemini API Processes Request
|
| 129 |
-
|
| 130 |
-
🌐 GOOGLE GEMINI API (External)
|
| 131 |
-
|
| 132 |
-
RECEIVES:
|
| 133 |
-
- System Prompt: "You are an AI assistant for FleetMind..."
|
| 134 |
-
- User Message: "Create an order for John Doe at 123 Main St, San
|
| 135 |
-
Francisco"
|
| 136 |
-
- Available Tools: [geocode_address, create_order]
|
| 137 |
-
|
| 138 |
-
AI ANALYZES:
|
| 139 |
-
"User wants to create an order. I have:
|
| 140 |
-
✅ Customer Name: John Doe
|
| 141 |
-
✅ Address: 123 Main St, San Francisco
|
| 142 |
-
❌ GPS Coordinates: Missing!
|
| 143 |
-
|
| 144 |
-
DECISION: Call geocode_address tool first to get coordinates."
|
| 145 |
-
|
| 146 |
-
RETURNS TO CODE:
|
| 147 |
-
response = {
|
| 148 |
-
candidates: [{
|
| 149 |
-
content: {
|
| 150 |
-
parts: [{
|
| 151 |
-
function_call: {
|
| 152 |
-
name: "geocode_address",
|
| 153 |
-
args: {
|
| 154 |
-
"address": "123 Main St, San Francisco"
|
| 155 |
-
}
|
| 156 |
-
}
|
| 157 |
-
}]
|
| 158 |
-
}
|
| 159 |
-
}]
|
| 160 |
-
}
|
| 161 |
-
|
| 162 |
-
---
|
| 163 |
-
STEP 6: _process_response() - Detects Function Call
|
| 164 |
-
|
| 165 |
-
FILE: chat/providers/gemini_provider.py
|
| 166 |
-
LINE: 226-393
|
| 167 |
-
|
| 168 |
-
FUNCTION: _process_response(response, conversation, chat)
|
| 169 |
-
response = Response from Gemini with function_call
|
| 170 |
-
conversation = ConversationManager object
|
| 171 |
-
chat = Gemini chat session
|
| 172 |
-
|
| 173 |
-
Function Code:
|
| 174 |
-
def _process_response(self, response, conversation, chat):
|
| 175 |
-
"""Process Gemini's response and handle function calls"""
|
| 176 |
-
tool_calls_made = []
|
| 177 |
-
|
| 178 |
-
try:
|
| 179 |
-
# Check ALL parts for function calls
|
| 180 |
-
parts = response.candidates[0].content.parts
|
| 181 |
-
logger.info(f"Processing response with {len(parts)}
|
| 182 |
-
part(s)")
|
| 183 |
-
# ↑
|
| 184 |
-
# LOGS: "Processing response with 1 part(s)"
|
| 185 |
-
|
| 186 |
-
for part in parts:
|
| 187 |
-
if hasattr(part, 'function_call'):
|
| 188 |
-
fc = part.function_call
|
| 189 |
-
if fc and hasattr(fc, 'name') and fc.name:
|
| 190 |
-
has_function_call = True
|
| 191 |
-
logger.info(f"Detected function call:
|
| 192 |
-
{fc.name}")
|
| 193 |
-
# ↑
|
| 194 |
-
# LOGS: "Detected function call:
|
| 195 |
-
geocode_address"
|
| 196 |
-
break
|
| 197 |
-
|
| 198 |
-
if has_function_call:
|
| 199 |
-
# Handle function calls (potentially multiple in
|
| 200 |
-
sequence)
|
| 201 |
-
current_response = response
|
| 202 |
-
max_iterations = 10
|
| 203 |
-
|
| 204 |
-
for iteration in range(max_iterations): # ← LOOP
|
| 205 |
-
STARTS
|
| 206 |
-
# Extract function call details
|
| 207 |
-
first_part =
|
| 208 |
-
current_response.candidates[0].content.parts[0]
|
| 209 |
-
function_call = first_part.function_call
|
| 210 |
-
function_name = function_call.name #
|
| 211 |
-
"geocode_address"
|
| 212 |
-
function_args = dict(function_call.args) #
|
| 213 |
-
{"address": "123 Main St, San Francisco"}
|
| 214 |
-
|
| 215 |
-
logger.info(f"Gemini executing function:
|
| 216 |
-
{function_name} (iteration {iteration + 1})")
|
| 217 |
-
# ↑
|
| 218 |
-
# LOGS: "Gemini executing function:
|
| 219 |
-
geocode_address (iteration 1)"
|
| 220 |
-
|
| 221 |
-
# Execute the tool
|
| 222 |
-
tool_result = execute_tool(function_name,
|
| 223 |
-
function_args)
|
| 224 |
-
# ↑
|
| 225 |
-
# CALLS execute_tool() NEXT
|
| 226 |
-
|
| 227 |
-
---
|
| 228 |
-
STEP 7: execute_tool() - Routes to Handler
|
| 229 |
-
|
| 230 |
-
FILE: chat/tools.py
|
| 231 |
-
LINE: 92-118
|
| 232 |
-
|
| 233 |
-
FUNCTION: execute_tool(tool_name, tool_input)
|
| 234 |
-
tool_name = "geocode_address"
|
| 235 |
-
tool_input = {"address": "123 Main St, San Francisco"}
|
| 236 |
-
|
| 237 |
-
Function Code:
|
| 238 |
-
def execute_tool(tool_name, tool_input):
|
| 239 |
-
"""Route tool execution to appropriate handler"""
|
| 240 |
-
try:
|
| 241 |
-
if tool_name == "geocode_address":
|
| 242 |
-
return handle_geocode_address(tool_input)
|
| 243 |
-
# ↑
|
| 244 |
-
# CALLS THIS NEXT
|
| 245 |
-
elif tool_name == "create_order":
|
| 246 |
-
return handle_create_order(tool_input)
|
| 247 |
-
else:
|
| 248 |
-
return {"success": False, "error": f"Unknown tool:
|
| 249 |
-
{tool_name}"}
|
| 250 |
-
except Exception as e:
|
| 251 |
-
logger.error(f"Tool execution error ({tool_name}): {e}")
|
| 252 |
-
return {"success": False, "error": str(e)}
|
| 253 |
-
|
| 254 |
-
---
|
| 255 |
-
STEP 8: handle_geocode_address()
|
| 256 |
-
|
| 257 |
-
FILE: chat/tools.py
|
| 258 |
-
LINE: 121-150
|
| 259 |
-
|
| 260 |
-
FUNCTION: handle_geocode_address(tool_input)
|
| 261 |
-
tool_input = {"address": "123 Main St, San Francisco"}
|
| 262 |
-
|
| 263 |
-
Function Code:
|
| 264 |
-
def handle_geocode_address(tool_input):
|
| 265 |
-
"""Execute geocoding tool"""
|
| 266 |
-
address = tool_input.get("address", "") # "123 Main St, San
|
| 267 |
-
Francisco"
|
| 268 |
-
|
| 269 |
-
if not address:
|
| 270 |
-
return {"success": False, "error": "Address is required"}
|
| 271 |
-
|
| 272 |
-
logger.info(f"Geocoding address: {address}")
|
| 273 |
-
# ↑
|
| 274 |
-
# LOGS: "Geocoding address: 123 Main St, San
|
| 275 |
-
Francisco"
|
| 276 |
-
|
| 277 |
-
result = geocoding_service.geocode(address)
|
| 278 |
-
# ↑
|
| 279 |
-
# CALLS geocoding_service.geocode() NEXT
|
| 280 |
-
|
| 281 |
-
return {
|
| 282 |
-
"success": True,
|
| 283 |
-
"latitude": result["lat"],
|
| 284 |
-
"longitude": result["lng"],
|
| 285 |
-
"formatted_address": result["formatted_address"],
|
| 286 |
-
"confidence": result["confidence"],
|
| 287 |
-
"message": f"Address geocoded successfully
|
| 288 |
-
({result['confidence']})"
|
| 289 |
-
}
|
| 290 |
-
|
| 291 |
-
---
|
| 292 |
-
STEP 9: GeocodingService.geocode()
|
| 293 |
-
|
| 294 |
-
FILE: chat/geocoding.py
|
| 295 |
-
LINE: 28-65
|
| 296 |
-
|
| 297 |
-
FUNCTION: GeocodingService.geocode(address)
|
| 298 |
-
address = "123 Main St, San Francisco"
|
| 299 |
-
|
| 300 |
-
Function Code:
|
| 301 |
-
def geocode(self, address):
|
| 302 |
-
"""Geocode an address to coordinates"""
|
| 303 |
-
if not address:
|
| 304 |
-
return self._error_response("Address is required")
|
| 305 |
-
|
| 306 |
-
# Use mock or real API
|
| 307 |
-
if self.use_mock: # True (no HERE_API_KEY configured)
|
| 308 |
-
return self._geocode_mock(address)
|
| 309 |
-
# ↑
|
| 310 |
-
# CALLS THIS NEXT
|
| 311 |
-
else:
|
| 312 |
-
return self._geocode_here(address)
|
| 313 |
-
|
| 314 |
-
---
|
| 315 |
-
STEP 10: _geocode_mock() - Returns Coordinates
|
| 316 |
-
|
| 317 |
-
FILE: chat/geocoding.py
|
| 318 |
-
LINE: 52-70
|
| 319 |
-
|
| 320 |
-
FUNCTION: _geocode_mock(address)
|
| 321 |
-
address = "123 Main St, San Francisco"
|
| 322 |
-
|
| 323 |
-
Function Code:
|
| 324 |
-
def _geocode_mock(self, address):
|
| 325 |
-
"""Mock geocoding using city detection"""
|
| 326 |
-
address_lower = address.lower()
|
| 327 |
-
|
| 328 |
-
# Try to detect city in address
|
| 329 |
-
for city_name, (lat, lng) in CITY_COORDINATES.items():
|
| 330 |
-
if city_name in address_lower:
|
| 331 |
-
logger.info(f"Mock geocoding detected city:
|
| 332 |
-
{city_name}")
|
| 333 |
-
# ↑
|
| 334 |
-
# LOGS: "Mock geocoding detected city: san
|
| 335 |
-
francisco"
|
| 336 |
-
|
| 337 |
-
return {
|
| 338 |
-
"lat": lat, # 37.7749
|
| 339 |
-
"lng": lng, # -122.4194
|
| 340 |
-
"formatted_address": address,
|
| 341 |
-
"confidence": "mock"
|
| 342 |
-
}
|
| 343 |
-
|
| 344 |
-
# Default to San Francisco if no city detected
|
| 345 |
-
return {
|
| 346 |
-
"lat": 37.7749,
|
| 347 |
-
"lng": -122.4194,
|
| 348 |
-
"formatted_address": address,
|
| 349 |
-
"confidence": "mock"
|
| 350 |
-
}
|
| 351 |
-
|
| 352 |
-
RETURNS:
|
| 353 |
-
{
|
| 354 |
-
"lat": 37.7749,
|
| 355 |
-
"lng": -122.4194,
|
| 356 |
-
"formatted_address": "123 Main St, San Francisco",
|
| 357 |
-
"confidence": "mock"
|
| 358 |
-
}
|
| 359 |
-
|
| 360 |
-
---
|
| 361 |
-
STEP 11: Back to handle_geocode_address()
|
| 362 |
-
|
| 363 |
-
FILE: chat/tools.py
|
| 364 |
-
LINE: 141-150
|
| 365 |
-
|
| 366 |
-
result = {
|
| 367 |
-
"lat": 37.7749,
|
| 368 |
-
"lng": -122.4194,
|
| 369 |
-
"formatted_address": "123 Main St, San Francisco",
|
| 370 |
-
"confidence": "mock"
|
| 371 |
-
}
|
| 372 |
-
|
| 373 |
-
RETURNS:
|
| 374 |
-
{
|
| 375 |
-
"success": True,
|
| 376 |
-
"latitude": 37.7749,
|
| 377 |
-
"longitude": -122.4194,
|
| 378 |
-
"formatted_address": "123 Main St, San Francisco",
|
| 379 |
-
"confidence": "mock",
|
| 380 |
-
"message": "Address geocoded successfully (mock)"
|
| 381 |
-
}
|
| 382 |
-
|
| 383 |
-
---
|
| 384 |
-
STEP 12: Back to _process_response() - Tool Result Received
|
| 385 |
-
|
| 386 |
-
FILE: chat/providers/gemini_provider.py
|
| 387 |
-
LINE: 285-310
|
| 388 |
-
|
| 389 |
-
tool_result = {
|
| 390 |
-
"success": True,
|
| 391 |
-
"latitude": 37.7749,
|
| 392 |
-
"longitude": -122.4194,
|
| 393 |
-
"formatted_address": "123 Main St, San Francisco",
|
| 394 |
-
"confidence": "mock",
|
| 395 |
-
"message": "Address geocoded successfully (mock)"
|
| 396 |
-
}
|
| 397 |
-
|
| 398 |
-
# Track for transparency
|
| 399 |
-
tool_calls_made.append({
|
| 400 |
-
"tool": "geocode_address",
|
| 401 |
-
"input": {"address": "123 Main St, San Francisco"},
|
| 402 |
-
"result": tool_result
|
| 403 |
-
})
|
| 404 |
-
|
| 405 |
-
conversation.add_tool_result("geocode_address", function_args,
|
| 406 |
-
tool_result)
|
| 407 |
-
|
| 408 |
-
# Send function result back to Gemini
|
| 409 |
-
current_response = chat.send_message(
|
| 410 |
-
genai.protos.Content(
|
| 411 |
-
parts=[genai.protos.Part(
|
| 412 |
-
function_response=genai.protos.FunctionResponse(
|
| 413 |
-
name="geocode_address",
|
| 414 |
-
response={"result": tool_result}
|
| 415 |
-
)
|
| 416 |
-
)]
|
| 417 |
-
)
|
| 418 |
-
)
|
| 419 |
-
# ↑
|
| 420 |
-
# 🌐 API CALL TO GEMINI WITH GEOCODING RESULT
|
| 421 |
-
|
| 422 |
-
---
|
| 423 |
-
STEP 13: Gemini Receives Geocoding Result
|
| 424 |
-
|
| 425 |
-
🌐 GOOGLE GEMINI API (External)
|
| 426 |
-
|
| 427 |
-
RECEIVES:
|
| 428 |
-
- Function: geocode_address
|
| 429 |
-
- Result: {
|
| 430 |
-
"success": True,
|
| 431 |
-
"latitude": 37.7749,
|
| 432 |
-
"longitude": -122.4194
|
| 433 |
-
}
|
| 434 |
-
|
| 435 |
-
AI ANALYZES:
|
| 436 |
-
"Great! I now have GPS coordinates:
|
| 437 |
-
✅ Customer Name: John Doe
|
| 438 |
-
✅ Address: 123 Main St, San Francisco
|
| 439 |
-
✅ Latitude: 37.7749
|
| 440 |
-
✅ Longitude: -122.4194
|
| 441 |
-
|
| 442 |
-
DECISION: Now I can create the order in the database!
|
| 443 |
-
Call create_order tool."
|
| 444 |
-
|
| 445 |
-
RETURNS TO CODE:
|
| 446 |
-
response = {
|
| 447 |
-
candidates: [{
|
| 448 |
-
content: {
|
| 449 |
-
parts: [{
|
| 450 |
-
function_call: {
|
| 451 |
-
name: "create_order",
|
| 452 |
-
args: {
|
| 453 |
-
"customer_name": "John Doe",
|
| 454 |
-
"delivery_address": "123 Main St, San
|
| 455 |
-
Francisco",
|
| 456 |
-
"delivery_lat": 37.7749,
|
| 457 |
-
"delivery_lng": -122.4194,
|
| 458 |
-
"priority": "standard"
|
| 459 |
-
}
|
| 460 |
-
}
|
| 461 |
-
}]
|
| 462 |
-
}
|
| 463 |
-
}]
|
| 464 |
-
}
|
| 465 |
-
|
| 466 |
-
---
|
| 467 |
-
STEP 14: Loop Continues - Detects create_order
|
| 468 |
-
|
| 469 |
-
FILE: chat/providers/gemini_provider.py
|
| 470 |
-
LINE: 252-285
|
| 471 |
-
|
| 472 |
-
# Still in the for loop (iteration 2)
|
| 473 |
-
first_part = current_response.candidates[0].content.parts[0]
|
| 474 |
-
has_fc = True # Another function call detected
|
| 475 |
-
|
| 476 |
-
function_call = first_part.function_call
|
| 477 |
-
function_name = function_call.name # "create_order"
|
| 478 |
-
function_args = dict(function_call.args) # {customer_name,
|
| 479 |
-
address, lat, lng...}
|
| 480 |
-
|
| 481 |
-
logger.info(f"Gemini executing function: {function_name}
|
| 482 |
-
(iteration 2)")
|
| 483 |
-
# ↑
|
| 484 |
-
# LOGS: "Gemini executing function: create_order
|
| 485 |
-
(iteration 2)"
|
| 486 |
-
|
| 487 |
-
# Execute the tool
|
| 488 |
-
tool_result = execute_tool(function_name, function_args)
|
| 489 |
-
# ↑
|
| 490 |
-
# CALLS execute_tool() AGAIN
|
| 491 |
-
|
| 492 |
-
---
|
| 493 |
-
STEP 15: execute_tool() - Routes to create_order
|
| 494 |
-
|
| 495 |
-
FILE: chat/tools.py
|
| 496 |
-
LINE: 92-118
|
| 497 |
-
|
| 498 |
-
FUNCTION: execute_tool(tool_name, tool_input)
|
| 499 |
-
tool_name = "create_order"
|
| 500 |
-
tool_input = {
|
| 501 |
-
"customer_name": "John Doe",
|
| 502 |
-
"delivery_address": "123 Main St, San Francisco",
|
| 503 |
-
"delivery_lat": 37.7749,
|
| 504 |
-
"delivery_lng": -122.4194,
|
| 505 |
-
"priority": "standard"
|
| 506 |
-
}
|
| 507 |
-
|
| 508 |
-
Function Code:
|
| 509 |
-
def execute_tool(tool_name, tool_input):
|
| 510 |
-
try:
|
| 511 |
-
if tool_name == "geocode_address":
|
| 512 |
-
return handle_geocode_address(tool_input)
|
| 513 |
-
elif tool_name == "create_order":
|
| 514 |
-
return handle_create_order(tool_input)
|
| 515 |
-
# ↑
|
| 516 |
-
# CALLS THIS NEXT
|
| 517 |
-
|
| 518 |
-
---
|
| 519 |
-
STEP 16: handle_create_order()
|
| 520 |
-
|
| 521 |
-
FILE: chat/tools.py
|
| 522 |
-
LINE: 153-242
|
| 523 |
-
|
| 524 |
-
FUNCTION: handle_create_order(tool_input)
|
| 525 |
-
tool_input = {
|
| 526 |
-
"customer_name": "John Doe",
|
| 527 |
-
"delivery_address": "123 Main St, San Francisco",
|
| 528 |
-
"delivery_lat": 37.7749,
|
| 529 |
-
"delivery_lng": -122.4194,
|
| 530 |
-
"priority": "standard"
|
| 531 |
-
}
|
| 532 |
-
|
| 533 |
-
Function Code:
|
| 534 |
-
def handle_create_order(tool_input):
|
| 535 |
-
"""Execute order creation tool"""
|
| 536 |
-
|
| 537 |
-
# Extract fields with defaults
|
| 538 |
-
customer_name = tool_input.get("customer_name") # "John Doe"
|
| 539 |
-
customer_phone = tool_input.get("customer_phone") # None
|
| 540 |
-
customer_email = tool_input.get("customer_email") # None
|
| 541 |
-
delivery_address = tool_input.get("delivery_address") # "123
|
| 542 |
-
Main St, San Francisco"
|
| 543 |
-
delivery_lat = tool_input.get("delivery_lat") # 37.7749
|
| 544 |
-
delivery_lng = tool_input.get("delivery_lng") # -122.4194
|
| 545 |
-
priority = tool_input.get("priority", "standard") #
|
| 546 |
-
"standard"
|
| 547 |
-
special_instructions = tool_input.get("special_instructions")
|
| 548 |
-
# None
|
| 549 |
-
weight_kg = tool_input.get("weight_kg", 5.0) # 5.0
|
| 550 |
-
|
| 551 |
-
# Validate required fields
|
| 552 |
-
if not all([customer_name, delivery_address, delivery_lat,
|
| 553 |
-
delivery_lng]):
|
| 554 |
-
return {"success": False, "error": "Missing required
|
| 555 |
-
fields..."}
|
| 556 |
-
|
| 557 |
-
# Generate order ID
|
| 558 |
-
now = datetime.now()
|
| 559 |
-
order_id = f"ORD-{now.strftime('%Y%m%d%H%M%S')}"
|
| 560 |
-
# ↑
|
| 561 |
-
# e.g., "ORD-20251114015858"
|
| 562 |
-
|
| 563 |
-
# Handle time window
|
| 564 |
-
time_window_end = now + timedelta(hours=6) # 6 hours from now
|
| 565 |
-
time_window_start = now + timedelta(hours=2) # 2 hours from
|
| 566 |
-
now
|
| 567 |
-
|
| 568 |
-
# Insert into database
|
| 569 |
-
query = """
|
| 570 |
-
INSERT INTO orders (
|
| 571 |
-
order_id, customer_name, customer_phone,
|
| 572 |
-
customer_email,
|
| 573 |
-
delivery_address, delivery_lat, delivery_lng,
|
| 574 |
-
time_window_start, time_window_end,
|
| 575 |
-
priority, weight_kg, status, special_instructions
|
| 576 |
-
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
|
| 577 |
-
%s)
|
| 578 |
-
"""
|
| 579 |
-
|
| 580 |
-
params = (
|
| 581 |
-
order_id, # "ORD-20251114015858"
|
| 582 |
-
customer_name, # "John Doe"
|
| 583 |
-
customer_phone, # None
|
| 584 |
-
customer_email, # None
|
| 585 |
-
delivery_address, # "123 Main St, San Francisco"
|
| 586 |
-
delivery_lat, # 37.7749
|
| 587 |
-
delivery_lng, # -122.4194
|
| 588 |
-
time_window_start, # 2025-11-14 03:58:58
|
| 589 |
-
time_window_end, # 2025-11-14 07:58:58
|
| 590 |
-
priority, # "standard"
|
| 591 |
-
weight_kg, # 5.0
|
| 592 |
-
"pending", # status
|
| 593 |
-
special_instructions # None
|
| 594 |
-
)
|
| 595 |
-
|
| 596 |
-
try:
|
| 597 |
-
execute_write(query, params)
|
| 598 |
-
# ↑
|
| 599 |
-
# CALLS THIS NEXT - DATABASE WRITE!
|
| 600 |
-
|
| 601 |
-
---
|
| 602 |
-
STEP 17: execute_write() - INSERT INTO DATABASE
|
| 603 |
-
|
| 604 |
-
FILE: database/connection.py
|
| 605 |
-
LINE: 71-97
|
| 606 |
-
|
| 607 |
-
FUNCTION: execute_write(query, params)
|
| 608 |
-
query = "INSERT INTO orders (...) VALUES (%s, %s, ...)"
|
| 609 |
-
params = ("ORD-20251114015858", "John Doe", None, None, "123
|
| 610 |
-
Main St...", ...)
|
| 611 |
-
|
| 612 |
-
Function Code:
|
| 613 |
-
def execute_write(query, params=None):
|
| 614 |
-
"""Execute a write query (INSERT, UPDATE, DELETE)"""
|
| 615 |
-
try:
|
| 616 |
-
# Connect to PostgreSQL
|
| 617 |
-
conn = get_db_connection()
|
| 618 |
-
# ↑
|
| 619 |
-
# Opens connection to localhost:5432/fleetmind
|
| 620 |
-
|
| 621 |
-
logger.info("Database connection established:
|
| 622 |
-
fleetmind@localhost")
|
| 623 |
-
|
| 624 |
-
cursor = conn.cursor()
|
| 625 |
-
|
| 626 |
-
# Execute INSERT query
|
| 627 |
-
cursor.execute(query, params)
|
| 628 |
-
# ↑
|
| 629 |
-
# 💾 EXECUTES SQL:
|
| 630 |
-
# INSERT INTO orders (order_id, customer_name, ...)
|
| 631 |
-
# VALUES ('ORD-20251114015858', 'John Doe', ...)
|
| 632 |
-
|
| 633 |
-
conn.commit() # ← SAVES TO POSTGRESQL PERMANENTLY!
|
| 634 |
-
|
| 635 |
-
rows_affected = cursor.rowcount # 1
|
| 636 |
-
|
| 637 |
-
cursor.close()
|
| 638 |
-
conn.close()
|
| 639 |
-
|
| 640 |
-
logger.info("Database connection closed")
|
| 641 |
-
|
| 642 |
-
return rows_affected # Returns 1
|
| 643 |
-
|
| 644 |
-
DATABASE STATE:
|
| 645 |
-
-- New row added to orders table:
|
| 646 |
-
ORDER_ID: ORD-20251114015858
|
| 647 |
-
CUSTOMER_NAME: John Doe
|
| 648 |
-
CUSTOMER_PHONE: NULL
|
| 649 |
-
CUSTOMER_EMAIL: NULL
|
| 650 |
-
DELIVERY_ADDRESS: 123 Main St, San Francisco
|
| 651 |
-
DELIVERY_LAT: 37.7749
|
| 652 |
-
DELIVERY_LNG: -122.4194
|
| 653 |
-
STATUS: pending
|
| 654 |
-
PRIORITY: standard
|
| 655 |
-
WEIGHT_KG: 5.0
|
| 656 |
-
CREATED_AT: 2025-11-14 01:58:58
|
| 657 |
-
|
| 658 |
-
---
|
| 659 |
-
STEP 18: Back to handle_create_order() - Success!
|
| 660 |
-
|
| 661 |
-
FILE: chat/tools.py
|
| 662 |
-
LINE: 224-242
|
| 663 |
-
|
| 664 |
-
execute_write(query, params) # Returned 1 (success)
|
| 665 |
-
|
| 666 |
-
logger.info(f"Order created: {order_id}")
|
| 667 |
-
# ↑
|
| 668 |
-
# LOGS: "Order created: ORD-20251114015858"
|
| 669 |
-
|
| 670 |
-
return {
|
| 671 |
-
"success": True,
|
| 672 |
-
"order_id": "ORD-20251114015858",
|
| 673 |
-
"status": "pending",
|
| 674 |
-
"customer": "John Doe",
|
| 675 |
-
"address": "123 Main St, San Francisco",
|
| 676 |
-
"deadline": "2025-11-14 07:58",
|
| 677 |
-
"priority": "standard",
|
| 678 |
-
"message": "Order ORD-20251114015858 created successfully!"
|
| 679 |
-
}
|
| 680 |
-
|
| 681 |
-
---
|
| 682 |
-
STEP 19: Back to _process_response() - Second Tool Complete
|
| 683 |
-
|
| 684 |
-
FILE: chat/providers/gemini_provider.py
|
| 685 |
-
LINE: 285-310
|
| 686 |
-
|
| 687 |
-
tool_result = {
|
| 688 |
-
"success": True,
|
| 689 |
-
"order_id": "ORD-20251114015858",
|
| 690 |
-
"status": "pending",
|
| 691 |
-
"customer": "John Doe",
|
| 692 |
-
...
|
| 693 |
-
}
|
| 694 |
-
|
| 695 |
-
# Track for transparency
|
| 696 |
-
tool_calls_made.append({
|
| 697 |
-
"tool": "create_order",
|
| 698 |
-
"input": {
|
| 699 |
-
"customer_name": "John Doe",
|
| 700 |
-
"delivery_address": "123 Main St, San Francisco",
|
| 701 |
-
"delivery_lat": 37.7749,
|
| 702 |
-
"delivery_lng": -122.4194
|
| 703 |
-
},
|
| 704 |
-
"result": tool_result
|
| 705 |
-
})
|
| 706 |
-
|
| 707 |
-
conversation.add_tool_result("create_order", function_args,
|
| 708 |
-
tool_result)
|
| 709 |
-
|
| 710 |
-
# Send function result back to Gemini
|
| 711 |
-
current_response = chat.send_message(
|
| 712 |
-
genai.protos.Content(
|
| 713 |
-
parts=[genai.protos.Part(
|
| 714 |
-
function_response=genai.protos.FunctionResponse(
|
| 715 |
-
name="create_order",
|
| 716 |
-
response={"result": tool_result}
|
| 717 |
-
)
|
| 718 |
-
)]
|
| 719 |
-
)
|
| 720 |
-
)
|
| 721 |
-
# ↑
|
| 722 |
-
# 🌐 API CALL TO GEMINI WITH ORDER CREATION RESULT
|
| 723 |
-
|
| 724 |
-
---
|
| 725 |
-
STEP 20: Gemini Generates Final Response
|
| 726 |
-
|
| 727 |
-
🌐 GOOGLE GEMINI API (External)
|
| 728 |
-
|
| 729 |
-
RECEIVES:
|
| 730 |
-
- Function: create_order
|
| 731 |
-
- Result: {
|
| 732 |
-
"success": True,
|
| 733 |
-
"order_id": "ORD-20251114015858",
|
| 734 |
-
"customer": "John Doe",
|
| 735 |
-
"address": "123 Main St, San Francisco"
|
| 736 |
-
}
|
| 737 |
-
|
| 738 |
-
AI ANALYZES:
|
| 739 |
-
"Perfect! Order successfully created in database!
|
| 740 |
-
Let me tell the user with a nice summary."
|
| 741 |
-
|
| 742 |
-
GENERATES TEXT RESPONSE:
|
| 743 |
-
"Awesome! I have created the order for John Doe.
|
| 744 |
-
|
| 745 |
-
**Here's a summary:**
|
| 746 |
-
• Order ID: ORD-20251114015858
|
| 747 |
-
• Customer: John Doe
|
| 748 |
-
• Address: 123 Main St, San Francisco
|
| 749 |
-
• Status: Pending
|
| 750 |
-
• Priority: Standard
|
| 751 |
-
|
| 752 |
-
The order has been successfully saved to the database!"
|
| 753 |
-
|
| 754 |
-
RETURNS TO CODE:
|
| 755 |
-
response = {
|
| 756 |
-
candidates: [{
|
| 757 |
-
content: {
|
| 758 |
-
parts: [{
|
| 759 |
-
text: "Awesome! I have created the order for John
|
| 760 |
-
Doe.\n\n**Here's a summary:**..."
|
| 761 |
-
}]
|
| 762 |
-
}
|
| 763 |
-
}]
|
| 764 |
-
}
|
| 765 |
-
|
| 766 |
-
---
|
| 767 |
-
STEP 21: _process_response() - Extract Final Text
|
| 768 |
-
|
| 769 |
-
FILE: chat/providers/gemini_provider.py
|
| 770 |
-
LINE: 272-356
|
| 771 |
-
|
| 772 |
-
# Loop detects no more function calls
|
| 773 |
-
logger.info(f"No more function calls after iteration 2")
|
| 774 |
-
|
| 775 |
-
# Extract text from final response
|
| 776 |
-
parts = current_response.candidates[0].content.parts
|
| 777 |
-
logger.info(f"Extracting text from {len(parts)} parts")
|
| 778 |
-
|
| 779 |
-
for idx, part in enumerate(parts):
|
| 780 |
-
if hasattr(part, 'text') and part.text:
|
| 781 |
-
logger.info(f"Part {idx} has text: {part.text[:50]}...")
|
| 782 |
-
final_text += part.text
|
| 783 |
-
|
| 784 |
-
# final_text = "Awesome! I have created the order for John Doe..."
|
| 785 |
-
|
| 786 |
-
logger.info(f"Returning response: {final_text[:100]}")
|
| 787 |
-
|
| 788 |
-
conversation.add_message("assistant", final_text)
|
| 789 |
-
|
| 790 |
-
return final_text, tool_calls_made
|
| 791 |
-
# ↑ ↑
|
| 792 |
-
# Response List of 2 tool calls [geocode, create_order]
|
| 793 |
-
|
| 794 |
-
RETURNS:
|
| 795 |
-
(
|
| 796 |
-
"Awesome! I have created the order for John Doe.\n\n**Here's a
|
| 797 |
-
summary:**...",
|
| 798 |
-
[
|
| 799 |
-
{"tool": "geocode_address", "input": {...}, "result":
|
| 800 |
-
{...}},
|
| 801 |
-
{"tool": "create_order", "input": {...}, "result": {...}}
|
| 802 |
-
]
|
| 803 |
-
)
|
| 804 |
-
|
| 805 |
-
---
|
| 806 |
-
STEP 22: Back Through All Functions
|
| 807 |
-
|
| 808 |
-
← Returns to: GeminiProvider.process_message() (line 206)
|
| 809 |
-
← Returns to: ChatEngine.process_message() (line 58)
|
| 810 |
-
← Returns to: handle_chat_message() (line 223)
|
| 811 |
-
← Returns to: send_message() (line 443)
|
| 812 |
-
← Returns to: Gradio UI (line 448)
|
| 813 |
-
|
| 814 |
-
---
|
| 815 |
-
STEP 23: Gradio Updates UI
|
| 816 |
-
|
| 817 |
-
FILE: ui/app.py
|
| 818 |
-
LINE: 448-452
|
| 819 |
-
|
| 820 |
-
send_btn.click(
|
| 821 |
-
fn=send_message,
|
| 822 |
-
outputs=[chatbot, tool_display, conversation_state, msg_input]
|
| 823 |
-
# ↑ ↑
|
| 824 |
-
# Updates Shows tool calls
|
| 825 |
-
)
|
| 826 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both Gradio UI and MCP SSE server together
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import subprocess
|
| 7 |
-
import sys
|
| 8 |
-
import time
|
| 9 |
-
import signal
|
| 10 |
-
import os
|
| 11 |
-
|
| 12 |
-
# Store processes for cleanup
|
| 13 |
-
processes = []
|
| 14 |
-
|
| 15 |
-
def signal_handler(sig, frame):
|
| 16 |
-
"""Handle shutdown gracefully"""
|
| 17 |
-
print("\n\n👋 Shutting down FleetMind...")
|
| 18 |
-
for proc in processes:
|
| 19 |
-
proc.terminate()
|
| 20 |
-
sys.exit(0)
|
| 21 |
-
|
| 22 |
-
# Register signal handler
|
| 23 |
-
signal.signal(signal.SIGINT, signal_handler)
|
| 24 |
-
signal.signal(signal.SIGTERM, signal_handler)
|
| 25 |
-
|
| 26 |
-
print("=" * 70)
|
| 27 |
-
print("FleetMind - Unified Launcher (Gradio UI + MCP SSE Server)")
|
| 28 |
-
print("=" * 70)
|
| 29 |
-
|
| 30 |
-
# Start MCP SSE server (app.py) in background
|
| 31 |
-
print("\n[1/2] Starting MCP SSE server...")
|
| 32 |
-
mcp_process = subprocess.Popen(
|
| 33 |
-
[sys.executable, "app.py"],
|
| 34 |
-
stdout=subprocess.PIPE,
|
| 35 |
-
stderr=subprocess.STDOUT,
|
| 36 |
-
text=True,
|
| 37 |
-
bufsize=1
|
| 38 |
-
)
|
| 39 |
-
processes.append(mcp_process)
|
| 40 |
-
print("✅ MCP SSE server started (background)")
|
| 41 |
-
|
| 42 |
-
# Give MCP server time to initialize
|
| 43 |
-
time.sleep(2)
|
| 44 |
-
|
| 45 |
-
# Start Gradio UI (ui/app.py) in foreground
|
| 46 |
-
print("\n[2/2] Starting Gradio UI...")
|
| 47 |
-
print("=" * 70)
|
| 48 |
-
ui_process = subprocess.Popen(
|
| 49 |
-
[sys.executable, "ui/app.py"],
|
| 50 |
-
stdout=sys.stdout,
|
| 51 |
-
stderr=sys.stderr
|
| 52 |
-
)
|
| 53 |
-
processes.append(ui_process)
|
| 54 |
-
|
| 55 |
-
print("\n✅ Both services running!")
|
| 56 |
-
print("=" * 70)
|
| 57 |
-
print("📍 Gradio UI: http://0.0.0.0:7860")
|
| 58 |
-
print("📍 MCP SSE: http://0.0.0.0:7860/sse")
|
| 59 |
-
print("=" * 70)
|
| 60 |
-
|
| 61 |
-
# Wait for UI process (it blocks)
|
| 62 |
-
try:
|
| 63 |
-
ui_process.wait()
|
| 64 |
-
except KeyboardInterrupt:
|
| 65 |
-
signal_handler(None, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scripts/test_db.py
DELETED
|
@@ -1,169 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script to verify PostgreSQL database operations
|
| 3 |
-
"""
|
| 4 |
-
|
| 5 |
-
import sys
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
from datetime import datetime, timedelta
|
| 8 |
-
|
| 9 |
-
# Add parent directory to path
|
| 10 |
-
sys.path.insert(0, str(Path(__file__).parent.parent))
|
| 11 |
-
|
| 12 |
-
from database.connection import execute_query, execute_write
|
| 13 |
-
import logging
|
| 14 |
-
|
| 15 |
-
logging.basicConfig(level=logging.INFO)
|
| 16 |
-
logger = logging.getLogger(__name__)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def test_insert_order():
|
| 20 |
-
"""Test inserting a new order"""
|
| 21 |
-
logger.info("Testing order insertion...")
|
| 22 |
-
|
| 23 |
-
now = datetime.now()
|
| 24 |
-
time_window_start = now + timedelta(hours=2)
|
| 25 |
-
time_window_end = now + timedelta(hours=6)
|
| 26 |
-
|
| 27 |
-
query = """
|
| 28 |
-
INSERT INTO orders (
|
| 29 |
-
order_id, customer_name, customer_phone, customer_email,
|
| 30 |
-
delivery_address, delivery_lat, delivery_lng,
|
| 31 |
-
time_window_start, time_window_end,
|
| 32 |
-
priority, weight_kg, status
|
| 33 |
-
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
| 34 |
-
"""
|
| 35 |
-
|
| 36 |
-
params = (
|
| 37 |
-
"ORD-TEST-001",
|
| 38 |
-
"John Doe",
|
| 39 |
-
"+1-555-0123",
|
| 40 |
-
"john.doe@example.com",
|
| 41 |
-
"123 Main Street, San Francisco, CA 94103",
|
| 42 |
-
37.7749,
|
| 43 |
-
-122.4194,
|
| 44 |
-
time_window_start,
|
| 45 |
-
time_window_end,
|
| 46 |
-
"standard",
|
| 47 |
-
5.5,
|
| 48 |
-
"pending"
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
try:
|
| 52 |
-
result = execute_write(query, params)
|
| 53 |
-
logger.info(f"✓ Order inserted successfully (rows affected: {result})")
|
| 54 |
-
return True
|
| 55 |
-
except Exception as e:
|
| 56 |
-
logger.error(f"✗ Failed to insert order: {e}")
|
| 57 |
-
return False
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def test_query_orders():
|
| 61 |
-
"""Test querying orders"""
|
| 62 |
-
logger.info("Testing order query...")
|
| 63 |
-
|
| 64 |
-
query = "SELECT * FROM orders WHERE status = %s"
|
| 65 |
-
params = ("pending",)
|
| 66 |
-
|
| 67 |
-
try:
|
| 68 |
-
results = execute_query(query, params)
|
| 69 |
-
logger.info(f"✓ Query successful: Found {len(results)} pending orders")
|
| 70 |
-
|
| 71 |
-
for row in results:
|
| 72 |
-
logger.info(f" Order ID: {row['order_id']}")
|
| 73 |
-
logger.info(f" Customer: {row['customer_name']}")
|
| 74 |
-
logger.info(f" Address: {row['delivery_address']}")
|
| 75 |
-
logger.info(f" Priority: {row['priority']}")
|
| 76 |
-
logger.info(f" Status: {row['status']}")
|
| 77 |
-
logger.info(" ---")
|
| 78 |
-
|
| 79 |
-
return True
|
| 80 |
-
except Exception as e:
|
| 81 |
-
logger.error(f"✗ Failed to query orders: {e}")
|
| 82 |
-
return False
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def test_update_order():
|
| 86 |
-
"""Test updating an order"""
|
| 87 |
-
logger.info("Testing order update...")
|
| 88 |
-
|
| 89 |
-
query = "UPDATE orders SET status = %s, assigned_driver_id = %s WHERE order_id = %s"
|
| 90 |
-
params = ("assigned", "DRV-001", "ORD-TEST-001")
|
| 91 |
-
|
| 92 |
-
try:
|
| 93 |
-
result = execute_write(query, params)
|
| 94 |
-
logger.info(f"✓ Order updated successfully (rows affected: {result})")
|
| 95 |
-
|
| 96 |
-
# Verify update
|
| 97 |
-
verify_query = "SELECT status, assigned_driver_id FROM orders WHERE order_id = %s"
|
| 98 |
-
verify_result = execute_query(verify_query, ("ORD-TEST-001",))
|
| 99 |
-
|
| 100 |
-
if verify_result:
|
| 101 |
-
row = verify_result[0]
|
| 102 |
-
logger.info(f" New status: {row['status']}")
|
| 103 |
-
logger.info(f" Assigned driver: {row['assigned_driver_id']}")
|
| 104 |
-
|
| 105 |
-
return True
|
| 106 |
-
except Exception as e:
|
| 107 |
-
logger.error(f"✗ Failed to update order: {e}")
|
| 108 |
-
return False
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
def test_delete_order():
|
| 112 |
-
"""Test deleting the test order"""
|
| 113 |
-
logger.info("Testing order deletion (cleanup)...")
|
| 114 |
-
|
| 115 |
-
query = "DELETE FROM orders WHERE order_id = %s"
|
| 116 |
-
params = ("ORD-TEST-001",)
|
| 117 |
-
|
| 118 |
-
try:
|
| 119 |
-
result = execute_write(query, params)
|
| 120 |
-
logger.info(f"✓ Order deleted successfully (rows affected: {result})")
|
| 121 |
-
return True
|
| 122 |
-
except Exception as e:
|
| 123 |
-
logger.error(f"✗ Failed to delete order: {e}")
|
| 124 |
-
return False
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
def main():
|
| 128 |
-
"""Run all database tests"""
|
| 129 |
-
logger.info("=" * 50)
|
| 130 |
-
logger.info("Starting FleetMind PostgreSQL Database Tests")
|
| 131 |
-
logger.info("=" * 50)
|
| 132 |
-
|
| 133 |
-
tests = [
|
| 134 |
-
("Insert Order", test_insert_order),
|
| 135 |
-
("Query Orders", test_query_orders),
|
| 136 |
-
("Update Order", test_update_order),
|
| 137 |
-
("Delete Order", test_delete_order),
|
| 138 |
-
]
|
| 139 |
-
|
| 140 |
-
results = []
|
| 141 |
-
for test_name, test_func in tests:
|
| 142 |
-
logger.info(f"\n--- {test_name} ---")
|
| 143 |
-
success = test_func()
|
| 144 |
-
results.append((test_name, success))
|
| 145 |
-
|
| 146 |
-
# Summary
|
| 147 |
-
logger.info("\n" + "=" * 50)
|
| 148 |
-
logger.info("Test Summary")
|
| 149 |
-
logger.info("=" * 50)
|
| 150 |
-
|
| 151 |
-
passed = sum(1 for _, success in results if success)
|
| 152 |
-
total = len(results)
|
| 153 |
-
|
| 154 |
-
for test_name, success in results:
|
| 155 |
-
status = "✓ PASSED" if success else "✗ FAILED"
|
| 156 |
-
logger.info(f"{test_name}: {status}")
|
| 157 |
-
|
| 158 |
-
logger.info(f"\nTotal: {passed}/{total} tests passed")
|
| 159 |
-
|
| 160 |
-
if passed == total:
|
| 161 |
-
logger.info("\n🎉 All tests passed! Your PostgreSQL database is working correctly!")
|
| 162 |
-
return 0
|
| 163 |
-
else:
|
| 164 |
-
logger.error("\n❌ Some tests failed. Please check the errors above.")
|
| 165 |
-
return 1
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
if __name__ == "__main__":
|
| 169 |
-
sys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
|
server.py
CHANGED
|
@@ -18,6 +18,10 @@ from contextvars import ContextVar
|
|
| 18 |
# Add project root to path
|
| 19 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
from fastmcp import FastMCP
|
| 22 |
|
| 23 |
# Import existing services (unchanged)
|
|
@@ -32,7 +36,7 @@ logging.basicConfig(
|
|
| 32 |
level=logging.INFO,
|
| 33 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 34 |
handlers=[
|
| 35 |
-
logging.FileHandler('
|
| 36 |
logging.StreamHandler()
|
| 37 |
]
|
| 38 |
)
|
|
|
|
| 18 |
# Add project root to path
|
| 19 |
sys.path.insert(0, str(Path(__file__).parent))
|
| 20 |
|
| 21 |
+
# Ensure logs directory exists
|
| 22 |
+
logs_dir = Path(__file__).parent / 'logs'
|
| 23 |
+
logs_dir.mkdir(exist_ok=True)
|
| 24 |
+
|
| 25 |
from fastmcp import FastMCP
|
| 26 |
|
| 27 |
# Import existing services (unchanged)
|
|
|
|
| 36 |
level=logging.INFO,
|
| 37 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 38 |
handlers=[
|
| 39 |
+
logging.FileHandler(logs_dir / 'fleetmind_mcp.log'),
|
| 40 |
logging.StreamHandler()
|
| 41 |
]
|
| 42 |
)
|
test_assignment_system.py
DELETED
|
@@ -1,225 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script for FleetMind Assignment System
|
| 3 |
-
Tests all 4 assignment tools and cascading logic
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import sys
|
| 7 |
-
import os
|
| 8 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 9 |
-
|
| 10 |
-
from chat.tools import (
|
| 11 |
-
handle_create_order,
|
| 12 |
-
handle_create_driver,
|
| 13 |
-
handle_create_assignment,
|
| 14 |
-
handle_get_assignment_details,
|
| 15 |
-
handle_update_assignment,
|
| 16 |
-
handle_unassign_order,
|
| 17 |
-
handle_delete_order,
|
| 18 |
-
handle_delete_driver,
|
| 19 |
-
handle_update_order,
|
| 20 |
-
handle_update_driver
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
print("=" * 70)
|
| 24 |
-
print("FleetMind Assignment System Test")
|
| 25 |
-
print("=" * 70)
|
| 26 |
-
|
| 27 |
-
# Test 1: Create test order
|
| 28 |
-
print("\n[TEST 1] Creating test order...")
|
| 29 |
-
order_result = handle_create_order({
|
| 30 |
-
"customer_name": "Test Customer",
|
| 31 |
-
"customer_phone": "+8801712345678",
|
| 32 |
-
"delivery_address": "Tejgaon College, Dhaka",
|
| 33 |
-
"delivery_lat": 23.7549,
|
| 34 |
-
"delivery_lng": 90.3909,
|
| 35 |
-
"priority": "standard",
|
| 36 |
-
"weight_kg": 5.0
|
| 37 |
-
})
|
| 38 |
-
|
| 39 |
-
if order_result.get("success"):
|
| 40 |
-
order_id = order_result["order_id"]
|
| 41 |
-
print(f"SUCCESS: Order created: {order_id}")
|
| 42 |
-
print(f" Status: {order_result.get('status', 'N/A')}")
|
| 43 |
-
else:
|
| 44 |
-
print(f"FAILED: {order_result.get('error')}")
|
| 45 |
-
sys.exit(1)
|
| 46 |
-
|
| 47 |
-
# Test 2: Create test driver
|
| 48 |
-
print("\n[TEST 2] Creating test driver...")
|
| 49 |
-
driver_result = handle_create_driver({
|
| 50 |
-
"name": "Test Driver",
|
| 51 |
-
"phone": "+8801812345678",
|
| 52 |
-
"vehicle_type": "motorcycle",
|
| 53 |
-
"current_lat": 23.7808,
|
| 54 |
-
"current_lng": 90.4130
|
| 55 |
-
})
|
| 56 |
-
|
| 57 |
-
if driver_result.get("success"):
|
| 58 |
-
driver_id = driver_result["driver_id"]
|
| 59 |
-
print(f"SUCCESS: Driver created: {driver_id}")
|
| 60 |
-
print(f" Status: {driver_result.get('status', 'N/A')}")
|
| 61 |
-
else:
|
| 62 |
-
print(f"FAILED: {driver_result.get('error')}")
|
| 63 |
-
sys.exit(1)
|
| 64 |
-
|
| 65 |
-
# Test 3: Create assignment (assign order to driver)
|
| 66 |
-
print("\n[TEST 3] Creating assignment (assigning order to driver)...")
|
| 67 |
-
assignment_result = handle_create_assignment({
|
| 68 |
-
"order_id": order_id,
|
| 69 |
-
"driver_id": driver_id
|
| 70 |
-
})
|
| 71 |
-
|
| 72 |
-
if assignment_result.get("success"):
|
| 73 |
-
assignment_id = assignment_result["assignment_id"]
|
| 74 |
-
print(f"SUCCESS: Assignment created: {assignment_id}")
|
| 75 |
-
route = assignment_result.get("route", {})
|
| 76 |
-
if route:
|
| 77 |
-
distance = route.get('distance', 'N/A')
|
| 78 |
-
duration = route.get('duration', 'N/A')
|
| 79 |
-
summary = route.get('route_summary', 'N/A')
|
| 80 |
-
print(f" Route distance: {distance}")
|
| 81 |
-
print(f" Route duration: {duration}")
|
| 82 |
-
print(f" Route summary: {summary}")
|
| 83 |
-
else:
|
| 84 |
-
print(f"FAILED: {assignment_result.get('error')}")
|
| 85 |
-
sys.exit(1)
|
| 86 |
-
|
| 87 |
-
# Test 4: Get assignment details
|
| 88 |
-
print("\n[TEST 4] Getting assignment details...")
|
| 89 |
-
details_result = handle_get_assignment_details({
|
| 90 |
-
"assignment_id": assignment_id
|
| 91 |
-
})
|
| 92 |
-
|
| 93 |
-
if details_result.get("success"):
|
| 94 |
-
assignments = details_result.get("assignments", [])
|
| 95 |
-
if assignments:
|
| 96 |
-
asn = assignments[0]
|
| 97 |
-
print(f"SUCCESS: Found assignment {asn['assignment_id']}")
|
| 98 |
-
print(f" Order: {asn['order_id']} (Customer: {asn.get('customer_name', 'N/A')})")
|
| 99 |
-
print(f" Driver: {asn['driver_id']} (Name: {asn.get('driver_name', 'N/A')})")
|
| 100 |
-
print(f" Status: {asn['status']}")
|
| 101 |
-
print(f" Distance: {asn.get('route_distance_meters', 0)} meters")
|
| 102 |
-
print(f" Duration: {asn.get('route_duration_seconds', 0)} seconds")
|
| 103 |
-
else:
|
| 104 |
-
print(f"FAILED: {details_result.get('error')}")
|
| 105 |
-
|
| 106 |
-
# Test 5: Try to delete order with active assignment (should fail)
|
| 107 |
-
print("\n[TEST 5] Trying to delete order with active assignment (should fail)...")
|
| 108 |
-
delete_order_result = handle_delete_order({
|
| 109 |
-
"order_id": order_id,
|
| 110 |
-
"confirm": True
|
| 111 |
-
})
|
| 112 |
-
|
| 113 |
-
if not delete_order_result.get("success"):
|
| 114 |
-
print(f"SUCCESS: Deletion blocked as expected")
|
| 115 |
-
print(f" Error: {delete_order_result.get('error', 'N/A')[:100]}...")
|
| 116 |
-
else:
|
| 117 |
-
print(f"FAILED: Order deletion should have been blocked!")
|
| 118 |
-
|
| 119 |
-
# Test 6: Try to delete driver with active assignment (should fail)
|
| 120 |
-
print("\n[TEST 6] Trying to delete driver with active assignment (should fail)...")
|
| 121 |
-
delete_driver_result = handle_delete_driver({
|
| 122 |
-
"driver_id": driver_id,
|
| 123 |
-
"confirm": True
|
| 124 |
-
})
|
| 125 |
-
|
| 126 |
-
if not delete_driver_result.get("success"):
|
| 127 |
-
print(f"SUCCESS: Deletion blocked as expected")
|
| 128 |
-
print(f" Error: {delete_driver_result.get('error', 'N/A')[:100]}...")
|
| 129 |
-
else:
|
| 130 |
-
print(f"FAILED: Driver deletion should have been blocked!")
|
| 131 |
-
|
| 132 |
-
# Test 7: Update assignment to in_progress
|
| 133 |
-
print("\n[TEST 7] Updating assignment status to 'in_progress'...")
|
| 134 |
-
update_result = handle_update_assignment({
|
| 135 |
-
"assignment_id": assignment_id,
|
| 136 |
-
"status": "in_progress"
|
| 137 |
-
})
|
| 138 |
-
|
| 139 |
-
if update_result.get("success"):
|
| 140 |
-
print(f"SUCCESS: Assignment updated to in_progress")
|
| 141 |
-
if update_result.get("cascading_actions"):
|
| 142 |
-
print(f" Cascading actions: {update_result['cascading_actions']}")
|
| 143 |
-
else:
|
| 144 |
-
print(f"FAILED: {update_result.get('error')}")
|
| 145 |
-
|
| 146 |
-
# Test 8: Update assignment to completed
|
| 147 |
-
print("\n[TEST 8] Updating assignment status to 'completed'...")
|
| 148 |
-
update_result = handle_update_assignment({
|
| 149 |
-
"assignment_id": assignment_id,
|
| 150 |
-
"status": "completed"
|
| 151 |
-
})
|
| 152 |
-
|
| 153 |
-
if update_result.get("success"):
|
| 154 |
-
print(f"SUCCESS: Assignment completed")
|
| 155 |
-
if update_result.get("cascading_actions"):
|
| 156 |
-
print(f" Cascading actions: {update_result['cascading_actions']}")
|
| 157 |
-
else:
|
| 158 |
-
print(f"FAILED: {update_result.get('error')}")
|
| 159 |
-
|
| 160 |
-
# Test 9: Verify order status changed to 'delivered'
|
| 161 |
-
print("\n[TEST 9] Verifying order status changed to 'delivered'...")
|
| 162 |
-
from database.connection import get_db_connection
|
| 163 |
-
conn = get_db_connection()
|
| 164 |
-
cursor = conn.cursor()
|
| 165 |
-
cursor.execute("SELECT status FROM orders WHERE order_id = %s", (order_id,))
|
| 166 |
-
result = cursor.fetchone()
|
| 167 |
-
cursor.close()
|
| 168 |
-
conn.close()
|
| 169 |
-
|
| 170 |
-
if result and result['status'] == "delivered":
|
| 171 |
-
print(f"SUCCESS: Order status is 'delivered'")
|
| 172 |
-
else:
|
| 173 |
-
print(f"FAILED: Order status is '{result['status'] if result else 'NOT FOUND'}'")
|
| 174 |
-
|
| 175 |
-
# Test 10: Verify driver status changed back to 'active'
|
| 176 |
-
print("\n[TEST 10] Verifying driver status changed back to 'active'...")
|
| 177 |
-
conn = get_db_connection()
|
| 178 |
-
cursor = conn.cursor()
|
| 179 |
-
cursor.execute("SELECT status FROM drivers WHERE driver_id = %s", (driver_id,))
|
| 180 |
-
result = cursor.fetchone()
|
| 181 |
-
cursor.close()
|
| 182 |
-
conn.close()
|
| 183 |
-
|
| 184 |
-
if result and result['status'] == "active":
|
| 185 |
-
print(f"SUCCESS: Driver status is 'active'")
|
| 186 |
-
else:
|
| 187 |
-
print(f"FAILED: Driver status is '{result['status'] if result else 'NOT FOUND'}'")
|
| 188 |
-
|
| 189 |
-
# Test 11: Now delete order (should succeed - assignment is completed)
|
| 190 |
-
print("\n[TEST 11] Deleting order with completed assignment (should succeed)...")
|
| 191 |
-
delete_order_result = handle_delete_order({
|
| 192 |
-
"order_id": order_id,
|
| 193 |
-
"confirm": True
|
| 194 |
-
})
|
| 195 |
-
|
| 196 |
-
if delete_order_result.get("success"):
|
| 197 |
-
print(f"SUCCESS: Order deleted")
|
| 198 |
-
if delete_order_result.get("cascading_info"):
|
| 199 |
-
print(f" Cascading info: {delete_order_result['cascading_info']}")
|
| 200 |
-
else:
|
| 201 |
-
print(f"FAILED: {delete_order_result.get('error')}")
|
| 202 |
-
|
| 203 |
-
# Test 12: Now delete driver (should fail - has assignment history)
|
| 204 |
-
print("\n[TEST 12] Trying to delete driver with assignment history (should fail)...")
|
| 205 |
-
delete_driver_result = handle_delete_driver({
|
| 206 |
-
"driver_id": driver_id,
|
| 207 |
-
"confirm": True
|
| 208 |
-
})
|
| 209 |
-
|
| 210 |
-
if not delete_driver_result.get("success"):
|
| 211 |
-
print(f"SUCCESS: Deletion blocked (driver has assignment history)")
|
| 212 |
-
print(f" Total assignments: {delete_driver_result.get('total_assignments', 'N/A')}")
|
| 213 |
-
else:
|
| 214 |
-
print(f"NOTICE: Driver deleted (assignment was cascade deleted with order)")
|
| 215 |
-
|
| 216 |
-
print("\n" + "=" * 70)
|
| 217 |
-
print("Assignment System Test Complete")
|
| 218 |
-
print("=" * 70)
|
| 219 |
-
print("\nAll critical tests passed!")
|
| 220 |
-
print("\nKey Findings:")
|
| 221 |
-
print(" - Assignment creation works with route calculation")
|
| 222 |
-
print(" - Cascading status updates work correctly")
|
| 223 |
-
print(" - Safety checks prevent invalid deletions")
|
| 224 |
-
print(" - Assignment lifecycle management is functional")
|
| 225 |
-
print("\nAssignment system is ready for production use!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_auto_assignment.py
DELETED
|
@@ -1,185 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script for auto assignment feature
|
| 3 |
-
Verifies that auto assignment selects the nearest driver meeting all requirements:
|
| 4 |
-
- Nearest driver by real-time route distance
|
| 5 |
-
- Driver has sufficient vehicle capacity (weight & volume)
|
| 6 |
-
- Driver has required skills (fragile handling, cold storage)
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
import sys
|
| 10 |
-
import os
|
| 11 |
-
from datetime import datetime, timedelta
|
| 12 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 13 |
-
|
| 14 |
-
from chat.tools import (
|
| 15 |
-
handle_create_order,
|
| 16 |
-
handle_create_driver,
|
| 17 |
-
handle_auto_assign_order,
|
| 18 |
-
handle_delete_order,
|
| 19 |
-
handle_delete_driver
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
print("=" * 70)
|
| 23 |
-
print("Testing Auto Assignment Feature")
|
| 24 |
-
print("=" * 70)
|
| 25 |
-
|
| 26 |
-
# Test 1: Create test order (fragile, requires special handling)
|
| 27 |
-
print("\n[1] Creating test order (fragile package)...")
|
| 28 |
-
import time
|
| 29 |
-
expected_time = datetime.now() + timedelta(hours=2)
|
| 30 |
-
|
| 31 |
-
order_result = handle_create_order({
|
| 32 |
-
"customer_name": "Auto Assignment Test",
|
| 33 |
-
"customer_phone": "+8801712345670",
|
| 34 |
-
"delivery_address": "Bashundhara, Dhaka",
|
| 35 |
-
"delivery_lat": 23.8223,
|
| 36 |
-
"delivery_lng": 90.4259,
|
| 37 |
-
"expected_delivery_time": expected_time.isoformat(),
|
| 38 |
-
"priority": "urgent",
|
| 39 |
-
"weight_kg": 5.0,
|
| 40 |
-
"volume_m3": 0.5,
|
| 41 |
-
"is_fragile": True, # Requires fragile_handler skill
|
| 42 |
-
"requires_cold_storage": False
|
| 43 |
-
})
|
| 44 |
-
|
| 45 |
-
if not order_result.get("success"):
|
| 46 |
-
print(f"FAILED: {order_result.get('error')}")
|
| 47 |
-
sys.exit(1)
|
| 48 |
-
|
| 49 |
-
order_id = order_result["order_id"]
|
| 50 |
-
print(f"SUCCESS: Order created: {order_id}")
|
| 51 |
-
print(f" Location: Bashundhara, Dhaka (23.8223, 90.4259)")
|
| 52 |
-
print(f" Weight: 5kg, Volume: 0.5m³")
|
| 53 |
-
print(f" Fragile: YES (requires fragile_handler skill)")
|
| 54 |
-
|
| 55 |
-
# Test 2: Create multiple drivers at different distances
|
| 56 |
-
print("\n[2] Creating test drivers at various distances...")
|
| 57 |
-
|
| 58 |
-
# Driver 1: Closest but NO fragile_handler skill (should be rejected)
|
| 59 |
-
time.sleep(0.1)
|
| 60 |
-
driver1_result = handle_create_driver({
|
| 61 |
-
"name": "Nearest Driver (No Skill)",
|
| 62 |
-
"phone": "+8801812345671",
|
| 63 |
-
"vehicle_type": "motorcycle",
|
| 64 |
-
"current_lat": 23.8200, # Very close to delivery
|
| 65 |
-
"current_lng": 90.4250,
|
| 66 |
-
"capacity_kg": 10.0,
|
| 67 |
-
"capacity_m3": 1.0,
|
| 68 |
-
"skills": ["express_delivery"] # Missing fragile_handler
|
| 69 |
-
})
|
| 70 |
-
|
| 71 |
-
driver1_id = driver1_result["driver_id"]
|
| 72 |
-
print(f"Driver 1: {driver1_id} - Nearest (23.8200, 90.4250)")
|
| 73 |
-
print(f" Skills: express_delivery (NO fragile_handler)")
|
| 74 |
-
|
| 75 |
-
# Driver 2: Medium distance WITH fragile_handler skill (should be selected)
|
| 76 |
-
time.sleep(0.1)
|
| 77 |
-
driver2_result = handle_create_driver({
|
| 78 |
-
"name": "Medium Distance Driver (Has Skill)",
|
| 79 |
-
"phone": "+8801812345672",
|
| 80 |
-
"vehicle_type": "van",
|
| 81 |
-
"current_lat": 23.8000, # Medium distance
|
| 82 |
-
"current_lng": 90.4000,
|
| 83 |
-
"capacity_kg": 15.0,
|
| 84 |
-
"capacity_m3": 2.0,
|
| 85 |
-
"skills": ["fragile_handler", "express_delivery"] # Has fragile_handler
|
| 86 |
-
})
|
| 87 |
-
|
| 88 |
-
driver2_id = driver2_result["driver_id"]
|
| 89 |
-
print(f"Driver 2: {driver2_id} - Medium (23.8000, 90.4000)")
|
| 90 |
-
print(f" Skills: fragile_handler, express_delivery (HAS required skill)")
|
| 91 |
-
|
| 92 |
-
# Driver 3: Far away WITH fragile_handler but INSUFFICIENT capacity (should be rejected)
|
| 93 |
-
time.sleep(0.1)
|
| 94 |
-
driver3_result = handle_create_driver({
|
| 95 |
-
"name": "Far Driver (Low Capacity)",
|
| 96 |
-
"phone": "+8801812345673",
|
| 97 |
-
"vehicle_type": "motorcycle",
|
| 98 |
-
"current_lat": 23.7500, # Far away
|
| 99 |
-
"current_lng": 90.3500,
|
| 100 |
-
"capacity_kg": 3.0, # Too small for 5kg package
|
| 101 |
-
"capacity_m3": 0.3,
|
| 102 |
-
"skills": ["fragile_handler"] # Has skill but insufficient capacity
|
| 103 |
-
})
|
| 104 |
-
|
| 105 |
-
driver3_id = driver3_result["driver_id"]
|
| 106 |
-
print(f"Driver 3: {driver3_id} - Farthest (23.7500, 90.3500)")
|
| 107 |
-
print(f" Skills: fragile_handler BUT capacity only 3kg (package is 5kg)")
|
| 108 |
-
|
| 109 |
-
# Test 3: Run auto assignment
|
| 110 |
-
print("\n[3] Running auto assignment...")
|
| 111 |
-
auto_result = handle_auto_assign_order({"order_id": order_id})
|
| 112 |
-
|
| 113 |
-
if not auto_result.get("success"):
|
| 114 |
-
print(f"FAILED: {auto_result.get('error')}")
|
| 115 |
-
print("\nCleaning up...")
|
| 116 |
-
handle_delete_order({"order_id": order_id, "confirm": True})
|
| 117 |
-
handle_delete_driver({"driver_id": driver1_id, "confirm": True})
|
| 118 |
-
handle_delete_driver({"driver_id": driver2_id, "confirm": True})
|
| 119 |
-
handle_delete_driver({"driver_id": driver3_id, "confirm": True})
|
| 120 |
-
sys.exit(1)
|
| 121 |
-
|
| 122 |
-
print(f"SUCCESS: Auto assignment completed!")
|
| 123 |
-
print(f"\n Assignment ID: {auto_result['assignment_id']}")
|
| 124 |
-
print(f" Method: {auto_result['method']}")
|
| 125 |
-
print(f" Selected Driver: {auto_result['driver_id']} ({auto_result['driver_name']})")
|
| 126 |
-
print(f" Selection Reason: {auto_result['selection_reason']}")
|
| 127 |
-
print(f" Distance: {auto_result['distance_km']} km")
|
| 128 |
-
print(f" Estimated Duration: {auto_result['estimated_duration_minutes']} minutes")
|
| 129 |
-
print(f" Candidates Evaluated: {auto_result['candidates_evaluated']}")
|
| 130 |
-
print(f" Suitable Candidates: {auto_result['suitable_candidates']}")
|
| 131 |
-
|
| 132 |
-
# Test 4: Verify correct driver was selected
|
| 133 |
-
print("\n[4] Verifying selection logic...")
|
| 134 |
-
|
| 135 |
-
selected_driver_id = auto_result['driver_id']
|
| 136 |
-
|
| 137 |
-
if selected_driver_id == driver1_id:
|
| 138 |
-
print("FAILED: Selected Driver 1 (should have been rejected - missing skill)")
|
| 139 |
-
success = False
|
| 140 |
-
elif selected_driver_id == driver2_id:
|
| 141 |
-
print("SUCCESS: Selected Driver 2 (correct choice!)")
|
| 142 |
-
print(" [OK] Has fragile_handler skill")
|
| 143 |
-
print(" [OK] Has sufficient capacity (15kg > 5kg)")
|
| 144 |
-
print(" [OK] Nearest driver meeting ALL requirements")
|
| 145 |
-
success = True
|
| 146 |
-
elif selected_driver_id == driver3_id:
|
| 147 |
-
print("FAILED: Selected Driver 3 (should have been rejected - insufficient capacity)")
|
| 148 |
-
success = False
|
| 149 |
-
else:
|
| 150 |
-
print(f"UNEXPECTED: Selected unknown driver {selected_driver_id}")
|
| 151 |
-
success = False
|
| 152 |
-
|
| 153 |
-
# Test 5: Verify that unsuitable drivers were filtered out
|
| 154 |
-
print("\n[5] Verification Summary:")
|
| 155 |
-
print(f" Total drivers: 3")
|
| 156 |
-
print(f" Driver 1: [X] Rejected (missing fragile_handler skill)")
|
| 157 |
-
print(f" Driver 2: [OK] Selected (nearest with skill + capacity)")
|
| 158 |
-
print(f" Driver 3: [X] Rejected (insufficient capacity: 3kg < 5kg)")
|
| 159 |
-
print(f" Suitable candidates found: {auto_result['suitable_candidates']}")
|
| 160 |
-
|
| 161 |
-
if auto_result['suitable_candidates'] == 1:
|
| 162 |
-
print("SUCCESS: Correctly identified only 1 suitable driver!")
|
| 163 |
-
else:
|
| 164 |
-
print(f"WARNING: Expected 1 suitable candidate, got {auto_result['suitable_candidates']}")
|
| 165 |
-
|
| 166 |
-
# Cleanup
|
| 167 |
-
print("\n" + "=" * 70)
|
| 168 |
-
print("Cleaning up test data...")
|
| 169 |
-
handle_delete_order({"order_id": order_id, "confirm": True})
|
| 170 |
-
handle_delete_driver({"driver_id": driver1_id, "confirm": True})
|
| 171 |
-
handle_delete_driver({"driver_id": driver2_id, "confirm": True})
|
| 172 |
-
handle_delete_driver({"driver_id": driver3_id, "confirm": True})
|
| 173 |
-
print("Cleanup complete!")
|
| 174 |
-
|
| 175 |
-
print("\n" + "=" * 70)
|
| 176 |
-
print("Auto Assignment Test Complete!")
|
| 177 |
-
print("=" * 70)
|
| 178 |
-
print("\nSummary:")
|
| 179 |
-
print(" - Auto assignment selected nearest suitable driver: [OK]" if success else " - Auto assignment failed: [FAILED]")
|
| 180 |
-
print(" - Filtered out drivers missing required skills: [OK]")
|
| 181 |
-
print(" - Filtered out drivers with insufficient capacity: [OK]")
|
| 182 |
-
print(" - Used real-time routing for distance calculation: [OK]")
|
| 183 |
-
|
| 184 |
-
if not success:
|
| 185 |
-
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_complete_delivery.py
DELETED
|
@@ -1,178 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script for complete_delivery tool
|
| 3 |
-
Verifies that delivery completion updates driver location correctly
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import sys
|
| 7 |
-
import os
|
| 8 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 9 |
-
|
| 10 |
-
from chat.tools import (
|
| 11 |
-
handle_create_order,
|
| 12 |
-
handle_create_driver,
|
| 13 |
-
handle_create_assignment,
|
| 14 |
-
handle_complete_delivery,
|
| 15 |
-
handle_get_driver_details,
|
| 16 |
-
handle_get_assignment_details
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
print("=" * 70)
|
| 20 |
-
print("Testing Delivery Completion Workflow")
|
| 21 |
-
print("=" * 70)
|
| 22 |
-
|
| 23 |
-
# Step 1: Create test order
|
| 24 |
-
print("\n[1] Creating test order...")
|
| 25 |
-
order_result = handle_create_order({
|
| 26 |
-
"customer_name": "Completion Test Customer",
|
| 27 |
-
"customer_phone": "+8801712345678",
|
| 28 |
-
"delivery_address": "Ahsanullah University, Dhaka",
|
| 29 |
-
"delivery_lat": 23.7808,
|
| 30 |
-
"delivery_lng": 90.4130,
|
| 31 |
-
"priority": "standard",
|
| 32 |
-
"weight_kg": 3.0
|
| 33 |
-
})
|
| 34 |
-
|
| 35 |
-
if not order_result.get("success"):
|
| 36 |
-
print(f"FAILED: {order_result.get('error')}")
|
| 37 |
-
sys.exit(1)
|
| 38 |
-
|
| 39 |
-
order_id = order_result["order_id"]
|
| 40 |
-
print(f"SUCCESS: Order created: {order_id}")
|
| 41 |
-
|
| 42 |
-
# Step 2: Create test driver at different location
|
| 43 |
-
print("\n[2] Creating test driver at starting location...")
|
| 44 |
-
driver_result = handle_create_driver({
|
| 45 |
-
"name": "Completion Test Driver",
|
| 46 |
-
"phone": "+8801812345678",
|
| 47 |
-
"vehicle_type": "motorcycle",
|
| 48 |
-
"current_lat": 23.7549, # Different from delivery location
|
| 49 |
-
"current_lng": 90.3909
|
| 50 |
-
})
|
| 51 |
-
|
| 52 |
-
if not driver_result.get("success"):
|
| 53 |
-
print(f"FAILED: {driver_result.get('error')}")
|
| 54 |
-
sys.exit(1)
|
| 55 |
-
|
| 56 |
-
driver_id = driver_result["driver_id"]
|
| 57 |
-
driver_start_lat = 23.7549
|
| 58 |
-
driver_start_lng = 90.3909
|
| 59 |
-
print(f"SUCCESS: Driver created: {driver_id}")
|
| 60 |
-
print(f" Driver starting location: ({driver_start_lat}, {driver_start_lng})")
|
| 61 |
-
|
| 62 |
-
# Step 3: Create assignment
|
| 63 |
-
print("\n[3] Creating assignment...")
|
| 64 |
-
assignment_result = handle_create_assignment({
|
| 65 |
-
"order_id": order_id,
|
| 66 |
-
"driver_id": driver_id
|
| 67 |
-
})
|
| 68 |
-
|
| 69 |
-
if not assignment_result.get("success"):
|
| 70 |
-
print(f"FAILED: {assignment_result.get('error')}")
|
| 71 |
-
sys.exit(1)
|
| 72 |
-
|
| 73 |
-
assignment_id = assignment_result["assignment_id"]
|
| 74 |
-
print(f"SUCCESS: Assignment created: {assignment_id}")
|
| 75 |
-
|
| 76 |
-
# Step 4: Get driver details BEFORE completion
|
| 77 |
-
print("\n[4] Getting driver location BEFORE delivery completion...")
|
| 78 |
-
driver_before = handle_get_driver_details({"driver_id": driver_id})
|
| 79 |
-
|
| 80 |
-
if driver_before.get("success"):
|
| 81 |
-
driver_data = driver_before["driver"]
|
| 82 |
-
location = driver_data["location"]
|
| 83 |
-
print(f"Driver location BEFORE: ({location['latitude']}, {location['longitude']})")
|
| 84 |
-
print(f" Status: {driver_data['status']}")
|
| 85 |
-
else:
|
| 86 |
-
print(f"FAILED to get driver details")
|
| 87 |
-
|
| 88 |
-
# Step 5: Complete delivery
|
| 89 |
-
print("\n[5] Completing delivery...")
|
| 90 |
-
completion_result = handle_complete_delivery({
|
| 91 |
-
"assignment_id": assignment_id,
|
| 92 |
-
"confirm": True,
|
| 93 |
-
"actual_distance_meters": 4500,
|
| 94 |
-
"notes": "Delivered successfully to security desk"
|
| 95 |
-
})
|
| 96 |
-
|
| 97 |
-
if not completion_result.get("success"):
|
| 98 |
-
print(f"FAILED: {completion_result.get('error')}")
|
| 99 |
-
sys.exit(1)
|
| 100 |
-
|
| 101 |
-
print(f"SUCCESS: Delivery completed!")
|
| 102 |
-
print(f" Assignment ID: {completion_result['assignment_id']}")
|
| 103 |
-
print(f" Order ID: {completion_result['order_id']}")
|
| 104 |
-
print(f" Customer: {completion_result['customer_name']}")
|
| 105 |
-
print(f" Driver: {completion_result['driver_name']}")
|
| 106 |
-
print(f" Completed at: {completion_result['completed_at']}")
|
| 107 |
-
|
| 108 |
-
# Check driver location update
|
| 109 |
-
driver_updated = completion_result.get("driver_updated", {})
|
| 110 |
-
print(f"\nDriver location UPDATE:")
|
| 111 |
-
print(f" New location: {driver_updated.get('new_location', 'N/A')}")
|
| 112 |
-
print(f" Updated at: {driver_updated.get('location_updated_at', 'N/A')}")
|
| 113 |
-
|
| 114 |
-
# Cascading actions
|
| 115 |
-
cascading = completion_result.get("cascading_actions", [])
|
| 116 |
-
if cascading:
|
| 117 |
-
print(f"\nCascading actions:")
|
| 118 |
-
for action in cascading:
|
| 119 |
-
print(f" - {action}")
|
| 120 |
-
|
| 121 |
-
# Step 6: Get driver details AFTER completion to verify location changed
|
| 122 |
-
print("\n[6] Verifying driver location AFTER delivery completion...")
|
| 123 |
-
driver_after = handle_get_driver_details({"driver_id": driver_id})
|
| 124 |
-
|
| 125 |
-
if driver_after.get("success"):
|
| 126 |
-
driver_data = driver_after["driver"]
|
| 127 |
-
location = driver_data["location"]
|
| 128 |
-
after_lat = location['latitude']
|
| 129 |
-
after_lng = location['longitude']
|
| 130 |
-
|
| 131 |
-
print(f"Driver location AFTER: ({after_lat}, {after_lng})")
|
| 132 |
-
print(f" Status: {driver_data['status']}")
|
| 133 |
-
|
| 134 |
-
# Verify location changed
|
| 135 |
-
delivery_lat = 23.7808
|
| 136 |
-
delivery_lng = 90.4130
|
| 137 |
-
|
| 138 |
-
if abs(after_lat - delivery_lat) < 0.0001 and abs(after_lng - delivery_lng) < 0.0001:
|
| 139 |
-
print(f"\nSUCCESS: Driver location updated to delivery address!")
|
| 140 |
-
print(f" Expected: ({delivery_lat}, {delivery_lng})")
|
| 141 |
-
print(f" Got: ({after_lat}, {after_lng})")
|
| 142 |
-
else:
|
| 143 |
-
print(f"\nFAILED: Driver location NOT updated correctly")
|
| 144 |
-
print(f" Expected: ({delivery_lat}, {delivery_lng})")
|
| 145 |
-
print(f" Got: ({after_lat}, {after_lng})")
|
| 146 |
-
else:
|
| 147 |
-
print(f"FAILED to get driver details after completion")
|
| 148 |
-
|
| 149 |
-
# Step 7: Verify assignment status
|
| 150 |
-
print("\n[7] Verifying assignment status...")
|
| 151 |
-
assignment_details = handle_get_assignment_details({"assignment_id": assignment_id})
|
| 152 |
-
|
| 153 |
-
if assignment_details.get("success"):
|
| 154 |
-
assignment = assignment_details.get("assignment", {})
|
| 155 |
-
print(f"Assignment status: {assignment.get('status')}")
|
| 156 |
-
print(f"Actual arrival: {assignment.get('actual_arrival')}")
|
| 157 |
-
|
| 158 |
-
order = assignment.get("order", {})
|
| 159 |
-
print(f"Order status: {order.get('status')}")
|
| 160 |
-
|
| 161 |
-
if assignment.get('status') == 'completed' and order.get('status') == 'delivered':
|
| 162 |
-
print(f"\nSUCCESS: Assignment and order statuses updated correctly!")
|
| 163 |
-
else:
|
| 164 |
-
print(f"\nFAILED: Statuses not updated correctly")
|
| 165 |
-
else:
|
| 166 |
-
print(f"FAILED to get assignment details")
|
| 167 |
-
|
| 168 |
-
print("\n" + "=" * 70)
|
| 169 |
-
print("Delivery Completion Test Complete!")
|
| 170 |
-
print("=" * 70)
|
| 171 |
-
|
| 172 |
-
# Cleanup
|
| 173 |
-
print("\nCleaning up test data...")
|
| 174 |
-
from chat.tools import handle_delete_order, handle_delete_driver
|
| 175 |
-
|
| 176 |
-
handle_delete_order({"order_id": order_id, "confirm": True})
|
| 177 |
-
handle_delete_driver({"driver_id": driver_id, "confirm": True})
|
| 178 |
-
print("Cleanup complete!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_delivery_timing.py
DELETED
|
@@ -1,273 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script for delivery timing and SLA tracking
|
| 3 |
-
Verifies:
|
| 4 |
-
- expected_delivery_time is mandatory when creating orders
|
| 5 |
-
- On-time delivery detection
|
| 6 |
-
- Late delivery detection
|
| 7 |
-
- Very late delivery detection (SLA violation)
|
| 8 |
-
- Failed delivery timing tracking
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
import sys
|
| 12 |
-
import os
|
| 13 |
-
from datetime import datetime, timedelta
|
| 14 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 15 |
-
|
| 16 |
-
from chat.tools import (
|
| 17 |
-
handle_create_order,
|
| 18 |
-
handle_create_driver,
|
| 19 |
-
handle_create_assignment,
|
| 20 |
-
handle_complete_delivery,
|
| 21 |
-
handle_fail_delivery,
|
| 22 |
-
handle_get_order_details
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
print("=" * 70)
|
| 26 |
-
print("Testing Delivery Timing & SLA Tracking")
|
| 27 |
-
print("=" * 70)
|
| 28 |
-
|
| 29 |
-
# Test 1: Create order WITHOUT expected_delivery_time (should fail)
|
| 30 |
-
print("\n[1] Testing: Create order WITHOUT expected_delivery_time (should fail)...")
|
| 31 |
-
result = handle_create_order({
|
| 32 |
-
"customer_name": "Test Customer",
|
| 33 |
-
"delivery_address": "Test Address",
|
| 34 |
-
"delivery_lat": 23.7808,
|
| 35 |
-
"delivery_lng": 90.4130,
|
| 36 |
-
"priority": "standard"
|
| 37 |
-
})
|
| 38 |
-
|
| 39 |
-
if not result.get("success"):
|
| 40 |
-
print(f"EXPECTED FAILURE: {result.get('error')}")
|
| 41 |
-
if "expected_delivery_time" in result.get('error', '').lower():
|
| 42 |
-
print("SUCCESS: Correctly requires expected_delivery_time!")
|
| 43 |
-
else:
|
| 44 |
-
print("WARNING: Error message should mention expected_delivery_time")
|
| 45 |
-
else:
|
| 46 |
-
print("FAILED: Should have required expected_delivery_time!")
|
| 47 |
-
sys.exit(1)
|
| 48 |
-
|
| 49 |
-
# Test 2: Create order with PAST expected_delivery_time (should fail)
|
| 50 |
-
print("\n[2] Testing: Create order with PAST delivery time (should fail)...")
|
| 51 |
-
past_time = (datetime.now() - timedelta(hours=2)).isoformat()
|
| 52 |
-
result = handle_create_order({
|
| 53 |
-
"customer_name": "Test Customer",
|
| 54 |
-
"delivery_address": "Test Address",
|
| 55 |
-
"delivery_lat": 23.7808,
|
| 56 |
-
"delivery_lng": 90.4130,
|
| 57 |
-
"expected_delivery_time": past_time,
|
| 58 |
-
"priority": "standard"
|
| 59 |
-
})
|
| 60 |
-
|
| 61 |
-
if not result.get("success"):
|
| 62 |
-
print(f"EXPECTED FAILURE: {result.get('error')}")
|
| 63 |
-
if "future" in result.get('error', '').lower():
|
| 64 |
-
print("SUCCESS: Correctly validates expected_delivery_time must be in future!")
|
| 65 |
-
else:
|
| 66 |
-
print("WARNING: Error should mention time must be in future")
|
| 67 |
-
else:
|
| 68 |
-
print("FAILED: Should have rejected past delivery time!")
|
| 69 |
-
sys.exit(1)
|
| 70 |
-
|
| 71 |
-
# Test 3: Create valid order with expected_delivery_time
|
| 72 |
-
print("\n[3] Creating valid order with expected_delivery_time...")
|
| 73 |
-
# Set deadline to 2 hours from now
|
| 74 |
-
expected_time = datetime.now() + timedelta(hours=2)
|
| 75 |
-
order_result = handle_create_order({
|
| 76 |
-
"customer_name": "Timing Test Customer",
|
| 77 |
-
"customer_phone": "+8801712345678",
|
| 78 |
-
"delivery_address": "Bashundhara, Dhaka",
|
| 79 |
-
"delivery_lat": 23.8223,
|
| 80 |
-
"delivery_lng": 90.4259,
|
| 81 |
-
"expected_delivery_time": expected_time.isoformat(),
|
| 82 |
-
"priority": "standard",
|
| 83 |
-
"weight_kg": 2.5,
|
| 84 |
-
"sla_grace_period_minutes": 15
|
| 85 |
-
})
|
| 86 |
-
|
| 87 |
-
if not order_result.get("success"):
|
| 88 |
-
print(f"FAILED: {order_result.get('error')}")
|
| 89 |
-
sys.exit(1)
|
| 90 |
-
|
| 91 |
-
order_id = order_result["order_id"]
|
| 92 |
-
print(f"SUCCESS: Order created: {order_id}")
|
| 93 |
-
print(f" Expected delivery: {order_result.get('expected_delivery')}")
|
| 94 |
-
print(f" SLA grace period: {order_result.get('sla_grace_period_minutes')} minutes")
|
| 95 |
-
|
| 96 |
-
# Test 4: Create driver
|
| 97 |
-
print("\n[4] Creating test driver...")
|
| 98 |
-
import time
|
| 99 |
-
time.sleep(0.1) # Avoid ID collision
|
| 100 |
-
driver_result = handle_create_driver({
|
| 101 |
-
"name": "Timing Test Driver",
|
| 102 |
-
"phone": "+8801812345678",
|
| 103 |
-
"vehicle_type": "motorcycle",
|
| 104 |
-
"current_lat": 23.7549,
|
| 105 |
-
"current_lng": 90.3909
|
| 106 |
-
})
|
| 107 |
-
|
| 108 |
-
if not driver_result.get("success"):
|
| 109 |
-
print(f"FAILED: {driver_result.get('error')}")
|
| 110 |
-
sys.exit(1)
|
| 111 |
-
|
| 112 |
-
driver_id = driver_result["driver_id"]
|
| 113 |
-
print(f"SUCCESS: Driver created: {driver_id}")
|
| 114 |
-
|
| 115 |
-
# Test 5: Create assignment
|
| 116 |
-
print("\n[5] Creating assignment...")
|
| 117 |
-
assignment_result = handle_create_assignment({
|
| 118 |
-
"order_id": order_id,
|
| 119 |
-
"driver_id": driver_id
|
| 120 |
-
})
|
| 121 |
-
|
| 122 |
-
if not assignment_result.get("success"):
|
| 123 |
-
print(f"FAILED: {assignment_result.get('error')}")
|
| 124 |
-
sys.exit(1)
|
| 125 |
-
|
| 126 |
-
assignment_id = assignment_result["assignment_id"]
|
| 127 |
-
print(f"SUCCESS: Assignment created: {assignment_id}")
|
| 128 |
-
|
| 129 |
-
# Test 6: Simulate ON-TIME delivery (complete before expected_delivery_time)
|
| 130 |
-
print("\n[6] Testing ON-TIME delivery (before deadline)...")
|
| 131 |
-
# Since we can't control system time, we check the logic exists
|
| 132 |
-
completion_result = handle_complete_delivery({
|
| 133 |
-
"assignment_id": assignment_id,
|
| 134 |
-
"confirm": True,
|
| 135 |
-
"notes": "Delivered on time"
|
| 136 |
-
})
|
| 137 |
-
|
| 138 |
-
if not completion_result.get("success"):
|
| 139 |
-
print(f"FAILED: {completion_result.get('error')}")
|
| 140 |
-
sys.exit(1)
|
| 141 |
-
|
| 142 |
-
print(f"SUCCESS: Delivery completed!")
|
| 143 |
-
print(f" Delivery status: {completion_result.get('delivery_status')}")
|
| 144 |
-
print(f" Timing info: {completion_result.get('timing', {})}")
|
| 145 |
-
|
| 146 |
-
# Check timing fields
|
| 147 |
-
timing = completion_result.get('timing', {})
|
| 148 |
-
if 'expected_delivery_time' in timing and 'actual_delivery_time' in timing:
|
| 149 |
-
print("SUCCESS: Timing information tracked correctly!")
|
| 150 |
-
print(f" Expected: {timing.get('expected_delivery_time')}")
|
| 151 |
-
print(f" Actual: {timing.get('actual_delivery_time')}")
|
| 152 |
-
print(f" Status: {timing.get('status')}")
|
| 153 |
-
if 'delay_minutes' in timing:
|
| 154 |
-
print(f" Delay: {timing.get('delay_minutes')} minutes")
|
| 155 |
-
else:
|
| 156 |
-
print("FAILED: Missing timing information!")
|
| 157 |
-
|
| 158 |
-
# Test 7: Verify delivered_at was set (BUG FIX)
|
| 159 |
-
print("\n[7] Verifying delivered_at field was set (BUG FIX test)...")
|
| 160 |
-
order_details = handle_get_order_details({"order_id": order_id})
|
| 161 |
-
if order_details.get("success"):
|
| 162 |
-
order = order_details.get("order", {})
|
| 163 |
-
timing_data = order.get("timing", {})
|
| 164 |
-
delivered_at = timing_data.get("delivered_at")
|
| 165 |
-
delivery_status = order.get("delivery_status")
|
| 166 |
-
|
| 167 |
-
if delivered_at:
|
| 168 |
-
print(f"SUCCESS: delivered_at field was set: {delivered_at}")
|
| 169 |
-
else:
|
| 170 |
-
print("FAILED: delivered_at field was NOT set (BUG NOT FIXED!)")
|
| 171 |
-
|
| 172 |
-
if delivery_status:
|
| 173 |
-
print(f"SUCCESS: delivery_status field was set: {delivery_status}")
|
| 174 |
-
else:
|
| 175 |
-
print("WARNING: delivery_status field was NOT set")
|
| 176 |
-
else:
|
| 177 |
-
print(f"FAILED to get order details: {order_details.get('error')}")
|
| 178 |
-
|
| 179 |
-
# Cleanup
|
| 180 |
-
print("\n" + "=" * 70)
|
| 181 |
-
print("Cleaning up test data...")
|
| 182 |
-
from chat.tools import handle_delete_order, handle_delete_driver
|
| 183 |
-
|
| 184 |
-
handle_delete_order({"order_id": order_id, "confirm": True})
|
| 185 |
-
handle_delete_driver({"driver_id": driver_id, "confirm": True})
|
| 186 |
-
print("Cleanup complete!")
|
| 187 |
-
|
| 188 |
-
# Test 8: Test failed delivery timing
|
| 189 |
-
print("\n" + "=" * 70)
|
| 190 |
-
print("[8] Testing FAILED delivery with timing tracking...")
|
| 191 |
-
|
| 192 |
-
# Create new order
|
| 193 |
-
time.sleep(0.1)
|
| 194 |
-
expected_time2 = datetime.now() + timedelta(hours=1)
|
| 195 |
-
order_result2 = handle_create_order({
|
| 196 |
-
"customer_name": "Failed Timing Test",
|
| 197 |
-
"customer_phone": "+8801712345679",
|
| 198 |
-
"delivery_address": "Mirpur, Dhaka",
|
| 199 |
-
"delivery_lat": 23.8103,
|
| 200 |
-
"delivery_lng": 90.4125,
|
| 201 |
-
"expected_delivery_time": expected_time2.isoformat(),
|
| 202 |
-
"priority": "standard"
|
| 203 |
-
})
|
| 204 |
-
|
| 205 |
-
if not order_result2.get("success"):
|
| 206 |
-
print(f"FAILED: {order_result2.get('error')}")
|
| 207 |
-
sys.exit(1)
|
| 208 |
-
|
| 209 |
-
order_id2 = order_result2["order_id"]
|
| 210 |
-
print(f"Order created: {order_id2}")
|
| 211 |
-
|
| 212 |
-
# Create driver
|
| 213 |
-
time.sleep(0.1)
|
| 214 |
-
driver_result2 = handle_create_driver({
|
| 215 |
-
"name": "Failed Test Driver",
|
| 216 |
-
"phone": "+8801812345679",
|
| 217 |
-
"vehicle_type": "car",
|
| 218 |
-
"current_lat": 23.7465,
|
| 219 |
-
"current_lng": 90.3760
|
| 220 |
-
})
|
| 221 |
-
|
| 222 |
-
driver_id2 = driver_result2["driver_id"]
|
| 223 |
-
print(f"Driver created: {driver_id2}")
|
| 224 |
-
|
| 225 |
-
# Create assignment
|
| 226 |
-
assignment_result2 = handle_create_assignment({
|
| 227 |
-
"order_id": order_id2,
|
| 228 |
-
"driver_id": driver_id2
|
| 229 |
-
})
|
| 230 |
-
|
| 231 |
-
assignment_id2 = assignment_result2["assignment_id"]
|
| 232 |
-
print(f"Assignment created: {assignment_id2}")
|
| 233 |
-
|
| 234 |
-
# Fail delivery
|
| 235 |
-
failure_result = handle_fail_delivery({
|
| 236 |
-
"assignment_id": assignment_id2,
|
| 237 |
-
"current_lat": 23.7600,
|
| 238 |
-
"current_lng": 90.3850,
|
| 239 |
-
"failure_reason": "customer_not_available",
|
| 240 |
-
"confirm": True,
|
| 241 |
-
"notes": "Customer phone was off"
|
| 242 |
-
})
|
| 243 |
-
|
| 244 |
-
if failure_result.get("success"):
|
| 245 |
-
print(f"SUCCESS: Failure recorded!")
|
| 246 |
-
print(f" Delivery status: {failure_result.get('delivery_status')}")
|
| 247 |
-
print(f" Timing info: {failure_result.get('timing', {})}")
|
| 248 |
-
|
| 249 |
-
timing = failure_result.get('timing', {})
|
| 250 |
-
if 'expected_delivery_time' in timing and 'failure_time' in timing:
|
| 251 |
-
print("SUCCESS: Failure timing information tracked!")
|
| 252 |
-
print(f" Expected: {timing.get('expected_delivery_time')}")
|
| 253 |
-
print(f" Failed at: {timing.get('failure_time')}")
|
| 254 |
-
print(f" Status: {timing.get('status')}")
|
| 255 |
-
else:
|
| 256 |
-
print(f"FAILED: {failure_result.get('error')}")
|
| 257 |
-
|
| 258 |
-
# Cleanup
|
| 259 |
-
print("\nCleaning up failed delivery test data...")
|
| 260 |
-
handle_delete_order({"order_id": order_id2, "confirm": True})
|
| 261 |
-
handle_delete_driver({"driver_id": driver_id2, "confirm": True})
|
| 262 |
-
print("Cleanup complete!")
|
| 263 |
-
|
| 264 |
-
print("\n" + "=" * 70)
|
| 265 |
-
print("Delivery Timing & SLA Tracking Test Complete!")
|
| 266 |
-
print("=" * 70)
|
| 267 |
-
print("\nSummary:")
|
| 268 |
-
print(" - expected_delivery_time is mandatory: YES")
|
| 269 |
-
print(" - Past delivery times rejected: YES")
|
| 270 |
-
print(" - delivered_at field gets set: YES (BUG FIXED)")
|
| 271 |
-
print(" - delivery_status tracked: YES")
|
| 272 |
-
print(" - Timing information returned: YES")
|
| 273 |
-
print(" - Failed delivery timing tracked: YES")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_directions.py
DELETED
|
@@ -1,111 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Quick test to verify route directions are being stored
|
| 3 |
-
"""
|
| 4 |
-
|
| 5 |
-
import sys
|
| 6 |
-
import os
|
| 7 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 8 |
-
|
| 9 |
-
from chat.tools import (
|
| 10 |
-
handle_create_order,
|
| 11 |
-
handle_create_driver,
|
| 12 |
-
handle_create_assignment,
|
| 13 |
-
handle_get_assignment_details
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
print("=" * 70)
|
| 17 |
-
print("Testing Route Directions Storage")
|
| 18 |
-
print("=" * 70)
|
| 19 |
-
|
| 20 |
-
# Create test order
|
| 21 |
-
print("\n[1] Creating test order...")
|
| 22 |
-
order_result = handle_create_order({
|
| 23 |
-
"customer_name": "Direction Test Customer",
|
| 24 |
-
"customer_phone": "+8801712345678",
|
| 25 |
-
"delivery_address": "Tejgaon College, Dhaka",
|
| 26 |
-
"delivery_lat": 23.7549,
|
| 27 |
-
"delivery_lng": 90.3909,
|
| 28 |
-
"priority": "standard",
|
| 29 |
-
"weight_kg": 5.0
|
| 30 |
-
})
|
| 31 |
-
|
| 32 |
-
if not order_result.get("success"):
|
| 33 |
-
print(f"FAILED: {order_result.get('error')}")
|
| 34 |
-
sys.exit(1)
|
| 35 |
-
|
| 36 |
-
order_id = order_result["order_id"]
|
| 37 |
-
print(f"SUCCESS: Order created: {order_id}")
|
| 38 |
-
|
| 39 |
-
# Create test driver
|
| 40 |
-
print("\n[2] Creating test driver...")
|
| 41 |
-
driver_result = handle_create_driver({
|
| 42 |
-
"name": "Direction Test Driver",
|
| 43 |
-
"phone": "+8801812345678",
|
| 44 |
-
"vehicle_type": "motorcycle",
|
| 45 |
-
"current_lat": 23.7808,
|
| 46 |
-
"current_lng": 90.4130
|
| 47 |
-
})
|
| 48 |
-
|
| 49 |
-
if not driver_result.get("success"):
|
| 50 |
-
print(f"FAILED: {driver_result.get('error')}")
|
| 51 |
-
sys.exit(1)
|
| 52 |
-
|
| 53 |
-
driver_id = driver_result["driver_id"]
|
| 54 |
-
print(f"SUCCESS: Driver created: {driver_id}")
|
| 55 |
-
|
| 56 |
-
# Create assignment (this should now store directions)
|
| 57 |
-
print("\n[3] Creating assignment with route directions...")
|
| 58 |
-
assignment_result = handle_create_assignment({
|
| 59 |
-
"order_id": order_id,
|
| 60 |
-
"driver_id": driver_id
|
| 61 |
-
})
|
| 62 |
-
|
| 63 |
-
if not assignment_result.get("success"):
|
| 64 |
-
print(f"FAILED: {assignment_result.get('error')}")
|
| 65 |
-
sys.exit(1)
|
| 66 |
-
|
| 67 |
-
assignment_id = assignment_result["assignment_id"]
|
| 68 |
-
print(f"SUCCESS: Assignment created: {assignment_id}")
|
| 69 |
-
|
| 70 |
-
# Get assignment details to verify directions are stored
|
| 71 |
-
print("\n[4] Retrieving assignment details to check directions...")
|
| 72 |
-
details_result = handle_get_assignment_details({
|
| 73 |
-
"assignment_id": assignment_id
|
| 74 |
-
})
|
| 75 |
-
|
| 76 |
-
if not details_result.get("success"):
|
| 77 |
-
print(f"FAILED: {details_result.get('error')}")
|
| 78 |
-
sys.exit(1)
|
| 79 |
-
|
| 80 |
-
assignment = details_result.get("assignment", {})
|
| 81 |
-
route = assignment.get("route", {})
|
| 82 |
-
directions = route.get("directions")
|
| 83 |
-
|
| 84 |
-
if directions:
|
| 85 |
-
print(f"SUCCESS: Route directions are stored!")
|
| 86 |
-
print(f" Number of steps: {len(directions)}")
|
| 87 |
-
print(f"\n First 3 steps:")
|
| 88 |
-
for i, step in enumerate(directions[:3], 1):
|
| 89 |
-
instruction = step.get("instruction", "N/A")
|
| 90 |
-
# Distance might be int (meters) or dict with text
|
| 91 |
-
distance_val = step.get("distance", 0)
|
| 92 |
-
if isinstance(distance_val, dict):
|
| 93 |
-
distance = distance_val.get("text", "N/A")
|
| 94 |
-
else:
|
| 95 |
-
distance = f"{distance_val}m"
|
| 96 |
-
print(f" {i}. {instruction} ({distance})")
|
| 97 |
-
else:
|
| 98 |
-
print("WARNING: No directions found in assignment")
|
| 99 |
-
print(f" This might mean the Routes API didn't return step-by-step directions")
|
| 100 |
-
|
| 101 |
-
print("\n" + "=" * 70)
|
| 102 |
-
print("Test Complete!")
|
| 103 |
-
print("=" * 70)
|
| 104 |
-
|
| 105 |
-
# Cleanup
|
| 106 |
-
print("\nCleaning up test data...")
|
| 107 |
-
from chat.tools import handle_delete_order, handle_delete_driver
|
| 108 |
-
|
| 109 |
-
handle_delete_order({"order_id": order_id, "confirm": True})
|
| 110 |
-
handle_delete_driver({"driver_id": driver_id, "confirm": True})
|
| 111 |
-
print("Cleanup complete!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_driver_validation.py
DELETED
|
@@ -1,187 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script for driver creation validation
|
| 3 |
-
Verifies that drivers cannot be created without required fields:
|
| 4 |
-
- name
|
| 5 |
-
- vehicle_type
|
| 6 |
-
- current_lat
|
| 7 |
-
- current_lng
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
import sys
|
| 11 |
-
import os
|
| 12 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 13 |
-
|
| 14 |
-
from chat.tools import handle_create_driver
|
| 15 |
-
|
| 16 |
-
print("=" * 70)
|
| 17 |
-
print("Testing Driver Creation Validation")
|
| 18 |
-
print("=" * 70)
|
| 19 |
-
|
| 20 |
-
# Test 1: Create driver WITHOUT name (should fail)
|
| 21 |
-
print("\n[1] Testing: Create driver WITHOUT name (should fail)...")
|
| 22 |
-
result = handle_create_driver({
|
| 23 |
-
"vehicle_type": "motorcycle",
|
| 24 |
-
"current_lat": 23.8103,
|
| 25 |
-
"current_lng": 90.4125
|
| 26 |
-
})
|
| 27 |
-
|
| 28 |
-
if not result.get("success"):
|
| 29 |
-
print(f"EXPECTED FAILURE: {result.get('error')}")
|
| 30 |
-
if "name" in result.get('error', '').lower():
|
| 31 |
-
print("SUCCESS: Correctly requires name!")
|
| 32 |
-
else:
|
| 33 |
-
print("WARNING: Error message should mention 'name'")
|
| 34 |
-
else:
|
| 35 |
-
print(f"FAILED: Should have required name! Created: {result.get('driver_id')}")
|
| 36 |
-
sys.exit(1)
|
| 37 |
-
|
| 38 |
-
# Test 2: Create driver WITHOUT vehicle_type (should fail)
|
| 39 |
-
print("\n[2] Testing: Create driver WITHOUT vehicle_type (should fail)...")
|
| 40 |
-
result = handle_create_driver({
|
| 41 |
-
"name": "Test Driver",
|
| 42 |
-
"current_lat": 23.8103,
|
| 43 |
-
"current_lng": 90.4125
|
| 44 |
-
})
|
| 45 |
-
|
| 46 |
-
if not result.get("success"):
|
| 47 |
-
print(f"EXPECTED FAILURE: {result.get('error')}")
|
| 48 |
-
if "vehicle_type" in result.get('error', '').lower():
|
| 49 |
-
print("SUCCESS: Correctly requires vehicle_type!")
|
| 50 |
-
else:
|
| 51 |
-
print("WARNING: Error message should mention 'vehicle_type'")
|
| 52 |
-
else:
|
| 53 |
-
print(f"FAILED: Should have required vehicle_type! Created: {result.get('driver_id')}")
|
| 54 |
-
sys.exit(1)
|
| 55 |
-
|
| 56 |
-
# Test 3: Create driver WITHOUT current_lat (should fail)
|
| 57 |
-
print("\n[3] Testing: Create driver WITHOUT current_lat (should fail)...")
|
| 58 |
-
result = handle_create_driver({
|
| 59 |
-
"name": "Test Driver",
|
| 60 |
-
"vehicle_type": "motorcycle",
|
| 61 |
-
"current_lng": 90.4125
|
| 62 |
-
})
|
| 63 |
-
|
| 64 |
-
if not result.get("success"):
|
| 65 |
-
print(f"EXPECTED FAILURE: {result.get('error')}")
|
| 66 |
-
if "current_lat" in result.get('error', '').lower():
|
| 67 |
-
print("SUCCESS: Correctly requires current_lat!")
|
| 68 |
-
else:
|
| 69 |
-
print("WARNING: Error message should mention 'current_lat'")
|
| 70 |
-
else:
|
| 71 |
-
print(f"FAILED: Should have required current_lat! Created: {result.get('driver_id')}")
|
| 72 |
-
sys.exit(1)
|
| 73 |
-
|
| 74 |
-
# Test 4: Create driver WITHOUT current_lng (should fail)
|
| 75 |
-
print("\n[4] Testing: Create driver WITHOUT current_lng (should fail)...")
|
| 76 |
-
result = handle_create_driver({
|
| 77 |
-
"name": "Test Driver",
|
| 78 |
-
"vehicle_type": "motorcycle",
|
| 79 |
-
"current_lat": 23.8103
|
| 80 |
-
})
|
| 81 |
-
|
| 82 |
-
if not result.get("success"):
|
| 83 |
-
print(f"EXPECTED FAILURE: {result.get('error')}")
|
| 84 |
-
if "current_lng" in result.get('error', '').lower():
|
| 85 |
-
print("SUCCESS: Correctly requires current_lng!")
|
| 86 |
-
else:
|
| 87 |
-
print("WARNING: Error message should mention 'current_lng'")
|
| 88 |
-
else:
|
| 89 |
-
print(f"FAILED: Should have required current_lng! Created: {result.get('driver_id')}")
|
| 90 |
-
sys.exit(1)
|
| 91 |
-
|
| 92 |
-
# Test 5: Create driver with INVALID latitude (should fail)
|
| 93 |
-
print("\n[5] Testing: Create driver with invalid latitude (should fail)...")
|
| 94 |
-
result = handle_create_driver({
|
| 95 |
-
"name": "Test Driver",
|
| 96 |
-
"vehicle_type": "motorcycle",
|
| 97 |
-
"current_lat": 95.0, # Invalid - must be -90 to 90
|
| 98 |
-
"current_lng": 90.4125
|
| 99 |
-
})
|
| 100 |
-
|
| 101 |
-
if not result.get("success"):
|
| 102 |
-
print(f"EXPECTED FAILURE: {result.get('error')}")
|
| 103 |
-
if "latitude" in result.get('error', '').lower() or "-90" in result.get('error', ''):
|
| 104 |
-
print("SUCCESS: Correctly validates latitude range!")
|
| 105 |
-
else:
|
| 106 |
-
print("WARNING: Error message should mention latitude validation")
|
| 107 |
-
else:
|
| 108 |
-
print(f"FAILED: Should have rejected invalid latitude!")
|
| 109 |
-
sys.exit(1)
|
| 110 |
-
|
| 111 |
-
# Test 6: Create driver with INVALID longitude (should fail)
|
| 112 |
-
print("\n[6] Testing: Create driver with invalid longitude (should fail)...")
|
| 113 |
-
result = handle_create_driver({
|
| 114 |
-
"name": "Test Driver",
|
| 115 |
-
"vehicle_type": "motorcycle",
|
| 116 |
-
"current_lat": 23.8103,
|
| 117 |
-
"current_lng": 200.0 # Invalid - must be -180 to 180
|
| 118 |
-
})
|
| 119 |
-
|
| 120 |
-
if not result.get("success"):
|
| 121 |
-
print(f"EXPECTED FAILURE: {result.get('error')}")
|
| 122 |
-
if "longitude" in result.get('error', '').lower() or "-180" in result.get('error', ''):
|
| 123 |
-
print("SUCCESS: Correctly validates longitude range!")
|
| 124 |
-
else:
|
| 125 |
-
print("WARNING: Error message should mention longitude validation")
|
| 126 |
-
else:
|
| 127 |
-
print(f"FAILED: Should have rejected invalid longitude!")
|
| 128 |
-
sys.exit(1)
|
| 129 |
-
|
| 130 |
-
# Test 7: Create driver with NON-NUMERIC coordinates (should fail)
|
| 131 |
-
print("\n[7] Testing: Create driver with non-numeric coordinates (should fail)...")
|
| 132 |
-
result = handle_create_driver({
|
| 133 |
-
"name": "Test Driver",
|
| 134 |
-
"vehicle_type": "motorcycle",
|
| 135 |
-
"current_lat": "not a number",
|
| 136 |
-
"current_lng": 90.4125
|
| 137 |
-
})
|
| 138 |
-
|
| 139 |
-
if not result.get("success"):
|
| 140 |
-
print(f"EXPECTED FAILURE: {result.get('error')}")
|
| 141 |
-
if "number" in result.get('error', '').lower() or "valid" in result.get('error', '').lower():
|
| 142 |
-
print("SUCCESS: Correctly validates coordinates are numbers!")
|
| 143 |
-
else:
|
| 144 |
-
print("WARNING: Error message should mention coordinates must be numbers")
|
| 145 |
-
else:
|
| 146 |
-
print(f"FAILED: Should have rejected non-numeric coordinates!")
|
| 147 |
-
sys.exit(1)
|
| 148 |
-
|
| 149 |
-
# Test 8: Create driver WITH all required fields (should succeed)
|
| 150 |
-
print("\n[8] Testing: Create driver with ALL required fields (should succeed)...")
|
| 151 |
-
result = handle_create_driver({
|
| 152 |
-
"name": "Valid Test Driver",
|
| 153 |
-
"phone": "+8801812345678",
|
| 154 |
-
"vehicle_type": "motorcycle",
|
| 155 |
-
"current_lat": 23.8103,
|
| 156 |
-
"current_lng": 90.4125
|
| 157 |
-
})
|
| 158 |
-
|
| 159 |
-
if result.get("success"):
|
| 160 |
-
driver_id = result.get("driver_id")
|
| 161 |
-
print(f"SUCCESS: Driver created: {driver_id}")
|
| 162 |
-
print(f" Name: {result.get('name')}")
|
| 163 |
-
print(f" Vehicle: {result.get('vehicle_type')}")
|
| 164 |
-
print(f" Location: ({result.get('location', {}).get('latitude')}, {result.get('location', {}).get('longitude')})")
|
| 165 |
-
|
| 166 |
-
# Cleanup
|
| 167 |
-
print("\nCleaning up test driver...")
|
| 168 |
-
from chat.tools import handle_delete_driver
|
| 169 |
-
handle_delete_driver({"driver_id": driver_id, "confirm": True})
|
| 170 |
-
print("Cleanup complete!")
|
| 171 |
-
else:
|
| 172 |
-
print(f"FAILED: Should have created driver with all required fields!")
|
| 173 |
-
print(f"Error: {result.get('error')}")
|
| 174 |
-
sys.exit(1)
|
| 175 |
-
|
| 176 |
-
print("\n" + "=" * 70)
|
| 177 |
-
print("Driver Creation Validation Test Complete!")
|
| 178 |
-
print("=" * 70)
|
| 179 |
-
print("\nSummary:")
|
| 180 |
-
print(" - name is mandatory: YES")
|
| 181 |
-
print(" - vehicle_type is mandatory: YES")
|
| 182 |
-
print(" - current_lat is mandatory: YES")
|
| 183 |
-
print(" - current_lng is mandatory: YES")
|
| 184 |
-
print(" - Latitude range validated (-90 to 90): YES")
|
| 185 |
-
print(" - Longitude range validated (-180 to 180): YES")
|
| 186 |
-
print(" - Coordinates must be numeric: YES")
|
| 187 |
-
print(" - Valid driver can be created: YES")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_duplicate_assignment.py
DELETED
|
@@ -1,145 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script to verify duplicate assignment prevention
|
| 3 |
-
Ensures that an order cannot be assigned to multiple drivers simultaneously
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import sys
|
| 7 |
-
import os
|
| 8 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 9 |
-
|
| 10 |
-
from chat.tools import (
|
| 11 |
-
handle_create_order,
|
| 12 |
-
handle_create_driver,
|
| 13 |
-
handle_create_assignment
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
print("=" * 70)
|
| 17 |
-
print("Testing Duplicate Assignment Prevention")
|
| 18 |
-
print("=" * 70)
|
| 19 |
-
|
| 20 |
-
# Step 1: Create test order
|
| 21 |
-
print("\n[1] Creating test order...")
|
| 22 |
-
order_result = handle_create_order({
|
| 23 |
-
"customer_name": "Duplicate Test Customer",
|
| 24 |
-
"customer_phone": "+8801712345678",
|
| 25 |
-
"delivery_address": "Uttara, Dhaka",
|
| 26 |
-
"delivery_lat": 23.8759,
|
| 27 |
-
"delivery_lng": 90.3795,
|
| 28 |
-
"priority": "standard",
|
| 29 |
-
"weight_kg": 3.0
|
| 30 |
-
})
|
| 31 |
-
|
| 32 |
-
if not order_result.get("success"):
|
| 33 |
-
print(f"FAILED: {order_result.get('error')}")
|
| 34 |
-
sys.exit(1)
|
| 35 |
-
|
| 36 |
-
order_id = order_result["order_id"]
|
| 37 |
-
print(f"SUCCESS: Order created: {order_id}")
|
| 38 |
-
|
| 39 |
-
# Step 2: Create first driver
|
| 40 |
-
print("\n[2] Creating first driver...")
|
| 41 |
-
driver1_result = handle_create_driver({
|
| 42 |
-
"name": "Driver One",
|
| 43 |
-
"phone": "+8801812345678",
|
| 44 |
-
"vehicle_type": "motorcycle",
|
| 45 |
-
"current_lat": 23.8103,
|
| 46 |
-
"current_lng": 90.4125
|
| 47 |
-
})
|
| 48 |
-
|
| 49 |
-
if not driver1_result.get("success"):
|
| 50 |
-
print(f"FAILED: {driver1_result.get('error')}")
|
| 51 |
-
sys.exit(1)
|
| 52 |
-
|
| 53 |
-
driver1_id = driver1_result["driver_id"]
|
| 54 |
-
print(f"SUCCESS: Driver 1 created: {driver1_id} (Driver One)")
|
| 55 |
-
|
| 56 |
-
# Step 3: Create second driver (add small delay to avoid ID collision)
|
| 57 |
-
print("\n[3] Creating second driver...")
|
| 58 |
-
import time
|
| 59 |
-
time.sleep(0.1) # Small delay to ensure different timestamp
|
| 60 |
-
driver2_result = handle_create_driver({
|
| 61 |
-
"name": "Driver Two",
|
| 62 |
-
"phone": "+8801912345678",
|
| 63 |
-
"vehicle_type": "car",
|
| 64 |
-
"current_lat": 23.7465,
|
| 65 |
-
"current_lng": 90.3760
|
| 66 |
-
})
|
| 67 |
-
|
| 68 |
-
if not driver2_result.get("success"):
|
| 69 |
-
print(f"FAILED: {driver2_result.get('error')}")
|
| 70 |
-
sys.exit(1)
|
| 71 |
-
|
| 72 |
-
driver2_id = driver2_result["driver_id"]
|
| 73 |
-
print(f"SUCCESS: Driver 2 created: {driver2_id} (Driver Two)")
|
| 74 |
-
|
| 75 |
-
# Step 4: Assign order to first driver
|
| 76 |
-
print(f"\n[4] Assigning order {order_id} to Driver One...")
|
| 77 |
-
assignment1_result = handle_create_assignment({
|
| 78 |
-
"order_id": order_id,
|
| 79 |
-
"driver_id": driver1_id
|
| 80 |
-
})
|
| 81 |
-
|
| 82 |
-
if not assignment1_result.get("success"):
|
| 83 |
-
print(f"FAILED: {assignment1_result.get('error')}")
|
| 84 |
-
sys.exit(1)
|
| 85 |
-
|
| 86 |
-
assignment1_id = assignment1_result["assignment_id"]
|
| 87 |
-
print(f"SUCCESS: Assignment created: {assignment1_id}")
|
| 88 |
-
print(f" Order {order_id} assigned to Driver One")
|
| 89 |
-
|
| 90 |
-
# Step 5: Attempt to assign the same order to second driver (should fail)
|
| 91 |
-
print(f"\n[5] Attempting to assign same order to Driver Two (should fail)...")
|
| 92 |
-
assignment2_result = handle_create_assignment({
|
| 93 |
-
"order_id": order_id,
|
| 94 |
-
"driver_id": driver2_id
|
| 95 |
-
})
|
| 96 |
-
|
| 97 |
-
if not assignment2_result.get("success"):
|
| 98 |
-
error_msg = assignment2_result.get('error', '')
|
| 99 |
-
print(f"EXPECTED FAILURE: {error_msg}")
|
| 100 |
-
|
| 101 |
-
# Verify error message contains expected information
|
| 102 |
-
if "already assigned" in error_msg.lower() and "Driver One" in error_msg:
|
| 103 |
-
print(f"SUCCESS: Error message correctly identifies existing assignment!")
|
| 104 |
-
print(f" - Mentions order is already assigned")
|
| 105 |
-
print(f" - Shows driver name (Driver One)")
|
| 106 |
-
print(f" - Shows assignment ID ({assignment1_id})")
|
| 107 |
-
else:
|
| 108 |
-
print(f"WARNING: Error message could be more descriptive")
|
| 109 |
-
else:
|
| 110 |
-
print(f"FAILED: Should have prevented duplicate assignment!")
|
| 111 |
-
print(f" Unexpected assignment created: {assignment2_result.get('assignment_id')}")
|
| 112 |
-
sys.exit(1)
|
| 113 |
-
|
| 114 |
-
# Step 6: Attempt to assign same order to SAME driver again (should also fail)
|
| 115 |
-
print(f"\n[6] Attempting to assign same order to Driver One again (should also fail)...")
|
| 116 |
-
assignment3_result = handle_create_assignment({
|
| 117 |
-
"order_id": order_id,
|
| 118 |
-
"driver_id": driver1_id
|
| 119 |
-
})
|
| 120 |
-
|
| 121 |
-
if not assignment3_result.get("success"):
|
| 122 |
-
error_msg = assignment3_result.get('error', '')
|
| 123 |
-
print(f"EXPECTED FAILURE: {error_msg}")
|
| 124 |
-
print(f"SUCCESS: Correctly prevents reassigning to same driver!")
|
| 125 |
-
else:
|
| 126 |
-
print(f"FAILED: Should have prevented duplicate assignment to same driver!")
|
| 127 |
-
sys.exit(1)
|
| 128 |
-
|
| 129 |
-
print("\n" + "=" * 70)
|
| 130 |
-
print("Duplicate Assignment Prevention Test Complete!")
|
| 131 |
-
print("=" * 70)
|
| 132 |
-
print("\nSummary:")
|
| 133 |
-
print(" - Order can be assigned to a driver: YES")
|
| 134 |
-
print(" - Same order can be assigned to another driver: NO (prevented)")
|
| 135 |
-
print(" - Same order can be reassigned to same driver: NO (prevented)")
|
| 136 |
-
print(" - Error message is informative: YES")
|
| 137 |
-
|
| 138 |
-
# Cleanup
|
| 139 |
-
print("\nCleaning up test data...")
|
| 140 |
-
from chat.tools import handle_delete_order, handle_delete_driver
|
| 141 |
-
|
| 142 |
-
handle_delete_order({"order_id": order_id, "confirm": True})
|
| 143 |
-
handle_delete_driver({"driver_id": driver1_id, "confirm": True})
|
| 144 |
-
handle_delete_driver({"driver_id": driver2_id, "confirm": True})
|
| 145 |
-
print("Cleanup complete!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_fail_delivery.py
DELETED
|
@@ -1,230 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script for fail_delivery tool
|
| 3 |
-
Verifies that delivery failure requires location and reason, and updates driver location correctly
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import sys
|
| 7 |
-
import os
|
| 8 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 9 |
-
|
| 10 |
-
from chat.tools import (
|
| 11 |
-
handle_create_order,
|
| 12 |
-
handle_create_driver,
|
| 13 |
-
handle_create_assignment,
|
| 14 |
-
handle_fail_delivery,
|
| 15 |
-
handle_get_driver_details,
|
| 16 |
-
handle_get_assignment_details
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
print("=" * 70)
|
| 20 |
-
print("Testing Delivery Failure Workflow")
|
| 21 |
-
print("=" * 70)
|
| 22 |
-
|
| 23 |
-
# Step 1: Create test order
|
| 24 |
-
print("\n[1] Creating test order...")
|
| 25 |
-
order_result = handle_create_order({
|
| 26 |
-
"customer_name": "Failure Test Customer",
|
| 27 |
-
"customer_phone": "+8801712345678",
|
| 28 |
-
"delivery_address": "Dhanmondi 32, Dhaka",
|
| 29 |
-
"delivery_lat": 23.7465,
|
| 30 |
-
"delivery_lng": 90.3760,
|
| 31 |
-
"priority": "standard",
|
| 32 |
-
"weight_kg": 2.5
|
| 33 |
-
})
|
| 34 |
-
|
| 35 |
-
if not order_result.get("success"):
|
| 36 |
-
print(f"FAILED: {order_result.get('error')}")
|
| 37 |
-
sys.exit(1)
|
| 38 |
-
|
| 39 |
-
order_id = order_result["order_id"]
|
| 40 |
-
print(f"SUCCESS: Order created: {order_id}")
|
| 41 |
-
|
| 42 |
-
# Step 2: Create test driver at different location
|
| 43 |
-
print("\n[2] Creating test driver at starting location...")
|
| 44 |
-
driver_result = handle_create_driver({
|
| 45 |
-
"name": "Failure Test Driver",
|
| 46 |
-
"phone": "+8801812345678",
|
| 47 |
-
"vehicle_type": "motorcycle",
|
| 48 |
-
"current_lat": 23.8103, # Mirpur area
|
| 49 |
-
"current_lng": 90.4125
|
| 50 |
-
})
|
| 51 |
-
|
| 52 |
-
if not driver_result.get("success"):
|
| 53 |
-
print(f"FAILED: {driver_result.get('error')}")
|
| 54 |
-
sys.exit(1)
|
| 55 |
-
|
| 56 |
-
driver_id = driver_result["driver_id"]
|
| 57 |
-
driver_start_lat = 23.8103
|
| 58 |
-
driver_start_lng = 90.4125
|
| 59 |
-
print(f"SUCCESS: Driver created: {driver_id}")
|
| 60 |
-
print(f" Driver starting location: ({driver_start_lat}, {driver_start_lng})")
|
| 61 |
-
|
| 62 |
-
# Step 3: Create assignment
|
| 63 |
-
print("\n[3] Creating assignment...")
|
| 64 |
-
assignment_result = handle_create_assignment({
|
| 65 |
-
"order_id": order_id,
|
| 66 |
-
"driver_id": driver_id
|
| 67 |
-
})
|
| 68 |
-
|
| 69 |
-
if not assignment_result.get("success"):
|
| 70 |
-
print(f"FAILED: {assignment_result.get('error')}")
|
| 71 |
-
sys.exit(1)
|
| 72 |
-
|
| 73 |
-
assignment_id = assignment_result["assignment_id"]
|
| 74 |
-
print(f"SUCCESS: Assignment created: {assignment_id}")
|
| 75 |
-
|
| 76 |
-
# Step 4: Get driver details BEFORE failure
|
| 77 |
-
print("\n[4] Getting driver location BEFORE failure...")
|
| 78 |
-
driver_before = handle_get_driver_details({"driver_id": driver_id})
|
| 79 |
-
|
| 80 |
-
if driver_before.get("success"):
|
| 81 |
-
driver_data = driver_before["driver"]
|
| 82 |
-
location = driver_data["location"]
|
| 83 |
-
print(f"Driver location BEFORE: ({location['latitude']}, {location['longitude']})")
|
| 84 |
-
print(f" Status: {driver_data['status']}")
|
| 85 |
-
else:
|
| 86 |
-
print(f"FAILED to get driver details")
|
| 87 |
-
|
| 88 |
-
# Step 5: Test validation - attempt to fail without location
|
| 89 |
-
print("\n[5] Testing validation - attempting to fail without GPS location...")
|
| 90 |
-
fail_without_location = handle_fail_delivery({
|
| 91 |
-
"assignment_id": assignment_id,
|
| 92 |
-
"failure_reason": "customer_not_available",
|
| 93 |
-
"confirm": True
|
| 94 |
-
})
|
| 95 |
-
|
| 96 |
-
if not fail_without_location.get("success"):
|
| 97 |
-
print(f"EXPECTED FAILURE: {fail_without_location.get('error')}")
|
| 98 |
-
else:
|
| 99 |
-
print(f"UNEXPECTED: Should have failed without location!")
|
| 100 |
-
|
| 101 |
-
# Step 6: Test validation - attempt to fail without reason
|
| 102 |
-
print("\n[6] Testing validation - attempting to fail without reason...")
|
| 103 |
-
fail_without_reason = handle_fail_delivery({
|
| 104 |
-
"assignment_id": assignment_id,
|
| 105 |
-
"current_lat": 23.7500,
|
| 106 |
-
"current_lng": 90.3800,
|
| 107 |
-
"confirm": True
|
| 108 |
-
})
|
| 109 |
-
|
| 110 |
-
if not fail_without_reason.get("success"):
|
| 111 |
-
print(f"EXPECTED FAILURE: {fail_without_reason.get('error')}")
|
| 112 |
-
else:
|
| 113 |
-
print(f"UNEXPECTED: Should have failed without reason!")
|
| 114 |
-
|
| 115 |
-
# Step 7: Test validation - attempt with invalid reason
|
| 116 |
-
print("\n[7] Testing validation - attempting with invalid failure reason...")
|
| 117 |
-
fail_invalid_reason = handle_fail_delivery({
|
| 118 |
-
"assignment_id": assignment_id,
|
| 119 |
-
"current_lat": 23.7500,
|
| 120 |
-
"current_lng": 90.3800,
|
| 121 |
-
"failure_reason": "invalid_reason",
|
| 122 |
-
"confirm": True
|
| 123 |
-
})
|
| 124 |
-
|
| 125 |
-
if not fail_invalid_reason.get("success"):
|
| 126 |
-
print(f"EXPECTED FAILURE: {fail_invalid_reason.get('error')}")
|
| 127 |
-
else:
|
| 128 |
-
print(f"UNEXPECTED: Should have failed with invalid reason!")
|
| 129 |
-
|
| 130 |
-
# Step 8: Fail delivery properly with location and reason
|
| 131 |
-
print("\n[8] Failing delivery with proper location and reason...")
|
| 132 |
-
# Driver reports failure from a location along the route (between start and delivery)
|
| 133 |
-
failure_lat = 23.7750 # Somewhere between Mirpur and Dhanmondi
|
| 134 |
-
failure_lng = 90.3950
|
| 135 |
-
failure_reason = "customer_not_available"
|
| 136 |
-
|
| 137 |
-
failure_result = handle_fail_delivery({
|
| 138 |
-
"assignment_id": assignment_id,
|
| 139 |
-
"current_lat": failure_lat,
|
| 140 |
-
"current_lng": failure_lng,
|
| 141 |
-
"failure_reason": failure_reason,
|
| 142 |
-
"confirm": True,
|
| 143 |
-
"notes": "Customer phone was switched off. Attempted contact 3 times."
|
| 144 |
-
})
|
| 145 |
-
|
| 146 |
-
if not failure_result.get("success"):
|
| 147 |
-
print(f"FAILED: {failure_result.get('error')}")
|
| 148 |
-
sys.exit(1)
|
| 149 |
-
|
| 150 |
-
print(f"SUCCESS: Delivery marked as failed!")
|
| 151 |
-
print(f" Assignment ID: {failure_result['assignment_id']}")
|
| 152 |
-
print(f" Order ID: {failure_result['order_id']}")
|
| 153 |
-
print(f" Customer: {failure_result['customer_name']}")
|
| 154 |
-
print(f" Driver: {failure_result['driver_name']}")
|
| 155 |
-
print(f" Failed at: {failure_result['failed_at']}")
|
| 156 |
-
print(f" Failure reason: {failure_result['failure_reason_display']}")
|
| 157 |
-
|
| 158 |
-
# Check driver location update
|
| 159 |
-
driver_loc = failure_result.get("driver_location", {})
|
| 160 |
-
print(f"\nDriver location UPDATE:")
|
| 161 |
-
print(f" New location: ({driver_loc.get('lat')}, {driver_loc.get('lng')})")
|
| 162 |
-
print(f" Updated at: {driver_loc.get('updated_at')}")
|
| 163 |
-
|
| 164 |
-
# Cascading actions
|
| 165 |
-
cascading = failure_result.get("cascading_actions", [])
|
| 166 |
-
if cascading:
|
| 167 |
-
print(f"\nCascading actions:")
|
| 168 |
-
for action in cascading:
|
| 169 |
-
print(f" - {action}")
|
| 170 |
-
|
| 171 |
-
# Step 9: Get driver details AFTER failure to verify location changed
|
| 172 |
-
print("\n[9] Verifying driver location AFTER failure...")
|
| 173 |
-
driver_after = handle_get_driver_details({"driver_id": driver_id})
|
| 174 |
-
|
| 175 |
-
if driver_after.get("success"):
|
| 176 |
-
driver_data = driver_after["driver"]
|
| 177 |
-
location = driver_data["location"]
|
| 178 |
-
after_lat = location['latitude']
|
| 179 |
-
after_lng = location['longitude']
|
| 180 |
-
|
| 181 |
-
print(f"Driver location AFTER: ({after_lat}, {after_lng})")
|
| 182 |
-
print(f" Status: {driver_data['status']}")
|
| 183 |
-
|
| 184 |
-
# Verify location matches reported failure location
|
| 185 |
-
if abs(after_lat - failure_lat) < 0.0001 and abs(after_lng - failure_lng) < 0.0001:
|
| 186 |
-
print(f"\nSUCCESS: Driver location updated to reported failure position!")
|
| 187 |
-
print(f" Expected: ({failure_lat}, {failure_lng})")
|
| 188 |
-
print(f" Got: ({after_lat}, {after_lng})")
|
| 189 |
-
else:
|
| 190 |
-
print(f"\nFAILED: Driver location NOT updated correctly")
|
| 191 |
-
print(f" Expected: ({failure_lat}, {failure_lng})")
|
| 192 |
-
print(f" Got: ({after_lat}, {after_lng})")
|
| 193 |
-
else:
|
| 194 |
-
print(f"FAILED to get driver details after failure")
|
| 195 |
-
|
| 196 |
-
# Step 10: Verify assignment status and failure reason
|
| 197 |
-
print("\n[10] Verifying assignment status and failure reason...")
|
| 198 |
-
assignment_details = handle_get_assignment_details({"assignment_id": assignment_id})
|
| 199 |
-
|
| 200 |
-
if assignment_details.get("success"):
|
| 201 |
-
assignment = assignment_details.get("assignment", {})
|
| 202 |
-
print(f"Assignment status: {assignment.get('status')}")
|
| 203 |
-
print(f"Failure reason: {assignment.get('failure_reason')}")
|
| 204 |
-
print(f"Notes: {assignment.get('notes')}")
|
| 205 |
-
print(f"Actual arrival: {assignment.get('actual_arrival')}")
|
| 206 |
-
|
| 207 |
-
order = assignment.get("order", {})
|
| 208 |
-
print(f"Order status: {order.get('status')}")
|
| 209 |
-
|
| 210 |
-
if (assignment.get('status') == 'failed' and
|
| 211 |
-
order.get('status') == 'failed' and
|
| 212 |
-
assignment.get('failure_reason') == failure_reason):
|
| 213 |
-
print(f"\nSUCCESS: Assignment and order statuses updated correctly!")
|
| 214 |
-
print(f"SUCCESS: Failure reason stored correctly!")
|
| 215 |
-
else:
|
| 216 |
-
print(f"\nFAILED: Statuses or failure reason not updated correctly")
|
| 217 |
-
else:
|
| 218 |
-
print(f"FAILED to get assignment details")
|
| 219 |
-
|
| 220 |
-
print("\n" + "=" * 70)
|
| 221 |
-
print("Delivery Failure Test Complete!")
|
| 222 |
-
print("=" * 70)
|
| 223 |
-
|
| 224 |
-
# Cleanup
|
| 225 |
-
print("\nCleaning up test data...")
|
| 226 |
-
from chat.tools import handle_delete_order, handle_delete_driver
|
| 227 |
-
|
| 228 |
-
handle_delete_order({"order_id": order_id, "confirm": True})
|
| 229 |
-
handle_delete_driver({"driver_id": driver_id, "confirm": True})
|
| 230 |
-
print("Cleanup complete!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_intelligent_assignment.py
DELETED
|
@@ -1,232 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test script for intelligent assignment feature (Gemini 2.0 Flash AI-driven)
|
| 3 |
-
Verifies that intelligent assignment uses AI to make optimal decisions considering:
|
| 4 |
-
- Order priority, fragility, time constraints
|
| 5 |
-
- Driver skills, capacity, vehicle type
|
| 6 |
-
- Real-time routing (distance, traffic, weather)
|
| 7 |
-
- Complex tradeoffs and reasoning
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
import sys
|
| 11 |
-
import os
|
| 12 |
-
from datetime import datetime, timedelta
|
| 13 |
-
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 14 |
-
|
| 15 |
-
from chat.tools import (
|
| 16 |
-
handle_create_order,
|
| 17 |
-
handle_create_driver,
|
| 18 |
-
handle_intelligent_assign_order,
|
| 19 |
-
handle_delete_order,
|
| 20 |
-
handle_delete_driver
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
print("=" * 70)
|
| 24 |
-
print("Testing Intelligent Assignment Feature (Gemini 2.0 Flash AI)")
|
| 25 |
-
print("=" * 70)
|
| 26 |
-
|
| 27 |
-
# Check for GOOGLE_API_KEY
|
| 28 |
-
import os
|
| 29 |
-
if not os.getenv("GOOGLE_API_KEY"):
|
| 30 |
-
print("\nERROR: GOOGLE_API_KEY environment variable not set!")
|
| 31 |
-
print("Please set GOOGLE_API_KEY before running this test.")
|
| 32 |
-
sys.exit(1)
|
| 33 |
-
|
| 34 |
-
print("\n✓ GOOGLE_API_KEY found")
|
| 35 |
-
|
| 36 |
-
# Test 1: Create complex order (urgent, fragile, high value)
|
| 37 |
-
print("\n[1] Creating complex test order...")
|
| 38 |
-
import time
|
| 39 |
-
expected_time = datetime.now() + timedelta(hours=1)
|
| 40 |
-
|
| 41 |
-
order_result = handle_create_order({
|
| 42 |
-
"customer_name": "Intelligent Assignment Test",
|
| 43 |
-
"customer_phone": "+8801712345680",
|
| 44 |
-
"delivery_address": "Gulshan 2, Dhaka",
|
| 45 |
-
"delivery_lat": 23.7925,
|
| 46 |
-
"delivery_lng": 90.4078,
|
| 47 |
-
"expected_delivery_time": expected_time.isoformat(),
|
| 48 |
-
"priority": "urgent", # HIGH priority
|
| 49 |
-
"weight_kg": 8.0,
|
| 50 |
-
"volume_m3": 0.8,
|
| 51 |
-
"order_value": 50000, # High value package
|
| 52 |
-
"is_fragile": True, # Requires fragile_handler
|
| 53 |
-
"requires_cold_storage": False,
|
| 54 |
-
"requires_signature": True
|
| 55 |
-
})
|
| 56 |
-
|
| 57 |
-
if not order_result.get("success"):
|
| 58 |
-
print(f"FAILED: {order_result.get('error')}")
|
| 59 |
-
sys.exit(1)
|
| 60 |
-
|
| 61 |
-
order_id = order_result["order_id"]
|
| 62 |
-
print(f"SUCCESS: Order created: {order_id}")
|
| 63 |
-
print(f" Priority: URGENT")
|
| 64 |
-
print(f" Location: Gulshan 2, Dhaka (23.7925, 90.4078)")
|
| 65 |
-
print(f" Weight: 8kg, Volume: 0.8m³, Value: ৳50,000")
|
| 66 |
-
print(f" Fragile: YES, Signature Required: YES")
|
| 67 |
-
print(f" Expected delivery: {expected_time.strftime('%Y-%m-%d %H:%M')}")
|
| 68 |
-
|
| 69 |
-
# Test 2: Create diverse drivers with different characteristics
|
| 70 |
-
print("\n[2] Creating test drivers with varying profiles...")
|
| 71 |
-
|
| 72 |
-
# Driver A: Nearest, but motorcycle (smaller capacity, faster in traffic)
|
| 73 |
-
time.sleep(0.1)
|
| 74 |
-
driverA_result = handle_create_driver({
|
| 75 |
-
"name": "Speedy Motorcycle Driver",
|
| 76 |
-
"phone": "+8801812345681",
|
| 77 |
-
"vehicle_type": "motorcycle",
|
| 78 |
-
"current_lat": 23.7900, # Very close
|
| 79 |
-
"current_lng": 90.4050,
|
| 80 |
-
"capacity_kg": 10.0, # Just enough
|
| 81 |
-
"capacity_m3": 1.0,
|
| 82 |
-
"skills": ["fragile_handler", "express_delivery"]
|
| 83 |
-
})
|
| 84 |
-
|
| 85 |
-
driverA_id = driverA_result["driver_id"]
|
| 86 |
-
print(f"Driver A: {driverA_id}")
|
| 87 |
-
print(f" Type: Motorcycle (fast, good for urgent deliveries)")
|
| 88 |
-
print(f" Location: Very close (23.7900, 90.4050)")
|
| 89 |
-
print(f" Capacity: 10kg (adequate)")
|
| 90 |
-
print(f" Skills: fragile_handler, express_delivery")
|
| 91 |
-
|
| 92 |
-
# Driver B: Medium distance, van (larger capacity, more stable for fragile)
|
| 93 |
-
time.sleep(0.1)
|
| 94 |
-
driverB_result = handle_create_driver({
|
| 95 |
-
"name": "Reliable Van Driver",
|
| 96 |
-
"phone": "+8801812345682",
|
| 97 |
-
"vehicle_type": "van",
|
| 98 |
-
"current_lat": 23.7850, # Medium distance
|
| 99 |
-
"current_lng": 90.4000,
|
| 100 |
-
"capacity_kg": 50.0, # Much larger capacity
|
| 101 |
-
"capacity_m3": 5.0,
|
| 102 |
-
"skills": ["fragile_handler", "overnight"]
|
| 103 |
-
})
|
| 104 |
-
|
| 105 |
-
driverB_id = driverB_result["driver_id"]
|
| 106 |
-
print(f"Driver B: {driverB_id}")
|
| 107 |
-
print(f" Type: Van (stable, better for fragile high-value items)")
|
| 108 |
-
print(f" Location: Medium distance (23.7850, 90.4000)")
|
| 109 |
-
print(f" Capacity: 50kg (excellent)")
|
| 110 |
-
print(f" Skills: fragile_handler, overnight")
|
| 111 |
-
|
| 112 |
-
# Driver C: Far, but truck (huge capacity, slow)
|
| 113 |
-
time.sleep(0.1)
|
| 114 |
-
driverC_result = handle_create_driver({
|
| 115 |
-
"name": "Heavy Truck Driver",
|
| 116 |
-
"phone": "+8801812345683",
|
| 117 |
-
"vehicle_type": "truck",
|
| 118 |
-
"current_lat": 23.7500, # Far away
|
| 119 |
-
"current_lng": 90.3700,
|
| 120 |
-
"capacity_kg": 200.0, # Overkill for this package
|
| 121 |
-
"capacity_m3": 20.0,
|
| 122 |
-
"skills": ["fragile_handler"]
|
| 123 |
-
})
|
| 124 |
-
|
| 125 |
-
driverC_id = driverC_result["driver_id"]
|
| 126 |
-
print(f"Driver C: {driverC_id}")
|
| 127 |
-
print(f" Type: Truck (overkill capacity, slower)")
|
| 128 |
-
print(f" Location: Far away (23.7500, 90.3700)")
|
| 129 |
-
print(f" Capacity: 200kg (excessive for 8kg package)")
|
| 130 |
-
print(f" Skills: fragile_handler")
|
| 131 |
-
|
| 132 |
-
# Test 3: Run intelligent assignment
|
| 133 |
-
print("\n[3] Running intelligent assignment (AI decision-making)...")
|
| 134 |
-
print("Calling Gemini AI to analyze all parameters...")
|
| 135 |
-
|
| 136 |
-
intelligent_result = handle_intelligent_assign_order({"order_id": order_id})
|
| 137 |
-
|
| 138 |
-
if not intelligent_result.get("success"):
|
| 139 |
-
print(f"FAILED: {intelligent_result.get('error')}")
|
| 140 |
-
print("\nCleaning up...")
|
| 141 |
-
handle_delete_order({"order_id": order_id, "confirm": True})
|
| 142 |
-
handle_delete_driver({"driver_id": driverA_id, "confirm": True})
|
| 143 |
-
handle_delete_driver({"driver_id": driverB_id, "confirm": True})
|
| 144 |
-
handle_delete_driver({"driver_id": driverC_id, "confirm": True})
|
| 145 |
-
sys.exit(1)
|
| 146 |
-
|
| 147 |
-
print(f"\nSUCCESS: Intelligent assignment completed!")
|
| 148 |
-
print(f"\n Assignment ID: {intelligent_result['assignment_id']}")
|
| 149 |
-
print(f" Method: {intelligent_result['method']}")
|
| 150 |
-
print(f" AI Provider: {intelligent_result['ai_provider']}")
|
| 151 |
-
print(f" Selected Driver: {intelligent_result['driver_id']} ({intelligent_result['driver_name']})")
|
| 152 |
-
print(f" Distance: {intelligent_result['distance_km']} km")
|
| 153 |
-
print(f" Estimated Duration: {intelligent_result['estimated_duration_minutes']} minutes")
|
| 154 |
-
print(f" Candidates Evaluated: {intelligent_result['candidates_evaluated']}")
|
| 155 |
-
print(f" Confidence Score: {intelligent_result.get('confidence_score', 'N/A')}")
|
| 156 |
-
|
| 157 |
-
# Test 4: Display AI reasoning
|
| 158 |
-
print("\n[4] AI Reasoning & Decision Analysis:")
|
| 159 |
-
ai_reasoning = intelligent_result.get('ai_reasoning', {})
|
| 160 |
-
|
| 161 |
-
if ai_reasoning:
|
| 162 |
-
print("\n PRIMARY FACTORS:")
|
| 163 |
-
for factor in ai_reasoning.get('primary_factors', []):
|
| 164 |
-
print(f" • {factor}")
|
| 165 |
-
|
| 166 |
-
print("\n TRADE-OFFS CONSIDERED:")
|
| 167 |
-
for tradeoff in ai_reasoning.get('trade_offs_considered', []):
|
| 168 |
-
print(f" • {tradeoff}")
|
| 169 |
-
|
| 170 |
-
print(f"\n RISK ASSESSMENT:")
|
| 171 |
-
print(f" {ai_reasoning.get('risk_assessment', 'N/A')}")
|
| 172 |
-
|
| 173 |
-
print(f"\n DECISION SUMMARY:")
|
| 174 |
-
print(f" {ai_reasoning.get('decision_summary', 'N/A')}")
|
| 175 |
-
else:
|
| 176 |
-
print(" WARNING: No AI reasoning provided")
|
| 177 |
-
|
| 178 |
-
# Test 5: Display alternatives considered
|
| 179 |
-
print("\n[5] Alternative Drivers Considered:")
|
| 180 |
-
alternatives = intelligent_result.get('alternatives_considered', [])
|
| 181 |
-
if alternatives:
|
| 182 |
-
for i, alt in enumerate(alternatives, 1):
|
| 183 |
-
print(f" {i}. Driver {alt.get('driver_id')}: {alt.get('reason_not_selected')}")
|
| 184 |
-
else:
|
| 185 |
-
print(" No alternatives data provided")
|
| 186 |
-
|
| 187 |
-
# Test 6: Verify AI made a sensible decision
|
| 188 |
-
print("\n[6] Decision Validation:")
|
| 189 |
-
selected_driver_id = intelligent_result['driver_id']
|
| 190 |
-
|
| 191 |
-
print(f"\n Selected driver: {selected_driver_id}")
|
| 192 |
-
|
| 193 |
-
if selected_driver_id == driverA_id:
|
| 194 |
-
print(" → Driver A (Motorcycle)")
|
| 195 |
-
print(" Rationale: Likely prioritized URGENCY + proximity over vehicle comfort")
|
| 196 |
-
elif selected_driver_id == driverB_id:
|
| 197 |
-
print(" → Driver B (Van)")
|
| 198 |
-
print(" Rationale: Likely balanced FRAGILE handling + capacity + reasonable distance")
|
| 199 |
-
elif selected_driver_id == driverC_id:
|
| 200 |
-
print(" → Driver C (Truck)")
|
| 201 |
-
print(" Rationale: Unusual choice - truck is overkill for 8kg package")
|
| 202 |
-
else:
|
| 203 |
-
print(f" → Unknown driver: {selected_driver_id}")
|
| 204 |
-
|
| 205 |
-
print(f"\n AI Decision Quality:")
|
| 206 |
-
print(f" • Driver has required skills: ✅")
|
| 207 |
-
print(f" • Driver has sufficient capacity: ✅")
|
| 208 |
-
print(f" • AI provided reasoning: {'✅' if ai_reasoning else '❌'}")
|
| 209 |
-
print(f" • AI evaluated multiple candidates: ✅")
|
| 210 |
-
|
| 211 |
-
# Cleanup
|
| 212 |
-
print("\n" + "=" * 70)
|
| 213 |
-
print("Cleaning up test data...")
|
| 214 |
-
handle_delete_order({"order_id": order_id, "confirm": True})
|
| 215 |
-
handle_delete_driver({"driver_id": driverA_id, "confirm": True})
|
| 216 |
-
handle_delete_driver({"driver_id": driverB_id, "confirm": True})
|
| 217 |
-
handle_delete_driver({"driver_id": driverC_id, "confirm": True})
|
| 218 |
-
print("Cleanup complete!")
|
| 219 |
-
|
| 220 |
-
print("\n" + "=" * 70)
|
| 221 |
-
print("Intelligent Assignment Test Complete!")
|
| 222 |
-
print("=" * 70)
|
| 223 |
-
print("\nSummary:")
|
| 224 |
-
print(" - Gemini 2.0 Flash AI successfully made assignment decision: [OK]")
|
| 225 |
-
print(" - AI provided detailed reasoning: [OK]")
|
| 226 |
-
print(" - AI considered multiple factors (distance, capacity, urgency, fragility): [OK]")
|
| 227 |
-
print(" - AI evaluated all available drivers: [OK]")
|
| 228 |
-
print(" - Assignment created successfully: [OK]")
|
| 229 |
-
print("\nModel used: Gemini 2.0 Flash (gemini-2.0-flash-exp)")
|
| 230 |
-
print("\nNote: The AI's specific driver choice may vary based on real-time")
|
| 231 |
-
print("routing data, traffic conditions, and the AI's weighted evaluation.")
|
| 232 |
-
print("What matters is that the decision is EXPLAINED and REASONABLE.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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@
|
|
| 1 |
-
<!DOCTYPE html>
|
| 2 |
-
<html lang="en">
|
| 3 |
-
<head>
|
| 4 |
-
<meta charset="UTF-8">
|
| 5 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
-
<title>FleetMind OAuth Test - Stytch Login</title>
|
| 7 |
-
<style>
|
| 8 |
-
body {
|
| 9 |
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
|
| 10 |
-
max-width: 800px;
|
| 11 |
-
margin: 50px auto;
|
| 12 |
-
padding: 20px;
|
| 13 |
-
background: #0f172a;
|
| 14 |
-
color: #e2e8f0;
|
| 15 |
-
}
|
| 16 |
-
.container {
|
| 17 |
-
background: #1e293b;
|
| 18 |
-
padding: 40px;
|
| 19 |
-
border-radius: 12px;
|
| 20 |
-
box-shadow: 0 8px 16px rgba(0,0,0,0.4);
|
| 21 |
-
}
|
| 22 |
-
h1 { color: #f1f5f9; }
|
| 23 |
-
h2 { color: #e2e8f0; border-bottom: 2px solid #334155; padding-bottom: 10px; }
|
| 24 |
-
button {
|
| 25 |
-
background: #3b82f6;
|
| 26 |
-
color: white;
|
| 27 |
-
border: none;
|
| 28 |
-
padding: 12px 24px;
|
| 29 |
-
border-radius: 6px;
|
| 30 |
-
font-size: 16px;
|
| 31 |
-
cursor: pointer;
|
| 32 |
-
margin: 10px 5px;
|
| 33 |
-
}
|
| 34 |
-
button:hover { background: #2563eb; }
|
| 35 |
-
.success { background: #10b981; padding: 15px; border-radius: 6px; margin: 15px 0; }
|
| 36 |
-
.error { background: #ef4444; padding: 15px; border-radius: 6px; margin: 15px 0; }
|
| 37 |
-
.info { background: #1e3a5f; padding: 15px; border-radius: 6px; margin: 15px 0; border-left: 4px solid #3b82f6; }
|
| 38 |
-
code {
|
| 39 |
-
background: #334155;
|
| 40 |
-
color: #60a5fa;
|
| 41 |
-
padding: 3px 8px;
|
| 42 |
-
border-radius: 4px;
|
| 43 |
-
font-family: 'Courier New', monospace;
|
| 44 |
-
display: block;
|
| 45 |
-
margin: 10px 0;
|
| 46 |
-
word-wrap: break-word;
|
| 47 |
-
}
|
| 48 |
-
input {
|
| 49 |
-
width: 100%;
|
| 50 |
-
padding: 10px;
|
| 51 |
-
margin: 10px 0;
|
| 52 |
-
border-radius: 6px;
|
| 53 |
-
border: 1px solid #334155;
|
| 54 |
-
background: #0f172a;
|
| 55 |
-
color: #e2e8f0;
|
| 56 |
-
font-size: 14px;
|
| 57 |
-
}
|
| 58 |
-
#result { margin-top: 20px; }
|
| 59 |
-
</style>
|
| 60 |
-
<script src="https://js.stytch.com/stytch.js"></script>
|
| 61 |
-
</head>
|
| 62 |
-
<body>
|
| 63 |
-
<div class="container">
|
| 64 |
-
<h1>🔐 FleetMind OAuth Test</h1>
|
| 65 |
-
<p>Test Stytch authentication flow locally</p>
|
| 66 |
-
|
| 67 |
-
<div class="info">
|
| 68 |
-
<strong>📋 Test Flow:</strong><br>
|
| 69 |
-
1. Enter your email below<br>
|
| 70 |
-
2. Click "Send Magic Link"<br>
|
| 71 |
-
3. Check your email inbox<br>
|
| 72 |
-
4. Click the magic link<br>
|
| 73 |
-
5. You'll be redirected back here with a session token<br>
|
| 74 |
-
6. Use that token to test the MCP tools
|
| 75 |
-
</div>
|
| 76 |
-
|
| 77 |
-
<h2>Step 1: Login with Stytch</h2>
|
| 78 |
-
<div id="loginSection">
|
| 79 |
-
<input type="email" id="emailInput" placeholder="Enter your email address" value="">
|
| 80 |
-
<button onclick="sendMagicLink()">📧 Send Magic Link</button>
|
| 81 |
-
</div>
|
| 82 |
-
|
| 83 |
-
<h2>Step 2: Session Token</h2>
|
| 84 |
-
<div id="tokenSection">
|
| 85 |
-
<p>After clicking the magic link, your session token will appear here:</p>
|
| 86 |
-
<code id="tokenDisplay">Waiting for login...</code>
|
| 87 |
-
<button onclick="copyToken()" id="copyBtn" style="display:none;">📋 Copy Token</button>
|
| 88 |
-
</div>
|
| 89 |
-
|
| 90 |
-
<h2>Step 3: Test MCP Tools</h2>
|
| 91 |
-
<div id="testSection">
|
| 92 |
-
<p>Once you have a token, test calling an MCP tool:</p>
|
| 93 |
-
<button onclick="testCountOrders()" id="testBtn" disabled>🧪 Test count_orders Tool</button>
|
| 94 |
-
<button onclick="testCreateOrder()" id="createBtn" disabled>➕ Test create_order Tool</button>
|
| 95 |
-
</div>
|
| 96 |
-
|
| 97 |
-
<div id="result"></div>
|
| 98 |
-
</div>
|
| 99 |
-
|
| 100 |
-
<script>
|
| 101 |
-
const STYTCH_PUBLIC_TOKEN = 'public-token-test-ce2e3056-c04f-4416-a853-dd26810e14db'; // Your Stytch public token
|
| 102 |
-
const SERVER_URL = 'http://localhost:7860';
|
| 103 |
-
|
| 104 |
-
let sessionToken = null;
|
| 105 |
-
let stytchClient = null;
|
| 106 |
-
|
| 107 |
-
// Initialize Stytch
|
| 108 |
-
window.onload = function() {
|
| 109 |
-
stytchClient = window.Stytch(STYTCH_PUBLIC_TOKEN);
|
| 110 |
-
|
| 111 |
-
// Check if we're being redirected back from Stytch
|
| 112 |
-
const urlParams = new URLSearchParams(window.location.search);
|
| 113 |
-
const token = urlParams.get('stytch_token_type');
|
| 114 |
-
const tokenValue = urlParams.get('token');
|
| 115 |
-
|
| 116 |
-
if (token === 'magic_links' && tokenValue) {
|
| 117 |
-
authenticateToken(tokenValue);
|
| 118 |
-
}
|
| 119 |
-
};
|
| 120 |
-
|
| 121 |
-
async function sendMagicLink() {
|
| 122 |
-
const email = document.getElementById('emailInput').value;
|
| 123 |
-
if (!email) {
|
| 124 |
-
showResult('Please enter an email address', 'error');
|
| 125 |
-
return;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
showResult('Sending magic link to ' + email + '...', 'info');
|
| 129 |
-
|
| 130 |
-
try {
|
| 131 |
-
const response = await fetch('https://test.stytch.com/v1/magic_links/email/login_or_create', {
|
| 132 |
-
method: 'POST',
|
| 133 |
-
headers: {
|
| 134 |
-
'Content-Type': 'application/json',
|
| 135 |
-
},
|
| 136 |
-
body: JSON.stringify({
|
| 137 |
-
email: email,
|
| 138 |
-
login_magic_link_url: window.location.href,
|
| 139 |
-
signup_magic_link_url: window.location.href,
|
| 140 |
-
public_token: STYTCH_PUBLIC_TOKEN
|
| 141 |
-
})
|
| 142 |
-
});
|
| 143 |
-
|
| 144 |
-
const data = await response.json();
|
| 145 |
-
|
| 146 |
-
if (response.ok) {
|
| 147 |
-
showResult('✅ Magic link sent! Check your email (' + email + ') and click the link.', 'success');
|
| 148 |
-
} else {
|
| 149 |
-
showResult('❌ Error: ' + (data.error_message || 'Failed to send magic link'), 'error');
|
| 150 |
-
}
|
| 151 |
-
} catch (error) {
|
| 152 |
-
showResult('❌ Network error: ' + error.message, 'error');
|
| 153 |
-
}
|
| 154 |
-
}
|
| 155 |
-
|
| 156 |
-
async function authenticateToken(token) {
|
| 157 |
-
showResult('Authenticating your magic link token...', 'info');
|
| 158 |
-
|
| 159 |
-
try {
|
| 160 |
-
const response = await fetch('https://test.stytch.com/v1/magic_links/authenticate', {
|
| 161 |
-
method: 'POST',
|
| 162 |
-
headers: {
|
| 163 |
-
'Content-Type': 'application/json',
|
| 164 |
-
},
|
| 165 |
-
body: JSON.stringify({
|
| 166 |
-
token: token,
|
| 167 |
-
public_token: STYTCH_PUBLIC_TOKEN
|
| 168 |
-
})
|
| 169 |
-
});
|
| 170 |
-
|
| 171 |
-
const data = await response.json();
|
| 172 |
-
|
| 173 |
-
if (response.ok && data.session_token) {
|
| 174 |
-
sessionToken = data.session_token;
|
| 175 |
-
document.getElementById('tokenDisplay').textContent = sessionToken;
|
| 176 |
-
document.getElementById('copyBtn').style.display = 'inline-block';
|
| 177 |
-
document.getElementById('testBtn').disabled = false;
|
| 178 |
-
document.getElementById('createBtn').disabled = false;
|
| 179 |
-
showResult('✅ Login successful! You can now test the MCP tools below.', 'success');
|
| 180 |
-
} else {
|
| 181 |
-
showResult('❌ Authentication failed: ' + (data.error_message || 'Unknown error'), 'error');
|
| 182 |
-
}
|
| 183 |
-
} catch (error) {
|
| 184 |
-
showResult('❌ Authentication error: ' + error.message, 'error');
|
| 185 |
-
}
|
| 186 |
-
}
|
| 187 |
-
|
| 188 |
-
function copyToken() {
|
| 189 |
-
navigator.clipboard.writeText(sessionToken);
|
| 190 |
-
showResult('✅ Token copied to clipboard!', 'success');
|
| 191 |
-
}
|
| 192 |
-
|
| 193 |
-
async function testCountOrders() {
|
| 194 |
-
if (!sessionToken) {
|
| 195 |
-
showResult('❌ Please login first to get a session token', 'error');
|
| 196 |
-
return;
|
| 197 |
-
}
|
| 198 |
-
|
| 199 |
-
showResult('Testing count_orders with your session token...', 'info');
|
| 200 |
-
|
| 201 |
-
try {
|
| 202 |
-
// Note: This is a simplified test - actual MCP protocol is more complex
|
| 203 |
-
const response = await fetch(SERVER_URL + '/test-auth', {
|
| 204 |
-
method: 'POST',
|
| 205 |
-
headers: {
|
| 206 |
-
'Authorization': 'Bearer ' + sessionToken,
|
| 207 |
-
'Content-Type': 'application/json',
|
| 208 |
-
},
|
| 209 |
-
body: JSON.stringify({
|
| 210 |
-
tool: 'count_orders',
|
| 211 |
-
params: {}
|
| 212 |
-
})
|
| 213 |
-
});
|
| 214 |
-
|
| 215 |
-
const data = await response.json();
|
| 216 |
-
showResult('Response: ' + JSON.stringify(data, null, 2), 'success');
|
| 217 |
-
} catch (error) {
|
| 218 |
-
showResult('Note: Direct HTTP testing not available. Use the token in Claude Desktop instead.\n\nYour token: ' + sessionToken, 'info');
|
| 219 |
-
}
|
| 220 |
-
}
|
| 221 |
-
|
| 222 |
-
async function testCreateOrder() {
|
| 223 |
-
if (!sessionToken) {
|
| 224 |
-
showResult('❌ Please login first to get a session token', 'error');
|
| 225 |
-
return;
|
| 226 |
-
}
|
| 227 |
-
|
| 228 |
-
showResult('Testing create_order with your session token...', 'info');
|
| 229 |
-
showResult('Note: For full testing, configure Claude Desktop to use this token.\n\nYour session token:\n' + sessionToken, 'info');
|
| 230 |
-
}
|
| 231 |
-
|
| 232 |
-
function showResult(message, type) {
|
| 233 |
-
const resultDiv = document.getElementById('result');
|
| 234 |
-
resultDiv.className = type;
|
| 235 |
-
resultDiv.innerHTML = message.replace(/\n/g, '<br>');
|
| 236 |
-
}
|
| 237 |
-
</script>
|
| 238 |
-
</body>
|
| 239 |
-
</html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unified_app.py
DELETED
|
@@ -1,210 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
FleetMind Unified App
|
| 3 |
-
Serves both Gradio UI and MCP SSE endpoint on the same port
|
| 4 |
-
Simple UI showing MCP connection information and server status
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import sys
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
sys.path.insert(0, str(Path(__file__).parent))
|
| 10 |
-
|
| 11 |
-
from fastapi import FastAPI
|
| 12 |
-
import uvicorn
|
| 13 |
-
import gradio as gr
|
| 14 |
-
import os
|
| 15 |
-
import json
|
| 16 |
-
|
| 17 |
-
print("=" * 70)
|
| 18 |
-
print("FleetMind - Unified Server (Gradio UI + MCP SSE)")
|
| 19 |
-
print("=" * 70)
|
| 20 |
-
|
| 21 |
-
# Configuration
|
| 22 |
-
MCP_SSE_ENDPOINT = "https://mcp-1st-birthday-fleetmind-dispatch-ai.hf.space/sse"
|
| 23 |
-
|
| 24 |
-
# Import MCP server
|
| 25 |
-
print("\n[1/2] Loading MCP server...")
|
| 26 |
-
try:
|
| 27 |
-
from server import mcp
|
| 28 |
-
print("[OK] MCP server loaded (29 tools, 2 resources)")
|
| 29 |
-
mcp_available = True
|
| 30 |
-
except Exception as e:
|
| 31 |
-
print(f"[WARNING] MCP server failed to load: {e}")
|
| 32 |
-
mcp_available = False
|
| 33 |
-
|
| 34 |
-
def get_claude_config():
|
| 35 |
-
"""Generate Claude Desktop configuration"""
|
| 36 |
-
config = {
|
| 37 |
-
"mcpServers": {
|
| 38 |
-
"fleetmind": {
|
| 39 |
-
"command": "npx",
|
| 40 |
-
"args": ["mcp-remote", MCP_SSE_ENDPOINT]
|
| 41 |
-
}
|
| 42 |
-
}
|
| 43 |
-
}
|
| 44 |
-
return json.dumps(config, indent=2)
|
| 45 |
-
|
| 46 |
-
def get_tools_list():
|
| 47 |
-
"""Get all 29 MCP tools"""
|
| 48 |
-
return [
|
| 49 |
-
["geocode_address", "Geocoding & Routing", "Convert address to GPS coordinates"],
|
| 50 |
-
["calculate_route", "Geocoding & Routing", "Calculate route with vehicle optimization"],
|
| 51 |
-
["calculate_intelligent_route", "Geocoding & Routing", "Weather + traffic aware routing"],
|
| 52 |
-
["create_order", "Order Management", "Create new delivery order"],
|
| 53 |
-
["count_orders", "Order Management", "Count orders by status"],
|
| 54 |
-
["fetch_orders", "Order Management", "Get list of orders with filters"],
|
| 55 |
-
["get_order_details", "Order Management", "Get full order details"],
|
| 56 |
-
["search_orders", "Order Management", "Search orders"],
|
| 57 |
-
["get_incomplete_orders", "Order Management", "Get pending/in-transit orders"],
|
| 58 |
-
["update_order", "Order Management", "Update order"],
|
| 59 |
-
["delete_order", "Order Management", "Delete order"],
|
| 60 |
-
["create_driver", "Driver Management", "Register new driver"],
|
| 61 |
-
["count_drivers", "Driver Management", "Count drivers by status"],
|
| 62 |
-
["fetch_drivers", "Driver Management", "Get list of drivers"],
|
| 63 |
-
["get_driver_details", "Driver Management", "Get full driver details"],
|
| 64 |
-
["search_drivers", "Driver Management", "Search drivers"],
|
| 65 |
-
["get_available_drivers", "Driver Management", "Get active drivers"],
|
| 66 |
-
["update_driver", "Driver Management", "Update driver"],
|
| 67 |
-
["delete_driver", "Driver Management", "Delete driver"],
|
| 68 |
-
["create_assignment", "Assignment Management", "Manual assignment"],
|
| 69 |
-
["auto_assign_order", "Assignment Management", "🤖 Auto-assign to nearest driver"],
|
| 70 |
-
["intelligent_assign_order", "Assignment Management", "🧠 Gemini 2.0 Flash AI assignment"],
|
| 71 |
-
["get_assignment_details", "Assignment Management", "Get assignment details"],
|
| 72 |
-
["update_assignment", "Assignment Management", "Update assignment"],
|
| 73 |
-
["unassign_order", "Assignment Management", "Remove assignment"],
|
| 74 |
-
["complete_delivery", "Assignment Management", "Mark delivery complete"],
|
| 75 |
-
["fail_delivery", "Assignment Management", "Mark delivery failed"],
|
| 76 |
-
["delete_all_orders", "Bulk Operations", "Bulk delete orders"],
|
| 77 |
-
["delete_all_drivers", "Bulk Operations", "Bulk delete drivers"],
|
| 78 |
-
]
|
| 79 |
-
|
| 80 |
-
# Create Gradio interface
|
| 81 |
-
print("\n[2/2] Creating Gradio UI...")
|
| 82 |
-
|
| 83 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="FleetMind MCP Server") as gradio_app:
|
| 84 |
-
gr.Markdown("# 🚚 FleetMind MCP Server")
|
| 85 |
-
gr.Markdown("**Enterprise Model Context Protocol Server for AI-Powered Delivery Dispatch**")
|
| 86 |
-
gr.Markdown("*Track 1: Building MCP Servers - Enterprise Category*")
|
| 87 |
-
|
| 88 |
-
gr.Markdown("---")
|
| 89 |
-
|
| 90 |
-
gr.Markdown("## 🔌 MCP Server Connection")
|
| 91 |
-
|
| 92 |
-
gr.Markdown("### 📡 SSE Endpoint URL")
|
| 93 |
-
gr.Textbox(value=MCP_SSE_ENDPOINT, label="Copy this endpoint", interactive=False, max_lines=1)
|
| 94 |
-
|
| 95 |
-
gr.Markdown("### ⚙️ Claude Desktop Configuration")
|
| 96 |
-
gr.Markdown("Copy and paste this into your `claude_desktop_config.json` file:")
|
| 97 |
-
gr.Code(value=get_claude_config(), language="json", label="claude_desktop_config.json")
|
| 98 |
-
|
| 99 |
-
gr.Markdown("### 📋 How to Connect")
|
| 100 |
-
gr.Markdown("""
|
| 101 |
-
**Step 1:** Install Claude Desktop from https://claude.ai/download
|
| 102 |
-
|
| 103 |
-
**Step 2:** Open your `claude_desktop_config.json` file and add the configuration shown above
|
| 104 |
-
|
| 105 |
-
**Step 3:** Restart Claude Desktop
|
| 106 |
-
|
| 107 |
-
**Step 4:** Look for "FleetMind" in the 🔌 icon menu in Claude Desktop
|
| 108 |
-
|
| 109 |
-
**Step 5:** Start using commands like:
|
| 110 |
-
- "Create a delivery order for John at 123 Main St"
|
| 111 |
-
- "Show me all pending orders"
|
| 112 |
-
- "Auto-assign order ORD-... to the nearest driver"
|
| 113 |
-
- "Use AI to intelligently assign order ORD-..." (Gemini 2.0 Flash!)
|
| 114 |
-
""")
|
| 115 |
-
|
| 116 |
-
gr.Markdown("---")
|
| 117 |
-
|
| 118 |
-
gr.Markdown("## 🛠️ Available MCP Tools (29 Total)")
|
| 119 |
-
gr.Dataframe(
|
| 120 |
-
value=get_tools_list(),
|
| 121 |
-
headers=["Tool Name", "Category", "Description"],
|
| 122 |
-
label="All FleetMind MCP Tools",
|
| 123 |
-
wrap=True
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
gr.Markdown("---")
|
| 127 |
-
|
| 128 |
-
gr.Markdown("## ⭐ Key Features")
|
| 129 |
-
gr.Markdown("""
|
| 130 |
-
- **29 AI Tools** - Complete fleet management suite
|
| 131 |
-
- **🧠 Gemini 2.0 Flash AI** - Intelligent assignment with detailed reasoning
|
| 132 |
-
- **🌦️ Weather-Aware Routing** - Safety-first delivery planning
|
| 133 |
-
- **🚦 Real-Time Traffic** - Google Routes API integration
|
| 134 |
-
- **📊 SLA Tracking** - Automatic on-time performance monitoring
|
| 135 |
-
- **🗄️ PostgreSQL Database** - Production-grade data storage (Neon)
|
| 136 |
-
- **🚀 Multi-Client Support** - Works with Claude Desktop, Continue, Cline, any MCP client
|
| 137 |
-
""")
|
| 138 |
-
|
| 139 |
-
gr.Markdown("---")
|
| 140 |
-
|
| 141 |
-
gr.Markdown("## 📚 Resources")
|
| 142 |
-
gr.Markdown("""
|
| 143 |
-
- **GitHub:** https://github.com/mashrur-rahman-fahim/fleetmind-mcp
|
| 144 |
-
- **HuggingFace Space:** https://huggingface.co/spaces/MCP-1st-Birthday/fleetmind-dispatch-ai
|
| 145 |
-
- **MCP Protocol:** https://modelcontextprotocol.io
|
| 146 |
-
""")
|
| 147 |
-
|
| 148 |
-
gr.Markdown("---")
|
| 149 |
-
gr.Markdown("*FleetMind v1.0 - Built for MCP 1st Birthday Hackathon*")
|
| 150 |
-
|
| 151 |
-
print("[OK] Gradio UI created")
|
| 152 |
-
|
| 153 |
-
# Create FastAPI app
|
| 154 |
-
print("\nCreating unified FastAPI server...")
|
| 155 |
-
app = FastAPI(title="FleetMind MCP Server + UI")
|
| 156 |
-
|
| 157 |
-
# Mount Gradio at root
|
| 158 |
-
app = gr.mount_gradio_app(app, gradio_app, path="/")
|
| 159 |
-
print("[OK] Gradio UI mounted at /")
|
| 160 |
-
|
| 161 |
-
# Add MCP SSE endpoint
|
| 162 |
-
if mcp_available:
|
| 163 |
-
# Mount the MCP server's SSE handler
|
| 164 |
-
print("[OK] MCP SSE endpoint will be available at /sse")
|
| 165 |
-
|
| 166 |
-
print("\n" + "=" * 70)
|
| 167 |
-
print("[STARTING] Unified server...")
|
| 168 |
-
print("=" * 70)
|
| 169 |
-
print("[UI] Gradio UI: http://0.0.0.0:7860")
|
| 170 |
-
if mcp_available:
|
| 171 |
-
print("[MCP] MCP SSE: http://0.0.0.0:7860/sse")
|
| 172 |
-
print("=" * 70)
|
| 173 |
-
|
| 174 |
-
# Add MCP SSE endpoint to FastAPI app
|
| 175 |
-
if mcp_available:
|
| 176 |
-
from starlette.requests import Request
|
| 177 |
-
from starlette.responses import StreamingResponse
|
| 178 |
-
|
| 179 |
-
# Get the MCP server's SSE handler
|
| 180 |
-
# We'll use the app.py server as a separate process
|
| 181 |
-
# For now, just create a simple info endpoint
|
| 182 |
-
@app.get("/sse")
|
| 183 |
-
async def mcp_sse_info():
|
| 184 |
-
"""
|
| 185 |
-
MCP SSE endpoint information.
|
| 186 |
-
|
| 187 |
-
Note: For actual MCP SSE connection, deploy app.py separately
|
| 188 |
-
or use the production endpoint shown in the UI.
|
| 189 |
-
"""
|
| 190 |
-
return {
|
| 191 |
-
"message": "MCP SSE endpoint",
|
| 192 |
-
"status": "Use the standalone app.py for full MCP SSE functionality",
|
| 193 |
-
"tools_count": 29,
|
| 194 |
-
"resources_count": 2,
|
| 195 |
-
"production_endpoint": MCP_SSE_ENDPOINT
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
print("[INFO] MCP SSE info endpoint added at /sse")
|
| 199 |
-
print("[NOTE] For full MCP functionality, the standalone MCP server (app.py) should be deployed")
|
| 200 |
-
|
| 201 |
-
# Run unified server
|
| 202 |
-
if __name__ == "__main__":
|
| 203 |
-
# Run FastAPI with Gradio
|
| 204 |
-
uvicorn.run(
|
| 205 |
-
app,
|
| 206 |
-
host="0.0.0.0",
|
| 207 |
-
port=7860,
|
| 208 |
-
log_level="info"
|
| 209 |
-
)
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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@@
|
|
| 1 |
-
"""Verify the user_id fix"""
|
| 2 |
-
|
| 3 |
-
from database.connection import get_db_connection
|
| 4 |
-
|
| 5 |
-
def verify_order_fix():
|
| 6 |
-
conn = get_db_connection()
|
| 7 |
-
cursor = conn.cursor()
|
| 8 |
-
|
| 9 |
-
# Check the specific order mentioned by user
|
| 10 |
-
order_id = "ORD-202511192234450754"
|
| 11 |
-
|
| 12 |
-
cursor.execute(
|
| 13 |
-
"SELECT order_id, user_id, customer_name, status FROM orders WHERE order_id = %s",
|
| 14 |
-
(order_id,)
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
result = cursor.fetchone()
|
| 18 |
-
|
| 19 |
-
if result:
|
| 20 |
-
print(f"\nOrder: {result['order_id']}")
|
| 21 |
-
print(f"User ID: {result['user_id']}")
|
| 22 |
-
print(f"Customer: {result['customer_name']}")
|
| 23 |
-
print(f"Status: {result['status']}")
|
| 24 |
-
|
| 25 |
-
if result['user_id'] == 'user_id':
|
| 26 |
-
print("\n[ERROR] Still has bad user_id value!")
|
| 27 |
-
elif result['user_id'].startswith('user_'):
|
| 28 |
-
print("\n[SUCCESS] User ID is now correct!")
|
| 29 |
-
else:
|
| 30 |
-
print(f"\n[WARNING] Unexpected user_id format: {result['user_id']}")
|
| 31 |
-
else:
|
| 32 |
-
print(f"\n[NOT FOUND] Order {order_id} not found")
|
| 33 |
-
|
| 34 |
-
# Check if any records still have bad user_id
|
| 35 |
-
print("\n--- Checking for remaining bad records ---")
|
| 36 |
-
|
| 37 |
-
for table in ['orders', 'drivers', 'assignments']:
|
| 38 |
-
cursor.execute(f"SELECT COUNT(*) as count FROM {table} WHERE user_id = %s", ('user_id',))
|
| 39 |
-
result = cursor.fetchone()
|
| 40 |
-
count = result['count'] if result else 0
|
| 41 |
-
|
| 42 |
-
if count > 0:
|
| 43 |
-
print(f"{table}: {count} records still have user_id='user_id'")
|
| 44 |
-
else:
|
| 45 |
-
print(f"{table}: All clean!")
|
| 46 |
-
|
| 47 |
-
cursor.close()
|
| 48 |
-
conn.close()
|
| 49 |
-
|
| 50 |
-
if __name__ == "__main__":
|
| 51 |
-
verify_order_fix()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|