项目简介

pg-schema-sync MCP服务器是 pg-schema-sync 工具的 MCP (Model Context Protocol) 封装。它允许LLM客户端(如 Cline)通过标准化的MCP协议调用pg-schema-sync的功能,实现PostgreSQL数据库Schema的对比、同步和迁移。

主要功能点

  • Schema差异验证: 比较源数据库和目标数据库的Schema差异,并生成详细的报告。
  • Migration SQL生成: 根据源数据库Schema生成用于更新目标数据库Schema的SQL脚本,但不自动执行。
  • Schema Migration应用: 自动生成并执行SQL脚本,将目标数据库Schema同步到源数据库Schema的状态。
  • 支持多种数据库对象: 支持Enum, Table, View, Function, Index等多种PostgreSQL数据库对象的Schema同步。
  • 灵活的配置: 通过 'config.yaml' 文件配置源和目标数据库连接信息,支持排除特定表或索引。

安装步骤

  1. 安装 pg-schema-sync 基础包:

    pip install .

    或 (如果发布到PyPI)

    pip install pg-schema-sync
  2. 安装 MCP 服务器 Wrapper 依赖:

    pip install -r mcp_server/requirements.txt
  3. 配置数据库连接信息: 在 MCP 服务器启动的当前工作目录下创建 'config.yaml' 文件,并按照 仓库README 中的 "設定" 部分的说明,配置源数据库和目标数据库的连接信息。

服务器配置

为了让 MCP 客户端(如 Cline)连接到 pg-schema-sync MCP 服务器,您需要在客户端的服务器配置文件中添加以下配置信息。请注意,MCP客户端不需要修改或理解以下JSON代码,只需复制粘贴到客户端的服务器配置中即可。

{
  "serverName": "pg-schema-sync-mcp-server",
  "command": "python",
  "args": ["mcp_server/index.py"],
  "env": {
    "PG_SYNC_CONFIG_PATH": "/path/to/your/config.yaml"
  },
  "capabilities": {
    "tools": [
      {
        "name": "verify_schema",
        "description": "验证源数据库和目标数据库Schema的差异并生成报告。",
        "inputSchema": {
          "type": "object",
          "properties": {
            "target_name": {
              "type": "string",
              "description": "config.yaml 中目标数据库配置的键名"
            },
            "exclude_tables": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "description": "要排除的表名列表",
              "default": []
            },
            "exclude_indexes": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "description": "要排除的索引名列表",
              "default": []
            },
            "use_alter": {
              "type": "boolean",
              "description": "实验性功能: 使用 ALTER TABLE 进行列的添加/删除",
              "default": false
            }
          },
          "required": [
            "target_name"
          ]
        },
        "outputSchema": {
          "type": "object",
          "properties": {
            "report": {
              "type": "object",
              "description": "详细的每个对象类型的验证报告"
            },
            "overall_status": {
              "type": "string",
              "description": "整体同步状态信息"
            }
          }
        }
      },
      {
        "name": "generate_migration_sql",
        "description": "生成用于更新目标数据库Schema的Migration SQL,但不应用。",
        "inputSchema": {
          "type": "object",
          "properties": {
            "target_name": {
              "type": "string",
              "description": "config.yaml 中目标数据库配置的键名"
            },
            "exclude_tables": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "description": "要排除的表名列表",
              "default": []
            },
            "exclude_indexes": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "description": "要排除的索引名列表",
              "default": []
            },
            "use_alter": {
              "type": "boolean",
              "description": "实验性功能: 使用 ALTER TABLE 进行列的添加/删除",
              "default": false
            }
          },
          "required": [
            "target_name"
          ]
        },
        "outputSchema": {
          "type": "object",
          "properties": {
            "migration_sql": {
              "type": "string",
              "description": "生成的用于Migration的SQL语句"
            },
            "skipped_sql": {
              "type": "string",
              "description": "跳过对象的SQL注释 (已同步)"
            },
            "migration_filename": {
              "type": "string",
              "description": "建议的Migration SQL文件名"
            },
            "skipped_filename": {
              "type": "string",
              "description": "建议的跳过对象SQL文件名"
            }
          }
        }
      },
      {
        "name": "apply_schema_migration",
        "description": "生成并应用Migration SQL,以更新目标数据库Schema。",
        "inputSchema": {
          "type": "object",
          "properties": {
            "target_name": {
              "type": "string",
              "description": "config.yaml 中目标数据库配置的键名"
            },
            "exclude_tables": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "description": "要排除的表名列表",
              "default": []
            },
            "exclude_indexes": {
              "type": "array",
              "items": {
                "type": "string"
              },
              "description": "要排除的索引名列表",
              "default": []
            },
            "use_alter": {
              "type": "boolean",
              "description": "实验性功能: 使用 ALTER TABLE 进行列的添加/删除",
              "default": false
            }
          },
          "required": [
            "target_name"
          ]
        },
        "outputSchema": {
          "type": "object",
          "properties": {
            "success": {
              "type": "boolean",
              "description": "Migration是否成功应用"
            },
            "message": {
              "type": "string",
              "description": "状态信息或错误详情"
            },
            "log": {
              "type": "string",
              "description": "执行语句的日志 (如果适用)"
            }
          }
        }
      }
    ]
  }
}

配置参数说明:

  • 'serverName': 服务器名称,可以自定义。
  • 'command': 启动服务器的命令,这里使用 'python'。
  • 'args': 启动命令的参数,指向 'mcp_server/index.py' 脚本。
  • 'env': 环境变量配置。
    • 'PG_SYNC_CONFIG_PATH': 必须配置。指定 'config.yaml' 文件的绝对路径。请将 '/path/to/your/config.yaml' 替换为您实际的 'config.yaml' 文件路径。

基本使用方法

  1. 启动 MCP 服务器: 配置完成后,在 MCP 客户端中启动 'pg-schema-sync-mcp-server' 服务器。服务器将通过标准输入/输出 (stdio) 与客户端通信。

  2. 使用 MCP 客户端调用工具: 通过 MCP 客户端,您可以调用以下工具来管理 PostgreSQL 数据库Schema:

    • 'verify_schema': 验证Schema差异。需要提供 'target_name' (config.yaml中配置的目标数据库名称) 以及可选的 'exclude_tables', 'exclude_indexes' 参数。
    • 'generate_migration_sql': 生成Migration SQL。 需要提供 'target_name' 以及可选的 'exclude_tables', 'exclude_indexes', 'use_alter' 参数。
    • 'apply_schema_migration': 应用Schema Migration。 需要提供 'target_name' 以及可选的 'exclude_tables', 'exclude_indexes', 'use_alter' 参数。

    请参考每个工具的 'inputSchema' 了解详细的输入参数和格式。工具执行结果将通过 'outputSchema' 定义的格式返回。

注意: 'apply_schema_migration' 工具会直接修改目标数据库Schema,请务必谨慎操作,并在必要时备份数据库。 实验性的 'use_alter' 参数也请谨慎使用,并仔细评估其潜在的数据风险。

信息

分类

开发者工具