list_tables
Retrieve all tables in a MySQL database to understand its structure and available data. Specify a database name or use the current connection to view tables.
Instructions
List all tables in the current or specified database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | No | Database name (optional, uses current if not specified) |
Implementation Reference
- src/index.ts:239-261 (handler)The handler function for the 'list_tables' tool. It connects to the MySQL pool, executes 'SHOW TABLES' (optionally from a specific database), extracts table names from the result rows, and returns a structured response with the list of tables.async ({ database }) => { const p = await getPool(); let sql = "SHOW TABLES"; if (database) { sql = `SHOW TABLES FROM \`${database}\``; } const [rows] = await p.query<RowDataPacket[]>(sql); const tables = rows.map((row) => Object.values(row)[0] as string); const output = { tables, database: database || null }; return { content: [ { type: "text" as const, text: JSON.stringify(tables, null, 2), }, ], structuredContent: output, }; }
- src/index.ts:236-238 (schema)Input schema for the 'list_tables' tool using Zod, defining an optional 'database' parameter.{ database: z.string().optional().describe("Database name (optional, uses current if not specified)"), },
- src/index.ts:233-262 (registration)Registration of the 'list_tables' tool on the MCP server, specifying name, description, input schema, and handler function.server.tool( "list_tables", "List all tables in the current or specified database", { database: z.string().optional().describe("Database name (optional, uses current if not specified)"), }, async ({ database }) => { const p = await getPool(); let sql = "SHOW TABLES"; if (database) { sql = `SHOW TABLES FROM \`${database}\``; } const [rows] = await p.query<RowDataPacket[]>(sql); const tables = rows.map((row) => Object.values(row)[0] as string); const output = { tables, database: database || null }; return { content: [ { type: "text" as const, text: JSON.stringify(tables, null, 2), }, ], structuredContent: output, }; } );