PostgreSQL智能代理服务

为AI代理提供PostgreSQL数据库模式智能、查询执行和DBA工具的服务,通过模型上下文协议实现。

  • by divinedev111
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 divinedev111/mcp-postgres

使用说明

mcp postgres

PostgreSQL的MCP服务器。为AI代理提供模式智能、查询执行和DBA工具——通过 模型上下文协议.

与通用数据库MCP服务器不同,MCP-postgres是postgres原生的。它提取表/列注释,理解Postgres特定的目录视图,提供索引分析,并附带可配置的访问级别,因此您不会授予LLM不受限制的数据库访问权限。

特性

模式智能

  • 列出具有大小和行数的模式、表、视图
  • 完整的表描述:列、类型、约束、索引、外键
  • 提取物 COMMENT ON 元数据——提供关于列含义的LLM语义上下文
  • 在整个数据库中按名称或注释搜索对象

查询执行

  • 只读 query 自动限行工具
  • 具备书写能力 execute 由访问级别控制的工具
  • EXPLAIN ANALYZE 具有人类可读的输出

DBA工具

  • 表统计:活/死元组、膨胀百分比、真空历史、扫描模式
  • 索引分析:使用统计数据、未使用的索引检测、缺失索引建议
  • 数据库健康状况:连接、缓存命中率、长时间运行的查询、吞吐量

安全

  • 四个访问级别: readonly, readwrite, admin, unrestricted
  • SQL语句分类(SELECT、DML、DDL、admin)及其执行
  • 审计日志记录到stderr(JSON,每个查询一个条目)

快速开始

npx mcp-postgres --connection-string "postgres://user:pass@localhost:5432/mydb"

或者使用环境变量:

DATABASE_URL="postgres://user:pass@localhost:5432/mydb" npx mcp-postgres

克劳德桌面

添加到您的 claude_desktop_config.json:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "mcp-postgres",
        "--connection-string",
        "postgres://user:pass@localhost:5432/mydb"
      ]
    }
  }
}

克劳德代码

添加到您的项目 .mcp.json:

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "mcp-postgres"],
      "env": {
        "DATABASE_URL": "postgres://user:pass@localhost:5432/mydb"
      }
    }
  }
}

工具

工具 描述 访问
list_schemas 列出具有表计数和大小的架构 只读
list_tables 列出带有注释、行数、大小的表 只读
describe_table 包含列、索引、FK、注释的完整表描述 只读
search_objects 按名称或注释搜索对象 只读
query 执行SELECT查询 只读
execute 执行插入/更新/删除/创建等操作 各不相同
explain_query 带可读输出的解释(分析) 只读*
table_stats 表统计、膨胀、真空信息 只读
index_analysis 索引使用情况、未使用的索引、缺少索引提示 只读
database_health 连接、缓存率、长查询、膨胀 只读

*explain_query 和 analyze=true 执行查询,因此它尊重语句的访问级别。

资源

URI 描述
postgres://schema/{name} 模式的完整DDL(带注释的CREATE TABLE语句)
postgres://extensions 已安装PostgreSQL扩展

提示

提示 描述
explore-database 引导式数据库探索——模式、表、关系
optimize-query 使用EXPLAIN、索引和建议分析慢速查询
health-check 全面的数据库健康评估

配置

CLI选项

--connection-string  PostgreSQL connection URL
--access-level       readonly|readwrite|admin|unrestricted (default: readonly)
--row-limit          Max rows returned per query (default: 500)
--schema             Default schema filter (default: public)
--audit              Enable query audit logging to stderr

环境变量

变量 描述
DATABASE_URL PostgreSQL连接URL
POSTGRES_URL 备选连接URL
MCP_POSTGRES_ACCESS_LEVEL 访问级别覆盖
MCP_POSTGRES_ROW_LIMIT 行限制覆盖

访问级别

级别 选择 插入/更新/删除 创建/更改/删除 删除/删除数据库
readonly 是 否 否 不
readwrite 是 是 否 否
admin 是 是 是
unrestricted 是 是 是

默认值为 readonly.使用所需的最低级别。

审计日志

启用 --audit.以JSON格式将每次工具调用记录到stderr:

{"timestamp":"2026-04-03T12:00:00.000Z","tool":"query","sql":"SELECT * FROM users","statementType":"select","accessLevel":"readonly","allowed":true,"durationMs":12,"rowCount":42}

将stderr传输到要捕获的文件: mcp-postgres --audit 2>audit.log

建筑

src/
├── index.ts             Entry point and CLI
├── server.ts            MCP server setup
├── config.ts            Configuration parsing
├── db/
│   ├── pool.ts          Connection pool management
│   └── query.ts         Query execution with timing
├── tools/
│   ├── schema.ts        Schema exploration tools
│   ├── query.ts         Query execution tools
│   └── performance.ts   DBA and health tools
├── resources/
│   └── schema.ts        Schema DDL resources
├── prompts/
│   └── index.ts         Prompt templates
└── safety/
    ├── classifier.ts    SQL statement classification
    ├── access.ts        Access level enforcement
    └── audit.ts         Audit logging

发展

npm install
npm test           # run tests
npm run build      # compile TypeScript
npm run dev -- --connection-string "postgres://..."  # run in dev mode

许可证

麻省理工学院