list_tables
Retrieve a list of all tables in a database by supplying its ID. Outputs a formatted markdown table showing table details.
Instructions
List all tables in a specific database.
Args: database_id: The ID of the database to query.
Returns: Formatted markdown table showing table details.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database_id | Yes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- server.py:160-222 (handler)The 'list_tables' tool handler function. It takes a database_id parameter, fetches table metadata from Metabase via GET /api/database/{database_id}/metadata, extracts table info (id, display_name, description, entity_type), sorts by display_name, and returns formatted markdown output.
async def list_tables(database_id: int, ctx: Context) -> str: """ List all tables in a specific database. Args: database_id: The ID of the database to query. Returns: Formatted markdown table showing table details. """ try: await ctx.info(f"Fetching tables for database {database_id}") result = await metabase_client.request("GET", f"/database/{database_id}/metadata") # Extract and format tables tables = result.get("tables", []) await ctx.debug(f"Found {len(tables)} tables in database {database_id}") formatted_tables = [ { "table_id": table.get("id"), "display_name": table.get("display_name"), "description": table.get("description") or "No description", "entity_type": table.get("entity_type") } for table in tables ] # Sort for better readability formatted_tables.sort(key=lambda x: x.get("display_name", "")) # Generate markdown output markdown_output = f"# Tables in Database {database_id}\n\n" markdown_output += f"**Total Tables:** {len(formatted_tables)}\n\n" if not formatted_tables: await ctx.warning(f"No tables found in database {database_id}") markdown_output += "*No tables found in this database.*\n" return markdown_output # Create markdown table markdown_output += "| Table ID | Display Name | Description | Entity Type |\n" markdown_output += "|----------|--------------|-------------|--------------|\n" for table in formatted_tables: table_id = table.get("table_id", "N/A") display_name = table.get("display_name", "N/A") description = table.get("description", "No description") entity_type = table.get("entity_type", "N/A") # Escape pipe characters description = description.replace("|", "\\|") display_name = display_name.replace("|", "\\|") markdown_output += f"| {table_id} | {display_name} | {description} | {entity_type} |\n" await ctx.info(f"Successfully formatted {len(formatted_tables)} tables") return markdown_output except Exception as e: error_msg = f"Error listing tables for database {database_id}: {e}" await ctx.error(error_msg) raise ToolError(error_msg) from e - server.py:159-159 (registration)The '@mcp.tool' decorator that registers the 'list_tables' function as an MCP tool with the FastMCP server instance.
@mcp.tool - server.py:160-160 (schema)The function signature defines the input schema: 'database_id: int' is the required parameter, and 'ctx: Context' is the MCP context. The return type is 'str' (formatted markdown).
async def list_tables(database_id: int, ctx: Context) -> str: