pg-mcp-server

pg-mcp-server

0

This project provides a PostgreSQL database server using the Model Context Protocol, enabling AI assistants to perform secure database operations. It emphasizes safety by requiring explicit transaction confirmation and offers configurable options for various database settings.

PostgreSQL MCP Server

  • A PostgreSQL database server based on the Model Context Protocol (MCP) that securely allows AI assistants to perform database operations such as querying, inserting, updating, and deleting data, as well as altering table structures.

Core Script Explanation

  • index.ts: Main entry point to initialize and start the MCP server.
  • tool-handlers.ts: Contains functions for handling database operations.
  • transaction-manager.ts: Manages the lifecycle of database transactions.
  • types.ts: Defines types and constants used in the project.
  • utils.ts: Provides common utility functions.
  • config.ts: Defines server configuration options, customizable via environment variables.

Installation and Running

  • Preconditions: Node.js 18.x or higher, PostgreSQL database instance, npm or yarn.
  • Install dependencies, compile TypeScript code, and configure necessary files to start the server.
  • Environment variables allow customization of transaction timeouts, monitoring intervals, and connection pool settings.

Usage

  • Run read-only SQL queries and data modifications through the MCP protocol.
  • Transactions must be explicitly confirmed to save data, otherwise, they will rollback.

Note

  • Ensure database users have appropriate permissions and configure sensible timeouts and connection pool parameters for production environments.