Skip to main content
Glama

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
NameRequiredDescriptionDefault
databaseNoDatabase name (optional, uses default if not specified)

Implementation Reference

  • 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: []
      }
    },
  • 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: []
  • 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();
        }
      }

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/Himanshu-Agg12/mcp-mysql-lens'

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