describe_table
Retrieve the structure of a PostgreSQL table by specifying its name and schema. This tool helps inspect column types, constraints, and relationships for data analysis and database management.
Instructions
Get table structure
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schema | No | Schema name (default: public) | |
| table | Yes | Table name |
Implementation Reference
- src/index.ts:447-510 (handler)The handler function that executes the describe_table tool: ensures DB connection, validates input, queries information_schema for column details (name, type, nullable, default, PK, max length), and returns JSON of results.private async handleDescribeTable(args: any) { await this.ensureConnection(); if (!args.table) { throw new McpError(ErrorCode.InvalidParams, 'Table name is required'); } const schema = args.schema || 'public'; try { const result = await this.client!.query(` SELECT c.column_name, c.data_type, c.is_nullable, c.column_default, CASE WHEN pk.constraint_type = 'PRIMARY KEY' THEN true ELSE false END AS is_primary_key, c.character_maximum_length FROM information_schema.columns c LEFT JOIN ( SELECT tc.constraint_type, kcu.column_name, kcu.table_name, kcu.table_schema FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name WHERE tc.constraint_type = 'PRIMARY KEY' ) pk ON c.column_name = pk.column_name AND c.table_name = pk.table_name AND c.table_schema = pk.table_schema WHERE c.table_schema = $1 AND c.table_name = $2 ORDER BY c.ordinal_position `, [schema, args.table]); return { content: [ { type: 'text', text: JSON.stringify(result.rows, null, 2), }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to describe table: ${getErrorMessage(error)}` ); } }
- src/index.ts:234-251 (registration)Tool registration in the list_tools handler: defines name 'describe_table', description, and input schema requiring 'table' (optional 'schema').{ name: 'describe_table', description: 'Get table structure', inputSchema: { type: 'object', properties: { table: { type: 'string', description: 'Table name', }, schema: { type: 'string', description: 'Schema name (default: public)', }, }, required: ['table'], }, },
- src/index.ts:237-250 (schema)Input schema for describe_table tool: object with required 'table' string and optional 'schema' string.inputSchema: { type: 'object', properties: { table: { type: 'string', description: 'Table name', }, schema: { type: 'string', description: 'Schema name (default: public)', }, }, required: ['table'], },
- src/index.ts:267-268 (helper)Dispatcher case in call_tool handler that routes 'describe_table' calls to the handleDescribeTable method.case 'describe_table': return await this.handleDescribeTable(request.params.arguments);