PostgreSQL MCP Server

by tobecrazy

306 downloads
Not rated
GitHub

About

PostgreSQL MCP Server is a Model Context Protocol server that provides CRUD operations for PostgreSQL database tables through a set of MCP tools. It uses the FastMCP library, runs in stdio mode, and is compatible with various MCP clients.

Details

Author
tobecrazy
Downloads
306
Categories
Database

- Connect to PostgreSQL databases
- Perform CRUD operations on specified tables
- Table and column-level access control
- Schema inspection
- Custom SQL query execution
- Configuration via YAML file

Clone the repository, install dependencies with pip install -r requirements.txt, edit the config.yaml file with database credentials and table permissions, then run python postgresql_mcp_server.py to start the server in stdio mode.

query

Execute a read-only SQL SELECT query and return results as JSON

execute

Execute a write SQL statement (INSERT, UPDATE, DELETE, TRUNCATE, CREATE, DROP). Returns rows affected.

schema

List all tables with their columns, types, and constraints for a given schema (default: public)

list_tables

List all tables in the database with row counts

transaction

Execute multiple SQL statements as a single atomic transaction. Rolls back all on any error.

PostgreSQL MCP Server

A Model Context Protocol (MCP) server that provides CRUD operations for PostgreSQL database tables.

Overview

This MCP server allows you to interact with PostgreSQL databases through a set of tools that provide Create, Read, Update, and Delete (CRUD) operations on specified tables. The server uses the FastMCP library and runs in stdio mode, making it compatible with various MCP clients.

Features

- Connect to PostgreSQL databases
- Perform CRUD operations on specified tables
- Table and column-level access control
- Schema inspection
- Custom SQL query execution
- Configuration via YAML file

Installation

1. Clone this repository:

   git clone https://github.com/yourusername/postgresql-mcp.git
cd postgresql-mcp

2. Install the required dependencies:

   pip install -r requirements.txt

Configuration

The server is configured using the config.yaml file. This file contains:

1. Database connection details
2. Table configurations, including:
- Which tables are accessible
- Which columns are allowed for operations
- Which operations (create, read, update, delete) are allowed

Example configuration:

database:
  host: localhost
  port: 5432
  dbname: postgres
  user: postgres
  password: postgres
  
tables:
  - name: users
    allowed_columns:
      - id
      - name
      - email
      - created_at
    allowed_operations:
      - create
      - read
      - update
      - delete
  
  - name: products
    allowed_columns:
      - id
      - name
      - price
      - description
      - category
    allowed_operations:
      - create
      - read
      - update
      - delete

Usage

Run the MCP server:

python postgresql_mcp_server.py

The server will start in stdio mode, ready to receive commands from an MCP client.

Available MCP Tools

list_tables

Lists all tables available in the configuration.

response = list_tables()

create_record

Creates a new record in the specified table.

response = create_record(
    table_name="users",
    data={
        "name": "John Doe",
        "email": "john@example.com"
    }
)

read_records

Reads records from the specified table with optional filtering.

response = read_records(
    table_name="users",
    filters={"name": "John Doe"},
    limit=10,
    offset=0
)

update_record

Updates a record in the specified table.

response = update_record(
    table_name="users",
    record_id=1,
    data={"email": "newemail@example.com"},
    id_column="id"
)

delete_record

Deletes a record from the specified table.

response = delete_record(
    table_name="users",
    record_id=1,
    id_column="id"
)

execute_query

Executes a custom SQL query.

response = execute_query(
    query="SELECT * FROM users WHERE age > %s",
    params=[18]
)

get_table_schema

Gets the schema information for a specific table.

response = get_table_schema(table_name="users")

Response Format

All tools return responses in a standard format:

{
  "status": "success",
  "message": "Optional message",
  "records": [...],  // For read operations
  "record": {...},   // For create/update operations
  "count": 10        // For read operations
}

Or in case of an error:

{
  "status": "error",
  "message": "Error message"
}

Security Considerations

- The execute_query tool allows arbitrary SQL execution, which could be a security risk. Consider restricting its use or implementing additional validation.
- Database credentials are stored in plain text in the config.yaml file. Consider using environment variables or a secure secret management solution in production.
- The server validates table and column access based on the configuration, but it's important to ensure that the configuration itself is secure.

License

MIT License

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.