list_tables
Retrieve a list of tables within a PostgreSQL database using schema name. Ideal for inspecting and managing database structures efficiently.
Instructions
List tables in the database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schema | No | Schema name (default: public) |
Implementation Reference
- src/index.ts:418-445 (handler)The main handler function for the 'list_tables' tool. It connects to the database if needed, queries the information_schema.tables for the specified schema (default 'public'), and returns the list of table names as JSON.private async handleListTables(args: any = {}) { await this.ensureConnection(); const schema = args.schema || 'public'; try { const result = await this.client!.query(` SELECT table_name FROM information_schema.tables WHERE table_schema = $1 ORDER BY table_name `, [schema]); return { content: [ { type: 'text', text: JSON.stringify(result.rows, null, 2), }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to list tables: ${getErrorMessage(error)}` ); } }
- src/index.ts:223-232 (schema)Input schema definition for the 'list_tables' tool, allowing an optional 'schema' parameter.inputSchema: { type: 'object', properties: { schema: { type: 'string', description: 'Schema name (default: public)', }, }, required: [], },
- src/index.ts:220-233 (registration)Registration of the 'list_tables' tool in the ListToolsRequestSchema handler's tools array.{ name: 'list_tables', description: 'List tables in the database', inputSchema: { type: 'object', properties: { schema: { type: 'string', description: 'Schema name (default: public)', }, }, required: [], }, },
- src/index.ts:265-266 (registration)Routing case in the CallToolRequestSchema handler that invokes the list_tables handler.case 'list_tables': return await this.handleListTables(request.params.arguments);