Skip to main content
Glama

Django Migrations MCP Service

by mrrobotke

Django 迁移 MCP 服务

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

特征

  • 检查迁移状态(相当于showmigrations
  • 创建带有验证的新迁移(相当于makemigrations
  • 应用带有安全检查的迁移(相当于migrate
  • 额外的验证和安全检查:
    • 顺序迁移顺序验证
    • 冲突检测
    • 依赖项验证
    • 迁移操作的安全性分析

安装

本地开发

  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

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

模型上下文协议服务将 Django 的迁移命令包装为 MCP 端点,从而可以轻松管理跨多个服务的迁移并与 CI/CD 管道集成。

  1. 特征
    1. 安装
      1. 本地开发
    2. 配置
      1. 用法
        1. 运行服务
        2. MCP 端点
      2. CI/CD 集成
        1. 发展
          1. 运行测试
          2. 代码风格
        2. 执照
          1. 贡献
            1. Docker 使用
              1. 可用的 Docker 配置
              2. 以编程方式使用命令
              3. 网络设置
              4. 使用 MCP 工具

            Related MCP Servers

            • A
              security
              A
              license
              A
              quality
              A dynamic service that creates and manages Model Context Protocol (MCP) servers, allowing users to spawn, customize, and control multiple MCP servers as child processes.
              Last updated -
              5
              0
              84
              TypeScript
              MIT License
              • Apple
              • Linux
            • -
              security
              A
              license
              -
              quality
              A Model Context Protocol (MCP) implementation for connecting to and working with various database systems.
              Last updated -
              17
              18
              TypeScript
              MIT License
              • Linux
              • Apple
            • A
              security
              F
              license
              A
              quality
              An all-in-one Model Context Protocol (MCP) server that connects your coding AI to numerous databases, data warehouses, data pipelines, and cloud services, streamlining development workflow through seamless integrations.
              Last updated -
              2
              Python
              • Apple
              • Linux
            • -
              security
              A
              license
              -
              quality
              A powerful Model Context Protocol (MCP) server implementation that provides standardized interaction with MongoDB databases, supporting complete CRUD operations, async patterns, and real-time updates via SSE.
              Last updated -
              Python
              MIT License

            View all related MCP servers

            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