SP

stuzero/pg-mcp-server

开发工具

本页由 AI 翻译,技术细节请以原文为准。 查看原文

540 stars 43 forks 质量 49 趋势 20MIT

一款用于 PostgreSQL 数据库的模型上下文协议(MCP)服务器,具备增强的 AI 代理功能。

概览

一款用于 PostgreSQL 数据库的模型上下文协议(MCP)服务器,具备增强的 AI 代理功能。 PG-MCP 是 PostgreSQL 数据库模型上下文协议的服务器实现。它为 AI 代理提供了全面的 API,通过 MCP 的资源导向架构,发现、连接、查询和理解 PostgreSQL 数据库。 该实现在参考 Postgres MCP 实现基础上进行了若干关键改进: 1. : 构建为带有SSE传输的完整服务器,用于生产环境 2.: 同时连接多个PostgreSQL数据库 3.: 从数据库目录中提取并暴露表/列描述 4.: 提供关于PostgreSQL扩展如PostGIS和pgvector 5的详细基于YAML的知识。: 包含分析查询执行计划的专用工具 6.

概述(AI 翻译)

一款用于 PostgreSQL 数据库的模型上下文协议(MCP)服务器,具备增强的 AI 代理功能。

关于PG-MCP项目的更多信息请见此处:

https://stuzero.github.io/pg-mcp/

概述

PG-MCP 是 PostgreSQL 数据库模型上下文协议的服务器实现。它为 AI 代理提供了全面的 API,通过 MCP 的资源导向架构,发现、连接、查询和理解 PostgreSQL 数据库。

该实现在参考 Postgres MCP 实现基础上进行了若干关键改进:

  1. 完整服务器实现:作为一个完整的服务器,带SSE传输,用于生产环境
  2. 多数据库支持:同时连接多个PostgreSQL数据库
  3. 丰富的目录信息:从数据库目录中提取并暴露表/列描述
  4. 扩展上下文:提供关于PostgreSQL扩展如PostGIS和pgvector的详细基于YAML的知识
  5. 查询解释:包含专门分析查询执行计划的工具
  6. 稳健的连接管理:数据库连接的正确生命周期,并处理安全连接ID。

特色

本页由 AI 翻译,技术细节请以原文为准。

README

以下为原文 README 节选,完整内容请查看原文。

PostgreSQL Model Context Protocol (PG-MCP) Server

A Model Context Protocol (MCP) server for PostgreSQL databases with enhanced capabilities for AI agents.

More info on the pg-mcp project here:

https://stuzero.github.io/pg-mcp/

Overview

PG-MCP is a server implementation of the Model Context Protocol for PostgreSQL databases. It provides a comprehensive API for AI agents to discover, connect to, query, and understand PostgreSQL databases through MCP’s resource-oriented architecture.

This implementation builds upon and extends the reference Postgres MCP implementation with several key enhancements:

  1. Full Server Implementation: Built as a complete server with SSE transport for production use
  2. Multi-database Support: Connect to multiple PostgreSQL databases simultaneously
  3. Rich Catalog Information: Extracts and exposes table/column descriptions from the database catalog
  4. Extension Context: Provides detailed YAML-based knowledge about PostgreSQL extensions like PostGIS and pgvector
  5. Query Explanation: Includes a dedicated tool for analyzing query execution plans
  6. Robust Connection Management: Proper lifecycle for database connections with secure connection ID handling

Features

Connection Management

  • Connect Tool: Register PostgreSQL connection strings and get a secure connection ID
  • Disconnect Tool: Explicitly close database connections when done
  • Connection Pooling: Efficient connection management with pooling

Query Tools

  • pg_query: Execute read-only SQL queries using a connection ID
  • pg_explain: Analyze query execution plans in JSON format

Schema Discovery Resources

  • List schemas with descriptions
  • List tables with descriptions and row counts
  • Get column details with data types and descriptions
  • View table constraints and indexes
  • Explore database extensions

Data Access Resources

  • Sample table data (with pagination)
  • Get approximate row counts

Extension Context

Built-in contextual information for PostgreSQL extensions like:

  • PostGIS: Spatial data types, functions, and examples
  • pgvector: Vector similarity search functions and best practices

Additional extensions can be easily added via YAML config files.

Installation

Prerequisites

  • Python 3.13+
  • PostgreSQL database(s)

Using Docker

# Clone the repository
git clone https://github.com/stuzero/pg-mcp-server.git
cd pg-mcp-server

# Build and run with Docker Compose
docker-compose up -d

Manual Installation

# Clone the repository
git clone https://github.com/stuzero/pg-mcp-server.git
cd pg-mcp-server

# Install dependencies and create a virtual environment ( .venv )
uv sync

# Activate the virtual environment
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Run the server
在 GitHub 查看完整 README

安装

This server does not publish a one-line install command.

Open the repository installation guide 新手?先看 mcpskill.org 教程 →