!export-db
Export data from a specified database table in PostgreSQL, MySQL, or Firestore using the db-mcp-tool, ensuring structured and precise extraction for further use.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table | Yes |
Implementation Reference
- src/index.ts:260-285 (handler)The main handler function for the '!export-db' tool. It checks if a database connection exists via dbService, then calls exportTableSchema on the table provided in args, returning the generated schema SQL or an error.async (args: { table: string }) => { if (!dbService) { return { content: [{ type: "text", text: "You must connect to a database first!" }], isError: true, }; } try { const schema = await dbService.exportTableSchema(args.table); return { content: [ { type: "text", text: schema, }, ], }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; return { content: [{ type: "text", text: `Failed to export table schema: ${errorMessage}` }], isError: true, }; } }
- src/index.ts:257-259 (schema)Input schema for '!export-db' tool: requires a 'table' string parameter for specifying which table's schema to export.{ table: z.string(), },
- src/index.ts:255-286 (registration)Registration of the '!export-db' tool on the MCP server using server.tool(), including name, schema, and handler function.server.tool( "!export-db", { table: z.string(), }, async (args: { table: string }) => { if (!dbService) { return { content: [{ type: "text", text: "You must connect to a database first!" }], isError: true, }; } try { const schema = await dbService.exportTableSchema(args.table); return { content: [ { type: "text", text: schema, }, ], }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; return { content: [{ type: "text", text: `Failed to export table schema: ${errorMessage}` }], isError: true, }; } } );
- src/services/database.ts:214-261 (helper)Core helper method 'exportTableSchema' in DatabaseService that implements the logic to generate CREATE TABLE SQL schema for a given table in PostgreSQL (via dynamic query on information_schema) or MySQL (via SHOW CREATE TABLE), throwing errors for unsupported cases like Firestore.async exportTableSchema(tableName: string): Promise<string> { switch (this.config.type) { case 'postgres': { if (!this.postgresClient) { throw new Error('PostgreSQL connection not found'); } const query = ` SELECT 'CREATE TABLE ' || quote_ident($1) || ' (' || string_agg( quote_ident(column_name) || ' ' || data_type || CASE WHEN character_maximum_length IS NOT NULL THEN '(' || character_maximum_length || ')' ELSE '' END || CASE WHEN is_nullable = 'NO' THEN ' NOT NULL' ELSE '' END, ', ' ) || ');' as create_table_sql FROM information_schema.columns WHERE table_name = $1 AND table_schema = 'public' GROUP BY table_name; `; const result = await this.postgresClient.query(query, [tableName]); return result.rows[0]?.create_table_sql || ''; } case 'mysql': { if (!this.mysqlConnection) { throw new Error('MySQL connection not found'); } const [result] = await this.mysqlConnection.query( 'SHOW CREATE TABLE ??', [tableName] ); return result[0]?.['Create Table'] || ''; } case 'firestore': { throw new Error('SQL schema export is not supported for Firestore'); } default: throw new Error('Unsupported database type'); } }