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.
git clone https://github.com/mrrobotke/django-migrations-mcp.git
cd django-migrations-mcp
pip install -r requirements.txt
Set the necessary environment variables:
export DJANGO_SETTINGS_MODULE="your_project.settings"
export MCP_SERVICE_PORT=8000 # Optional, defaults to 8000
Start the service directly with Python:
python -m migrations_mcp.service
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
Check the status of all migrations:
from mcp import MCPClient
client = MCPClient()
migrations = await client.call("show_migrations")
Create new Django migrations:
result = await client.call("make_migrations", {
"app_labels": ["myapp"], # Optional
"dry_run": True # Optional
})
Run Django migrations:
result = await client.call("migrate", {
"app_label": "myapp", # Optional
"migration_name": "0001", # Optional
"fake": False, # Optional
"plan": True # Optional
})
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 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 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
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
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"}}'
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.
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"
]
}
}
}
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.
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.