list_tables
Retrieve all tables from a SQL Server database to understand database structure and available data sources. Optionally filter by schema name to focus on specific table groups.
Instructions
Lists all the tables in the database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schemaName | No | Schema name to filter tables (default: dbo) | dbo |
Implementation Reference
- src/tools/list-tables.ts:5-58 (handler)Core handler function that executes the SQL query to list tables from INFORMATION_SCHEMA.TABLES, optionally filtered by schemaName, and returns formatted JSON result.export async function listTables( db: DatabaseConnection, schemaName?: string ): Promise<CallToolResult> { try { const pool = db.getPool() let query = ` SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' `.trim() const request = pool.request() if (schemaName) { query += ' AND TABLE_SCHEMA = @schemaName' request.input('schemaName', schemaName) } query += ' ORDER BY TABLE_SCHEMA, TABLE_NAME' const result = await request.query(query) return { content: [ { type: 'text', text: JSON.stringify( { tables: result.recordset, count: result.recordset.length, }, null, 2 ), }, ], } } catch (error) { return { content: [ { type: 'text', text: `Erro: ${error instanceof Error ? error.message : 'Erro desconhecido'}`, }, ], isError: true, } } }
- src/schemas.ts:20-26 (schema)Zod input schema for the list_tables tool defining optional schemaName parameter.export const listTablesInput = z.object({ schemaName: z .string() .optional() .describe('Schema name to filter tables (default: dbo)') .default('dbo'), })
- src/services/SqlServerMCPService.ts:98-101 (registration)Registration of the list_tables handler in the MCP service's toolHandlers map, which wraps and calls the core listTables function.handlers.set('list_tables', async (database, args) => { const { schemaName } = args as ListTablesInput return await listTables(database, schemaName) })
- src/tools/index.ts:37-40 (registration)Tool registration metadata in toolsList() array, including name, description, and input schema for list tools request.{ name: 'list_tables', description: 'Lists all the tables in the database', inputSchema: zodToJsonSchema(listTablesInput),
- src/schemas.ts:67-67 (registration)Inclusion of listTablesInput schema in the toolsSchemas object for tool name mapping.list_tables: listTablesInput,