test_server_standalone.py•4.88 kB
#!/usr/bin/env python3
"""Test if the MCP server can start standalone"""
import subprocess
import sys
import time
import json
from pathlib import Path
def test_server_startup():
"""Test if the server can start without errors"""
print("Testing NIX MCP Server startup...")
print("=" * 60)
# Set up environment
env = {
"NIX_MCP_DEBUG": "DEBUG",
"PYTHONUNBUFFERED": "1",
"NODEOS_ENV": "dev",
"PATH": os.environ.get("PATH", "")
}
# Path to main.py
main_py = Path(__file__).parent / "main.py"
# Try to start the server
print(f"Starting server from: {main_py}")
try:
# Run with uv
cmd = ["uv", "run", "python", str(main_py)]
print(f"Command: {' '.join(cmd)}")
# Start the process
process = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env,
text=True
)
# Send a simple MCP initialization message
init_message = {
"jsonrpc": "2.0",
"method": "initialize",
"params": {
"protocolVersion": "0.1.0",
"capabilities": {}
},
"id": 1
}
print("\nSending initialization message...")
process.stdin.write(json.dumps(init_message) + "\n")
process.stdin.flush()
# Wait a moment for response
time.sleep(2)
# Check if process is still running
if process.poll() is None:
print("✅ Server is running!")
# Try to read any output
try:
process.stdin.close()
stdout, stderr = process.communicate(timeout=1)
if stdout:
print("\nSTDOUT:")
print(stdout)
if stderr:
print("\nSTDERR (debug logs):")
print(stderr)
except subprocess.TimeoutExpired:
print("Server still running (timeout reached)")
process.terminate()
else:
# Process ended
stdout, stderr = process.communicate()
print(f"❌ Server exited with code: {process.returncode}")
if stdout:
print("\nSTDOUT:")
print(stdout)
if stderr:
print("\nSTDERR:")
print(stderr)
return False
except FileNotFoundError as e:
print(f"❌ Could not find command: {e}")
print("Make sure 'uv' is installed and in PATH")
return False
except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()
return False
return True
def test_direct_import():
"""Test if we can import the server module directly"""
print("\nTesting direct import...")
print("=" * 60)
sys.path.insert(0, str(Path(__file__).parent / "src"))
try:
from nix_mcp.server_fastmcp import mcp
print("✅ Successfully imported server module")
# Check tools are registered
print(f"Tools registered: {list(mcp.tools.keys()) if hasattr(mcp, 'tools') else 'Unknown'}")
return True
except ImportError as e:
print(f"❌ Import failed: {e}")
return False
except Exception as e:
print(f"❌ Unexpected error: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
import os
print("NIX MCP Server Diagnostic Test")
print("=" * 60)
print(f"Python: {sys.version}")
print(f"Working directory: {os.getcwd()}")
print(f"Script location: {__file__}")
print()
# Test import
import_ok = test_direct_import()
# Test server startup
print()
startup_ok = test_server_startup()
# Summary
print("\n" + "=" * 60)
print("SUMMARY")
print("=" * 60)
print(f"Direct import: {'✅ PASS' if import_ok else '❌ FAIL'}")
print(f"Server startup: {'✅ PASS' if startup_ok else '❌ FAIL'}")
if import_ok and startup_ok:
print("\n✅ All tests passed! Server should work with Claude.")
print("\nTry reconnecting in Claude Desktop using /mcp command")
else:
print("\n❌ Some tests failed. Check the errors above.")
print("\nCommon issues:")
print("1. Missing dependencies - run: uv sync")
print("2. Path issues - check PYTHONPATH")
print("3. Network issues - check NODEOS_API and RODEOS_API endpoints")
sys.exit(0 if (import_ok and startup_ok) else 1)