#!/usr/bin/env python3
"""
Test script for UofT Student Helper MCP Server
Tests all available tools and provides a comprehensive report.
"""
import requests
import json
import sys
from typing import Dict, Any
class Colors:
"""ANSI color codes for terminal output"""
GREEN = '\033[92m'
RED = '\033[91m'
YELLOW = '\033[93m'
BLUE = '\033[94m'
RESET = '\033[0m'
BOLD = '\033[1m'
def print_header(text: str):
"""Print a formatted header"""
print(f"\n{Colors.BOLD}{Colors.BLUE}{'=' * 60}{Colors.RESET}")
print(f"{Colors.BOLD}{Colors.BLUE}{text:^60}{Colors.RESET}")
print(f"{Colors.BOLD}{Colors.BLUE}{'=' * 60}{Colors.RESET}\n")
def print_success(text: str):
"""Print success message"""
print(f"{Colors.GREEN}✓ {text}{Colors.RESET}")
def print_error(text: str):
"""Print error message"""
print(f"{Colors.RED}✗ {text}{Colors.RESET}")
def print_info(text: str):
"""Print info message"""
print(f"{Colors.YELLOW}ℹ {text}{Colors.RESET}")
def test_health(base_url: str) -> bool:
"""Test the health endpoint"""
print_info("Testing health endpoint...")
try:
response = requests.get(f"{base_url}/health", timeout=5)
if response.status_code == 200:
print_success(f"Health check passed: {response.json()}")
return True
else:
print_error(f"Health check failed with status {response.status_code}")
return False
except Exception as e:
print_error(f"Health check failed: {str(e)}")
return False
def test_tool(base_url: str, tool_name: str, arguments: Dict[str, Any]) -> bool:
"""Test a specific tool"""
print_info(f"Testing tool: {tool_name}...")
payload = {
"tool": tool_name,
"arguments": arguments
}
try:
response = requests.post(
f"{base_url}/call",
headers={"Content-Type": "application/json"},
json=payload,
timeout=10
)
if response.status_code == 200:
result = response.json()
if "error" in result:
print_error(f"Tool returned error: {result['error']}")
return False
else:
print_success(f"Tool executed successfully")
print(f" Result preview: {json.dumps(result, indent=2)[:200]}...")
return True
else:
print_error(f"Tool call failed with status {response.status_code}")
print(f" Response: {response.text}")
return False
except Exception as e:
print_error(f"Tool call failed: {str(e)}")
return False
def main():
"""Run all tests"""
# Configuration
base_url = sys.argv[1] if len(sys.argv) > 1 else "http://localhost:3002"
print_header("UofT Student Helper MCP Server Tests")
print(f"Testing server at: {base_url}\n")
# Test results
results = {
"passed": 0,
"failed": 0,
"total": 0
}
# Test 1: Health check
print_header("Test 1: Health Check")
if test_health(base_url):
results["passed"] += 1
else:
results["failed"] += 1
results["total"] += 1
# Test 2: Get enrolled courses
print_header("Test 2: Get Enrolled Courses")
if test_tool(base_url, "get_enrolled_courses", {}):
results["passed"] += 1
else:
results["failed"] += 1
results["total"] += 1
# Test 3: Get course details
print_header("Test 3: Get Course Details")
if test_tool(base_url, "get_course_details", {"course_code": "CSC148H1"}):
results["passed"] += 1
else:
results["failed"] += 1
results["total"] += 1
# Test 4: Get course schedule
print_header("Test 4: Get Course Schedule")
if test_tool(base_url, "get_course_schedule", {}):
results["passed"] += 1
else:
results["failed"] += 1
results["total"] += 1
# Test 5: Get syllabus
print_header("Test 5: Get Syllabus")
if test_tool(base_url, "get_syllabus", {"course_code": "CSC148H1"}):
results["passed"] += 1
else:
results["failed"] += 1
results["total"] += 1
# Test 6: Get course assignments
print_header("Test 6: Get Course Assignments")
if test_tool(base_url, "get_course_assignments", {"course_code": "CSC148H1"}):
results["passed"] += 1
else:
results["failed"] += 1
results["total"] += 1
# Test 7: Get course announcements
print_header("Test 7: Get Course Announcements")
if test_tool(base_url, "get_course_announcements", {"course_code": "CSC148H1", "limit": 3}):
results["passed"] += 1
else:
results["failed"] += 1
results["total"] += 1
# Test 8: Get all Quercus courses
print_header("Test 8: Get All Quercus Courses")
if test_tool(base_url, "get_all_quercus_courses", {}):
results["passed"] += 1
else:
results["failed"] += 1
results["total"] += 1
# Print summary
print_header("Test Summary")
print(f"Total tests: {results['total']}")
print_success(f"Passed: {results['passed']}")
if results['failed'] > 0:
print_error(f"Failed: {results['failed']}")
success_rate = (results['passed'] / results['total']) * 100
print(f"\nSuccess rate: {success_rate:.1f}%\n")
if results['failed'] == 0:
print_success("All tests passed! 🎉")
return 0
else:
print_error("Some tests failed. Please check the output above.")
return 1
if __name__ == "__main__":
sys.exit(main())