search_tables
Search for database tables and views matching a name pattern using wildcards.
Instructions
Search for tables and views matching a pattern.
Args: pattern (str): Search text or wildcard expression
Returns: str: Matching table and view names, one per line
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- tools/sql_tools.py:81-94 (handler)The handler function for the search_tables MCP tool. Connects to a MySQL database and executes 'SHOW TABLES LIKE %s' with the given pattern, returning matching table/view names.
def search_tables(self, pattern: str) -> str: """Search for tables and views matching a pattern. Args: pattern (str): Search text or wildcard expression Returns: str: Matching table and view names, one per line """ with self.get_connection() as conn: cursor = conn.cursor() cursor.execute("SHOW TABLES LIKE %s", (pattern,)) matches = [row[0] for row in cursor.fetchall()] return "\n".join(matches) if matches else f"No tables or views match '{pattern}'" - server.py:14-14 (registration)Registers the search_tables method as an MCP tool using FastMCP's decorator pattern.
mcp.tool()(sql_tools.search_tables) - tools/sql_tools.py:19-43 (helper)Helper context manager that provides a MySQL database connection used by the search_tables handler.
@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() - test_sql_tools.py:62-64 (helper)Test case for the search_tables tool, verifying it returns a string when given a wildcard pattern.
def test_search_tables(sql_tools: SQLTools): result = sql_tools.search_tables("%") assert isinstance(result, str)