- Python Testing Tools MCP Server
Python Testing Tools MCP Server
Configuration
The server uses environment variables for configuration:
GEMINI_API_KEY: Required - Your Google Gemini API key for AI-powered test generationGEMINI_MODEL: Optional - Gemini model to use (default:gemini-2.5-flash)
The BAML configuration in baml_src/main.baml defines:
- AI function signatures for test generation, fuzz input creation, and coverage-focused test generation
- Structured response formats using BAML classes (PythonTestFile, FuzzInput, CoverageAnalysis)
- Detailed prompts for comprehensive test coverage, including branch and path analysis
Installation
- Create and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate
- Install dependencies from
requirements.txt:
pip install -r requirements.txt
- Set your Gemini API key:
export GEMINI_API_KEY="your-api-key-here"
Running the Server
To run the MCP server:
python mcp_server.py
Running the Server in Claude Code
The documentation for FastMCP and Claude Code can be found here: https://gofastmcp.com/integrations/claude-code
Step 1: Install the MCP Server
Install the server using FastMCP:
fastmcp install claude-code python_testing_mcp_server.py --env GEMINI_API_KEY='your-api-key-here'
Step 2: Configure Claude Code
Your .claude.json configuration should look like this:
{
"/path/to/your/project": {
"allowedTools": [],
"history": [],
"mcpContextUris": [],
"mcpServers": {
"python_testing_tools": {
"type": "stdio",
"command": "uv",
"args": [
"run",
"--with",
"fastmcp",
"fastmcp",
"run",
"/path/to/your/project/python_testing_mcp_server.py"
],
"env": {
"GEMINI_API_KEY": "your-api-key-here"
}
}
},
"enabledMcpjsonServers": [],
"disabledMcpjsonServers": [],
"hasTrustDialogAccepted": false,
"projectOnboardingSeenCount": 0,
"hasClaudeMdExternalIncludesApproved": false,
"hasClaudeMdExternalIncludesWarningShown": false
}
}
Note: Replace /path/to/your/project with the actual path to your project directory, and your-api-key-here with your actual Gemini API key.
Step 3: Start Claude Code
Start Claude Code:
claude
Step 4: Verify Connection
Check if the MCP server is connected:
/mcp
Step 5: Test the Tools
Test the unit test generation tool:
create unit tests for @demo/basic_example_functions.py
Test the coverage testing tool:
generate comprehensive coverage tests for @demo/basic_example_functions.py
Test the fuzz testing tool:
fuzz test the add function in @demo/basic_example_functions.py
These should create test files in the demo folder with comprehensive test coverage.
Available Tools
Unit Test Generator
- Function:
generate_unit_tests_tool - Description: Generates comprehensive unit tests for all functions in a Python file using AI
- Parameters:
file_path(str) - Path to the Python file to generate tests for
- Features:
- Generates 4-6 test cases per function covering normal, edge, and error cases
- Uses proper unittest framework with
self.assertRaises()for exception testing - Automatically handles module imports and class naming conventions
- Creates properly formatted test files with correct indentation
Fuzz Tester
- Function:
fuzz_test_function_tool - Description: Performs intelligent fuzz testing on a specific function using AI-generated inputs
- Parameters:
file_path(str) - Path to the Python file containing the functionfunction_name(str) - Name of the specific function to fuzz test
- Features:
- Generates 20 diverse test inputs including edge cases and malformed data
- Tests with boundary values, large inputs, and unusual data combinations
- Reports crashes with detailed error traces and input values
- Uses
ast.literal_eval()for safe input parsing
Coverage Tester
- Function:
generate_coverage_tests_tool - Description: Generates comprehensive test cases designed to achieve maximum code coverage using advanced AST analysis and AI-powered test generation
- Parameters:
file_path(str) - Path to the Python file to generate coverage tests for
- Features:
- Advanced AST Analysis: Automatically detects branches, loops, exception paths, and return statements
- Intelligent Coverage Targeting: Generates tests specifically designed to cover all code paths
- Built-in Coverage Measurement: Integrates coverage.py for real-time coverage reporting
- Comprehensive Test Scenarios: Creates tests for:
- Branch coverage (if/elif/else conditions)
- Loop coverage (zero, one, multiple iterations, break/continue)
- Exception coverage (try/except/finally blocks)
- Return path coverage (all possible return statements)
- Parameter coverage (different types, boundary values, edge cases)
- Type compatibility testing (invalid inputs, TypeError scenarios)
- State-of-the-Art Testing: Includes infinity/NaN testing, large number handling, empty collections
- Automatic Import Resolution: Properly includes all necessary imports (sys, math, etc.)
- Coverage Reporting: Generates detailed coverage reports showing achieved coverage percentages
Server Config
{
"/path/to/your/project": {
"mcpServers": {
"python_testing_tools": {
"type": "stdio",
"command": "uv",
"args": [
"run",
"--with",
"fastmcp",
"fastmcp",
"run",
"/path/to/your/project/python_testing_mcp_server.py"
],
"env": {
"GEMINI_API_KEY": "your-api-key-here"
}
}
}
}
}