Skip to main content

MCP PostgreSQL Demo

A FastMCP server that enables LLMs to connect and interact with PostgreSQL databases. This project demonstrates how to use the Model Context Protocol (MCP) to allow Language Models to query and explore database schemas and tables.

Features

  • Schema Exploration: Retrieve metadata about database schemas
  • Table Inspection: Get detailed information about table structures
  • Database Querying: Execute SQL queries against the database
  • YAML Formatting: Results are returned in YAML format for easy consumption by LLMs

Resources

The server exposes the following MCP resources:

  • database://{schema} - Get information about all tables in a schema
  • database://{schema}/tables/{table} - Get detailed information about a specific table

Tools

  • query_database - Execute SQL queries against the database (SELECT queries only)

Prompts

The server includes the following predefined prompts:

  • prompt_schema_description - Ask for a description of a database schema
  • prompt_table_description - Ask for a description of a specific table
  • prompt_query_database - Ask for data from a specific table

Prerequisites

  • Python 3.12 or higher
  • PostgreSQL database
  • UV package manager (recommended)

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd mcp-demo
    
  2. Create a virtual environment:

    python -m venv .venv
    source .venv/bin/activate  # On Windows: .venv\Scripts\activate
    
  3. Install UV (if not already installed):

    pip install uv
    
  4. Install dependencies with UV:

    uv sync
    
  5. Configure environment variables:

    • Copy .env.example to .env
    • Update the values according to your PostgreSQL configuration

Configuration

The application is configured using environment variables:

Variable Description Default
APP_NAME Application name mcp-demo
DB_HOST PostgreSQL host localhost
DB_PORT PostgreSQL port 5432
DB_USER PostgreSQL username postgres
DB_PASSWORD PostgreSQL password postgres
DB_NAME PostgreSQL database name postgres

Usage

  1. First, uncomment the run function in src/main.py by removing the comment from these lines at the bottom of the file:

    # if __name__ == "__main__":
    #     print("Starting FastMCP server...")
    #     mcp.run()
    
  2. Start the FastMCP server:

    python -m src.main
    
  3. The server will be available for LLMs to connect to and query your PostgreSQL database. With the server running, the MCP can be loaded into client applications for interaction.

Client Configuration

To use this MCP in a client application, add the following configuration to your client's MCP configuration file (e.g., .cursor/mcp.json):

{
  "mcpServers": {
    "postgres-mcp-server": {
      "command": "/path/to/your/venv/bin/mcp",
      "args": ["run", "/path/to/your/postgres-mcp/src/main.py"],
      "env": {
        "APP_NAME": "mcp-demo",
        "DB_HOST": "localhost",
        "DB_PORT": "5432",
        "DB_USER": "postgres",
        "DB_PASSWORD": "postgres",
        "DB_NAME": "postgres"
      }
    }
  }
}

Be sure to replace the paths with the actual paths to your virtual environment and project directory, and update the environment variables to match your PostgreSQL configuration.

Development

Install development dependencies with UV:

uv pip install -e ".[dev]"

Development tools included:

  • JupyterLab for notebooks
  • Pyright for type checking
  • Ruff for linting

Docker

To run the application with Docker:

  1. Build the Docker image:

    docker build -t mcp-demo .
    
  2. Run the container:

    docker run --env-file .env.docker -p 8000:8000 mcp-demo
    

Example Usage

Get Schema Information

from mcp.client import get_client

client = get_client("http://localhost:8000")
schema_info = client.get_resource("database://public")
print(schema_info)

Get Table Details

table_info = client.get_resource("database://public/tables/users")
print(table_info)

Execute a Query

result = client.invoke_tool("query_database", {"query": "SELECT * FROM users LIMIT 10"})
print(result)

License

[Add your license information here]

Contributors

Metadata

Release files for iflow-mcp_tibiritabara_mcp-demo 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iflow-mcp_tibiritabara_mcp-demo 0.1.0
File Size Uploaded
iflow_mcp_tibiritabara_mcp_demo-0.1.0.tar.gz 67.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iflow-mcp_tibiritabara_mcp-demo 0.1.0
File Interpreter ABI Platform
iflow_mcp_tibiritabara_mcp_demo-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 74.3 kB

Release files / iflow_mcp_tibiritabara_mcp_demo-0.1.0.tar.gz

Download URL iflow_mcp_tibiritabara_mcp_demo-0.1.0.tar.gz
Size 67.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b0f31778a2942d68afc09e4e8c3a930c90b0446c7f2697d9116dca25afc5fb92
BLAKE2b-256 checksum
How to use checksums
0029e3444d9edc381e18bbd151f15def0a17ce238a1b239c0e09b7541eec7046
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.8

Release files / iflow_mcp_tibiritabara_mcp_demo-0.1.0-py3-none-any.whl

Download URL iflow_mcp_tibiritabara_mcp_demo-0.1.0-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63af82c2f614d64313b6cf78965be913fca2c007dd77df14c9a7a244ff94eb53
BLAKE2b-256 checksum
How to use checksums
5ab1133b93dfde9e81cfe6fab3627060b042dda687d407eb93f3316a70fbaead
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.8

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page