test_syntax.pyā¢3.33 kB
#!/usr/bin/env python3
"""
Syntax and structure test for the Kali MCP Server
Tests the code without requiring external dependencies
"""
import ast
import sys
from pathlib import Path
def test_python_syntax(file_path):
"""Test if Python file has valid syntax"""
try:
with open(file_path, 'r') as f:
content = f.read()
# Parse the AST to check syntax
ast.parse(content)
return True, "Valid Python syntax"
except SyntaxError as e:
return False, f"Syntax error: {e}"
except Exception as e:
return False, f"Error: {e}"
def test_imports(file_path):
"""Test which imports are available"""
available_imports = []
missing_imports = []
standard_libs = ['asyncio', 'json', 'logging', 'os', 're', 'subprocess', 'sys', 'pathlib', 'typing']
third_party = ['fastmcp', 'pydantic']
for lib in standard_libs:
try:
__import__(lib)
available_imports.append(lib)
except ImportError:
missing_imports.append(lib)
for lib in third_party:
try:
__import__(lib)
available_imports.append(lib)
except ImportError:
missing_imports.append(lib)
return available_imports, missing_imports
def count_tools_in_file(file_path):
"""Count @mcp.tool() decorators in the file"""
try:
with open(file_path, 'r') as f:
content = f.read()
# Count @mcp.tool() decorators
tool_count = content.count('@mcp.tool()')
# Extract function names
import re
pattern = r'@mcp\.tool\(\)\s*(?:async\s+)?def\s+(\w+)'
tools = re.findall(pattern, content, re.MULTILINE)
return tool_count, tools
except Exception as e:
return 0, []
def main():
"""Run all tests"""
server_file = Path(__file__).parent / 'kali_mcp_server' / 'server.py'
print("š Testing Kali MCP Server")
print("=" * 50)
# Test syntax
print("1. Testing Python syntax...")
syntax_ok, syntax_msg = test_python_syntax(server_file)
print(f" {'ā' if syntax_ok else 'ā'} {syntax_msg}")
if not syntax_ok:
sys.exit(1)
# Test imports
print("\n2. Testing imports...")
available, missing = test_imports(server_file)
print(f" ā Available: {', '.join(available)}")
if missing:
print(f" ā Missing: {', '.join(missing)}")
# Count tools
print("\n3. Analyzing MCP tools...")
tool_count, tools = count_tools_in_file(server_file)
print(f" ā Found {tool_count} MCP tools:")
for tool in tools:
print(f" - {tool}")
# Test file structure
print("\n4. Testing file structure...")
required_files = [
'requirements.txt',
'pyproject.toml',
'setup_capabilities.sh',
'install.sh',
'run_server.py',
'.env.example',
'README.md'
]
for file in required_files:
if (Path(__file__).parent / file).exists():
print(f" ā {file}")
else:
print(f" ā {file} (missing)")
print("\n" + "=" * 50)
print("ā Testing complete!")
if missing:
print(f"\nš¦ To install missing dependencies:")
print(f" pip install {' '.join(missing)}")
if __name__ == "__main__":
main()