Skip to main content
Glama
sbfulfil

PostgreSQL MCP Server

by sbfulfil

get_table_relationships

Identify foreign key relationships for PostgreSQL tables to understand database structure and table connections.

Instructions

Get foreign key relationships for a table

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
table_nameYesName of the table
schemaNoSchema name (default: public)public

Implementation Reference

  • The main handler function that executes the tool logic: connects to Postgres, queries information_schema for foreign key constraints referencing the given table, formats and returns the relationships.
    async getTableRelationships(tableName, schema = 'public') {
      const client = await this.connectToDatabase();
      
      try {
        const query = `
          SELECT 
            kcu.column_name,
            ccu.table_schema AS foreign_table_schema,
            ccu.table_name AS foreign_table_name,
            ccu.column_name AS foreign_column_name,
            rc.constraint_name,
            rc.update_rule,
            rc.delete_rule
          FROM information_schema.table_constraints AS tc 
          JOIN information_schema.key_column_usage AS kcu
            ON tc.constraint_name = kcu.constraint_name
            AND tc.table_schema = kcu.table_schema
          JOIN information_schema.constraint_column_usage AS ccu
            ON ccu.constraint_name = tc.constraint_name
            AND ccu.table_schema = tc.table_schema
          JOIN information_schema.referential_constraints AS rc
            ON tc.constraint_name = rc.constraint_name
          WHERE tc.constraint_type = 'FOREIGN KEY' 
            AND tc.table_schema = $1 
            AND tc.table_name = $2;
        `;
        
        const result = await client.query(query, [schema, tableName]);
        
        return {
          content: [
            {
              type: 'text',
              text: result.rows.length > 0 ? 
                `Foreign key relationships for "${schema}.${tableName}":\n\n` + 
                result.rows.map(row => 
                  `${row.column_name} → ${row.foreign_table_schema}.${row.foreign_table_name}.${row.foreign_column_name}\n` +
                  `  Constraint: ${row.constraint_name}\n` +
                  `  On Update: ${row.update_rule}, On Delete: ${row.delete_rule}`
                ).join('\n\n') :
                `No foreign key relationships found for table "${schema}.${tableName}"`
            },
          ],
        };
      } finally {
        await client.end();
      }
    }
  • The tool's metadata and input schema definition returned by ListToolsRequestHandler, specifying name, description, inputSchema with table_name (required) and schema (optional).
    {
      name: 'get_table_relationships',
      description: 'Get foreign key relationships for a table',
      inputSchema: {
        type: 'object',
        properties: {
          table_name: {
            type: 'string',
            description: 'Name of the table',
          },
          schema: {
            type: 'string',
            description: 'Schema name (default: public)',
            default: 'public'
          }
        },
        required: ['table_name'],
      },
    },
  • src/index.js:151-152 (registration)
    Tool registration in the switch statement of CallToolRequestSchema handler, dispatching calls to the getTableRelationships method.
    case 'get_table_relationships':
      return await this.getTableRelationships(args.table_name, args?.schema || 'public');
Behavior2/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 of behavioral disclosure. It states the action ('Get') but does not clarify if this is a read-only operation, what permissions might be required, how results are formatted, or any limitations like rate limits. This leaves significant gaps in understanding the tool's behavior.

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, direct sentence with no wasted words, making it highly concise and front-loaded. It efficiently communicates the core purpose without unnecessary elaboration, earning a top score for brevity and clarity.

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

Completeness2/5

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

Given the lack of annotations and output schema, the description is incomplete. It does not address behavioral aspects like safety, permissions, or result format, which are crucial for a tool that retrieves database metadata. This leaves the agent with insufficient context to use the tool effectively.

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 schema description coverage is 100%, with clear descriptions for both parameters in the input schema. The description does not add any additional meaning beyond what the schema provides, such as explaining the significance of foreign key relationships or usage examples. Baseline 3 is appropriate as the schema handles the parameter documentation adequately.

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') and resource ('foreign key relationships for a table'), making the purpose unambiguous. However, it does not explicitly differentiate from sibling tools like 'describe_table' or 'get_indexes', which might also provide metadata about tables, so it falls short of a perfect score.

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. With sibling tools like 'describe_table' and 'get_indexes' available, there is no indication of whether this tool is complementary, overlapping, or exclusive, leaving the agent to infer usage context.

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

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