This guide covers development setup, testing, and contribution guidelines for the Metasploit MCP Server.
- Python 3.8 or higher
- Poetry (Installation Guide)
- Metasploit Framework with RPC enabled
# Clone the repository
git clone <repository-url>
cd MetasploitMCP
# Install dependencies with Poetry
poetry install
# Activate the virtual environment
poetry shell
# Run tests to verify setup
poetry run pytest# Development mode with debug logging
poetry run metasploit-mcp --transport http --host 127.0.0.1 --port 8085
# With custom Metasploit connection
MSF_PASSWORD=yourpassword MSF_SERVER=127.0.0.1 MSF_PORT=55553 poetry run metasploit-mcp --transport http# Run all tests
poetry run pytest
# Run specific test file
poetry run pytest tests/test_helpers.py -v
# Run with coverage
poetry run pytest tests/ --cov=. --cov-report=html# Format code (if you add formatting tools)
poetry run black src tests
# Lint code (if you add linting tools)
poetry run flake8 src tests
# Type checking (if you add mypy)
poetry run mypy srcMetasploitMCP/
├── src/
│ └── metasploit_mcp/ # Main package
│ ├── __init__.py # Package entry point with main()
│ ├── server.py # MCP server implementation
│ ├── event_loop_monitor.py # Async event loop monitoring
│ ├── instance_manager.py # Metasploit instance management
│ └── jsonrpc_patch.py # JSON-RPC monkeypatch for pymetasploit3
├── pyproject.toml # Poetry configuration and dependencies
├── tests/ # Test suite
│ ├── conftest.py # Pytest configuration and fixtures
│ ├── harness.py # Metasploitable 3 integration harness
│ └── test_*.py # Test modules
└── docs/ # Documentation
├── DEVELOPMENT.md # This file
├── POETRY_MIGRATION.md # Poetry migration guide
└── API.md # API documentation
MetasploitMCP includes a monkeypatch module (metasploit_mcp/jsonrpc_patch.py) that adds JSON-RPC protocol support to pymetasploit3. This allows runtime selection between msgpack RPC (default) and JSON-RPC via the MSF_RPC_PROTOCOL environment variable.
The patch intercepts pymetasploit3's serialization/deserialization methods:
pymetasploit3.utils.encode()- Serializes RPC requestspymetasploit3.utils.decode()- Deserializes RPC responsesMsfRpcClient.__init__()- Sets HTTP Content-Type headersMsfRpcClient.post_request()- Ensures correct headers for protocol
# Use JSON-RPC
export MSF_RPC_PROTOCOL=jsonrpc
# Use msgpack (default)
export MSF_RPC_PROTOCOL=msgpack
# or omit the variable# Run JSON-RPC patch tests
poetry run pytest tests/test_jsonrpc_patch.py -v
# Test with JSON-RPC enabled
MSF_RPC_PROTOCOL=jsonrpc poetry run metasploit-mcp --transport httpThe patch is automatically applied when the module is imported (if JSON-RPC is enabled). It must be imported before any MsfRpcClient instances are created, which is why it's imported early in metasploit_mcp/server.py.
- Unit Tests: Test individual functions and methods
- Integration Tests: Test tool workflows with mocked Metasploit backend
- IP Validation Tests: Test bind address validation logic
- Use descriptive test names that explain what is being tested
- Follow the AAA pattern: Arrange, Act, Assert
- Mock external dependencies (Metasploit RPC, network calls)
- Test both success and error cases
- Use appropriate fixtures from
conftest.py
mock_asyncio_to_thread: Mocksasyncio.to_threadfor async testingmock_client: Provides a mocked Metasploit RPC clientmock_exploit_environment: Sets up exploit testing environment
| Variable | Default | Description |
|---|---|---|
MSF_PASSWORD |
yourpassword |
Metasploit RPC password |
MSF_SERVER |
127.0.0.1 |
Metasploit RPC server address |
MSF_PORT |
55553 |
Metasploit RPC port |
MSF_SSL |
false |
Enable SSL for Metasploit RPC |
MSF_RPC_PROTOCOL |
msgpack |
RPC protocol: msgpack (default) or jsonrpc |
PAYLOAD_SAVE_DIR |
~/payloads |
Directory to save generated payloads |
LOG_LEVEL |
INFO |
Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name - Make your changes and add tests
- Ensure all tests pass:
poetry run pytest - Update documentation if needed
- Commit with descriptive messages
- Push to your fork and create a Pull Request
- Follow PEP 8 Python style guidelines
- Use type hints where appropriate
- Write docstrings for public functions and classes
- Keep functions focused and reasonably sized
- Use meaningful variable and function names
Use conventional commit format:
feat:for new featuresfix:for bug fixesdocs:for documentation changestest:for test additions/changesrefactor:for code refactoringchore:for maintenance tasks
- Metasploit Connection Failed: Ensure Metasploit RPC is running and credentials are correct
- Port Already in Use: Use
--find-portflag or specify a different port - Import Errors: Ensure Poetry environment is activated
# Enable debug logging
LOG_LEVEL=DEBUG poetry run metasploit-mcp --transport http# Start Metasploit with RPC
msfconsole -q
msf6 > load msgrpc ServerHost=127.0.0.1 ServerPort=55553 User=msf Pass=yourpassword
# In another terminal, run the server
MSF_PASSWORD=yourpassword poetry run metasploit-mcp --transport http