Skip to main content
Glama
README.md
<p align="center">
  <img src="assets/banner.svg" alt="ENICar MCP Code Reviewer Banner" width="100%" />
</p>

<p align="center">
  <img src="https://img.shields.io/badge/Python-3.11%2B-3776AB?style=for-the-badge&logo=python&logoColor=white" alt="Python 3.11+"/>
  <img src="https://img.shields.io/badge/LangGraph-Multi--Agent-0F172A?style=for-the-badge&logo=langchain&logoColor=white" alt="LangGraph"/>
  <img src="https://img.shields.io/badge/MCP-Server%20SDK-8B5CF6?style=for-the-badge" alt="MCP Server"/>
  <img src="https://img.shields.io/badge/FastAPI-Webhook%20API-009688?style=for-the-badge&logo=fastapi&logoColor=white" alt="FastAPI"/>
  <img src="https://img.shields.io/badge/License-MIT-22C55E?style=for-the-badge" alt="MIT License"/>
  <img src="https://img.shields.io/badge/Author-Oussama%20Mejri%20(ENICarthage)-0EA5E9?style=for-the-badge" alt="ENICarthage"/>
</p>

# ENICar MCP Code Reviewer

A multi-agent pull request reviewer built on **Claude tool calling**, **LangGraph**, **FastAPI**, and a custom **Model Context Protocol (MCP) server**. It reads a pull request diff, detects bugs and security vulnerabilities, pauses for human approval via LangGraph interrupts, and posts inline review comments to GitHub.

It also ships with a **30-case evaluation suite** of PR diffs with seeded bugs so prompt or model changes are measured objectively.

