Skip to main content
Glama

describe_view

Retrieve the SQL definition and column structure of PostgreSQL views to understand database schema and query logic.

Instructions

Get the definition and columns of a view.

Args:
    view_name: Name of the view
    schema: Schema name (default: public)
    
Returns:
    View definition SQL and column list

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
view_nameYes
schemaNopublic

Implementation Reference

  • Core handler function that executes the SQL queries to fetch view definition from information_schema.views and column details from information_schema.columns.
    def describe_view(self, view_name: str, schema: str = "public") -> dict[str, Any]:
        """Get view definition and columns.
        
        Args:
            view_name: View name
            schema: Schema name
            
        Returns:
            Dict with view definition and columns
        """
        result = {
            "name": view_name,
            "schema": schema,
            "definition": "",
            "columns": [],
        }
        
        with self.get_cursor() as cursor:
            # Get view definition
            cursor.execute("""
                SELECT view_definition
                FROM information_schema.views
                WHERE table_schema = %s AND table_name = %s
            """, (schema, view_name))
            row = cursor.fetchone()
            if row:
                result["definition"] = row["view_definition"]
            
            # Get columns
            cursor.execute("""
                SELECT 
                    column_name,
                    data_type,
                    is_nullable
                FROM information_schema.columns 
                WHERE table_schema = %s AND table_name = %s
                ORDER BY ordinal_position
            """, (schema, view_name))
            result["columns"] = [dict(row) for row in cursor.fetchall()]
        
        return result
  • MCP tool registration using @mcp.tool() decorator. This is the entry point for the tool, which delegates to the PostgresClient instance and adds error handling and not-found response.
    @mcp.tool()
    @handle_db_error
    def describe_view(view_name: str, schema: str = "public") -> dict:
        """Get the definition and columns of a view.
        
        Args:
            view_name: Name of the view
            schema: Schema name (default: public)
            
        Returns:
            View definition SQL and column list
        """
        client = get_client()
        result = client.describe_view(view_name, schema)
        
        if not result["definition"]:
            return not_found_response("View", f"{schema}.{view_name}")
        
        return result

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