list_databases
List all databases in a MySQL server and output the names as a formatted list, one per line, for easy inspection.
Instructions
List all databases in the MySQL server.
Returns: str: A formatted string containing all database names, one per line
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- tools/sql_tools.py:45-55 (handler)The `list_databases()` method on the `SQLTools` class. It opens a MySQL connection, executes 'SHOW DATABASES', and returns the database names as a newline-separated string.
def list_databases(self) -> str: """List all databases in the MySQL server. Returns: str: A formatted string containing all database names, one per line """ with self.get_connection() as conn: cursor = conn.cursor() cursor.execute("SHOW DATABASES") databases = [db[0] for db in cursor.fetchall()] return "\n".join(databases) - server.py:11-11 (registration)The tool is registered as an MCP tool on the 'CodingBuddy' server via `mcp.tool()(sql_tools.list_databases)`.
mcp.tool()(sql_tools.list_databases) - tools/sql_tools.py:19-43 (helper)The `get_connection()` context manager used by `list_databases()` to obtain a MySQL database connection with credentials from environment variables.
@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()