Developed and maintained by **Oussama Mejri**, engineering student at **ENICarthage** (*École Nationale d'Ingénieurs de Carthage*).

---

## Architecture & Workflow

```mermaid
flowchart LR
    GH["GitHub PR"] -->|"Webhook (HMAC-SHA256)"| S["FastAPI Service\n(server.py)"]
    S --> F["fetch_diff"]
    F --> P["1. Planner"]
    P --> R["2a. Security & Code Reviewer"]
    P --> T["2b. Sandboxed Test Runner"]
    R --> Z["3. Summarizer"]
    T --> Z
    Z --> H{"4. Human Approval\n(LangGraph interrupt)"}
    H -->|"Approved"| POST["5. post_review"]
    H -->|"Rejected"| E(("END"))
    POST --> GH

    subgraph MCP ["Custom MCP Server (mcp_server.py)"]
      M1["get_pr_diff"]
      M2["read_file"]
      M3["run_tests"]
      M4["post_review_comment"]
    end
    CD["Claude Desktop / MCP Client"] --> M1
```

---

## Components Overview

| Component | Purpose |
|---|---|
| `src/review_agent/mcp_server.py` | MCP server (Python MCP SDK) exposing `get_pr_diff`, `read_file`, `run_tests`, and `post_review_comment` over stdio or streamable HTTP (`:8765`). |
| `src/review_agent/graph.py` | LangGraph workflow: `fetch_diff` → `planner` → parallel `security_reviewer` & `test_runner` → `summarizer` → `human_approval` interrupt → `post_review`. |
| `src/review_agent/llm.py` | Explicit Claude tool-calling loop with schema-validated Pydantic `submit_result` output, automatic invalid-output repair, and per-call token/cost tracking. |
| `src/review_agent/server.py` | FastAPI webhook service verifying `X-Hub-Signature-256`, running background reviews with SQLite LangGraph checkpointing, and exposing admin endpoints (`/reviews`, `/reviews/{id}/decision`). |
| `src/review_agent/diff.py` | Unified diff parser that prefixes lines with new-file line numbers for accurate inline GitHub comments. |
| `src/review_agent/workspace.py` | GitHub REST client & local workspace abstraction, separating inline hunk comments from body comments. |
| `src/review_agent/tools.py` | Sandboxed test runner scoped to `REVIEW_WORKDIR` (disabled by default in the webhook server). |
| `src/review_agent/cli.py` | Command-line interface (`review-agent`) for local diffs and GitHub PRs. |
| `evals/` | 30 seeded-bug PR diffs across security, correctness, concurrency, reliability, and performance with precision/recall & latency reporting. |
| `.github/workflows/ci.yml` | Ruff lint, unit tests, Docker image build, and automated evaluation workflow. |
| `docs/deploy-aws.md` | Deployment guide for AWS EC2 with Docker and Caddy HTTPS. |

---

## Quick Start

```bash
git clone https://github.com/oussama-mejri007/enicar-mcp-code-reviewer.git
cd enicar-mcp-code-reviewer

python -m venv .venv
# Linux / macOS:
source .venv/bin/activate
# Windows:
# .venv\Scripts\activate

pip install -e ".[dev]"
cp .env.example .env   # Add ANTHROPIC_API_KEY and GITHUB_TOKEN

pytest -q                                   # Runs unit tests with fake client (no API key needed)

review-agent review owner/repo#42           # Review a GitHub PR (prompts before posting)
review-agent review --diff my.diff          # Review a local diff file
```

---

## Using as an MCP Server (Claude Desktop)

Add the following configuration to `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "code-review": {
      "command": "/path/to/.venv/bin/review-agent-mcp",
      "env": { "GITHUB_TOKEN": "ghp_..." }
    }
  }
}
```

Or launch via streamable HTTP:

```bash
review-agent-mcp --http
```

Then ask your MCP client: *"Get the diff for oussama-mejri007/demo PR 3 and point out any security issues."*

---

## Evaluation Suite

```bash
python -m evals.run_eval                                   # Run all 30 seeded-bug cases
python -m evals.run_eval --model claude-haiku-4-5-20251001 --limit 10
```

Each case pairs realistic code changes with a planted bug (SQL injection, unverified JWTs, missing authorization, off-by-one pagination, TOCTOU races, SSRF, N+1 queries, etc.). A bug counts as detected when a comment lands on the target file within two lines of the planted bug; extra comments count as false positives. Results are exported to `evals/results/` as JSON and Markdown.

---

## Design Decisions

- **Explicit Tool Loop**: Enforced `tool_choice: any` with Pydantic schema validation on `submit_result`, feeding validation errors back to the model and tracking exact token usage per node.
- **Line-Numbered Diffs**: Every hunk line is prefixed with its new-file line number so inline GitHub comments target the right line reliably.
- **Human-in-the-Loop Interrupts**: LangGraph pauses at `human_approval` and checkpoints state in SQLite, surviving restarts while waiting for maintainer review.
- **Safe Inline vs. Body Splitting**: Comments outside diff hunks automatically move into the review summary body so GitHub never rejects the full review.
- **Sandboxed Test Execution**: Runs exclusively inside `REVIEW_WORKDIR`, disabled by default in webhook mode, with non-root execution inside Docker.

---

## Project Layout

```text
enicar-mcp-code-reviewer/
├── .github/workflows/ci.yml      # Lint, pytest, Docker build & eval workflow
├── assets/banner.svg             # Project header banner
├── docs/deploy-aws.md            # AWS EC2 + Caddy deployment guide
├── evals/                        # 30 seeded-bug cases & evaluation runner
├── src/review_agent/
│   ├── cli.py                    # CLI entrypoint
│   ├── diff.py                   # Unified diff parser & line numbering
│   ├── graph.py                  # LangGraph multi-agent workflow
│   ├── llm.py                    # Claude tool-calling loop & token accounting
│   ├── mcp_server.py             # MCP server exposing GitHub & test tools
│   ├── schemas.py                # Pydantic output schemas
│   ├── server.py                 # FastAPI webhook & approval API
│   ├── tools.py                  # Sandboxed test runner
│   └── workspace.py              # GitHub & local workspace abstraction
├── tests/                        # Unit tests (fake client, no network required)
├── Dockerfile                    # Non-root production container
├── LICENSE                       # MIT License (Oussama Mejri)
└── pyproject.toml                # Package metadata & dependencies
```

---

## License

This project is licensed under the **MIT License** — see the [LICENSE](LICENSE) file for details.

Copyright (c) 2026 **Oussama Mejri (ENICarthage)**

---

## Author

**Oussama Mejri**  
Engineering Student at **ENICarthage** (*École Nationale d'Ingénieurs de Carthage*)  
- GitHub: [@oussama-mejri007](https://github.com/oussama-mejri007)  
- Email: [mejri3954@gmail.com](mailto:mejri3954@gmail.com)