mcpField
P
PostgreSQL MCP Server

PostgreSQL MCP Server

本地服务
github.com/tobecrazy数据库178 周前更新

关于 PostgreSQL MCP Server

PostgreSQL MCP Server 是用于数据库表操作、结构检查和 SQL 查询的 MCP 服务。它通过 YAML 规则配置表、列及 CRUD 操作权限。

主要功能

  1. 01连接 PostgreSQL
  2. 02对表执行 CRUD 操作
  3. 03表和列级访问控制
  4. 04检查数据库结构
  5. 05执行自定义 SQL
  6. 06使用 YAML 配置

适用场景

  1. 01管理已配置表中的记录
  2. 02检查表结构
  3. 03执行带参数的 SQL 查询

MCP 相关

该服务以 stdio 模式运行,向 MCP 客户端提供表列表、CRUD 操作、结构查询和自定义 SQL 执行工具。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

PostgreSQL MCP 服务

一个为 PostgreSQL 数据库表提供 CRUD 操作的模型上下文协议(MCP)服务。

概述

此 MCP 服务提供一组工具,可对指定表执行创建、读取、更新和删除(CRUD)操作,从而让你能够与 PostgreSQL 数据库交互。服务使用 FastMCP 库,并以 stdio 模式运行,因此兼容各种 MCP 客户端。

功能

  • 连接到 PostgreSQL 数据库
  • 对指定表执行 CRUD 操作
  • 表级和列级访问控制
  • 检查架构
  • 执行自定义 SQL 查询
  • 通过 YAML 文件进行配置

安装

  1. 克隆此仓库:

    git clone https://github.com/yourusername/postgresql-mcp.git
    cd postgresql-mcp
    
  2. 安装所需的依赖项:

    pip install -r requirements.txt
    

配置

服务使用 config.yaml 文件进行配置。此文件包含:

  1. 数据库连接详情
  2. 表配置,包括:
    • 可访问的表
    • 允许操作的列
    • 允许执行的操作(创建、读取、更新、删除)

配置示例:

database:
  host: localhost
  port: 5432
  dbname: postgres
  user: postgres
  password: postgres
  
tables:
  - name: users
    allowed_columns:
      - id
      - name
      - email
      - created_at
    allowed_operations:
      - create
      - read
      - update
      - delete
  
  - name: products
    allowed_columns:
      - id
      - name
      - price
      - description
      - category
    allowed_operations:
      - create
      - read
      - update
      - delete

用法

运行 MCP 服务:

python postgresql_mcp_server.py

服务将以 stdio 模式启动,准备接收来自 MCP 客户端的命令。

可用的 MCP 工具

list_tables

列出配置中可用的所有表。

response = list_tables()
create_record

在指定表中创建一条新记录。

response = create_record(
    table_name="users",
    data={
        "name": "John Doe",
        "email": "[email protected]"
    }
)
read_records

读取指定表中的记录,可选进行筛选。

response = read_records(
    table_name="users",
    filters={"name": "John Doe"},
    limit=10,
    offset=0
)
update_record

更新指定表中的一条记录。

response = update_record(
    table_name="users",
    record_id=1,
    data={"email": "[email protected]"},
    id_column="id"
)
delete_record

删除指定表中的一条记录。

response = delete_record(
    table_name="users",
    record_id=1,
    id_column="id"
)
execute_query

执行自定义 SQL 查询。

response = execute_query(
    query="SELECT * FROM users WHERE age > %s",
    params=[18]
)
get_table_schema

获取指定表的架构信息。

response = get_table_schema(table_name="users")

响应格式

所有工具都以标准格式返回响应:

{
  "status": "success",
  "message": "Optional message",
  "records": [...],  // For read operations
  "record": {...},   // For create/update operations
  "count": 10        // For read operations
}

发生错误时:

{
  "status": "error",
  "message": "Error message"
}

安全注意事项

  • execute_query 工具允许执行任意 SQL,这可能带来安全风险。请考虑限制其使用,或实施额外的验证。
  • 数据库凭据以纯文本形式存储在 config.yaml 文件中。请考虑在生产环境中使用环境变量或安全的密钥管理解决方案。
  • 服务会根据配置验证表和列的访问权限,但务必确保配置本身安全。

许可证

MIT 许可证

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。