Django Migrations MCP server

Integrates Django migrations across distributed services, enabling coordinated database schema changes and enhanced migration safety for large-scale projects.
Back to servers
Setup instructions
Provider
mrrobotke
Release date
Feb 12, 2025
Language
Python
Stats
5 stars

The Django Migrations MCP Service is a practical tool that makes it easier to manage Django migrations in distributed environments by wrapping Django's migration commands into MCP endpoints. This service simplifies migration management across multiple services and integrates well with CI/CD pipelines.

Installation

Local Setup

  1. Clone the repository and install dependencies:
git clone https://github.com/mrrobotke/django-migrations-mcp.git
cd django-migrations-mcp
pip install -r requirements.txt

Configuration

Set the necessary environment variables:

export DJANGO_SETTINGS_MODULE="your_project.settings"
export MCP_SERVICE_PORT=8000  # Optional, defaults to 8000

Usage

Starting the Service

You can run the service either directly with Python or using Docker:

Python method:

python -m migrations_mcp.service

Docker method:

docker build -t django-migrations-mcp .
docker run -e DJANGO_SETTINGS_MODULE=your_project.settings \
          -v /path/to/your/django/project:/app/project \
          -p 8000:8000 \
          django-migrations-mcp

Using MCP Endpoints

The service provides several endpoints that correspond to Django's migration commands:

Show Migrations

To check the current migration status (equivalent to Django's showmigrations):

from mcp import MCPClient

client = MCPClient()
migrations = await client.call("show_migrations")

Make Migrations

To create new migrations with validation:

result = await client.call("make_migrations", {
    "app_labels": ["myapp"],  # Optional
    "dry_run": True  # Optional
})

Apply Migrations

To apply migrations with safety checks:

result = await client.call("migrate", {
    "app_label": "myapp",  # Optional
    "migration_name": "0001",  # Optional
    "fake": False,  # Optional
    "plan": True  # Optional
})

HTTP API Access

You can also access the service using HTTP requests:

Show Migrations:

curl -X POST http://localhost:8000/mcp \
  -H "Content-Type: application/json" \
  -d '{"method": "show_migrations"}'

Make Migrations:

curl -X POST http://localhost:8000/mcp \
  -H "Content-Type: application/json" \
  -d '{"method": "make_migrations", "params": {"apps": ["your_app"]}}'

Apply Migrations:

curl -X POST http://localhost:8000/mcp \
  -H "Content-Type: application/json" \
  -d '{"method": "migrate", "params": {"app": "your_app"}}'

CI/CD Integration

Here's an example GitHub Actions workflow for migration checks:

name: Django Migrations Check

on:
  pull_request:
    paths:
      - '*/migrations/*.py'
      - '*/models.py'

jobs:
  check-migrations:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v2
    
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.11'
    
    - name: Install dependencies
      run: |
        pip install -r requirements.txt
    
    - name: Start MCP service
      run: |
        python -m migrations_mcp.service &
    
    - name: Check migrations
      run: |
        python ci/check_migrations.py

Example migration check script:

import asyncio
from mcp import MCPClient

async def check_migrations():
    client = MCPClient()
    
    # Check current status
    migrations = await client.call("show_migrations")
    
    # Try making migrations
    result = await client.call("make_migrations", {"dry_run": True})
    if not result.success:
        print(f"Error: {result.message}")
        exit(1)
    
    print("Migration check passed!")

if __name__ == "__main__":
    asyncio.run(check_migrations())

Docker Configurations

Basic Setup

docker run -d \
  --name django-migrations-mcp \
  -e DJANGO_SETTINGS_MODULE=your_project.settings \
  -e MCP_SERVICE_PORT=8000 \
  -v /path/to/your/django/project:/app/project \
  -p 8000:8000 \
  django-migrations-mcp

With Redis Integration

docker run -d \
  --name django-migrations-mcp \
  -e DJANGO_SETTINGS_MODULE=your_project.settings \
  -e MCP_SERVICE_PORT=8000 \
  -e REDIS_URL=redis://host.docker.internal:6379 \
  -v /path/to/your/django/project:/app/project \
  -p 8000:8000 \
  --network host \
  django-migrations-mcp

Production Environment

docker run -d \
  --name django-migrations-mcp \
  -e DJANGO_SETTINGS_MODULE=your_project.settings \
  -e MCP_SERVICE_PORT=8000 \
  -e REDIS_URL=redis://your-redis-host:6379 \
  -v /path/to/your/django/project:/app/project \
  -p 8000:8000 \
  --restart unless-stopped \
  --network your-network \
  django-migrations-mcp

How to install this MCP server

For Claude Code

To add this MCP server to Claude Code, run this command in your terminal:

claude mcp add-json "django-migrations-mcp" '{"command":"docker","args":["run","-d","--name","django-migrations-mcp","-e","DJANGO_SETTINGS_MODULE=your_project.settings","-e","MCP_SERVICE_PORT=8000","-v","/path/to/your/django/project:/app/project","-p","8000:8000","django-migrations-mcp"]}'

See the official Claude Code MCP documentation for more details.

For Cursor

There are two ways to add an MCP server to Cursor. The most common way is to add the server globally in the ~/.cursor/mcp.json file so that it is available in all of your projects.

If you only need the server in a single project, you can add it to the project instead by creating or adding it to the .cursor/mcp.json file.

Adding an MCP server to Cursor globally

To add a global MCP server go to Cursor Settings > Tools & Integrations and click "New MCP Server".

When you click that button the ~/.cursor/mcp.json file will be opened and you can add your server like this:

{
    "mcpServers": {
        "django-migrations-mcp": {
            "command": "docker",
            "args": [
                "run",
                "-d",
                "--name",
                "django-migrations-mcp",
                "-e",
                "DJANGO_SETTINGS_MODULE=your_project.settings",
                "-e",
                "MCP_SERVICE_PORT=8000",
                "-v",
                "/path/to/your/django/project:/app/project",
                "-p",
                "8000:8000",
                "django-migrations-mcp"
            ]
        }
    }
}

Adding an MCP server to a project

To add an MCP server to a project you can create a new .cursor/mcp.json file or add it to the existing one. This will look exactly the same as the global MCP server example above.

How to use the MCP server

Once the server is installed, you might need to head back to Settings > MCP and click the refresh button.

The Cursor agent will then be able to see the available tools the added MCP server has available and will call them when it needs to.

You can also explicitly ask the agent to use the tool by mentioning the tool name and describing what the function does.

For Claude Desktop

To add this MCP server to Claude Desktop:

1. Find your configuration file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2. Add this to your configuration file:

{
    "mcpServers": {
        "django-migrations-mcp": {
            "command": "docker",
            "args": [
                "run",
                "-d",
                "--name",
                "django-migrations-mcp",
                "-e",
                "DJANGO_SETTINGS_MODULE=your_project.settings",
                "-e",
                "MCP_SERVICE_PORT=8000",
                "-v",
                "/path/to/your/django/project:/app/project",
                "-p",
                "8000:8000",
                "django-migrations-mcp"
            ]
        }
    }
}

3. Restart Claude Desktop for the changes to take effect

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later