Skip to main content

A production-grade PostgreSQL Model Context Protocol (MCP) server.

Project description

MCPg

A production-grade Model Context Protocol server for PostgreSQL — letting AI agents safely inspect, query, operate, and tune a Postgres database.*

Status: v0.5.0 released; trunk at 114 MCP tools. Beyond v0.5.0's surface (NL→SQL via Anthropic / OpenAI / Gemini, per-request SET ROLE multi-tenancy, server-side cursors, hybrid vector+FTS search, TimescaleDB wrappers, HTTP bearer-token auth, Prometheus /metrics, RLS testing, FK cascade graphs, and the rest of the Tier-A/B/C shortlist), trunk adds **Apache AGE graph

  • Cypher** (six new tools — list_graphs, describe_graph, create_graph, drop_graph, run_cypher, generate_graph_diagram), read-replica routing (MCPG_REPLICA_URLS round-robins read-only queries across replicas with degraded-replica detection and primary fallback), and OIDC / JWT bearer-token validation (MCPG_AUTH_MODE=oidc swaps the static token for full JWT validation against an OIDC issuer's JWKS, with optional role-claim mapping that composes with the tenancy driver). CI matrix runs the integration suite against PostgreSQL 14, 15, 16, 17, and 18. See docs/cookbook.md for common agent recipes, docs/tour.md for the tool tour, CHANGELOG.md and docs/PROGRESS.md for detail.

Quick start

git clone https://github.com/devopam/MCPg && cd MCPg
uv sync
MCPG_DATABASE_URL=postgresql://localhost/mydb uv run mcpg

See the Installation Guide and User Guide to get started.

Goals

  • Safe by default — read-only access mode, every SQL statement parsed and validated; no string-interpolated queries.
  • Broad scope — both an application data access layer and a database operations toolkit (health checks, index tuning, EXPLAIN analysis), gated by an access mode.
  • Test-driven — every feature backed by tests against a real Postgres (PG 14–17 in CI).
  • Production-ready — connection pooling, scalability, multi-tenancy, thorough documentation.

Capability surface

  • Catalog introspection — schemas, tables, columns, indexes, constraints, views, functions, triggers, sequences, partitions, policies, roles, grants, enums, domains, composite types, foreign-data wrappers, foreign servers, foreign tables, user mappings, publications, subscriptions, foreign keys, extensions, generated columns.
  • Visualisationgenerate_schema_diagram (Mermaid ER) + generate_fk_cascade_graph (Mermaid blast-radius graph of ON DELETE / ON UPDATE CASCADE FKs) + generate_graph_diagram (Mermaid view of an Apache AGE property graph).
  • Structural diffcompare_schemas returns a typed diff between two schemas; validate_migration re-runs a candidate against a transient sample of real rows so failures the diff misses (NOT NULL on existing NULLs, CHECK violations, type narrowings) surface before apply.
  • Query intelligencerun_select, run_select_parallel, explain_query, analyze_query_plan, why_is_this_slow, recommend_indexes, analyze_workload, check_database_health, detect_n_plus_one.
  • NL → SQLtranslate_nl_to_sql (Anthropic / OpenAI / Gemini via MCPG_NL2SQL_PROVIDER). Generated SQL passes through the same SafeSqlDriver allowlist as run_select before execution.
  • Server-side cursorsopen_cursor / fetch_cursor / close_cursor / list_cursors for pageable reads over millions of rows; each cursor holds a dedicated connection so long-lived cursors can't starve the main pool.
  • Searchfuzzy_search (trigram), full_text_search, vector_search + vector_range_search + hybrid_search (pgvector + FTS via reciprocal-rank fusion), geo_search (PostGIS k-NN).
  • Apache AGE graph + Cypherlist_graphs, describe_graph, run_cypher, create_graph, drop_graph, generate_graph_diagram. Write tools gated under MCPG_ALLOW_DDL.
  • Composite + advisor toolssummarize_table (one-call snapshot), find_unused_objects, find_sensitive_columns (PII heuristic), lint_naming_conventions, test_rls_for_role (debug RLS as a target role), list_locks, find_blocking_chains, read_pg_stat_io (PG16+), generate_test_data (synthetic INSERT generator).
  • Live ops & maintenance (gated) — list_active_queries, run_maintenance (VACUUM/ANALYZE), cancel_query, terminate_backend, run_write, run_ddl, enable_extension.
  • Data movementexport_query / export_table (in-process CSV/JSON), dump_database / restore_database (subprocess gate), import_csv / import_json (COPY FROM STDIN + parametrised executemany), copy_table_between_databases (cross-DB pipeline).
  • Event streams (gated) — subscribe_channel / poll_notifications / unsubscribe_channel / list_notification_subscriptions bridge PostgreSQL LISTEN / NOTIFY into the MCP tool-poll model.
  • Staged migrations (gated) — prepare_migration clones a target schema into a shadow, applies the candidate SQL there, and surfaces the structural diff for review; validate_migration applies the candidate to a transient shadow with sample data; complete_migration / cancel_migration / list_pending_migrations round out the workflow.
  • TimescaleDB hypertables (gated) — list_hypertables, list_chunks, create_hypertable, add_compression_policy, add_retention_policy. Degrade to available=false when the extension isn't installed.
  • ORM bridges — eight read-only catalog → DSL exporters: generate_prisma_schema, generate_drizzle_schema, generate_sqlalchemy_models, generate_sqlc_schema, generate_diesel_schema, generate_jooq_config, generate_ent_schemas, generate_ecto_schemas.
  • Observability — Prometheus /metrics endpoint on the HTTP transport + get_metrics_exposition MCP tool for stdio. Three series: mcpg_tool_calls_total{tool,status} (counter), mcpg_tool_duration_seconds_* (histogram).
  • HTTP authMCPG_HTTP_AUTH_TOKEN for static bearer (default MCPG_AUTH_MODE=static); MCPG_AUTH_MODE=oidc for full JWT validation against an OIDC issuer's JWKS, with optional MCPG_OIDC_ROLE_CLAIM → PG role mapping.
  • Multi-tenancy via SET ROLEMCPG_DEFAULT_ROLE (static) and the X-MCPG-Role HTTP header drive a tenant driver that wraps every query in BEGIN ... SET LOCAL ROLE "<role>" ... so one MCPg process can serve N tenants from a single pool.
  • Read-replica routingMCPG_REPLICA_URLS round-robins force_readonly queries across replicas with degraded-replica detection + primary fallback; list_replicas reports per-replica health.

Documentation

License

MIT — see LICENSE. The vendored SQL-safety kernel at src/mcpg/_vendor/sql/ is also MIT-licensed; see NOTICE for provenance.

* : While best intent has been put to make it production grade, it is still a developmental project and is expected to have issues. Please refer to License Terms for details on indemnity.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcpg-0.5.1.tar.gz (230.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcpg-0.5.1-py3-none-any.whl (247.7 kB view details)

Uploaded Python 3

File details

Details for the file mcpg-0.5.1.tar.gz.

File metadata

  • Download URL: mcpg-0.5.1.tar.gz
  • Upload date:
  • Size: 230.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mcpg-0.5.1.tar.gz
Algorithm Hash digest
SHA256 63ea8a9f00814ac8128edd6a145ce91d0da0e1c0040f25a50c41d2fd8ef65b5c
MD5 de964e1c5c769c8312106b8eab1c4e6e
BLAKE2b-256 d5a7ba1f13555d2bf83029f1e422fd7f11f16518dd5e637357965ce9ed48e2c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcpg-0.5.1.tar.gz:

Publisher: publish.yml on devopam/MCPg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mcpg-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: mcpg-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 247.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mcpg-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0019a2a1f3b178702d20fb61adf7d653d6d0417654accee5e89e7ceae9b95fae
MD5 e4033a476542009043b1d17fcd9e3658
BLAKE2b-256 34d34dd157e6b0fdcac329000e74d4c51e46d91a0ac7e31efef666854839c896

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcpg-0.5.1-py3-none-any.whl:

Publisher: publish.yml on devopam/MCPg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page