amazon-q-postgresql-mcp-server
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
- List all table in a PostgreSQL databases
- Get Schema info for specific tables
- 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
- Clone the Repository:
git clone ...
cd postgresql-mcp-server
- Build the project :
cargo build --release --bin postgresql_server
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
- Create or edit the MCP
vim ~/.aws/amazonq/mcp.json
- 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"
]
}
}
}
- Testing Q Amanzon Server
- Start Q Amazon
qchat
-
Ask Amazon Q to list tables in the database
Can you list all tables in my PostgreSQL database using the postgres operator?
-
Try asking for schema of specific table:
What columns are in the student table?
-
Execute a SQL query:
List 100 students sorted by name.
Project Structure
src/main.rs
: Entry point that comman-line-arguments, logging setup, and server initializationsrc/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:
- 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
}
- 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
}
- 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
}