A Model Context Protocol (MCP) server that provides tools for interacting with a PostgreSQL database. This server enables AI assistants to execute SQL queries, create tables, and list database tables through the MCP protocol.
Features
The server provides the following tools:
**read_query**: Execute SELECT queries on the PostgreSQL database
**write_query**: Execute INSERT, UPDATE, or DELETE queries on the PostgreSQL database
**create_table**: Create a new table in the PostgreSQL database
**list_tables**: List all user tables in the database (with optional schema filtering)
**explain_query** EXPLAIN a query on the PostgreSQL database
Installation
Prerequisites
Go 1.23 or later
PostgreSQL database server
Steps
Clone the repository:
```bash
git clone https://github.com/sql-mcp-server.git
cd sql-mcp-server
```
Install dependencies:
```bash
go mod download
```
Build the server:
```bash
go build -o sql-mcp-server
```
Configuration
The server requires database connection details through environment variables. Create a `.env` file in the project root with the following variables:
Usage
Running the Server
MCP Configuration
To use this server with an AI assistant that supports MCP, add the following to your MCP configuration:
Tool Examples
List Tables
List all user tables in the database:
List tables in a specific schema:
Create Table
Create a new table:
Read Query
Execute a SELECT query:
Write Query
Execute an INSERT query:
Execute an UPDATE query:
Execute a DELETE query:
Security Considerations
The server validates query types to ensure that only appropriate operations are performed with each tool.
Input sanitization is performed for schema names to prevent SQL injection.
Consider using a dedicated database user with limited permissions for this server.
In production environments, enable SSL by setting `DB_SSLMODE` to `require` or higher.
Dependencies
[github.com/joho/godotenv](https://github.com/joho/godotenv) - For loading environment variables from .env file
[github.com/lib/pq](https://github.com/lib/pq) - PostgreSQL driver for Go
[github.com/mark3labs/mcp-go](https://github.com/mark3labs/mcp-go) - Go SDK for Model Context Protocol