amazon-q-postgresql-mcp-server

amazon-q-postgresql-mcp-server

0

This project is a Rust-based server that implements the MCP protocol, enabling integration with Amazon Q to interact with PostgreSQL databases. It allows for performing read-only operations such as listing tables, retrieving table schemas, and executing SQL queries with results returned in JSON format.

PostgreSQL MCP Server for Amazon Q

A Rust-based Modular Capability Provider (MCP) server that enables Amazon Q interact PostgreSQL database. Server allows users to query database struture read-only queries throught the Amazon Q chat Interface.

Overview

This project implements a PostgreSQL server that follows the MCP protocol used by Amazon Q CLI

  1. List all table in a PostgreSQL databases
  2. Get Schema info for specific tables
  3. Execute read-only SQL queries with result return as JSON

Prerequisites

  • Rust 1.70
  • Cargo package manager
cargo new postgresql_server
  • PostgreSQL database
  • Amazon Q CLI

Installation

  1. Clone the Repository:
git clone ...
cd postgresql-mcp-server
  1. Build the project :
cargo build --release --bin postgresql_server
BUILD SUCCESSFUL

Usage

Running the Server

  • Server requires a PostgreSQL connection string as a command-line-argument :
./target/release/postgresql_server "postgresql://username:password@hostname:port/database"

Intergrating with Amazo Q CLI

  1. Create or edit the MCP
vim ~/.aws/amazonq/mcp.json
  1. Add the PosgreSQL server configuration to file
{
  "mcpServers": {
    "calculator": {
      "command": "/home/ec2-user/sample-building-mcp-servers-with-rust/target/release/calculator_server",
      "args": []
    },
    "rds": {
      "command": "/home/ec2-user/sample-building-mcp-servers-with-rust/target/release/rds_server",
      "args": []
    },
    "s3": {
      "command": "/home/ec2-user/sample-building-mcp-servers-with-rust/target/release/s3_server",
      "args": []
    },
    "postgres": {
      "command": "/home/ec2-user/sample-building-mcp-servers-with-rust/target/release/postgresql_server",
      "args": [
        "postgresql://postgres:<DB-PASSWORD>@<DB-ENDPOINT>.com:5432/demo"
      ]
    }
  }
}
  1. Testing Q Amanzon Server
  • Start Q Amazon
qchat
image
  • Ask Amazon Q to list tables in the database Can you list all tables in my PostgreSQL database using the postgres operator?

    image
  • Try asking for schema of specific table: What columns are in the student table?

    image
  • Execute a SQL query: List 100 students sorted by name.

    image

Project Structure

  • src/main.rs: Entry point that comman-line-arguments, logging setup, and server initialization
  • src/operator.rs Implementation of the PostgreSQL with database interaction tools.

Tool Implementation in operator.rs

The server implements three main tools using the tool artribute from the RMCP framework:

  1. List Tables:
#[tool(description = "List tables in the PostgreSQL database")]
async fn list_tables(&self) -> String {
    // Implementation that queries information_schema.tables
    // and returns a JSON string of table names
}
  1. Get Table Schema :
#[tool(description = "Get schema for a specific table")]
async fn get_table_schema(
    &self,
    #[tool(param)]
    #[schemars(description = "Name of the table to get schema for")]
    table_name: String,
) -> String {
    // Implementation that queries information_schema.columns
    // and returns a JSON string of column names and data types
}

  1. Execute SQL Query:
#[tool(description = "Run a read-only SQL query")]
async fn query(
    &self,
    #[tool(param)]
    #[schemars(description = "SQL query to execute (read-only)")]
    sql: String,
) -> String {
    // Implementation that executes the SQL query
    // and returns a JSON string of the results
}