get_table_info
Retrieve table schema, indexes, and sample data from SQLite databases to understand structure and content for analysis or troubleshooting.
Instructions
Get comprehensive information about a table including schema, indexes, and sample data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table_name | Yes | Name of the table to analyze | |
| sample_rows | No | Number of sample rows to return |
Implementation Reference
- src/index.ts:363-454 (handler)The core handler function for 'get_table_info' tool. It retrieves table schema (columns, types, constraints), row count, indexes, and optional sample rows from the connected SQLite database.private async getTableInfo(args: { table_name: string; sample_rows?: number }): Promise<CallToolResult> { if (!this.db) { throw new Error("No database connected. Use connect_database first."); } try { // Get table schema const columns = this.db .prepare("PRAGMA table_info(?)") .all(args.table_name) as { cid: number; name: string; type: string; notnull: number; dflt_value: any; pk: number; }[]; if (columns.length === 0) { throw new Error(`Table '${args.table_name}' not found`); } // Get row count const countResult = this.db .prepare(`SELECT COUNT(*) as count FROM ${args.table_name}`) .get() as { count: number }; // Get indexes const indexes = this.db .prepare("PRAGMA index_list(?)") .all(args.table_name) as { name: string; unique: number }[]; // Get sample data const sampleRows = args.sample_rows || 5; const sampleData = this.db .prepare(`SELECT * FROM ${args.table_name} LIMIT ?`) .all(sampleRows); // Format schema const schema = columns .map(col => { const nullable = col.notnull === 0 ? "NULL" : "NOT NULL"; const pk = col.pk > 0 ? " PRIMARY KEY" : ""; const defaultVal = col.dflt_value !== null ? ` DEFAULT ${col.dflt_value}` : ""; return ` ${col.name} ${col.type} ${nullable}${pk}${defaultVal}`; }) .join("\n"); // Format indexes const indexInfo = indexes.length > 0 ? indexes.map(idx => ` ${idx.name} (${idx.unique ? "UNIQUE" : "NON-UNIQUE"})`).join("\n") : " No indexes"; // Format sample data let sampleText = ""; if (sampleData.length > 0) { const headers = Object.keys(sampleData[0] as Record<string, unknown>); const rows = sampleData.map((row) => headers.map(header => String((row as Record<string, unknown>)[header] ?? "NULL")).join(" | ") ); const headerRow = headers.join(" | "); const separator = headers.map(h => "-".repeat(Math.max(h.length, 4))).join("-|-"); sampleText = [headerRow, separator, ...rows].join("\n"); } else { sampleText = "No data in table"; } const info = `Table: ${args.table_name} Row count: ${countResult.count} Schema: ${schema} Indexes: ${indexInfo} Sample data (${Math.min(sampleRows, sampleData.length)} rows): ${sampleText}`; return { content: [ { type: "text", text: info, } satisfies TextContent, ], }; } catch (error) { throw new Error(`Failed to get table info: ${error instanceof Error ? error.message : String(error)}`); } }
- src/index.ts:133-151 (registration)Registers the 'get_table_info' tool in the ListToolsRequestHandler, defining its name, description, and input schema.{ name: "get_table_info", description: "Get comprehensive information about a table including schema, indexes, and sample data", inputSchema: { type: "object", properties: { table_name: { type: "string", description: "Name of the table to analyze", }, sample_rows: { type: "number", description: "Number of sample rows to return", default: 5, }, }, required: ["table_name"], }, },
- src/index.ts:136-150 (schema)Defines the input schema for the 'get_table_info' tool, specifying required 'table_name' and optional 'sample_rows'.inputSchema: { type: "object", properties: { table_name: { type: "string", description: "Name of the table to analyze", }, sample_rows: { type: "number", description: "Number of sample rows to return", default: 5, }, }, required: ["table_name"], },
- src/index.ts:177-178 (registration)Dispatches calls to the 'get_table_info' handler in the CallToolRequestHandler switch statement.case "get_table_info": return await this.getTableInfo(args as { table_name: string; sample_rows?: number });