Skip to main content
Glama

list_tables

Retrieve all tables from a PostgreSQL schema to understand database structure. Specify a schema name to view its tables and their types.

Instructions

List all tables in a specific schema.

Args:
    schema: Schema name to list tables from (default: public)
    
Returns:
    List of tables with name and type

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
schemaNopublic

Implementation Reference

  • MCP tool handler for 'list_tables'. Fetches tables using PostgresClient and formats output using TableSummary models.
    @mcp.tool()
    @handle_db_error
    def list_tables(schema: str = "public") -> dict:
        """List all tables in a specific schema.
        
        Args:
            schema: Schema name to list tables from (default: public)
            
        Returns:
            List of tables with name and type
        """
        client = get_client()
        tables = client.list_tables(schema)
        
        return {
            "schema": schema,
            "tables": [TableSummary.from_row(t).model_dump() for t in tables],
        }
  • Pydantic model defining the output schema for table summaries in list_tables response.
    class TableSummary(BaseModel):
        """Table info for list responses."""
        
        model_config = {"populate_by_name": True}
        
        name: str
        type: str = "BASE TABLE"
        schema_name: str = "public"
        
        @classmethod
        def from_row(cls, row: dict) -> "TableSummary":
            return cls(
                name=row.get("table_name", ""),
                type=row.get("table_type", "BASE TABLE"),
                schema_name=row.get("table_schema", "public"),
            )
  • FastMCP decorator registering the list_tables function as an MCP tool.
    @mcp.tool()
  • Core implementation in PostgresClient that queries information_schema.tables to list tables in a schema.
    def list_tables(self, schema: str = "public") -> list[dict]:
        """List all tables in a schema.
        
        Args:
            schema: Schema name (default: public)
            
        Returns:
            List of table dicts
        """
        query = """
            SELECT table_name, table_type, table_schema
            FROM information_schema.tables 
            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