Skip to main content
Glama
mrrobotke

Django Migrations MCP Service

by mrrobotke

Django 迁移 MCP 服务

一个模型上下文协议 (MCP) 服务,用于管理分布式环境中的 Django 迁移。该服务包装了 Django 的迁移命令并将其公开为 MCP 端点,从而可以轻松管理跨多个服务的迁移并与 CI/CD 流水线集成。

特征

  • 检查迁移状态(相当于showmigrations

  • 创建带有验证的新迁移(相当于makemigrations

  • 应用带有安全检查的迁移(相当于migrate

  • 额外的验证和安全检查:

    • 顺序迁移顺序验证

    • 冲突检测

    • 依赖项验证

    • 迁移操作的安全性分析

Related MCP server: mcp-dbs

安装

本地开发

  1. 克隆存储库:

git clone https://github.com/mrrobotke/django-migrations-mcp.git cd django-migrations-mcp
  1. 安装依赖项:

pip install -r requirements.txt

配置

设置以下环境变量:

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

用法

运行服务

  1. 直接使用 Python:

python -m migrations_mcp.service
  1. 使用 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

MCP 端点

  1. 显示迁移:

from mcp import MCPClient client = MCPClient() migrations = await client.call("show_migrations")
  1. 进行迁移:

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

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

CI/CD 集成

GitHub Actions 工作流程示例:

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

check_migrations.py 脚本示例:

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())

发展

运行测试

pytest migrations_mcp/tests/

代码风格

该项目遵循 PEP 8 准则。请使用以下格式格式化您的代码:

black migrations_mcp/ isort migrations_mcp/

执照

MIT 许可证。详情请参阅 LICENSE 文件。

贡献

  1. 分叉存储库

  2. 创建你的功能分支( git checkout -b feature/amazing-feature

  3. 提交您的更改( git commit -m 'Add amazing feature'

  4. 推送到分支( git push origin feature/amazing-feature

  5. 打开拉取请求

Docker 使用

该项目包含一个docker-commands.json文件,该文件为不同的部署场景提供了结构化的命令。您可以直接使用这些命令,也可以在脚本中解析它们。

可用的 Docker 配置

  1. Redis MCP 服务器

# Run Redis MCP server docker run -i --rm mcp/redis redis://host.docker.internal:6379
  1. Django 迁移 MCP 服务器

# 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
  1. 开发环境

# Using docker-compose docker-compose up -d --build
  1. 测试环境

# Run tests in container docker run --rm \ -e DJANGO_SETTINGS_MODULE=your_project.settings \ -e PYTHONPATH=/app \ -v ${PWD}:/app \ django-migrations-mcp \ pytest
  1. 生产环境

# Production setup with health check 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

以编程方式使用命令

您可以以编程方式解析和使用这些命令:

import json import subprocess # Load commands with open('docker-commands.json') as f: commands = json.load(f) # Run Redis MCP server redis_config = commands['mcpServers']['redis'] subprocess.run([redis_config['command']] + redis_config['args']) # Run Django Migrations MCP server django_config = commands['mcpServers']['djangoMigrations'] subprocess.run([django_config['command']] + django_config['args'])

网络设置

  1. 开发网络

docker network create mcp-dev-network
  1. 生产网络

docker network create --driver overlay --attachable mcp-prod-network

使用 MCP 工具

该服务公开了几个可以通过 curl 或任何 HTTP 客户端访问的端点:

  1. 显示迁移

curl -X POST http://localhost:8000/mcp \ -H "Content-Type: application/json" \ -d '{"method": "show_migrations"}'
  1. 进行迁移

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

curl -X POST http://localhost:8000/mcp \ -H "Content-Type: application/json" \ -d '{"method": "migrate", "params": {"app": "your_app"}}'
-
security - not tested
F
license - not found
-
quality - not tested

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mrrobotke/django-migrations-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server