#!/usr/bin/env python3
"""
Simple test script to verify the Unsloth MCP server is working.
This can be run directly or with uv.
"""
import sys
import os
import requests
from typing import Dict
from bs4 import BeautifulSoup
# Since the functions are wrapped by @mcp.tool, we need to recreate them
BASE_URL = "https://docs.unsloth.ai"
def fetch_page_content(url: str) -> Dict[str, str]:
"""Fetch content from a documentation page."""
try:
response = requests.get(url, timeout=10)
response.raise_for_status()
soup = BeautifulSoup(response.content, 'html.parser')
# Extract main content
content_selectors = [
'div[data-testid="page-content"]',
'.page-content',
'main',
'.markdown-body',
'article'
]
content = ""
title = soup.find('title').get_text() if soup.find('title') else "Unsloth Documentation"
for selector in content_selectors:
content_elem = soup.select_one(selector)
if content_elem:
content = content_elem.get_text(strip=True)
break
if not content:
# Fallback to body content
body = soup.find('body')
if body:
content = body.get_text(strip=True)
return {
"title": title,
"content": content,
"url": url
}
except Exception as e:
return {
"title": "Error",
"content": f"Failed to fetch content: {str(e)}",
"url": url
}
def test_search_unsloth_docs(query: str) -> str:
"""Test version of search_unsloth_docs."""
try:
main_page = fetch_page_content(BASE_URL)
query_lower = query.lower()
content_lower = main_page["content"].lower()
if query_lower in content_lower:
lines = main_page["content"].split('\n')
relevant_lines = []
for i, line in enumerate(lines):
if query_lower in line.lower():
start = max(0, i - 3)
end = min(len(lines), i + 4)
relevant_lines.extend(lines[start:end])
relevant_lines.append("---")
if relevant_lines:
return f"Found relevant information about '{query}':\n\n" + "\n".join(relevant_lines)
return f"No specific information found for '{query}' in the main documentation. Here's the general overview:\n\n{main_page['content'][:1000]}..."
except Exception as e:
return f"Error searching documentation: {str(e)}"
def test_get_unsloth_quickstart() -> str:
"""Test version of get_unsloth_quickstart."""
try:
main_page = fetch_page_content(BASE_URL)
content = main_page["content"]
quickstart_keywords = ["quickstart", "install", "getting started", "pip install"]
relevant_sections = []
lines = content.split('\n')
for i, line in enumerate(lines):
for keyword in quickstart_keywords:
if keyword.lower() in line.lower():
start = max(0, i - 2)
end = min(len(lines), i + 5)
section = "\n".join(lines[start:end])
if section not in relevant_sections:
relevant_sections.append(section)
break
if relevant_sections:
return "Unsloth Quickstart Guide:\n\n" + "\n\n---\n\n".join(relevant_sections)
else:
return f"Unsloth Documentation Overview:\n\n{content[:2000]}..."
except Exception as e:
return f"Error fetching quickstart guide: {str(e)}"
def test_server_functions():
"""Test the server functions directly without MCP client."""
print("=== Testing Unsloth MCP Server Functions ===\n")
# Test 1: Get quickstart guide
print("1. Testing get_unsloth_quickstart()...")
try:
result = test_get_unsloth_quickstart()
print(f"✓ Success: {len(result)} characters returned")
print(f"Preview: {result[:200]}...\n")
except Exception as e:
print(f"✗ Error: {e}\n")
print("-" * 50)
# Test 2: Search for specific topic
print("2. Testing search_unsloth_docs('installation')...")
try:
result = test_search_unsloth_docs("installation")
print(f"✓ Success: {len(result)} characters returned")
print(f"Preview: {result[:200]}...\n")
except Exception as e:
print(f"✗ Error: {e}\n")
print("-" * 50)
# Test 3: Test basic connectivity
print("3. Testing basic web connectivity...")
try:
page_data = fetch_page_content(BASE_URL)
print(f"✓ Success: Fetched '{page_data['title']}'")
print(f"Content length: {len(page_data['content'])} characters")
print(f"Preview: {page_data['content'][:200]}...\n")
except Exception as e:
print(f"✗ Error: {e}\n")
print("-" * 50)
# Test 4: Search functionality
print("4. Testing search for 'fine-tuning'...")
try:
result = test_search_unsloth_docs("fine-tuning")
print(f"✓ Success: {len(result)} characters returned")
print(f"Preview: {result[:200]}...\n")
except Exception as e:
print(f"✗ Error: {e}\n")
print("=== Test Complete ===")
print("\n💡 If tests pass, your FastMCP server should work correctly!")
print(" Run it with: uv run python unsloth_mcp_server.py")
if __name__ == "__main__":
test_server_functions()