list_databases
List all accessible databases on a MySQL server to identify available databases for querying.
Instructions
List all accessible databases on the MySQL server
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/index.ts:135-149 (handler)The handler for the list_databases tool. Executes 'SHOW DATABASES' query via the connection pool and returns the list of databases as formatted JSON.
case "list_databases": { console.error('[Tool] Executing list_databases'); const { rows } = await executeQuery( pool, 'SHOW DATABASES' ); return { content: [{ type: "text", text: JSON.stringify(rows, null, 2) }] }; } - src/index.ts:66-74 (registration)Registers the list_databases tool with its name, description, and empty input schema (no parameters required).
{ name: "list_databases", description: "List all accessible databases on the MySQL server", inputSchema: { type: "object", properties: {}, required: [] } }, - src/connection.ts:63-109 (helper)The executeQuery helper function called by the list_databases handler to run 'SHOW DATABASES' against MySQL.
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(); } } }