add_card_to_dashboard
Add a card to a dashboard at a custom grid position and size. Provide the dashboard ID and card ID, then optionally set column, row, width, and height.
Instructions
Add an existing card to a dashboard at a specified position and size.
Args: dashboard_id: The ID of the dashboard to add the card to. card_id: The ID of the card to add. col: Column position on the dashboard grid (default: 0). row: Row position on the dashboard grid (default: 0). size_x: Width of the card in grid units (default: 6). size_y: Height of the card in grid units (default: 4).
Returns: The created dashboard card object.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dashboard_id | Yes | ||
| card_id | Yes | ||
| col | No | ||
| row | No | ||
| size_x | No | ||
| size_y | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- server.py:1582-1653 (handler)The `add_card_to_dashboard` tool implementation. It fetches the existing dashboard, preserves current dashcards with their layout and mappings, appends a new dashcard entry (with id=-1 to signal a new card), and PUTs the updated dashcards array to the Metabase API.
@mcp.tool async def add_card_to_dashboard( dashboard_id: int, card_id: int, ctx: Context, col: int = 0, row: int = 0, size_x: int = 6, size_y: int = 4, ) -> dict[str, Any]: """ Add an existing card to a dashboard at a specified position and size. Args: dashboard_id: The ID of the dashboard to add the card to. card_id: The ID of the card to add. col: Column position on the dashboard grid (default: 0). row: Row position on the dashboard grid (default: 0). size_x: Width of the card in grid units (default: 6). size_y: Height of the card in grid units (default: 4). Returns: The created dashboard card object. """ try: await ctx.info(f"Adding card {card_id} to dashboard {dashboard_id}") # Fetch existing dashboard to get current dashcards dashboard = await metabase_client.request("GET", f"/dashboard/{dashboard_id}") existing_dashcards = dashboard.get("dashcards", dashboard.get("ordered_cards", [])) # Preserve existing dashcards with their current layout and mappings dashcards = [ { "id": dc["id"], "card_id": dc.get("card_id"), "row": dc.get("row"), "col": dc.get("col"), "size_x": dc.get("size_x"), "size_y": dc.get("size_y"), "parameter_mappings": list(dc.get("parameter_mappings") or []), "visualization_settings": dc.get("visualization_settings") or {}, "inline_parameters": list(dc.get("inline_parameters") or []), } for dc in existing_dashcards ] # Append new card with id: -1 to indicate a new entry dashcards.append({ "id": -1, "card_id": card_id, "row": row, "col": col, "size_x": size_x, "size_y": size_y, "parameter_mappings": [], "visualization_settings": {}, }) result = await metabase_client.request( "PUT", f"/dashboard/{dashboard_id}", json={"dashcards": dashcards} ) await ctx.info( f"Successfully added card {card_id} to dashboard {dashboard_id} at ({col}, {row})" ) return result except Exception as e: error_msg = f"Error adding card {card_id} to dashboard {dashboard_id}: {e}" await ctx.error(error_msg) raise ToolError(error_msg) from e - server.py:1582-1582 (registration)The tool is registered via the `@mcp.tool` decorator on line 1582, which registers `add_card_to_dashboard` as an MCP tool on the FastMCP server instance.
@mcp.tool