Skip to main content
Glama
sajithrw

MCP MySQL Server

by sajithrw

mysql_get_table_stats

Retrieve table statistics like row count and size from MySQL databases to monitor performance and optimize queries.

Instructions

Get statistics about a table (row count, size, etc.)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tableYesTable name to get statistics for
databaseNoDatabase name (uses current database if not specified)

Implementation Reference

  • Executes the mysql_get_table_stats tool by querying the INFORMATION_SCHEMA.TABLES for statistics like row count, data length, index length, etc., for the specified table.
    private async handleGetTableStats(args: any) {
      if (!this.pool) {
        throw new Error("Not connected to MySQL. Use mysql_connect first.");
      }
    
      const { table, database } = args;
      
      if (!table) {
        throw new Error("Table name is required");
      }
    
      try {
        // Get table information from information_schema
        const dbCondition = database ? `AND TABLE_SCHEMA = '${database}'` : "";
        const query = `
          SELECT 
            TABLE_NAME,
            ENGINE,
            TABLE_ROWS,
            DATA_LENGTH,
            INDEX_LENGTH,
            DATA_FREE,
            AUTO_INCREMENT,
            CREATE_TIME,
            UPDATE_TIME,
            CHECK_TIME
          FROM INFORMATION_SCHEMA.TABLES 
          WHERE TABLE_NAME = '${table}' ${dbCondition}
        `;
    
        const [results] = await this.pool.execute(query);
        return {
          content: [
            {
              type: "text",
              text: `Statistics for table '${table}':\n${JSON.stringify(results, null, 2)}`,
            },
          ],
        };
      } catch (error) {
        throw new Error(`Failed to get table statistics: ${error instanceof Error ? error.message : String(error)}`);
      }
    }
  • Input schema definition for the mysql_get_table_stats tool, specifying parameters table (required) and optional database.
    inputSchema: {
      type: "object",
      properties: {
        table: {
          type: "string",
          description: "Table name to get statistics for",
        },
        database: {
          type: "string",
          description: "Database name (uses current database if not specified)",
        },
      },
      required: ["table"],
    },
  • src/index.ts:214-231 (registration)
    Tool registration in the listTools response, defining name, description, and inputSchema for mysql_get_table_stats.
    {
      name: "mysql_get_table_stats",
      description: "Get statistics about a table (row count, size, etc.)",
      inputSchema: {
        type: "object",
        properties: {
          table: {
            type: "string",
            description: "Table name to get statistics for",
          },
          database: {
            type: "string",
            description: "Database name (uses current database if not specified)",
          },
        },
        required: ["table"],
      },
    },
  • src/index.ts:261-262 (registration)
    Dispatch case in the CallToolRequest handler that routes to the mysql_get_table_stats handler function.
    case "mysql_get_table_stats":
      return await this.handleGetTableStats(args);

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/sajithrw/mcp-mysql'

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