list_schemas
Retrieve all database schemas to understand structure, manage objects, and organize PostgreSQL data efficiently.
Instructions
List all schemas in the database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The main handler function for the 'list_schemas' tool. It queries the information_schema.schemata to retrieve schema information, categorizes them as System or User schemas, and formats the response using helper functions.@mcp.tool(description="List all schemas in the database") async def list_schemas() -> ResponseType: """List all schemas in the database.""" try: sql_driver = await get_sql_driver() rows = await sql_driver.execute_query( """ SELECT schema_name, schema_owner, CASE WHEN schema_name LIKE 'pg_%' THEN 'System Schema' WHEN schema_name = 'information_schema' THEN 'System Information Schema' ELSE 'User Schema' END as schema_type FROM information_schema.schemata ORDER BY schema_type, schema_name """ ) schemas = [row.cells for row in rows] if rows else [] return format_text_response(format_schemas_as_text(schemas)) except Exception as e: logger.error(f"Error listing schemas: {e}") return format_error_response(str(e))
- Helper function to format the list of schemas into a compact, readable text string, grouping user and system schemas.def format_schemas_as_text(schemas: list[dict]) -> str: """Format schemas list compactly without emojis, preserving details.""" if not schemas: return "No schemas found." # Group by schema type system = [s for s in schemas if s.get("schema_type") in ("System Schema", "System Information Schema")] user = [s for s in schemas if s.get("schema_type") == "User Schema"] out: list[str] = [] if user: items = [f"{s['schema_name']}(owner={s.get('schema_owner', 'N/A')})" for s in user] out.append("UserSchemas: " + "; ".join(items)) if system: shown = system[:10] items = [f"{s['schema_name']}({s.get('schema_type', 'N/A')})" for s in shown] line = f"SystemSchemas({len(system)}): " + "; ".join(items) if len(system) > 10: line += f"; +{len(system) - 10} more" out.append(line) return "\n".join(out)
- src/postgres_mcp_pro_plus/server.py:214-214 (registration)The @mcp.tool decorator registers the list_schemas function as an MCP tool.@mcp.tool(description="List all schemas in the database")