Skip to main content
Glama
TranChiHuu

MCP SQL Server

by TranChiHuu

describe_table

Retrieve schema information for a database table to understand its structure, including column names, data types, and constraints.

Instructions

Get schema information for a specific table

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tableNameYesName of the table to describe

Implementation Reference

  • The main handler function that implements the logic for the 'describe_table' tool. It queries the database information schema for PostgreSQL or uses DESCRIBE for MySQL to retrieve column information for the specified table.
    async describeTable(tableName) {
      if (!this.currentConfig) {
        throw new Error('Not connected to any database. Call connect_database first.');
      }
    
      if (this.currentConfig.type === 'postgresql') {
        if (!this.postgresPool) {
          throw new Error('PostgreSQL connection not initialized');
        }
    
        const query = `
          SELECT 
            column_name,
            data_type,
            is_nullable,
            column_default
          FROM information_schema.columns
          WHERE table_schema = 'public' AND table_name = $1
          ORDER BY ordinal_position;
        `;
        const result = await this.postgresPool.query(query, [tableName]);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  table: tableName,
                  columns: result.rows,
                },
                null,
                2
              ),
            },
          ],
        };
      } else if (this.currentConfig.type === 'mysql') {
        if (!this.mysqlConnection) {
          throw new Error('MySQL connection not initialized');
        }
    
        const [rows] = await this.mysqlConnection.query(
          `DESCRIBE ${tableName}`
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  table: tableName,
                  columns: rows,
                },
                null,
                2
              ),
            },
          ],
        };
      } else {
        throw new Error(`Unsupported database type: ${this.currentConfig.type}`);
      }
    }
  • The input schema definition for the 'describe_table' tool, specifying that it requires a 'tableName' string parameter.
    inputSchema: {
      type: 'object',
      properties: {
        tableName: {
          type: 'string',
          description: 'Name of the table to describe',
        },
      },
      required: ['tableName'],
    },
  • index.js:183-196 (registration)
    The registration of the 'describe_table' tool in the ListTools response, including name, description, and input schema.
    {
      name: 'describe_table',
      description: 'Get schema information for a specific table',
      inputSchema: {
        type: 'object',
        properties: {
          tableName: {
            type: 'string',
            description: 'Name of the table to describe',
          },
        },
        required: ['tableName'],
      },
    },
  • The dispatch case in the CallToolRequestSchema handler that invokes the describeTable method for the 'describe_table' tool.
    case 'describe_table':
      return await this.describeTable(args.tableName);

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/TranChiHuu/postgres-mysql-mcp-server'

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