AI代码审查服务

一个基于FastAPI和Azure OpenAI构建的代码审查API服务,可自动分析合并请求差异并返回结构化评审建议,适用于开发团队的CI/CD流程。

  • by yogi0001122
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 yogi0001122/code-review-mcp

使用说明

Azure OpenAI Code Review PoC

Azure OpenAI Code Review is a proof of concept that demonstrates automated code review using FastAPI and Azure OpenAI.

The FastAPI service accepts merge request diffs or code snippets through an HTTP API, sends the submitted content to a configured Azure OpenAI model deployment, and returns structured review findings and recommendations.

The current implementation follows an MCP-inspired client-and-agent interaction pattern but does not implement the official Model Context Protocol or use the official MCP SDK.

Purpose

This proof of concept is designed to:

  • Explore how large language models can automate merge request code reviews.
  • Validate communication between the FastAPI service and Azure OpenAI endpoints.
  • Evaluate the generation of structured, context-aware code review recommendations.
  • Establish a foundation for a future production-grade solution with enhanced context management, security guardrails, observability, and scalability.

Note: The current implementation uses a FastAPI-based HTTP interface and does not implement the official Model Context Protocol or use the official MCP SDK.

Future Roadmap: MCP Server Integration

This PoC provides the foundation for a scalable, production-grade AI Code Review Assistant.

Future versions are expected to implement an official MCP server architecture to support:

  • Standardized communication between MCP clients and the code review service.
  • MCP tools for reviewing merge request diffs and code snippets.
  • Integration with GitLab merge requests and GitHub pull requests.
  • Improved repository and project-level context handling.
  • Configurable coding standards and review policies.
  • Security, privacy, and responsible AI guardrails.
  • Authentication, authorization, monitoring, and audit logging.
  • Scalable deployment and automated CI/CD integration.

Table of Contents

  1. Features
  2. Prerequisites
  3. Installation
  4. Running Locally
  5. Kubernetes Deployment
  6. Environment Variables & Secrets
  7. API Usage
  8. Sample Test
  9. Troubleshooting
  10. Contributing
  11. License

Features

  • Review Merge Requests automatically using Azure OpenAI.
  • Detects code issues, typos, and provides suggestions.
  • REST API endpoint for integration with other tools.
  • Can run locally or in Kubernetes.

Prerequisites

  • Python 3.11+
  • Docker (optional for containerization)
  • Kubernetes cluster (for deployment i.e. local setup using minikube)
  • Azure OpenAI subscription and deployed model
  • kubectl and Helm installed if deploying to Kubernetes

Installation

  1. Clone the repository:
git clone <repo-url>
cd <repo-directory>
  1. Create a Python virtual environment:
python -m venv venv
source venv/bin/activate   # Linux/macOS
venv\Scripts\activate      # Windows
  1. Install dependencies:
pip install -r requirements.txt

Environment Variables & Secrets

This project requires the following environment variables. Store them in a .env file at the root of your project:

# .env file

# Your Azure OpenAI API key
AZURE_OPENAI_KEY=your_azure_openai_key_here

# Your Azure OpenAI endpoint URL
AZURE_OPENAI_ENDPOINT=https://your-resource-name.cognitiveservices.azure.com/

# The deployed model name in Azure OpenAI
MODEL_NAME=gpt-4o-mini-2

Running Locally

Start the FastAPI application:

uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload

Access the API:


Kubernetes Deployment

  1. Build Docker image:
docker build -t code-review-mcp:latest .
  1. Push to container registry (optional):
docker tag code-review-mcp:latest <registry-url>/code-review-mcp:latest
docker push <registry-url>/code-review-mcp:latest
  1. Deploy using Helm:
# Create or overwrite helm/values.yaml with the required configuration

cat <<EOF > helm/values.yaml
image:
  repository: image.registry.com/code-review-mcp
  tag: latest

service:
  port: 8000

azureOpenAI:
  endpoint: "https://your-azure-openai-endpoint.openai.azure.com/"
  key: "your-azure-openai-key"
  model_name: "gpt-4o-mini-2"
EOF
helm install code-review-mcp ./helm-chart -n ai-review --create-namespace
  1. Verify pod status:
kubectl get pods -n ai-review
kubectl logs -f pod/<pod-name> -n ai-review


Sample Test

Run the following curl command to test locally:

curl -X POST http://localhost:8080/mcp/tool-call \
-H "Content-Type: application/json" \
-d '{"tool":"review_merge_request","arguments":{"diff":"print(\"prinf(hello)\")"}}'
### Merge Request Review Summary

It looks like you've shared a line from a GitLab Merge Request (MR) diff.  
In the code snippet, there's a small typo in the `print` function — it’s incorrectly written as `prinf`.

### **Suggested Change**

print("hello")


Troubleshooting

  • SSL Issues with Azure OpenAI:
    Make sure the pod or local machine can reach the Azure endpoint. For Kubernetes, consider using host networking or check firewall rules.

  • Internal Server Error (500):
    Check logs:

    kubectl logs -f pod/<pod-name> -n ai-review
    

    Ensure that environment variables are correctly set and loaded.

  • Curl connection fails:
    Verify network connectivity to *.cognitiveservices.azure.com.


Contributing

  • Fork the repository
  • Create a feature branch: git checkout -b feature/my-feature
  • Commit changes: git commit -m 'Add new feature'
  • Push branch: git push origin feature/my-feature
  • Open a Pull Request