get-table-schema
Retrieve detailed database table schema including RLS policies, indexes, and constraints to understand table structure and relationships for database analysis and management.
Instructions
Returns the detailed schema(including RLS, indexes, constraints, etc.) of a specific table
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| apiKey | No | API key for authentication (optional if provided via --api_key) | |
| tableName | Yes | Name of the table |
Implementation Reference
- src/shared/tools.ts:374-406 (handler)Handler function that fetches the detailed schema of a specific table from the backend API endpoint /api/metadata/{tableName}, handles the response, adds background context, and formats the output.withUsageTracking('get-table-schema', async ({ apiKey, tableName }) => { try { const actualApiKey = getApiKey(apiKey); const response = await fetch(`${API_BASE_URL}/api/metadata/${tableName}`, { method: 'GET', headers: { 'x-api-key': actualApiKey, }, }); const result = await handleApiResponse(response); return await addBackgroundContext({ content: [ { type: 'text', text: formatSuccessMessage('Schema retrieved', result), }, ], }); } catch (error) { const errMsg = error instanceof Error ? error.message : 'Unknown error occurred'; return { content: [ { type: 'text', text: `Error getting table schema: ${errMsg}`, }, ], isError: true, }; } })
- src/shared/tools.ts:367-373 (schema)Zod input schema defining parameters: apiKey (optional string) and tableName (required string).{ apiKey: z .string() .optional() .describe('API key for authentication (optional if provided via --api_key)'), tableName: z.string().describe('Name of the table'), },
- src/shared/tools.ts:364-407 (registration)Complete MCP server.tool registration including name, description, input schema, and wrapped handler for the get-table-schema tool.server.tool( 'get-table-schema', 'Returns the detailed schema(including RLS, indexes, constraints, etc.) of a specific table', { apiKey: z .string() .optional() .describe('API key for authentication (optional if provided via --api_key)'), tableName: z.string().describe('Name of the table'), }, withUsageTracking('get-table-schema', async ({ apiKey, tableName }) => { try { const actualApiKey = getApiKey(apiKey); const response = await fetch(`${API_BASE_URL}/api/metadata/${tableName}`, { method: 'GET', headers: { 'x-api-key': actualApiKey, }, }); const result = await handleApiResponse(response); return await addBackgroundContext({ content: [ { type: 'text', text: formatSuccessMessage('Schema retrieved', result), }, ], }); } catch (error) { const errMsg = error instanceof Error ? error.message : 'Unknown error occurred'; return { content: [ { type: 'text', text: `Error getting table schema: ${errMsg}`, }, ], isError: true, }; } }) );
- src/shared/tools.ts:192-197 (helper)Helper function getApiKey that returns the global API key, throwing error if not set.const getApiKey = (_toolApiKey?: string): string => { if (!GLOBAL_API_KEY) { throw new Error('API key is required. Pass --api_key when starting the MCP server.'); } return GLOBAL_API_KEY; };
- src/shared/tools.ts:174-188 (helper)Helper wrapper withUsageTracking that adds usage tracking around tool handlers, logging success or failure.function withUsageTracking<T extends unknown[], R>( toolName: string, handler: (...args: T) => Promise<R> ): (...args: T) => Promise<R> { return async (...args: T): Promise<R> => { try { const result = await handler(...args); await trackToolUsage(toolName, true); return result; } catch (error) { await trackToolUsage(toolName, false); throw error; } }; }