list_tables
Retrieve all table names from a MySQL database to understand its structure and available data sources. Specify a database name or use the default connection.
Instructions
List all tables in a specified database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | No | Database name (optional, uses default if not specified) |
Implementation Reference
- src/index.ts:151-169 (handler)The main handler logic for the 'list_tables' tool. Extracts the optional database argument, calls executeQuery helper with 'SHOW FULL TABLES', and returns results as formatted JSON.case "list_tables": { console.error('[Tool] Executing list_tables'); const database = request.params.arguments?.database as string | undefined; const { rows } = await executeQuery( pool, 'SHOW FULL TABLES', [], database ); return { content: [{ type: "text", text: JSON.stringify(rows, null, 2) }] }; }
- src/index.ts:75-88 (registration)Registration of the list_tables tool in the ListTools response, defining its name, description, and input schema.{ name: "list_tables", description: "List all tables in a specified database", inputSchema: { type: "object", properties: { database: { type: "string", description: "Database name (optional, uses default if not specified)" } }, required: [] } },
- src/index.ts:78-86 (schema)Input schema definition for list_tables tool, specifying optional 'database' parameter.inputSchema: { type: "object", properties: { database: { type: "string", description: "Database name (optional, uses default if not specified)" } }, required: []
- src/connection.ts:63-108 (helper)Shared helper function executeQuery used by list_tables to safely execute the SHOW FULL TABLES SQL query with connection management, database switching, timeout protection, and result limiting.export async function executeQuery( pool: mysql.Pool, sql: string, params: any[] = [], database?: string ): Promise<{ rows: any; fields: mysql.FieldPacket[] }> { console.error(`[Query] Executing: ${sql}`); let connection: mysql.PoolConnection | null = null; try { // Get connection from pool connection = await pool.getConnection(); // Use specific database if provided if (database) { console.error(`[Query] Using database: ${database}`); await connection.query(`USE \`${database}\``); } // Execute query with timeout const [rows, fields] = await Promise.race([ connection.query(sql, params), new Promise<never>((_, reject) => { setTimeout(() => reject(new Error('Query timeout')), DEFAULT_TIMEOUT); }), ]); // Apply row limit if result is an array const limitedRows = Array.isArray(rows) && rows.length > DEFAULT_ROW_LIMIT ? rows.slice(0, DEFAULT_ROW_LIMIT) : rows; // Log result summary console.error(`[Query] Success: ${Array.isArray(rows) ? rows.length : 1} rows returned`); return { rows: limitedRows, fields }; } catch (error) { console.error('[Error] Query execution failed:', error); throw error; } finally { // Release connection back to pool if (connection) { connection.release(); } }