Add MCP Resource Template Support for Dynamic Database Schema Discovery #123
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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}/tablespostgres://{database_name}/{schema_name}/viewspostgres://databasespostgres://{database_name}/schemas2. Multi-Database Connection Management
File:
src/postgres_mcp/utils/sql_driver.py(new file, 152 lines)Implemented robust connection pooling and management:
3. Utility Enhancements
Files:
src/postgres_mcp/utils/reponse.py(new file, 16 lines)format_text_response()- Standardized text response formattingformat_error_response()- Standardized error response formattingsrc/postgres_mcp/utils/url.py(new file, 17 lines)fix_connection_url()- Automatic URL encoding for special characters in passwordssrc/postgres_mcp/moldes/model.py(8 lines added)AccessModeenum for database access control4. Server Refactoring
File:
src/postgres_mcp/server.py(net -547 lines)