test_server.py•4.17 kB
#!/usr/bin/env python
"""
Test script to verify the MCP Webpage Server is working correctly
"""
import sys
import os
# Add parent directory to path to find src
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def test_server():
"""Test all server functionality"""
print("Testing Webpage Server MCP Server\n")
# Import server functions
try:
from src.main import list_pages, get_page, get_sitemap
print("[OK] Server modules imported successfully")
except ImportError as e:
print(f"[ERROR] Failed to import server: {e}")
return False
# Check configuration
from src.main import BASE_URL, SITEMAP_PATH
print(f"\n[INFO] Configuration:")
print(f" Base URL: {BASE_URL}")
print(f" Sitemap Path: {SITEMAP_PATH}")
# Test 1: List pages
print("\nTest 1: Listing pages...")
try:
pages = list_pages()
if pages:
print(f"[OK] Found {len(pages)} pages:")
for page in pages[:5]: # Show first 5
print(f" - {page}")
if len(pages) > 5:
print(f" ... and {len(pages) - 5} more")
else:
print("[WARNING] No pages found in sitemap")
except Exception as e:
print(f"[ERROR] Failed to list pages: {e}")
return False
# Test 2: Get sitemap resource
print("\nTest 2: Getting sitemap resource...")
try:
sitemap = get_sitemap()
if sitemap and len(sitemap) > 0:
print(f"[OK] Sitemap fetched ({len(sitemap)} bytes)")
else:
print("[WARNING] Sitemap is empty")
except Exception as e:
print(f"[ERROR] Failed to get sitemap: {e}")
return False
# Test 3: Get page
print("\nTest 3: Getting a webpage...")
try:
# Try to get the first page from the list
if pages:
test_path = pages[0]
print(f"[INFO] Fetching page: {test_path}")
result = get_page(test_path)
if 'error' in result:
print(f"[WARNING] Failed to fetch page: {result.get('message', 'Unknown error')}")
else:
print(f"[OK] Page fetched successfully:")
print(f" URL: {result.get('url')}")
print(f" Status: {result.get('status_code')}")
print(f" Content-Type: {result.get('content_type')}")
print(f" HTML length: {len(result.get('html', ''))} bytes")
else:
print("[SKIP] No pages to test")
except Exception as e:
print(f"[ERROR] Failed to get page: {e}")
return False
# Test 4: Rate limiting
print("\nTest 4: Testing rate limiting...")
try:
# Make multiple requests to test rate limiter
test_path = pages[0] if pages else "/"
success_count = 0
rate_limited = False
for i in range(12): # Try more than the limit (10)
result = get_page(test_path, user_id="test_user")
if 'error' in result and result.get('error') == 'Rate limit exceeded':
rate_limited = True
print(f"[OK] Rate limiting triggered at request {i + 1}")
print(f" Reset in: {result.get('reset_in_seconds')} seconds")
break
else:
success_count += 1
if not rate_limited:
print(f"[WARNING] Rate limiting not triggered after {success_count} requests")
except Exception as e:
print(f"[ERROR] Rate limiting test failed: {e}")
return False
print("\n" + "=" * 50)
print("[SUCCESS] All tests passed! Server is ready.")
print("\nTo deploy to Dedalus:")
print(" dedalus deploy ./src/main.py --name 'webpage-server'")
print("\nTo use with Dedalus SDK:")
print(" mcp_servers=['your-org/webpage-server']")
print("\nEnvironment Variables:")
print(" BASE_URL: The base URL of the website to query (default: https://example.com)")
print("\nNote: Sitemap is read from assets/sitemap.xml")
return True
if __name__ == "__main__":
success = test_server()
sys.exit(0 if success else 1)