Skip to content

Commit e9948c7

Browse files
authored
fix: resolve get_database_schema resource validation errors (#22)
Fixes SQL query validation errors when accessing database://schema resource with views. - Fixed SQL query to use pg_class directly via LEFT JOINs instead of ::regclass - Added obj_description, col_description, and shobj_description to SafeSqlDriver allowed functions - Ensures database://schema resource works correctly with tables, views, and materialized views Co-authored-by: shitalm <[email protected]>
1 parent cf0044d commit e9948c7

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/postgres_mcp/resources/database_resources.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ async def get_database_schema() -> str: # pyright: ignore[reportUnusedFunction]
3535
sql_driver: Any = await get_sql_driver_func()
3636

3737
# Query information_schema for tables
38+
# Use pg_class directly to avoid ::regclass issues with views
3839
tables_query = """
3940
SELECT
4041
t.table_schema,
4142
t.table_name,
4243
t.table_type,
43-
obj_description((t.table_schema||'.'||t.table_name)::regclass) as table_comment
44+
obj_description(c.oid, 'pg_class') as table_comment
4445
FROM information_schema.tables t
46+
LEFT JOIN pg_namespace n ON n.nspname = t.table_schema
47+
LEFT JOIN pg_class c ON c.relnamespace = n.oid AND c.relname = t.table_name
4548
WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema')
4649
ORDER BY t.table_schema, t.table_name
4750
"""

src/postgres_mcp/sql/safe_sql.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ class SafeSqlDriver(SqlDriver):
306306
"unicode_version",
307307
"icu_unicode_version",
308308
# Database object information functions
309+
"obj_description",
310+
"col_description",
311+
"shobj_description",
309312
"pg_column_size",
310313
"pg_column_compression",
311314
"pg_column_toast_chunk_id",

0 commit comments

Comments
 (0)