pg-context
pg-context
Model Context Protocol (MCP)-Server, der KI-Codierungsassistenten tiefen PostgreSQL-Kontext bietet: Live-Schemata als DDL, Index-Gesundheit, Fremdschlüssel-Zuordnungen, Abfrageausführungspläne und Leistungsstatistiken.
pg-context verbindet sich direkt mit Ihrer PostgreSQL-Datenbank im Nur-Lese-Modus und stellt Tools und Ressourcen für KI-Assistenten wie Cursor, Claude Code, Cline und Antigravity bereit.
Warum pg-context?
Generische SQL-Tools führen oft beliebige Abfragen aus, ohne dem KI-Assistenten strukturellen Kontext zu geben. Dadurch raten Modelle Spaltennamen, erfinden Beziehungen und erzeugen ungültige Joins.
pg-context löst dieses Problem, indem es Folgendes bereitstellt:
Schemakontext ohne Raten: Exportiert reale Tabellenstrukturen als PostgreSQL-
CREATE TABLE-DDL-Anweisungen mit Fremdschlüssel-Kommentaren und Indexdefinitionen.Nur-Lese-Sicherheit: Setzt automatisch
default_transaction_read_only = ONfür jede Clientverbindung und lehnt ändernde SQL-Anweisungen ab (wieDROP,DELETE,UPDATE,INSERT,TRUNCATE,ALTER).Optimierungseinblicke: Erkennt nicht indizierte Fremdschlüssel, hohe Sequential-Scan-Anteile, ungenutzte Indizes und langsame Abfragen aus
pg_stat_statements.Erweiterungsbewusstsein: Identifiziert PostGIS-, TimescaleDB- und pgvector-Erweiterungen.
Related MCP server: PostgreSQL MCP Server
Schnellstart
Sie können pg-context direkt über npx ausführen, ohne manuelle Installation:
npx -y pg-context-mcpOder installieren Sie es global:
npm install -g pg-context-mcp
pg-context-mcpKonfiguration
pg-context liest Verbindungsparameter aus Umgebungsvariablen oder Standard-Verbindungsstrings für PostgreSQL:
Variable | Beschreibung | Standard |
| Standard-PostgreSQL-Verbindungs-URI |
|
| Datenbankhost |
|
| Datenbank-Port |
|
| Datenbankname |
|
| Datenbankbenutzer |
|
| Datenbankpasswort |
|
| SSL-Verbindung aktivieren ( |
|
| Kommagetrennte Liste der Ziel-Schemata |
|
| Größe des Verbindungspools |
|
| TTL des Schema-Caches in Sekunden |
|
| Standardzeilengrenze für |
|
| Harte Obergrenze für |
|
Client-Integration
1. Claude Desktop & Antigravity
Fügen Sie den folgenden Eintrag zu Ihrer claude_desktop_config.json oder den Antigravity-MCP-Einstellungen hinzu:
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}2. Cursor
Fügen Sie Folgendes zu Ihrer Cursor-MCP-Konfiguration hinzu (.cursor/mcp.json oder 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)
Fügen Sie diese Konfiguration in cline_mcp_settings.json ein:
{
"mcpServers": {
"pg-context": {
"command": "npx",
"args": ["-y", "pg-context-mcp"],
"env": {
"DATABASE_URL": "postgresql://user:password@localhost:5432/my_database"
}
}
}
}Tool-Referenz
pg-context stellt 12 MCP-Tools bereit:
Tool | Parameter | Beschreibung |
| keine | Listet alle Nicht-System-Schemata in der Datenbank auf. |
|
| Listet Tabellen und Sichten mit Größen, geschätzten Zeilenanzahlen und Kommentaren auf. |
|
| Gibt das vollständige strukturelle DDL einer Tabelle zurück, einschließlich Spalten, Typen, Standardwerten, NOT NULL, Fremdschlüsseln und Indizes. |
|
| Zeigt Fremdschlüsselbeziehungen als lesbare |
|
| Zeigt Indizes, Definitionen, Größen und Scan-Anzahlen an. |
|
| Erkennt nicht indizierte Fremdschlüssel, Sequential-Scan-Warnungen und ungenutzte Indizes. |
|
| Führt Nur-Lese-Abfragen mit erzwungenen Grenzwerten aus und gibt Markdown-Tabellen zurück. |
|
| Gibt Abfrageausführungspläne von PostgreSQL |
| keine | Gibt die PostgreSQL-Version, die Gesamtgröße der Datenbank, Verbindungsanzahlen und Puffereinstellungen zurück. |
|
| Gibt Sequential- vs. Index-Scans, lebende Zeilen, tote Zeilen und Wartungsdaten zurück. |
|
| Ruft die langsamsten Abfragen ab, die von |
| keine | Listet installierte PostgreSQL-Erweiterungen auf und kennzeichnet Fähigkeiten für PostGIS, TimescaleDB und pgvector. |
Ressourcen-Referenz
pg-context stellt 3 MCP-Ressourcen bereit:
Ressourcen-URI | MIME-Typ | Beschreibung |
|
| Vollständiger DDL-Dump aller Tabellen über die konfigurierten Schemata. |
|
| DDL-Definition für eine einzelne Tabelle. |
|
| Übersicht über die Datenbankgesundheit: Speichergrößen, tote Zeilen und Optimierungswarnungen. |
Entwicklung
# 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 startLizenz
MIT (c) Semen
Available Tools
12 toolsdescribe_tableB
Get full structural details of a table as CREATE TABLE DDL, including columns, data types, constraints, foreign keys, and indexes
Inputs: table (required), schema.
explain_queryB
Get query execution plan via PostgreSQL EXPLAIN (or EXPLAIN ANALYZE) to debug performance bottlenecks
Inputs: sql (required), analyze.
get_database_infoA
Get high-level PostgreSQL database runtime information: version, size, active connection count, and server settings
Inputs: no named parameters.
get_slow_queriesB
Retrieve top slowest queries recorded by pg_stat_statements extension (if enabled in PostgreSQL)
Inputs: limit.
get_table_statsA
Get scan statistics, tuple counts, dead row accumulation, and maintenance history for tables
Inputs: table, schema.
list_extensionsA
List installed PostgreSQL extensions with version information and specialized framework detection (PostGIS, TimescaleDB, pgvector)
Inputs: no named parameters.
list_foreign_keysA
List foreign key relationships between tables to understand data model associations and joins
Inputs: table, schema.
list_indexesA
List indexes on tables with sizes and scan usage statistics to evaluate index health
Inputs: table, schema.
list_schemasA
List all available database schemas (excluding system schemas like pg_catalog and information_schema)
Inputs: no named parameters.
list_tablesA
List all tables and views in a given schema with estimated row counts, sizes, and comments
Inputs: schema.
read_queryA
Execute a safe read-only SQL query (SELECT, WITH, SHOW) with strict LIMIT enforcement and formatting
Inputs: sql (required), limit.
suggest_indexesB
Analyze query patterns, scan ratios, and missing foreign key indexes to provide index recommendations
Inputs: table, schema.
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 12 tools
Each tool targets a distinct aspect of PostgreSQL database introspection: schemas, tables, relationships, indexes, query execution, performance stats, and extensions. There is no overlap in purpose.
All tools follow a consistent verb_noun pattern in snake_case (e.g., list_schemas, describe_table, read_query), making the API predictable and easy to navigate.
With 12 tools, the server covers the key areas of database analysis without being bloated. Each tool serves a distinct role, and the count is appropriate for the domain.
The set covers schema exploration, table details, indexing, query analysis, performance monitoring, and extension information. No obvious gaps for a read-only database context server.
Related MCP Connectors
Hosted PostgreSQL MCP server for Claude, ChatGPT and Cursor, with each action switched per account.
PostgreSQL, MySQL, and SQL Server in one session. 26 read-only MCP tools for AI agents.
Hosted MCP server for PostgreSQL diagnostics: slow queries, missing indexes, connection pressure.
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceA Model Context Protocol server that provides AI assistants with secure, read-only access to PostgreSQL databases while offering comprehensive tools for schema exploration, query validation, and performance optimization.MIT
- FlicenseNot gradedqualityDmaintenanceAn MCP server that enables AI assistants to interact with PostgreSQL databases by executing SQL queries and inspecting database schemas. It provides tools for standardized database exploration and management through the Model Context Protocol.-
- AlicenseAqualityCmaintenanceAn MCP server that gives AI agents direct read-only access to PostgreSQL databases, enabling natural language analytics through tools for schema exploration, querying, trend analysis, and data quality checks.115MIT
- AlicenseAqualityDmaintenanceA Model Context Protocol (MCP) server that gives AI assistants deep visibility into databases, inspecting schemas, detecting index problems, analyzing table bloat, and explaining query plans across PostgreSQL, MySQL, and SQLite.921 npm1MIT