Skip to main content
Glama
srthkdev

DBeaver MCP Server

by srthkdev

get_table_schema

Retrieve schema details for a database table, including columns, data types, and optional index information, to understand its structure and relationships.

Instructions

Get schema information for a specific table

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionIdYesThe ID or name of the DBeaver connection
includeIndexesNoInclude index information
tableNameYesThe name of the table to describe

Implementation Reference

  • Primary execution handler for the get_table_schema tool. Resolves connection, fetches schema via DBeaverClient, optionally removes indexes, returns JSON response.
    private async handleGetTableSchema(args: { 
      connectionId: string; 
      tableName: string; 
      includeIndexes?: boolean 
    }) {
      const connectionId = sanitizeConnectionId(args.connectionId);
      const connection = await this.configParser.getConnection(connectionId);
      
      if (!connection) {
        throw new McpError(ErrorCode.InvalidParams, `Connection not found: ${connectionId}`);
      }
      
      const schema = await this.dbeaverClient.getTableSchema(connection, args.tableName);
      
      if (!args.includeIndexes) {
        (schema as any).indexes = undefined;
      }
      
      return {
        content: [{
          type: 'text' as const,
          text: JSON.stringify(schema, null, 2),
        }],
      };
    }
  • Input schema for validating tool arguments: requires connectionId and tableName, optional includeIndexes.
    inputSchema: {
      type: 'object',
      properties: {
        connectionId: {
          type: 'string',
          description: 'The ID or name of the DBeaver connection',
        },
        tableName: {
          type: 'string',
          description: 'The name of the table to describe',
        },
        includeIndexes: {
          type: 'boolean',
          description: 'Include index information',
          default: true
        }
      },
      required: ['connectionId', 'tableName'],
    },
  • src/index.ts:320-342 (registration)
    Registration of the get_table_schema tool in the tools list, defining name, description, and input schema.
    {
      name: 'get_table_schema',
      description: 'Get schema information for a specific table',
      inputSchema: {
        type: 'object',
        properties: {
          connectionId: {
            type: 'string',
            description: 'The ID or name of the DBeaver connection',
          },
          tableName: {
            type: 'string',
            description: 'The name of the table to describe',
          },
          includeIndexes: {
            type: 'boolean',
            description: 'Include index information',
            default: true
          }
        },
        required: ['connectionId', 'tableName'],
      },
    },
  • Helper method in DBeaverClient that builds driver-specific schema query, executes it, and parses into SchemaInfo.
    async getTableSchema(connection: DBeaverConnection, tableName: string): Promise<SchemaInfo> {
      const schemaQuery = this.buildSchemaQuery(connection.driver, tableName);
      const result = await this.executeQuery(connection, schemaQuery);
      
      return this.parseSchemaResult(result, tableName);
    }
  • Parses raw query results from schema query into structured column information for SchemaInfo.
    private parseSchemaResult(result: any, tableName: string): SchemaInfo {
      const columns: any[] = [];
      
      if (result.rows && result.columns) {
        // Parse each row as a column definition
        result.rows.forEach((row: any[]) => {
          const columnInfo: any = {
            name: '',
            type: 'string',
            nullable: true,
            isPrimaryKey: false,
            isAutoIncrement: false
          };
          
          // Map columns based on the query result structure
          result.columns.forEach((colName: string, idx: number) => {
            const value = row[idx];
            
            switch (colName.toLowerCase()) {
              case 'column_name':
              case 'name':
                columnInfo.name = value || '';
                break;
              case 'data_type':
              case 'type':
                columnInfo.type = value || 'string';
                break;
              case 'is_nullable':
              case 'nullable':
                columnInfo.nullable = value === 'YES' || value === 'Y' || value === true;
                break;
              case 'column_default':
              case 'default':
                columnInfo.defaultValue = value;
                break;
              case 'column_key':
              case 'key':
                columnInfo.isPrimaryKey = value === 'PRI' || value === 'PRIMARY';
                break;
              case 'extra':
                columnInfo.isAutoIncrement = value && value.toLowerCase().includes('auto_increment');
                break;
              case 'character_maximum_length':
              case 'length':
                columnInfo.length = parseInt(value) || undefined;
                break;
              case 'numeric_precision':
              case 'precision':
                columnInfo.precision = parseInt(value) || undefined;
                break;
              case 'numeric_scale':
              case 'scale':
                columnInfo.scale = parseInt(value) || undefined;
                break;
            }
          });
          
          if (columnInfo.name) {
            columns.push(columnInfo);
          }
        });
      }
      
      return {
        tableName,
        columns,
        indexes: [],
        constraints: []
      };
    }
  • Utility function generating driver-specific SQL queries to fetch table schema information.
    export function buildSchemaQuery(driver: string, tableName: string): string {
      const driverLower = driver.toLowerCase();
      
      if (driverLower.includes('postgresql') || driverLower.includes('postgres')) {
        return `
          SELECT 
            column_name,
            data_type,
            is_nullable,
            column_default,
            character_maximum_length,
            numeric_precision,
            numeric_scale
          FROM information_schema.columns 
          WHERE table_name = '${tableName}'
          ORDER BY ordinal_position;
        `;
      } else if (driverLower.includes('mysql')) {
        return `
          SELECT 
            COLUMN_NAME as column_name,
            DATA_TYPE as data_type,
            IS_NULLABLE as is_nullable,
            COLUMN_DEFAULT as column_default,
            CHARACTER_MAXIMUM_LENGTH as character_maximum_length,
            NUMERIC_PRECISION as numeric_precision,
            NUMERIC_SCALE as numeric_scale,
            COLUMN_KEY as column_key,
            EXTRA as extra
          FROM information_schema.COLUMNS 
          WHERE TABLE_NAME = '${tableName}'
          ORDER BY ORDINAL_POSITION;
        `;
      } else if (driverLower.includes('sqlite')) {
        return `PRAGMA table_info(${tableName});`;
      } else if (driverLower.includes('oracle')) {
        return `
          SELECT 
            column_name,
            data_type,
            nullable,
            data_default,
            data_length,
            data_precision,
            data_scale
          FROM user_tab_columns 
          WHERE table_name = UPPER('${tableName}')
          ORDER BY column_id;
        `;
      } else if (driverLower.includes('mssql') || driverLower.includes('sqlserver')) {
        return `
          SELECT 
            COLUMN_NAME as column_name,
            DATA_TYPE as data_type,
            IS_NULLABLE as is_nullable,
            COLUMN_DEFAULT as column_default,
            CHARACTER_MAXIMUM_LENGTH as character_maximum_length,
            NUMERIC_PRECISION as numeric_precision,
            NUMERIC_SCALE as numeric_scale
          FROM INFORMATION_SCHEMA.COLUMNS 
          WHERE TABLE_NAME = '${tableName}'
          ORDER BY ORDINAL_POSITION;
        `;
      } else {
        // Generic fallback
        return `
          SELECT 
            column_name,
            data_type,
            is_nullable,
            column_default
          FROM information_schema.columns 
          WHERE table_name = '${tableName}';
        `;
      }
    }
  • TypeScript interface defining the structure of table schema output.
    export interface SchemaInfo {
      tableName: string;
      columns: ColumnInfo[];
      indexes: IndexInfo[];
      constraints: ConstraintInfo[];
    }

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/srthkdev/dbeaver-mcp-server'

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