Skip to main content

A PostgreSQL MCP server project

Project description

Simple PostgreSQL MCP Server

This is a template project for those looking to build their own MCP servers. I designed it to be dead simple to understand and adapt - the code is straightforward with MCP docs attached so you can quickly get up to speed.

What is MCP?

TL;DR - It's a way to write plugins for AI

Model Context Protocol (MCP) is a standard way for LLMs to interact with external tools and data. In a nutshell:

  • Tools allow the LLM to execute commands (like running a database query)
  • Resources are data you can attach to conversations (like attaching a file to a prompt)
  • Prompts are templates that generate consistent LLM instructions

Features

This PostgreSQL MCP server implements:

  1. Tools

    • execute_query - Run SQL queries against your database
    • test_connection - Verify the database connection is working
  2. Resources

    • db://tables - List of all tables in the schema
    • db://tables/{table_name} - Schema information for a specific table
    • db://schema - Complete schema information for all tables in the database
  3. Prompts

    • Query generation templates
    • Analytical query builders
    • Based on the templates in this repo

Prerequisites

  • Python 3.8+
  • uv - Modern Python package manager and installer
  • npx (included with Node.js)
  • PostgreSQL database you can connect to

Quick Setup

  1. Create a virtual environment and install dependencies:

    # Create a virtual environment with uv
    uv venv
    
    # Activate the virtual environment
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
    # Install dependencies
    uv pip install -r requirements.txt
    
  2. Run the server with the MCP Inspector:

    # Replace with YOUR actual database credentials
    npx @modelcontextprotocol/inspector uv --directory . run postgres -e DSN=postgresql://username:password@hostname:port/database -e SCHEMA=public
    

    Note: If this is your first time running npx, you'll be prompted to approve the installation. Type 'y' to proceed.

    After running this command, you'll see the MCP Inspector interface launched in your browser. You should see a message like:

    MCP Inspector is up and running at http://localhost:5173
    

    If the browser doesn't open automatically, copy and paste the URL into your browser. You should see something like this: MCP Inspector Interface

  3. Using the Inspector:

    • Click the "Connect" button in the interface (unless there's an error message in the console on the bottom left)
    • Explore the "Tools", "Resources", and "Prompts" tabs to see available functionality
    • Try clicking on listed commands or typing resource names to retrieve resources and prompts
    • The interface allows you to test queries and see how the MCP server responds
  4. Take a look at the official docs

    Official server developers guide: https://modelcontextprotocol.io/quickstart/server

    More on the inspector: https://modelcontextprotocol.io/docs/tools/inspector

Connect Your AI Tool to the Server

You can configure the MCP server for your AI assistant by creating an MCP configuration file:

{
   "mcpServers": {
      "postgres": {
         "command": "/path/to/uv",
         "args": [
            "--directory",
            "/path/to/simple-psql-mcp",
            "run",
            "postgres"
         ],
         "env": {
            "DSN": "postgresql://username:password@localhost:5432/my-db",
            "SCHEMA": "public"
         }
      }
   }
}

Alternatively, you can generate this config file using the included script:

# Make the script executable
chmod +x generate_mcp_config.sh

# Run the configuration generator
./generate_mcp_config.sh

When prompted, enter your PostgreSQL DSN and schema name.

How to use it

You can now ask the LLM questions about your data in natural language:

  • "What are all the tables in my database?"
  • "Show me the top 5 users by creation date"
  • "Count addresses by state"

For testing, Claude Desktop supports MCP natively and works with all features (tools, resources, and prompts) right out of the box.

Example Database (Optional)

If you don't have a database ready or encounter connection issues, you can use the included example database:

# Make the script executable
chmod +x example-db/create-db.sh

# Run the database setup script
./example-db/create-db.sh

This script creates a Docker container with a PostgreSQL database pre-populated with sample users and addresses tables. After running, you can connect using:

npx @modelcontextprotocol/inspector uv --directory . run postgres -e DSN=postgresql://postgres:postgres@localhost:5432/user_database -e SCHEMA=public

Next Steps

To extend this project with your own MCP servers:

  1. Create a new directory under /src (e.g., /src/my-new-mcp)
  2. Implement your MCP server following the PostgreSQL example
  3. Add your new MCP to pyproject.toml:
[project.scripts]
postgres = "src.postgres:main"
my-new-mcp = "src.my-new-mcp:main"

You can then run your new MCP with:

npx @modelcontextprotocol/inspector uv --directory . run my-new-mcp

Documentation

Security

This is an experimental project meant to empower developers to create their own MCP server. I did minimum to make sure it won't die immediately when you try it, but be careful - it's very easy to run SQL injections with this tool. The server will check if the query starts with SELECT, but beyond that nothing is guaranteed. TL;DR - don't run in production unless you're the founder and there are no paying clients.

License

MIT

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

Built Distribution

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

File details

Details for the file iflow_mcp_netanelbollag_simple_psql_mcp-1.0.1.tar.gz.

File metadata

  • Download URL: iflow_mcp_netanelbollag_simple_psql_mcp-1.0.1.tar.gz
  • Upload date:
  • Size: 417.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_netanelbollag_simple_psql_mcp-1.0.1.tar.gz
Algorithm Hash digest
SHA256 596fd6933ca6cdf9404372b54b0f59fb8431a3756986be926a9f091ecf0cd4b3
MD5 216f3343f3af49645d98d6dfdaadfd3c
BLAKE2b-256 b348742a2aa8c48125231945235060283f6cab4034385c36924a9b3a58ab29b8

See more details on using hashes here.

File details

Details for the file iflow_mcp_netanelbollag_simple_psql_mcp-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_netanelbollag_simple_psql_mcp-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_netanelbollag_simple_psql_mcp-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fdf6078cc77fe10e3d8c299c4e335578758997d27ef5a54303a088de500818be
MD5 35a4ae622f3f5e81cf0159c9d73a78ae
BLAKE2b-256 82c136aecc3a96600c857b1701890a6ff9a19238619979d503cf9571bfb2067e

See more details on using hashes here.

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