# Step 02: Setup Proper Python Packaging

## Prerequisites
- Step 01 completed (diagnostic analysis)
- Understanding of Python package structure
- FastMCP installation verified

## Objectives
1. Fix the relative import issues in main.py
2. Create proper Python package structure for MCP modules
3. Ensure all modules can be imported correctly
4. Set up proper __init__.py files

## Implementation Details

### Step 1: Fix MCP Package Structure
```bash
# Create proper __init__.py files
touch _mcp/__init__.py
touch _mcp/servers/__init__.py
touch _mcp/context_logic/__init__.py
```

### Step 2: Convert Relative Imports to Absolute
**In `_mcp/main.py`**, change from:
```python
from .servers.coverage import create_coverage_server
```
To:
```python
from _mcp.servers.coverage import create_coverage_server
```

### Step 3: Update Server Module Imports
**In each server module**, change relative imports to absolute:
```python
# Before
from ..context_logic import coverage_analysis

# After  
from _mcp.context_logic import coverage_analysis
```

### Step 4: Create Package Entry Point
Create `_mcp/__init__.py` with:
```python
"""
Storm Logger MCP Server Package
Enterprise-grade MCP server for Storm Logger development
"""

__version__ = "2.0.0"
__author__ = "Storm Development Team"

from .main import create_storm_mcp_server

__all__ = ["create_storm_mcp_server"]
```

### Step 5: Update Python Path Handling
Create `_mcp/server_runner.py`:
```python
#!/usr/bin/env python3
"""
Storm Logger MCP Server Runner
Handles proper Python path setup and server initialization
"""

import sys
import os
from pathlib import Path

# Add project root to Python path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from _mcp.main import create_storm_mcp_server

if __name__ == "__main__":
    server = create_storm_mcp_server()
    print("Storm Logger MCP Server started successfully!")
```

## Testing/Validation

### Step 1: Test Package Imports
```bash
# Test from project root
python -c "from _mcp import main; print('Main module imports successfully')"

# Test server modules
python -c "from _mcp.servers import coverage; print('Coverage server imports successfully')"
```

### Step 2: Test Server Creation
```bash
# Test server creation
python -c "
from _mcp.main import create_storm_mcp_server
server = create_storm_mcp_server()
print('Server created successfully')
print(f'Server name: {server.name}')
"
```

### Step 3: Test All Server Modules
```bash
# Test each server module individually
for module in coverage testing security patterns gitlab python core; do
    echo "Testing $module..."
    python -c "from _mcp.servers.$module import create_${module}_server; print('$module server OK')"
done
```

## Success Criteria
- [ ] All import errors resolved
- [ ] Main server can be created without errors
- [ ] All server modules can be imported
- [ ] Package structure is properly organized

## JSON Update Instructions

When starting this step:
```json
{
  "steps": {
    "02": {
      "status": "in_progress",
      "progress": 25,
      "issues": ["Fixing import structure"],
      "next_action": "Update all relative imports to absolute"
    }
  }
}
```

When 50% complete (imports fixed):
```json
{
  "steps": {
    "02": {
      "status": "in_progress", 
      "progress": 50,
      "issues": ["Testing server module imports"],
      "next_action": "Validate all server modules can be imported"
    }
  }
}
```

When complete:
```json
{
  "steps": {
    "02": {
      "status": "completed",
      "progress": 100,
      "issues": [],
      "next_action": "Proceed to Step 03"
    }
  },
  "current_step": "03",
  "overall_progress": 20
}
```

## Troubleshooting

### Common Issues

1. **Still getting relative import errors**
   - Check that all imports are converted to absolute
   - Verify __init__.py files are in place
   - Check Python path setup

2. **Module not found errors**
   - Verify project root is in Python path
   - Check file names match import statements
   - Ensure __init__.py files exist

3. **FastMCP import errors**
   - Check FastMCP installation: `pip show fastmcp`
   - Verify correct FastMCP version
   - Check if virtual environment is activated

### Debug Commands
```bash
# Check Python can find the modules
python -c "import sys; sys.path.append('.'); import _mcp; print(_mcp.__file__)"

# Test individual server imports
python -c "
import sys
sys.path.append('.')
try:
    from _mcp.servers.coverage import create_coverage_server
    print('Coverage server import: SUCCESS')
except Exception as e:
    print(f'Coverage server import: FAILED - {e}')
"
```

## Next Steps
After completing this step:
1. Update JSON status to "completed"
2. Proceed to Step 03: Implement Missing Server Modules
3. Use the fixed import structure to implement missing modules

## Notes
- All changes must maintain backward compatibility
- Document any changes made to import statements
- Test thoroughly before proceeding to next step
- Keep backup of original files if needed