pg-context
pg-context
模型上下文协议 (MCP) 服务器,为 AI 编码助手提供深度 PostgreSQL 上下文:实时的模式以 DDL 形式呈现、索引健康状况、外键关联、查询执行计划和性能统计。
pg-context 以只读模式直接连接到您的 PostgreSQL 数据库,并为 Cursor、Claude Code、Cline 和 Antigravity 等 AI 助手提供工具和资源。
为什么选择 pg-context?
通用的 SQL 工具通常执行任意查询,却不向 AI 助手提供结构上下文。这会导致模型猜测列名、臆造关系并生成无效的连接。
pg-context 通过以下方式解决这一问题:
零猜测模式上下文:以 PostgreSQL
CREATE TABLEDDL 语句的形式导出真实的表结构,包含外键注释和索引定义。只读安全:自动在每个客户端连接上设置
default_transaction_read_only = ON,并拒绝修改数据的 SQL 语句(例如DROP、DELETE、UPDATE、INSERT、TRUNCATE、ALTER)。优化洞察:检测未建立索引的外键、高顺序扫描比率、未使用的索引,以及来自
pg_stat_statements的慢查询。扩展感知:识别 PostGIS、TimescaleDB 和 pgvector 扩展。
快速开始
您可以通过 npx 直接运行 pg-context,无需手动安装:
npx -y pg-context-mcp或者全局安装:
npm install -g pg-context-mcp
pg-context-mcp配置
pg-context 从环境变量或标准 PostgreSQL 连接字符串中读取连接参数:
变量 | 描述 | 默认值 |
| 标准 PostgreSQL 连接 URI |
|
| 数据库主机 |
|
| 数据库端口 |
|
| 数据库名称 |
|
| 数据库用户 |
|
| 数据库密码 |
|
| 启用 SSL 连接( |
|
| 逗号分隔的目标模式列表 |
|
| 连接池大小 |
|
| 模式缓存 TTL(秒) |
|
|
|
|
|
|
|
客户端集成
1. Claude Desktop 与 Antigravity
将以下条目添加到您的 claude_desktop_config.json 或 Antigravity MCP 设置中:
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}2. Cursor
将以下内容添加到您的 Cursor MCP 配置中(.cursor/mcp.json 或 Cursor 设置 > 功能 > MCP):
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}3. Cline(VS Code 扩展)
将此配置添加到 cline_mcp_settings.json 中:
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}工具参考
pg-context 提供 12 个 MCP 工具:
工具 | 参数 | 描述 |
| 无 | 列出数据库中所有非系统模式。 |
|
| 列出表和视图及其大小、估计行数和注释。 |
|
| 返回表的完整结构 DDL,包括列、类型、默认值、NOT NULL、外键和索引。 |
|
| 以可读的 |
|
| 显示索引、定义、大小和扫描计数。 |
|
| 识别未建立索引的外键、顺序扫描警告和未使用的索引。 |
|
| 以强制限制执行只读查询,并返回 Markdown 表格。 |
|
| 从 PostgreSQL 的 |
| 无 | 返回 PostgreSQL 版本、数据库总大小、连接计数和缓冲区设置。 |
|
| 返回顺序扫描与索引扫描、活动行、死行以及维护日期。 |
|
| 获取由 |
| 无 | 列出已安装的 PostgreSQL 扩展,并标记 PostGIS、TimescaleDB 和 pgvector 的能力。 |
资源参考
pg-context 提供 3 个 MCP 资源:
资源 URI | MIME 类型 | 描述 |
|
| 所有已配置模式中所有表的完整 DDL 转储。 |
|
| 单个表的 DDL 定义。 |
|
| 数据库健康概览:存储大小、死行和优化警报。 |
开发
# Clone the repository
git clone https://github.com/mewsyy/pg-context.git
cd pg-context
# Install dependencies
npm install
# Run test suite
npm test
# Build TypeScript
npm run build
# Start local server
npm start许可证
MIT (c) Semen
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Connectors
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
MCP server for managing Prisma Postgres.
MCP server for interacting with the Supabase platform
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/mewsyy/pg-context'
If you have feedback or need assistance with the MCP directory API, please join our Discord server