Skip to main content
Glama

list_constraints

Retrieve all constraints for a PostgreSQL table, including primary keys, foreign keys, unique constraints, and check constraints, to understand table relationships and data integrity rules.

Instructions

List all constraints for a table (PK, FK, UNIQUE, CHECK).

Args:
    table_name: Name of the table
    schema: Schema name (default: public)
    
Returns:
    List of constraints with type, columns, and references

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
table_nameYes
schemaNopublic

Implementation Reference

  • MCP tool handler for 'list_constraints', decorated with @mcp.tool() for registration and execution. Processes raw constraints from client and groups multi-column ones.
    @mcp.tool()
    @handle_db_error
    def list_constraints(table_name: str, schema: str = "public") -> dict:
        """List all constraints for a table (PK, FK, UNIQUE, CHECK).
        
        Args:
            table_name: Name of the table
            schema: Schema name (default: public)
            
        Returns:
            List of constraints with type, columns, and references
        """
        client = get_client()
        constraints = client.list_constraints(table_name, schema)
        
        # Group by constraint name to handle multi-column constraints
        grouped = {}
        for c in constraints:
            name = c["constraint_name"]
            if name not in grouped:
                grouped[name] = {
                    "name": name,
                    "type": c["constraint_type"],
                    "columns": [],
                    "references_table": c.get("references_table"),
                    "references_column": c.get("references_column"),
                    "check_clause": c.get("check_clause"),
                }
            if c.get("column_name"):
                grouped[name]["columns"].append(c["column_name"])
        
        return {
            "table_name": table_name,
            "schema": schema,
            "constraints": list(grouped.values()),
        }
  • Core helper method in PostgresClient that executes SQL query against information_schema to fetch raw constraint data.
    def list_constraints(self, table_name: str, schema: str = "public") -> list[dict]:
        """List constraints for a table.
        
        Args:
            table_name: Table name
            schema: Schema name
            
        Returns:
            List of constraint dicts
        """
        query = """
            SELECT 
                tc.constraint_name,
                tc.constraint_type,
                tc.table_name,
                kcu.column_name,
                ccu.table_name AS references_table,
                ccu.column_name AS references_column,
                cc.check_clause
            FROM information_schema.table_constraints tc
            LEFT JOIN information_schema.key_column_usage kcu
                ON tc.constraint_name = kcu.constraint_name
                AND tc.table_schema = kcu.table_schema
            LEFT JOIN information_schema.constraint_column_usage ccu
                ON tc.constraint_name = ccu.constraint_name
                AND tc.table_schema = ccu.table_schema
                AND tc.constraint_type = 'FOREIGN KEY'
            LEFT JOIN information_schema.check_constraints cc
                ON tc.constraint_name = cc.constraint_name
                AND tc.table_schema = cc.constraint_schema
            WHERE tc.table_schema = %s
                AND tc.table_name = %s
            ORDER BY tc.constraint_type, tc.constraint_name
        """
        with self.get_cursor() as cursor:
            cursor.execute(query, (schema, table_name))
            return [dict(row) for row in cursor.fetchall()]
Behavior3/5

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

No annotations are provided, so the description carries the full burden. It describes the return format ('List of constraints with type, columns, and references'), which is helpful behavioral context. However, it doesn't mention permissions needed, whether it's read-only, potential rate limits, or error conditions.

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 efficiently structured with a clear purpose statement followed by Args and Returns sections. Every sentence adds value: the first explains what the tool does, the second documents parameters, and the third describes the return format.

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

Completeness4/5

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

For a 2-parameter tool with no annotations and no output schema, the description provides good coverage: clear purpose, parameter explanations, and return format. It could be more complete by mentioning permissions or error handling, but it's substantially adequate for the tool's complexity.

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

Parameters4/5

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

With 0% schema description coverage, the description compensates by explaining both parameters: 'table_name: Name of the table' and 'schema: Schema name (default: public)'. It adds meaning beyond the bare schema, though it doesn't elaborate on format requirements or constraints.

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

Purpose5/5

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

The description clearly states the specific action ('List all constraints') and resource ('for a table'), specifying the constraint types (PK, FK, UNIQUE, CHECK). It distinguishes from siblings like list_tables (lists tables) and list_indexes (lists indexes) by focusing specifically on constraints.

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

Usage Guidelines3/5

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

The description implies usage when needing constraint information for a specific table, but doesn't explicitly state when to use this tool versus alternatives like describe_table (which might include constraints) or other list_* tools. No guidance on prerequisites or exclusions is provided.

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

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