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
Provider
mrrobotke
Release date
Feb 12, 2025
Language
Python
Stats
4 stars

The Django Migrations MCP service wraps Django's migration commands and exposes them as Model Context Protocol (MCP) endpoints. This approach makes it easy to manage database migrations across multiple services and streamlines integration with CI/CD pipelines, especially in distributed environments.

Installation

Local Development Setup

  1. Clone the repository:
git clone https://github.com/mrrobotke/django-migrations-mcp.git
cd django-migrations-mcp
  1. Install dependencies:
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

Running the Service

Python Method

Start the service directly with Python:

python -m migrations_mcp.service

Docker Method

Run the service using Docker:

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 the MCP Endpoints

Show Migrations

Check the status of all migrations:

from mcp import MCPClient

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

Make Migrations

Create new Django migrations:

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

Apply Migrations

Run Django migrations:

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

CI/CD Integration

GitHub Actions Example

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

Migration Check Script

Example script to verify migrations in CI:

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

Using HTTP Clients

You can also use standard HTTP clients to interact with the service:

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

How to add this MCP server to 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 > MCP and click "Add new global MCP server".

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

{
    "mcpServers": {
        "cursor-rules-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "cursor-rules-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 explictly ask the agent to use the tool by mentioning the tool name and describing what the function does.

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