get_tables
Retrieve a list of tables from SQL databases (Postgres, MySQL, SQLite) using a connection string to manage and analyze database structures efficiently.
Instructions
Get list of table in database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| connection_string | Yes | ||
| db_type | Yes |
Implementation Reference
- src/tools.ts:16-49 (handler)The handler function that implements the core logic of the 'get_tables' tool. It uses Sequelize to connect to the database, executes dialect-specific queries to fetch table names, processes the results (e.g., filtering system schemas in Postgres), and returns the list as a JSON-formatted text response in MCP protocol format.async ({db_type, connection_string}) => { const sequelize = new Sequelize(connection_string, { dialect: db_type, }) let result: any[] = [] let tables: string[] = [] switch (db_type) { case 'postgres': [result] = await sequelize.query('SELECT * FROM information_schema.tables') tables = result .filter((table: any) => table.table_schema !== 'information_schema' && table.table_schema !== 'pg_catalog') .map((table: any) => `${table.table_schema}.${table.table_name}`) break case 'mysql': [result] = await sequelize.query('SELECT * FROM information_schema.tables') tables = result.map((table: any) => table.table_name) break case 'sqlite': [result] = await sequelize.query('SELECT * FROM sqlite_master WHERE type = "table"') tables = result.map((table: any) => table.name) break } return { content: [ { type: 'text', text: JSON.stringify(tables), } ], } },
- src/tools.ts:8-15 (schema)The input schema for the 'get_tables' tool, defined with Zod validators for 'db_type' (Postgres, MySQL, SQLite) and 'connection_string', along with tool metadata like title and description.{ title: 'Get Tables', description: 'Get list of table in database', inputSchema: { db_type: z.enum(['postgres', 'mysql', 'sqlite']), connection_string: z.string(), }, },
- src/tools.ts:6-50 (registration)The registration of the 'get_tables' tool using McpServer.registerTool, including the tool name, schema, and inline handler function.server.registerTool( 'get_tables', { title: 'Get Tables', description: 'Get list of table in database', inputSchema: { db_type: z.enum(['postgres', 'mysql', 'sqlite']), connection_string: z.string(), }, }, async ({db_type, connection_string}) => { const sequelize = new Sequelize(connection_string, { dialect: db_type, }) let result: any[] = [] let tables: string[] = [] switch (db_type) { case 'postgres': [result] = await sequelize.query('SELECT * FROM information_schema.tables') tables = result .filter((table: any) => table.table_schema !== 'information_schema' && table.table_schema !== 'pg_catalog') .map((table: any) => `${table.table_schema}.${table.table_name}`) break case 'mysql': [result] = await sequelize.query('SELECT * FROM information_schema.tables') tables = result.map((table: any) => table.table_name) break case 'sqlite': [result] = await sequelize.query('SELECT * FROM sqlite_master WHERE type = "table"') tables = result.map((table: any) => table.name) break } return { content: [ { type: 'text', text: JSON.stringify(tables), } ], } }, )