"""Test script for LangSearch MCP Server"""
import asyncio
import os
from dotenv import load_dotenv
from main import web_search, semantic_rerank
# Load environment variables
load_dotenv()
async def test_web_search():
"""Test the web_search tool"""
print("=" * 60)
print("Testing Web Search Tool")
print("=" * 60)
try:
result = await web_search(
query="latest developments in AI 2026",
count=3,
summary=True,
freshness="month",
language="en" # English only
)
print(f"\n✅ Search successful!")
print(f"Query: {result.query}")
print(f"Total Results: {result.total_results}")
print(f"\nTop {len(result.results)} results:\n")
for i, page in enumerate(result.results, 1):
print(f"{i}. {page.name}")
print(f" URL: {page.url}")
print(f" Snippet: {page.snippet[:100]}...")
if page.summary:
print(f" Summary: {page.summary[:150]}...")
print()
except Exception as e:
print(f"\n❌ Web search failed: {e}")
return False
return True
async def test_semantic_rerank():
"""Test the semantic_rerank tool"""
print("=" * 60)
print("Testing Semantic Rerank Tool")
print("=" * 60)
documents = [
"Machine learning is a subset of artificial intelligence that enables computers to learn from data.",
"The weather today is sunny with a chance of rain in the afternoon.",
"Deep learning uses neural networks with multiple layers to process complex patterns.",
"Python is a popular programming language used in data science.",
"Neural networks are inspired by the structure of the human brain."
]
try:
result = await semantic_rerank(
query="What is machine learning?",
documents=documents,
top_n=3
)
print(f"\n✅ Rerank successful!")
print(f"Model: {result.model}")
print(f"\nTop {len(result.results)} most relevant documents:\n")
for i, doc in enumerate(result.results, 1):
print(f"{i}. [Score: {doc.relevance_score:.4f}] (Original index: {doc.index})")
print(f" {doc.text[:100]}...")
print()
except Exception as e:
print(f"\n❌ Semantic rerank failed: {e}")
return False
return True
async def main():
"""Run all tests"""
print("\n🚀 Starting LangSearch MCP Server Tests\n")
# Check API key
api_key = os.getenv("LANGSEARCH_API_KEY")
if not api_key:
print("❌ LANGSEARCH_API_KEY not found in environment variables")
print("Please set it in your .env file")
return
print(f"✓ API Key found: {api_key[:10]}...{api_key[-4:]}\n")
# Run tests
search_passed = await test_web_search()
rerank_passed = await test_semantic_rerank()
# Summary
print("=" * 60)
print("Test Summary")
print("=" * 60)
print(f"Web Search: {'✅ PASSED' if search_passed else '❌ FAILED'}")
print(f"Semantic Rerank: {'✅ PASSED' if rerank_passed else '❌ FAILED'}")
if search_passed and rerank_passed:
print("\n🎉 All tests passed! Server is working correctly.")
else:
print("\n⚠️ Some tests failed. Check the errors above.")
if __name__ == "__main__":
asyncio.run(main())