list_views
Retrieve all view names from a PostgreSQL schema to analyze database structure and understand available data perspectives.
Instructions
List all views in a schema.
Args:
schema: Schema name (default: public)
Returns:
List of views with name
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schema | No | public |
Implementation Reference
- postgres_mcp/server.py:312-330 (handler)MCP tool handler for 'list_views'. Registered via @mcp.tool() decorator. Executes the tool logic by calling PostgresClient.list_views() and formatting the response using ViewSummary models.@mcp.tool() @handle_db_error def list_views(schema: str = "public") -> dict: """List all views in a schema. Args: schema: Schema name (default: public) Returns: List of views with name """ client = get_client() views = client.list_views(schema) return { "schema": schema, "views": [ViewSummary.from_row(v).model_dump() for v in views], }
- postgres_mcp/server.py:312-312 (registration)The @mcp.tool() decorator registers the list_views function as an MCP tool.@mcp.tool()
- postgres_mcp/models.py:157-169 (schema)Pydantic BaseModel defining the output schema for view summaries. Used to serialize raw database rows into structured responses.class ViewSummary(BaseModel): """View info for list responses.""" name: str schema_name: str = "public" @classmethod def from_row(cls, row: dict) -> "ViewSummary": return cls( name=row.get("table_name", ""), schema_name=row.get("table_schema", "public"), )
- Core helper method in PostgresClient that executes the SQL query to retrieve views from information_schema.views.def list_views(self, schema: str = "public") -> list[dict]: """List views in a schema. Args: schema: Schema name Returns: List of view dicts """ query = """ SELECT table_name, table_schema FROM information_schema.views WHERE table_schema = %s ORDER BY table_name """ with self.get_cursor() as cursor: cursor.execute(query, (schema,)) return [dict(row) for row in cursor.fetchall()]