list_tables
Retrieve all table names from a MySQL database to understand its structure. Specify a database or use the current connection to view available 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)Handler function that gets a connection pool, executes a SHOW TABLES query (optionally scoped to a specific database), extracts table names from results, and returns both text and structured content 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 using Zod defining an optional 'database' string parameter for specifying the database to list tables from.{ database: z.string().optional().describe("Database name (optional, uses current if not specified)"), },
- src/index.ts:233-262 (registration)Complete registration of the 'list_tables' tool via McpServer.tool(), including 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, }; } );