get_table_schema
Retrieve the schema information for a specific table, including column names and data types, to understand its structure.
Instructions
Get the schema information for a specific table.
Args: table_name (str): Name of the table to get schema for
Returns: str: A formatted string containing the table schema information Each column's information is on a new line
Raises: Error: If the table doesn't exist or schema retrieval fails
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| table_name | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- tools/sql_tools.py:213-243 (handler)The get_table_schema method on SQLTools class that connects to MySQL and executes DESCRIBE on the specified table, returning formatted schema information (Field, Type, Null, Key, Default, Extra) for each column.
def get_table_schema(self, table_name: str) -> str: """Get the schema information for a specific table. Args: table_name (str): Name of the table to get schema for Returns: str: A formatted string containing the table schema information Each column's information is on a new line Raises: Error: If the table doesn't exist or schema retrieval fails """ with self.get_connection() as conn: cursor = conn.cursor(dictionary=True, buffered=True) cursor.execute(f"DESCRIBE `{table_name}`") schema = cursor.fetchall() if not schema: return f"No schema found for table '{table_name}'" output = [f"Schema for table '{table_name}':"] for column in schema: output.append(f"Column: {column['Field']}") output.append(f"Type: {column['Type']}") output.append(f"Null: {column['Null']}") output.append(f"Key: {column['Key']}") output.append(f"Default: {column['Default']}") output.append(f"Extra: {column['Extra']}") output.append("") return "\n".join(output) - server.py:16-16 (registration)Registration of get_table_schema as an MCP tool via mcp.tool()(sql_tools.get_table_schema)
mcp.tool()(sql_tools.get_table_schema) - test_sql_tools.py:76-82 (helper)Test case for get_table_schema that verifies it returns a string containing 'Schema for table'
def test_table_get_schema(sql_tools: SQLTools, tables: List[str]): if not tables: pytest.skip("No tables available for table-specific tests.") result = sql_tools.get_table_schema(tables[0]) assert isinstance(result, str) assert "Schema for table" in result