Skip to content

Conversation

@ryohang
Copy link

@ryohang ryohang commented Dec 26, 2025

Summary:
This PR refactors the PostgreSQL MCP server to use MCP resource templates, enabling dynamic discovery of database schemas, tables, and views across multiple databases. This replaces the previous static resource approach with a more flexible template-based system.

Key Changes

1. New Resource Template Architecture

File: src/postgres_mcp/resource.py (new file, 678 lines)

Created register_resource_templates() function to register parameterized resource URIs:

Resource Endpoints

  • postgres://{database_name}/{schema_name}/tables

    • Get comprehensive table information for a specific database and schema
    • Returns: schemas, columns with comments, constraints, indexes, size, row counts
  • postgres://{database_name}/{schema_name}/views

    • Get comprehensive view information for a specific database and schema
    • Returns: schemas, columns with comments, view definitions, dependencies
  • postgres://databases

    • List all available databases in the PostgreSQL server
    • Returns: database_name, owner, encoding, collation, ctype, size
  • postgres://{database_name}/schemas

    • List all schemas in a specific database
    • Returns: schema_name, schema_owner, schema_type (user/system)

2. Multi-Database Connection Management

File: src/postgres_mcp/utils/sql_driver.py (new file, 152 lines)

Implemented robust connection pooling and management:

# Key functions
- get_sql_driver() - Get driver for main database
- get_sql_driver_for_database(database_name) - Get/create driver for specific database
- get_current_database_name() - Retrieve active database name

3. Utility Enhancements

Files:

  • src/postgres_mcp/utils/reponse.py (new file, 16 lines)

    • format_text_response() - Standardized text response formatting
    • format_error_response() - Standardized error response formatting
  • src/postgres_mcp/utils/url.py (new file, 17 lines)

    • fix_connection_url() - Automatic URL encoding for special characters in passwords
  • src/postgres_mcp/moldes/model.py (8 lines added)

    • AccessMode enum for database access control

4. Server Refactoring

File: src/postgres_mcp/server.py (net -547 lines)

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.

3 participants