Skip to main content
Glama

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
NameRequiredDescriptionDefault
schemaNopublic

Implementation Reference

  • 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],
        }
  • The @mcp.tool() decorator registers the list_views function as an MCP tool.
    @mcp.tool()
  • 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()]

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/JaviMaligno/postgres-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server