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 4 commits 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

@jba jba mentioned this pull request Jul 1, 2025
@gk16
Copy link
Author

gk16 commented Jul 1, 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

Thanks a lot for your nice words.
Yes, I agree. As more complete golang examples coming in, it would be nice to have a seperate repo to host them, like https://github.com/modelcontextprotocol/servers (they are mostly Python and TS examples)

@jba
Copy link
Contributor

jba commented Jul 4, 2025

The modelcontextprotocol/servers repo is not going to happen.

Here's what we'd like to do:

  • Accept the Go code into this repo.
  • You should host all the code, include scripts and docker config, in your own repo, and add a link here to it.

One concern is that the additional non-Go code is a maintenance burden for us. We are also concerned about security.

You can leave all the other code here for now if it helps you test my suggested changes.

Thanks again for this great piece of work!

@gk16
Copy link
Author

gk16 commented Jul 10, 2025

The modelcontextprotocol/servers repo is not going to happen.

Here's what we'd like to do:

  • Accept the Go code into this repo.
  • You should host all the code, include scripts and docker config, in your own repo, and add a link here to it.

One concern is that the additional non-Go code is a maintenance burden for us. We are also concerned about security.

You can leave all the other code here for now if it helps you test my suggested changes.

Thanks again for this great piece of work!

Thank a lot for your review. I did learn a lot from it.

I have addressed the comments. Please take another look when you have a chance.

As for the all other code, do you mean I can keep main.go, postgres_server.go, postgres_server_test.go, and README.md these 4 files here and move all other files into a seperate repo, right?

@gk16 gk16 requested a review from jba July 10, 2025 08:22
@jba
Copy link
Contributor

jba commented Jul 10, 2025

As for the all other code, do you mean I can keep main.go, postgres_server.go, postgres_server_test.go, and README.md these 4 files here and move all other files into a seperate repo, right?

Yes.

Copy link
Contributor

@jba jba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. Proceed with removing the non-Go files.

Leave the README as it is for your own repo, but for this repo, leave an edited version that links to your repo.

if trimmedPath != "" {
pathParts = strings.Split(trimmedPath, "/")
// Filter out empty parts that might result from consecutive slashes
var filteredParts []string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use slices.DeleteFunc.

{
name: "edge case - table name with special characters",
resourceURI: "postgres://localhost:5432/testdb/test-table_123/schema",
expectedTable: "test-table_123",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test with "///" to test removal of empty parts.

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