adropofliquid/postgres-mcp-server
If you are the rightful owner of postgres-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
A Model Context Protocol (MCP) server providing read-only access to PostgreSQL databases, enabling AI clients to interact safely with database structures and data.
listTables
Lists all tables in the public schema.
executeSelectQuery
Executes SELECT queries safely, limited to 100 rows.
describeTable
Describes the structure of a specific table.
PostgreSQL MCP Server
A Model Context Protocol (MCP) server that provides read-only access to PostgreSQL databases. This server exposes tools that allow AI clients to interact with PostgreSQL databases safely by executing SELECT queries, listing tables, and describing table structures.
Features
The server provides three main tools:
- List Tables: Lists all tables in the public schema
- Execute SELECT Query: Executes SELECT queries safely (read-only)
- Describe Table: Shows table structure including columns, data types, and constraints
Prerequisites
- Java 17 or higher
- PostgreSQL database
- Gradle 8.0+
Configuration
Database Configuration
Set the following environment variables or update src/main/resources/application.yml
:
export POSTGRES_URL=jdbc:postgresql://localhost:5432/your_database
export POSTGRES_USERNAME=your_username
export POSTGRES_PASSWORD=your_password
Application Configuration
The server is configured to run as an MCP SSE (Server-Sent Events) server. The configuration is in application.yml
:
spring:
ai:
mcp:
server:
sse:
Building and Running
Build the Application
./gradlew build
Run the Server
./gradlew bootRun
Or run the built JAR:
java -jar build/libs/postgres-0.0.1-SNAPSHOT.jar
Using with MCP Clients
This server communicates via SSE (Server-Sent Events) over HTTP and follows the MCP protocol specification. Any MCP-compatible client can connect to it at http://localhost:8080/mcp/sse
.
Available Tools
1. listTables
Lists all tables in the public schema.
Usage: "Show me all tables in the database"
2. executeSelectQuery
Executes SELECT queries safely. Only SELECT statements are allowed for security.
Parameters:
query
: The SELECT SQL query to execute
Usage: "Execute the query: SELECT * FROM users LIMIT 10"
Security Features:
- Only SELECT queries are allowed
- Blocks INSERT, UPDATE, DELETE, DROP, CREATE, ALTER statements
- Limited to 100 rows in output for performance
3. describeTable
Describes the structure of a specific table.
Parameters:
tableName
: The name of the table to describe
Usage: "Describe the structure of the users table"
Output includes:
- Column names and data types
- Nullable constraints
- Default values
- Primary keys
- Foreign key relationships
Security
This server implements several security measures:
- Read-Only Access: Only SELECT queries are permitted
- Query Validation: Dangerous SQL keywords are blocked
- Schema Restriction: Only accesses the public schema
- Result Limiting: Query results are limited to 100 rows
- Input Sanitization: SQL injection protection via parameterized queries
Example Queries
Once connected to an MCP client like Claude Desktop, you can ask:
- "What tables are available in this database?"
- "Show me the first 10 rows from the users table"
- "Describe the structure of the products table"
- "Find all orders from the last month"
Troubleshooting
Connection Issues
- Verify PostgreSQL is running
- Check database credentials
- Ensure the database is accessible from the server location
- Verify the JDBC URL format
MCP Client Issues
- Ensure the server starts without errors
- Check that SSE transport is properly configured and the port is available
- Verify the client can access
http://localhost:8080/mcp/sse
- Check firewall settings if connecting from external clients
Performance Considerations
- Large result sets are automatically limited to 100 rows
- Complex queries may timeout - consider adding WHERE clauses
- Index your tables for better query performance
Development
Adding New Tools
To add new tools, create methods in PostgresToolsService
and annotate them with @Tool
:
@Tool(description = "Your tool description")
fun yourNewTool(
@ToolParam(description = "Parameter description") param: String
): String {
// Implementation
}
Contributing
- Fork the repository
- Create a feature branch
- Implement your changes 4Submit a pull request
License
This project is licensed under the MIT License.