test_fastmcp.py•3.15 kB
#!/usr/bin/env python3
"""
Quick test script to verify FastMCP server setup
Tests that all imports work and server can be instantiated
"""
import sys
def test_imports():
"""Test that all required modules can be imported"""
try:
from mcp.server.fastmcp import FastMCP
print("✓ FastMCP import successful")
return True
except ImportError as e:
print(f"✗ FastMCP import failed: {e}")
return False
def test_server_creation():
"""Test that server can be created"""
try:
from mcp.server.fastmcp import FastMCP
app = FastMCP(name="test-server")
print("✓ Server creation successful")
return True
except Exception as e:
print(f"✗ Server creation failed: {e}")
return False
def test_tool_registration():
"""Test that tools can be registered"""
try:
from mcp.server.fastmcp import FastMCP
app = FastMCP(name="test-server")
@app.tool()
def test_tool(x: int) -> str:
"""Test tool"""
return str(x)
print("✓ Tool registration successful")
return True
except Exception as e:
print(f"✗ Tool registration failed: {e}")
return False
def test_resource_registration():
"""Test that resources can be registered"""
try:
from mcp.server.fastmcp import FastMCP
app = FastMCP(name="test-server")
@app.resource("test://resource")
def test_resource() -> str:
"""Test resource"""
return "test"
print("✓ Resource registration successful")
return True
except Exception as e:
print(f"✗ Resource registration failed: {e}")
return False
def test_server_file():
"""Test that the main server file can be imported"""
try:
sys.path.insert(0, '.')
import fast_mcp_server
print("✓ Server file import successful")
print(f" Server name: {fast_mcp_server.app.name}")
print(f" Number of tools: {len([f for f in dir(fast_mcp_server.app) if not f.startswith('_')])}")
return True
except Exception as e:
print(f"✗ Server file import failed: {e}")
import traceback
traceback.print_exc()
return False
def main():
"""Run all tests"""
print("Testing FastMCP Server Setup...")
print("-" * 50)
tests = [
("Imports", test_imports),
("Server Creation", test_server_creation),
("Tool Registration", test_tool_registration),
("Resource Registration", test_resource_registration),
("Server File", test_server_file),
]
results = []
for name, test_func in tests:
print(f"\n[{name}]")
results.append(test_func())
print("\n" + "=" * 50)
passed = sum(results)
total = len(results)
if passed == total:
print(f"✓ All tests passed! ({passed}/{total})")
return 0
else:
print(f"✗ Some tests failed ({passed}/{total} passed)")
return 1
if __name__ == "__main__":
sys.exit(main())