Skip to content

learnwithparam/mcp-pr-review-server

Repository files navigation

Building MCP Servers for GitHub PR Review

learnwithparam.com

Build a production Model Context Protocol (MCP) server that connects to GitHub, fetches pull request diffs, runs LLM-powered code review, and posts structured review comments back. Exposes MCP tools any MCP client (Claude Desktop, Cursor) can call.

Start learning at learnwithparam.com. Regional pricing available with discounts of up to 60%.

What You'll Learn

  • MCP server architecture with stdio and HTTP transports
  • Exposing tools and resources through MCP SDK decorators
  • Fetching pull request diffs with PyGithub
  • Structured review generation with Pydantic schemas
  • Posting review comments back to GitHub programmatically
  • Running a dual-mode container that serves both an MCP server and a FastAPI inspector

Tech Stack

  • Python MCP SDK (mcp) - Official Model Context Protocol server library
  • FastAPI - Companion HTTP inspection service on top of the same business logic
  • PyGithub - GitHub REST API client for diffs and reviews
  • Pydantic - Typed request/response schemas for structured reviews
  • LLM Provider Pattern - Supports Fireworks, OpenRouter, Gemini, OpenAI
  • Docker - Containerized development

Getting Started

Prerequisites

  • Python 3.11+
  • uv (installed automatically by make setup)
  • A GitHub personal access token with repo scope
  • An API key from any supported LLM provider

Quick Start

# One command to set up and run the FastAPI inspector
make dev

# Or step by step:
make setup          # Create .env and install dependencies
# Edit .env with your API key and GITHUB_TOKEN
make run            # Start the FastAPI server

Run the MCP Server

make mcp            # Runs `python mcp_server.py` over stdio

Connect to Claude Desktop

Add this to your claude_desktop_config.json so Claude Desktop can spawn the MCP server over stdio and call its tools directly:

{
  "mcpServers": {
    "pr-review": {
      "command": "uv",
      "args": ["run", "python", "mcp_server.py"],
      "cwd": "/absolute/path/to/mcp-pr-review-server",
      "env": {
        "GITHUB_TOKEN": "ghp_...",
        "OPENAI_API_KEY": "sk-..."
      }
    }
  }
}

Restart Claude Desktop and the fetch_pr_diff, review_pr, and post_review_comments tools will appear in the tool picker. Cursor and any other MCP client follow the same pattern.

With Docker

make build          # Build the Docker image
make up             # Start the container
make logs           # View logs
make down           # Stop the container

API Documentation

Once the FastAPI service is running, open http://localhost:8000/docs for the interactive Swagger UI. The /inspect/{owner}/{repo}/{pr_number} endpoint is handy for debugging the pipeline outside an MCP client.

Challenges

Work through these incrementally to build the full application:

  1. MCP Server Skeleton - Stand up an MCP server with list_tools and a health check
  2. fetch_pr_diff Tool - Expose a tool that takes owner, repo, and pr_number and returns the unified diff
  3. review_pr Tool - Feed the diff to the LLM provider and return a structured review
  4. ReviewComment Schema - Model line-level comments with file, line, severity, and suggestion
  5. post_review_comments Tool - Post the structured review back to GitHub via PyGithub
  6. FastAPI Inspector - Add /inspect/{owner}/{repo}/{pr_number} to debug the pipeline without an MCP client

Makefile Targets

make help           Show all available commands
make setup          Initial setup (create .env, install deps)
make dev            Setup and run (one command!)
make run            Start FastAPI server
make mcp            Run MCP server over stdio
make build          Build Docker image
make up             Start container
make down           Stop container
make clean          Remove venv and cache

Learn more

About

Building MCP Servers for GitHub PR Review

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors