Skip to main content
Glama

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 = ON für jede Clientverbindung und lehnt ändernde SQL-Anweisungen ab (wie DROP, 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-mcp

Oder installieren Sie es global:

npm install -g pg-context-mcp
pg-context-mcp

Konfiguration

pg-context liest Verbindungsparameter aus Umgebungsvariablen oder Standard-Verbindungsstrings für PostgreSQL:

Variable

Beschreibung

Standard

DATABASE_URL

Standard-PostgreSQL-Verbindungs-URI

undefined

PGHOST

Datenbankhost

localhost

PGPORT

Datenbank-Port

5432

PGDATABASE

Datenbankname

postgres

PGUSER

Datenbankbenutzer

postgres

PGPASSWORD

Datenbankpasswort

""

PGSSL

SSL-Verbindung aktivieren (true/false)

false

PG_SCHEMAS

Kommagetrennte Liste der Ziel-Schemata

public

PG_MAX_CONNECTIONS

Größe des Verbindungspools

5

PG_CACHE_TTL

TTL des Schema-Caches in Sekunden

60

PG_QUERY_LIMIT

Standardzeilengrenze für read_query

100

PG_QUERY_LIMIT_MAX

Harte Obergrenze für read_query

1000


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

list_schemas

keine

Listet alle Nicht-System-Schemata in der Datenbank auf.

list_tables

schema (Standard: "public")

Listet Tabellen und Sichten mit Größen, geschätzten Zeilenanzahlen und Kommentaren auf.

describe_table

table, schema (Standard: "public")

Gibt das vollständige strukturelle DDL einer Tabelle zurück, einschließlich Spalten, Typen, Standardwerten, NOT NULL, Fremdschlüsseln und Indizes.

list_foreign_keys

schema (Standard: "public"), table (optional)

Zeigt Fremdschlüsselbeziehungen als lesbare source -> target-Zuordnungen an.

list_indexes

schema (Standard: "public"), table (optional)

Zeigt Indizes, Definitionen, Größen und Scan-Anzahlen an.

suggest_indexes

schema (Standard: "public"), table (optional)

Erkennt nicht indizierte Fremdschlüssel, Sequential-Scan-Warnungen und ungenutzte Indizes.

read_query

sql, limit (optional)

Führt Nur-Lese-Abfragen mit erzwungenen Grenzwerten aus und gibt Markdown-Tabellen zurück.

explain_query

sql, analyze (boolesch, Standard: false)

Gibt Abfrageausführungspläne von PostgreSQL EXPLAIN oder EXPLAIN ANALYZE zurück.

get_database_info

keine

Gibt die PostgreSQL-Version, die Gesamtgröße der Datenbank, Verbindungsanzahlen und Puffereinstellungen zurück.

get_table_stats

schema (Standard: "public"), table (optional)

Gibt Sequential- vs. Index-Scans, lebende Zeilen, tote Zeilen und Wartungsdaten zurück.

get_slow_queries

limit (Standard: 15)

Ruft die langsamsten Abfragen ab, die von pg_stat_statements aufgezeichnet wurden.

list_extensions

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

pg://schema/full

text/x-sql

Vollständiger DDL-Dump aller Tabellen über die konfigurierten Schemata.

pg://schema/{tableName}

text/x-sql

DDL-Definition für eine einzelne Tabelle.

pg://stats/overview

text/markdown

Ü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 start

Lizenz

MIT (c) Semen

Available Tools

12 tools
describe_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

    A4/5.0

    Scored across 12 tools

    Disambiguation5/5

    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.

    Naming Consistency5/5

    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.

    Tool Count5/5

    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.

    Completeness5/5

    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

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      A 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
    • F
      license
      Not graded
      quality
      D
      maintenance
      An 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.
      -
    • A
      license
      A
      quality
      C
      maintenance
      An 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.
      11
      5
      MIT
    • A
      license
      A
      quality
      D
      maintenance
      A 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.
      9
      21 npm
      1
      MIT