Skip to content

mcp/examples: add PostgreSQL MCP server example #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gk16
Copy link

@gk16 gk16 commented Jun 29, 2025

Add standalone PostgreSQL MCP server example with development environment and tooling. This implementation provides read-only database access, schema introspection, and query capabilities while maintaining clean dependency separation from the main SDK.

Motivation and Context

This is part of #33. This PR adds example of implementing mcp on PostgreSQL from https://modelcontextprotocol.io/examples.
The PR provides a production-ready PostgreSQL MCP server example with proper deployment options, testing, and development tooling.

How Has This Been Tested?

  • 17 test cases, including unit tests, integration tests, and edge case handling
  • Full docker-compose setup with PostgreSQL database and sample data
  • Tested against live PostgreSQL with sample schema (users, products, orders tables)

Breaking Changes

  • No Breaking Changes: This is purely additive - a new example in a separate subproject

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Architecture Decisions

  • Independent Module: Created examples/postgres/go.mod for this example
  • Read-Only Safety: All database operations use read-only transactions to prevent accidental data modification

Files Added

  • main.go - PostgreSQL MCP server implementation with schema introspection and query tools
  • main_test.go - Comprehensive test suite with mock database testing
  • go.mod/go.sum - Independent module definition with local SDK reference
  • Dockerfile - Multi-stage build for production deployment
  • docker-compose.yml - Complete development environment with PostgreSQL
  • init.sql - Sample database schema and data for testing
  • Makefile - Build automation and development workflow
  • demo.sh - Interactive demonstration script
  • README.md - Complete documentation following Python server format
  • .env.example - Configuration template
  • .gitignore - Proper exclusions for Go and Docker artifacts

Development Experience

  • Quick Start: make dev sets up complete environment
  • Multiple Run Modes: stdio (default) and HTTP (debugging) modes

MCP Features Implemented

  • Resources: Dynamic table discovery with schema information
  • Tools: Read-only SQL query execution with transaction safety
  • Error Handling: Proper MCP error responses and comprehensive error coverage
  • Resource URIs: Standard format postgres://host/table/schema

This implementation serves as a reference for building production MCP servers while maintaining clean project architecture.

@gk16 gk16 marked this pull request as ready for review June 29, 2025 00:22
@jba
Copy link
Contributor

jba commented Jun 30, 2025

Thanks for this large contribution. It's a lot of work, and we appreciate that. It's cool that you have a complete implementation, with a Makefile, shell scripts, Dockerfile, and so on.

This is definitely worth living somewhere. I'm just not sure it should live in this repo. There is a lot of code to maintain. What if we get a bug report against the Dockerfile or docker compose config? Or some bug about a runtime failure in a particular installation? I don't know if the maintainers of this repo are up to that sort of commitment.

We're going to think about this and get back to you.

/cc @findleyr @samthanawalla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants