Skip to main content
Glama
wirdes

db-mcp-tool

!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
NameRequiredDescriptionDefault
tableYes

Implementation Reference

  • 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,
        };
      }
    }
  • 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,
          };
        }
      }
    );
  • 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');
        }
    }
Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/wirdes/db-mcp-tool'

If you have feedback or need assistance with the MCP directory API, please join our Discord server