get_table_row_count
Get the approximate row count for a specified table to quickly assess data volume and table size.
Instructions
Get the approximate row count for a table.
Args: table_name (str): Table name to inspect
Returns: str: Row count information
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table_name | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- tools/sql_tools.py:163-180 (handler)The `get_table_row_count` method on the `SQLTools` class — executes `SELECT COUNT(*) FROM` for a given table and returns a formatted string with the row count.
def get_table_row_count(self, table_name: str) -> str: """Get the approximate row count for a table. Args: table_name (str): Table name to inspect Returns: str: Row count information """ with self.get_connection() as conn: cursor = conn.cursor() cursor.execute(f"SELECT COUNT(*) FROM `{table_name}`") row_count = cursor.fetchone() if row_count is None: return f"Unable to retrieve row count for '{table_name}'" return f"{table_name} row count: {row_count[0]}" - server.py:19-19 (registration)Registration of `get_table_row_count` as an MCP tool via `mcp.tool()` decorator.
mcp.tool()(sql_tools.get_table_row_count) - tools/sql_tools.py:163-163 (schema)The method signature `def get_table_row_count(self, table_name: str) -> str:` defines the input (table_name: str) and output (str) types.
def get_table_row_count(self, table_name: str) -> str: - tools/sql_tools.py:19-43 (helper)The `get_connection` context manager helper used by `get_table_row_count` to obtain a database connection.
@contextmanager def get_connection(self): """Context manager for database connections. Yields: mysql.connector.connection: Database connection object Raises: Error: If connection to the database fails """ connection = None try: connection = mysql.connector.connect( host=self.host, user=self.user, password=self.password, database=self.database ) yield connection except Error as e: print(f"Error connecting to MySQL database: {e}") raise finally: if connection and connection.is_connected(): connection.close()