create_model
Create a curated dataset model in Metabase by defining an SQL query and optional column metadata, description, and collection placement.
Instructions
Create a new model in Metabase.
A model is a special type of saved question that acts as a curated dataset. Models can define metadata for their columns and serve as building blocks for other questions.
Args: name: Name of the model. database_id: ID of the database to query. query: SQL query that defines the model. description: Optional description of the model. collection_id: Optional collection to place the model in. result_metadata: Optional list of column metadata dicts. Each dict can include keys like "name", "display_name", "base_type", "semantic_type", "description", and "field_ref". visualization_settings: Optional visualization configuration.
Returns: The created model object.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| database_id | Yes | ||
| query | Yes | ||
| description | No | ||
| collection_id | No | ||
| result_metadata | No | ||
| visualization_settings | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- server.py:699-763 (handler)The `create_model` tool handler function. It creates a new model in Metabase by POSTing to /api/card with type 'model', supporting optional description, collection_id, result_metadata, and visualization_settings parameters.
@mcp.tool async def create_model( name: str, database_id: int, query: str, ctx: Context, description: str | None = None, collection_id: int | None = None, result_metadata: list[dict[str, Any]] | None = None, visualization_settings: dict[str, Any] | None = None, ) -> dict[str, Any]: """ Create a new model in Metabase. A model is a special type of saved question that acts as a curated dataset. Models can define metadata for their columns and serve as building blocks for other questions. Args: name: Name of the model. database_id: ID of the database to query. query: SQL query that defines the model. description: Optional description of the model. collection_id: Optional collection to place the model in. result_metadata: Optional list of column metadata dicts. Each dict can include keys like "name", "display_name", "base_type", "semantic_type", "description", and "field_ref". visualization_settings: Optional visualization configuration. Returns: The created model object. """ try: await ctx.info(f"Creating new model '{name}' in database {database_id}") payload: dict[str, Any] = { "name": name, "type": "model", "database_id": database_id, "dataset_query": { "database": database_id, "type": "native", "native": {"query": query}, }, "display": "table", "visualization_settings": visualization_settings or {}, } if description: payload["description"] = description if collection_id is not None: payload["collection_id"] = collection_id await ctx.debug(f"Model will be placed in collection {collection_id}") if result_metadata is not None: payload["result_metadata"] = result_metadata await ctx.debug(f"Model will have {len(result_metadata)} column metadata entries") result = await metabase_client.request("POST", "/card", json=payload) await ctx.info(f"Successfully created model with ID {result.get('id')}") return result except Exception as e: error_msg = f"Error creating model: {e}" await ctx.error(error_msg) raise ToolError(error_msg) from e - server.py:699-709 (schema)Function signature and parameter type definitions for the create_model tool (name, database_id, query, ctx, description, collection_id, result_metadata, visualization_settings).
@mcp.tool async def create_model( name: str, database_id: int, query: str, ctx: Context, description: str | None = None, collection_id: int | None = None, result_metadata: list[dict[str, Any]] | None = None, visualization_settings: dict[str, Any] | None = None, ) -> dict[str, Any]: - server.py:700-700 (registration)The @mcp.tool decorator on line 700 registers create_model as an MCP tool.
async def create_model(