pg-context
MCP server providing deep PostgreSQL context to AI assistants, including schema DDL, index health, foreign key associations, query execution plans, and performance statistics via read-only tools.
README
pg-context
Model Context Protocol (MCP) server that gives AI coding assistants deep PostgreSQL context: live schemas as DDL, index health, foreign key associations, query execution plans, and performance statistics.
pg-context connects directly to your PostgreSQL database in read-only mode and exposes tools and resources for AI assistants such as Cursor, Claude Code, Cline, and Antigravity.
Why pg-context?
Generic SQL tools often execute arbitrary queries without giving the AI assistant structural context. This causes models to guess column names, hallucinate relations, and generate invalid joins.
pg-context resolves this problem by providing:
- Zero-guess schema context: Exports real table structures formatted as PostgreSQL
CREATE TABLEDDL statements with foreign key comments and index definitions. - Read-only security: Automatically sets
default_transaction_read_only = ONon every client connection and rejects mutating SQL statements (such asDROP,DELETE,UPDATE,INSERT,TRUNCATE,ALTER). - Optimization insights: Detects unindexed foreign keys, high sequential scan ratios, unused indexes, and slow queries from
pg_stat_statements. - Extension awareness: Identifies PostGIS, TimescaleDB, and pgvector extensions.
Quick Start
You can run pg-context directly via npx without manual installation:
npx -y pg-context-mcp
Or install it globally:
npm install -g pg-context-mcp
pg-context-mcp
Configuration
pg-context reads connection parameters from environment variables or standard PostgreSQL connection strings:
| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
Standard PostgreSQL connection URI | undefined |
PGHOST |
Database host | localhost |
PGPORT |
Database port | 5432 |
PGDATABASE |
Database name | postgres |
PGUSER |
Database user | postgres |
PGPASSWORD |
Database password | "" |
PGSSL |
Enable SSL connection (true/false) |
false |
PG_SCHEMAS |
Comma-separated list of target schemas | public |
PG_MAX_CONNECTIONS |
Connection pool size | 5 |
PG_CACHE_TTL |
Schema cache TTL in seconds | 60 |
PG_QUERY_LIMIT |
Default row limit for read_query |
100 |
PG_QUERY_LIMIT_MAX |
Hard upper limit for read_query |
1000 |
Client Integration
1. Claude Desktop & Antigravity
Add the following entry to your claude_desktop_config.json or Antigravity MCP settings:
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}
2. Cursor
Add the following to your Cursor MCP configuration (.cursor/mcp.json or Cursor Settings > Features > 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 Extension)
Add this configuration into cline_mcp_settings.json:
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}
Tools Reference
pg-context exposes 12 MCP tools:
| Tool | Parameters | Description |
|---|---|---|
list_schemas |
none | Lists all non-system schemas in the database. |
list_tables |
schema (default: "public") |
Lists tables and views with sizes, estimated row counts, and comments. |
describe_table |
table, schema (default: "public") |
Returns full structural DDL of a table including columns, types, defaults, NOT NULL, foreign keys, and indexes. |
list_foreign_keys |
schema (default: "public"), table (optional) |
Displays foreign key relationships as readable source -> target associations. |
list_indexes |
schema (default: "public"), table (optional) |
Displays indexes, definitions, sizes, and scan counts. |
suggest_indexes |
schema (default: "public"), table (optional) |
Identifies unindexed foreign keys, sequential scan warnings, and unused indexes. |
read_query |
sql, limit (optional) |
Executes read-only queries with enforced limits and returns Markdown tables. |
explain_query |
sql, analyze (boolean, default: false) |
Returns query execution plans from PostgreSQL EXPLAIN or EXPLAIN ANALYZE. |
get_database_info |
none | Returns PostgreSQL version, total database size, connection counts, and buffer settings. |
get_table_stats |
schema (default: "public"), table (optional) |
Returns sequential vs index scans, live rows, dead rows, and maintenance dates. |
get_slow_queries |
limit (default: 15) |
Fetches top slowest queries recorded by pg_stat_statements. |
list_extensions |
none | Lists installed PostgreSQL extensions and flags capabilities for PostGIS, TimescaleDB, and pgvector. |
Resources Reference
pg-context exposes 3 MCP resources:
| Resource URI | MIME Type | Description |
|---|---|---|
pg://schema/full |
text/x-sql |
Full DDL dump of all tables across configured schemas. |
pg://schema/{tableName} |
text/x-sql |
DDL definition for an individual table. |
pg://stats/overview |
text/markdown |
Database health overview: storage sizes, dead rows, and optimization alerts. |
Development
# 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
License
MIT (c) Semen
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。