Skip to main content
Glama
melihbirim

PostgreSQL MCP Server

by melihbirim

describe_table

Retrieve table schema details, including column definitions and constraints, for PostgreSQL databases using the PostgreSQL MCP Server's describe_table tool.

Instructions

Get detailed information about a table's structure, columns, and constraints

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tableNameYesName of the table to describe

Implementation Reference

  • The handler function that implements the logic for the 'describe_table' tool. It queries PostgreSQL information_schema for columns, primary keys, and foreign keys, then formats a detailed textual description of the table structure.
    async ({ tableName }) => {
      try {
        // Get column information
        const columnsQuery = `
          SELECT 
            column_name,
            data_type,
            is_nullable,
            column_default,
            character_maximum_length,
            numeric_precision,
            numeric_scale
          FROM information_schema.columns 
          WHERE table_schema = 'public' AND table_name = $1
          ORDER BY ordinal_position;
        `;
        
        const columns = await executeQuery(columnsQuery, [tableName]);
        
        if (columns.length === 0) {
          return {
            content: [
              {
                type: "text",
                text: `Table '${tableName}' not found or has no columns.`,
              },
            ],
          };
        }
    
        // Get primary key information
        const pkQuery = `
          SELECT column_name
          FROM information_schema.table_constraints tc
          JOIN information_schema.key_column_usage kcu 
            ON tc.constraint_name = kcu.constraint_name
          WHERE tc.table_schema = 'public' 
            AND tc.table_name = $1 
            AND tc.constraint_type = 'PRIMARY KEY'
          ORDER BY kcu.ordinal_position;
        `;
        
        const primaryKeys = await executeQuery(pkQuery, [tableName]);
    
        // Get foreign key information
        const fkQuery = `
          SELECT 
            kcu.column_name,
            ccu.table_name AS referenced_table,
            ccu.column_name AS referenced_column
          FROM information_schema.table_constraints tc
          JOIN information_schema.key_column_usage kcu 
            ON tc.constraint_name = kcu.constraint_name
          JOIN information_schema.constraint_column_usage ccu 
            ON ccu.constraint_name = tc.constraint_name
          WHERE tc.table_schema = 'public' 
            AND tc.table_name = $1 
            AND tc.constraint_type = 'FOREIGN KEY';
        `;
        
        const foreignKeys = await executeQuery(fkQuery, [tableName]);
    
        // Format response
        let response = `Table: ${tableName}\n\nColumns:\n`;
        
        columns.forEach(col => {
          const isPk = primaryKeys.some(pk => pk.column_name === col.column_name);
          const fk = foreignKeys.find(fk => fk.column_name === col.column_name);
          
          response += `- ${col.column_name}: ${col.data_type}`;
          
          if (col.character_maximum_length) {
            response += `(${col.character_maximum_length})`;
          } else if (col.numeric_precision) {
            response += `(${col.numeric_precision}${col.numeric_scale ? `,${col.numeric_scale}` : ''})`;
          }
          
          response += ` ${col.is_nullable === 'YES' ? 'NULL' : 'NOT NULL'}`;
          
          if (col.column_default) {
            response += ` DEFAULT ${col.column_default}`;
          }
          
          if (isPk) {
            response += ' [PRIMARY KEY]';
          }
          
          if (fk) {
            response += ` [FK -> ${fk.referenced_table}.${fk.referenced_column}]`;
          }
          
          response += '\n';
        });
    
        return {
          content: [
            {
              type: "text",
              text: response,
            },
          ],
        };
      } catch (error) {
        const errorMessage = error instanceof Error ? error.message : "Unknown error";
        return {
          content: [
            {
              type: "text",
              text: `Error describing table '${tableName}': ${errorMessage}`,
            },
          ],
        };
      }
    }
  • The Zod input schema defining the 'tableName' parameter for the 'describe_table' tool.
    {
      tableName: z.string().describe("Name of the table to describe"),
    },
  • src/index.ts:171-291 (registration)
    The registration of the 'describe_table' tool using server.tool(), including name, description, input schema, and handler reference.
    server.tool(
      "describe_table",
      "Get detailed information about a table's structure, columns, and constraints",
      {
        tableName: z.string().describe("Name of the table to describe"),
      },
      async ({ tableName }) => {
        try {
          // Get column information
          const columnsQuery = `
            SELECT 
              column_name,
              data_type,
              is_nullable,
              column_default,
              character_maximum_length,
              numeric_precision,
              numeric_scale
            FROM information_schema.columns 
            WHERE table_schema = 'public' AND table_name = $1
            ORDER BY ordinal_position;
          `;
          
          const columns = await executeQuery(columnsQuery, [tableName]);
          
          if (columns.length === 0) {
            return {
              content: [
                {
                  type: "text",
                  text: `Table '${tableName}' not found or has no columns.`,
                },
              ],
            };
          }
    
          // Get primary key information
          const pkQuery = `
            SELECT column_name
            FROM information_schema.table_constraints tc
            JOIN information_schema.key_column_usage kcu 
              ON tc.constraint_name = kcu.constraint_name
            WHERE tc.table_schema = 'public' 
              AND tc.table_name = $1 
              AND tc.constraint_type = 'PRIMARY KEY'
            ORDER BY kcu.ordinal_position;
          `;
          
          const primaryKeys = await executeQuery(pkQuery, [tableName]);
    
          // Get foreign key information
          const fkQuery = `
            SELECT 
              kcu.column_name,
              ccu.table_name AS referenced_table,
              ccu.column_name AS referenced_column
            FROM information_schema.table_constraints tc
            JOIN information_schema.key_column_usage kcu 
              ON tc.constraint_name = kcu.constraint_name
            JOIN information_schema.constraint_column_usage ccu 
              ON ccu.constraint_name = tc.constraint_name
            WHERE tc.table_schema = 'public' 
              AND tc.table_name = $1 
              AND tc.constraint_type = 'FOREIGN KEY';
          `;
          
          const foreignKeys = await executeQuery(fkQuery, [tableName]);
    
          // Format response
          let response = `Table: ${tableName}\n\nColumns:\n`;
          
          columns.forEach(col => {
            const isPk = primaryKeys.some(pk => pk.column_name === col.column_name);
            const fk = foreignKeys.find(fk => fk.column_name === col.column_name);
            
            response += `- ${col.column_name}: ${col.data_type}`;
            
            if (col.character_maximum_length) {
              response += `(${col.character_maximum_length})`;
            } else if (col.numeric_precision) {
              response += `(${col.numeric_precision}${col.numeric_scale ? `,${col.numeric_scale}` : ''})`;
            }
            
            response += ` ${col.is_nullable === 'YES' ? 'NULL' : 'NOT NULL'}`;
            
            if (col.column_default) {
              response += ` DEFAULT ${col.column_default}`;
            }
            
            if (isPk) {
              response += ' [PRIMARY KEY]';
            }
            
            if (fk) {
              response += ` [FK -> ${fk.referenced_table}.${fk.referenced_column}]`;
            }
            
            response += '\n';
          });
    
          return {
            content: [
              {
                type: "text",
                text: response,
              },
            ],
          };
        } catch (error) {
          const errorMessage = error instanceof Error ? error.message : "Unknown error";
          return {
            content: [
              {
                type: "text",
                text: `Error describing table '${tableName}': ${errorMessage}`,
              },
            ],
          };
        }
      }
    );
  • Helper function 'executeQuery' used by the 'describe_table' handler to safely execute read-only SQL queries against the connected PostgreSQL database.
    async function executeQuery(query: string, params: any[] = []): Promise<any[]> {
      const client = await getDbConnection();
      
      // Basic safety checks for read-only operations
      const normalizedQuery = query.trim().toLowerCase();
      const readOnlyPrefixes = ['select', 'show', 'describe', 'explain', 'with'];
      const isReadOnly = readOnlyPrefixes.some(prefix => normalizedQuery.startsWith(prefix));
      
      if (!isReadOnly) {
        throw new Error("Only read-only queries (SELECT, SHOW, DESCRIBE, EXPLAIN, WITH) are allowed for security.");
      }
      
      try {
        const result = await client.query(query, params);
        return result.rows;
      } catch (error) {
        const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
        throw new Error(`Query execution failed: ${errorMessage}`);
      }
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states this is a read operation ('Get detailed information'), which is clear, but it doesn't cover aspects like permissions needed, error handling, or what the output format looks like (e.g., structured data or raw text). This leaves gaps for an agent to understand the tool's behavior fully.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core purpose without any wasted words. It is appropriately sized for a simple tool with one parameter, making it easy for an agent to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's low complexity (1 parameter, no annotations, no output schema), the description is adequate but incomplete. It covers the basic purpose but lacks details on usage context, behavioral traits, and output expectations, which are needed for full agent understanding in a database environment.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, with the single parameter 'tableName' documented as 'Name of the table to describe'. The description adds no additional meaning beyond this, such as format examples or constraints, so it meets the baseline of 3 where the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb ('Get detailed information') and resource ('table's structure, columns, and constraints'), making the purpose specific and understandable. It distinguishes from siblings like 'list_tables' (which lists names) and 'get_schema' (which might cover broader schema info), though it doesn't explicitly name these alternatives.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'get_schema' or 'execute_query', nor does it mention prerequisites such as needing an active database connection. It implies usage for table details but lacks explicit context or exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

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/melihbirim/pg-mcp'

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