list_columns
Retrieve column names and data types for a specified database table. Input a table name to get its columns and types.
Instructions
List columns for a given table.
Args: table_name (str): Table name to inspect
Returns: str: Column names and types for the table
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table_name | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- tools/sql_tools.py:96-118 (handler)The actual implementation of the list_columns method in the SQLTools class. It takes a table_name parameter, connects to MySQL, runs 'SHOW COLUMNS FROM `table_name`', and returns a formatted string listing each column's Field, Type, Null, Key, Default, and Extra attributes.
def list_columns(self, table_name: str) -> str: """List columns for a given table. Args: table_name (str): Table name to inspect Returns: str: Column names and types for the table """ with self.get_connection() as conn: cursor = conn.cursor(dictionary=True, buffered=True) cursor.execute(f"SHOW COLUMNS FROM `{table_name}`") columns = cursor.fetchall() if not columns: return f"No columns found for table '{table_name}'" output = [f"Columns for table '{table_name}':"] for column in columns: output.append( f"{column['Field']} {column['Type']} NULL={column['Null']} KEY={column['Key']} DEFAULT={column['Default']} EXTRA={column['Extra']}" ) return "\n".join(output) - server.py:15-15 (registration)Registration of list_columns as an MCP tool via the FastMCP decorator pattern.
mcp.tool()(sql_tools.list_columns) - tools/sql_tools.py:96-104 (schema)The function signature serves as the schema: it accepts a single 'table_name: str' parameter and returns a 'str'.
def list_columns(self, table_name: str) -> str: """List columns for a given table. Args: table_name (str): Table name to inspect Returns: str: Column names and types for the table """ - tools/sql_tools.py:19-43 (helper)The get_connection context manager is a helper used by list_columns to obtain a MySQL 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()