example_client.pyโข4.01 kB
#!/usr/bin/env python3
"""
MCP Wikipedia Server Example Client
This script demonstrates how to interact with the Wikipedia MCP server.
"""
import asyncio
import sys
import json
from pathlib import Path
# Add the src directory to the path for imports
sys.path.insert(0, str(Path(__file__).parent / "src"))
async def demo_wikipedia_tools():
"""Demonstrate the Wikipedia MCP server tools."""
print("๐ MCP Wikipedia Server Demo")
print("=" * 40)
# This is a demonstration of how you would call the tools
# In a real MCP client, you would use the MCP protocol
print("\n1. ๐ Searching for 'Python programming language'...")
search_example = {
"tool": "fetch_wikipedia_info",
"parameters": {
"query": "Python programming language"
}
}
print(f" Tool call: {json.dumps(search_example, indent=2)}")
print(" Expected: Article title, summary, and URL")
print("\n2. ๐ Getting sections for 'Artificial Intelligence'...")
sections_example = {
"tool": "list_wikipedia_sections",
"parameters": {
"topic": "Artificial Intelligence"
}
}
print(f" Tool call: {json.dumps(sections_example, indent=2)}")
print(" Expected: List of section titles")
print("\n3. ๐ Getting 'History' section content...")
content_example = {
"tool": "get_section_content",
"parameters": {
"topic": "Machine Learning",
"section_title": "History"
}
}
print(f" Tool call: {json.dumps(content_example, indent=2)}")
print(" Expected: Section content text")
print("\n" + "=" * 40)
print("๐ To use these tools:")
print(" 1. Start the server: python src/mcp_server/mcp_server.py")
print(" 2. Connect via MCP protocol (see GUIDE.md)")
print(" 3. Call tools using the JSON format shown above")
print("\n๐ For a real MCP client implementation, see:")
print(" - src/mcp_server/mcp_client.py")
print(" - GUIDE.md (Integration examples)")
async def test_direct_wikipedia():
"""Test Wikipedia functionality directly (without MCP)."""
print("\n๐งช Direct Wikipedia API Test")
print("-" * 30)
try:
import wikipedia
# Test basic search
print("Searching for 'Python'...")
page = wikipedia.page("Python (programming language)")
print(f"โ
Found: {page.title}")
print(f" URL: {page.url}")
print(f" Summary: {page.summary[:100]}...")
# Test sections
print(f"\n๐ Sections ({len(page.sections)} total):")
for i, section in enumerate(page.sections[:5]):
print(f" {i+1}. {section}")
if len(page.sections) > 5:
print(f" ... and {len(page.sections) - 5} more")
# Test section content
if page.sections:
first_section = page.sections[0]
content = page.section(first_section)
if content:
print(f"\n๐ '{first_section}' section:")
print(f" {content[:150]}...")
print("\nโ
Wikipedia API is working correctly!")
except ImportError:
print("โ Wikipedia package not installed")
print(" Run: pip install wikipedia")
except Exception as e:
print(f"โ Error testing Wikipedia: {e}")
def main():
"""Main function."""
print("MCP Wikipedia Server - Example Client")
print("=====================================")
# Run the demo
asyncio.run(demo_wikipedia_tools())
# Test Wikipedia directly
asyncio.run(test_direct_wikipedia())
print("\n๐ฏ Next Steps:")
print(" โข Read GUIDE.md for detailed usage instructions")
print(" โข Check QUICK_REF.md for command reference")
print(" โข Start the server with: python src/mcp_server/mcp_server.py")
if __name__ == "__main__":
main()