Autonomous code generation agent for DPAIA tasks using Claude AI and MCP tools.
- Python 3.8+
- Anthropic API key
- Generated
LIB.pyfile (must be created separately) - MCP server URL
- Install dependencies:
pip install -r requirements.txt- Create
.envfile:
cp .env.example .env
# Edit .env and add your ANTHROPIC_API_KEYpython codegen_dpaia_agent.py \
--lib-path /path/to/LIB.py \
--mcp-url http://your-mcp-server-url--lib-path: Path to generated LIB.py file (required)--mcp-url: MCP server URL (required)--config: Path to JSON configuration file--repo: Repository in format owner/repo--issue-url: GitHub issue URL--issue-title: Issue title--issue-body: Issue description--model: Claude model to use (default: claude-sonnet-4-20250514)--max-tokens: Max tokens per request (default: 8000)--execute: Execute generated code and iteratively improve based on errors--max-iterations: Maximum number of iterations when using --execute (default: 12)--output: Output file path for generated code (see Output Behavior below)--instance-id: Custom instance ID (default: timestamp)--log-level: Logging level: DEBUG, INFO, WARNING, ERROR (default: INFO)
python codegen_dpaia_agent.py \
--lib-path ./LIB.py \
--mcp-url http://localhost:8080 \
--repo owner/repo \
--issue-title "Fix N+1 query problem" \
--issue-body "Detect and resolve N+1 SELECT queries" \
--execute \
--max-iterations 10 \
--output generated_code.pyThe agent follows a simple iterative pipeline:
-
Repository Setup: Clones the target repository into an isolated instance
-
LIB Loading: Loads the generated LIB.py file containing MCP tool functions
-
Code Generation (two modes):
- Without execution: Generates thinking → generates code → saves results
- With execution (
--executeflag): Iteratively:- Generates thinking about the task
- Generates code based on thinking and previous context
- Executes the code
- Analyzes execution results and errors
- Uses error feedback to improve in the next iteration
- Repeats until success or max iterations reached
-
Output: Saves generated code, execution logs, and iteration history:
results_codegen/- execution logsgenerated_code_iterations/<instance_id>/iteration_01.py,iteration_02.py, etc. - individual iteration code files--outputfile (if specified):- Without
--execute: Saves the single iteration's code - With
--execute: Saves all iterations concatenated into one file with headers (# === ITERATION 1 ===,# === ITERATION 2 ===, etc.)
- Without