Azure PostgreSQL MCP Server 使用说明

项目简介

Azure PostgreSQL MCP Server (预览版) 是一个基于 Model Context Protocol (MCP) 标准构建的服务器,旨在让AI应用程序和模型能够以标准化的方式与 Azure Database for PostgreSQL 中托管的数据进行交互。通过此服务器,AI应用可以安全、可扩展地访问数据库上下文信息,实现更智能的数据交互。

主要功能点

  • 工具 (Tools):
    • 'query_data': 执行读取查询,从 Azure Database for PostgreSQL 数据库检索数据。
    • 'update_values': 在数据库中插入或更新记录。
    • 'create_table': 在数据库中创建新表。
    • 'drop_table': 删除数据库中的表。
  • 资源 (Resources):
    • 'Tables': 提供数据库中每个表的模式信息和元数据,包括列名和数据类型等。

安装步骤

  1. 克隆仓库:

    git clone https://github.com/Azure-Samples/azure-postgresql-mcp.git
    cd azure-postgresql-mcp

    或者,您也可以只下载 'azure_postgresql_mcp.py' 文件到您的工作目录。

  2. 创建虚拟环境:

    python -m venv azure-postgresql-mcp-venv

    激活虚拟环境 (根据您的操作系统选择相应的命令):

    • Windows (cmd.exe): '.\azure-postgresql-mcp-venv\Scripts\activate.bat'
    • Windows (PowerShell): '.\azure-postgresql-mcp-venv\Scripts\Activate.ps1'
    • Linux/MacOS: 'source ./azure-postgresql-mcp-venv/bin/activate'
  3. 安装依赖:

    pip install mcp[cli]
    pip install psycopg[binary]

服务器配置

重要: 以下配置信息用于配置 MCP 客户端 (如 Claude Desktop, Visual Studio Code) 连接到 Azure PostgreSQL MCP Server。您需要将此配置添加到您的 MCP 客户端设置中。

Claude Desktop 配置

在 Claude Desktop 应用的设置中,编辑 'claude_desktop_config.json' 文件,在 '"mcpServers"' 部分添加以下配置:

{
    "mcpServers": {
        "azure-postgresql-mcp": {
            "command": "<虚拟环境Python路径>",
            "args": [
                "<azure_postgresql_mcp.py 文件路径>"
            ],
            "env": {
                "PGHOST": "<Azure PostgreSQL 实例的完全限定名称>",
                "PGUSER": "<Azure PostgreSQL 用户名>",
                "PGPASSWORD": "<密码>",
                "PGDATABASE": "<数据库名称>"
            }
        }
    }
}

参数说明:

  • 'command': 指向您创建的虚拟环境中的 Python 解释器可执行文件的路径。
  • 'args': 包含 MCP 服务器脚本 'azure_postgresql_mcp.py' 文件路径的列表。
  • 'env': 环境变量配置,用于连接到您的 Azure PostgreSQL 数据库。
    • 'PGHOST': 您的 Azure Database for PostgreSQL 灵活服务器实例的完全限定名称。
    • 'PGUSER': 您的 PostgreSQL 用户名。
    • 'PGPASSWORD': 您的 PostgreSQL 密码。
    • 'PGDATABASE': 您要连接的数据库名称。

Visual Studio Code 配置

在 Visual Studio Code 的设置 (settings.json) 中,在 '"mcp"' 部分添加以下配置:

{
    "mcp": {
        "servers": {
            "azure-postgresql-mcp": {
                "command": "<虚拟环境Python路径>",
                "args": [
                    "<azure_postgresql_mcp.py 文件路径>"
                ],
                "env": {
                    "PGHOST": "<Azure PostgreSQL 实例的完全限定名称>",
                    "PGUSER": "<Azure PostgreSQL 用户名>",
                    "PGPASSWORD": "<密码>",
                    "PGDATABASE": "<数据库名称>"
                }
            }
        }
    }
}

参数说明: 与 Claude Desktop 配置相同。

基本使用方法

  1. 确保已完成安装步骤和服务器配置。
  2. 启动 MCP 客户端 (如 Claude Desktop 或 Visual Studio Code)。
  3. 在 MCP 客户端中选择或连接到 "azure-postgresql-mcp" 服务器。
  4. 使用自然语言指令,通过客户端提供的工具和资源与您的 Azure PostgreSQL 数据库进行交互。例如,您可以查询数据、更新表、创建新表或查看表结构。

注意: 启动 MCP 服务器无需手动运行 'azure_postgresql_mcp.py' 脚本。MCP 客户端会根据您在配置中提供的 'command' 和 'args' 自动启动服务器。

信息

分类

数据库与文件