fix: improve create_card dataset_query schema for MCP compatibility#15
Open
Kai-Oesterling wants to merge 1 commit intoCognitionAI:mainfrom
Open
fix: improve create_card dataset_query schema for MCP compatibility#15Kai-Oesterling wants to merge 1 commit intoCognitionAI:mainfrom
Kai-Oesterling wants to merge 1 commit intoCognitionAI:mainfrom
Conversation
The previous schema using `z.object({}).passthrough()` for dataset_query
caused issues when MCP clients passed nested objects. The Metabase API
returned 400 errors because the dataset_query structure wasn't being
properly validated and passed through.
This fix:
- Adds explicit schema for native queries with database, type, and native fields
- Adds explicit schema for MBQL queries with database, type, and query fields
- Maintains backward compatibility with passthrough() for additional fields
- Improves description to guide users on required structure
Tested with SQL Server native queries - cards now create successfully.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
create_cardtool fails with HTTP 400 errors when called via MCP clients (Claude Desktop, etc.). The Metabase API rejects the request because thedataset_queryobject isn't properly structured.Root Cause
The current schema definition:
While
passthrough()should allow additional properties, MCP clients don't properly serialize deeply nested objects when the base schema is an empty object{}.Example Error
When trying to create a card with a native SQL query:
{ "name": "Test Card", "dataset_query": { "type": "native", "database": 2, "native": { "query": "SELECT * FROM users" } } }The API returns:
Request failed with status code 400Solution
Define explicit Zod schemas for the two main query types:
Combined with
z.union()to accept either format.Additional Improvements
display("table") andvisualization_settings({})Testing
Tested with:
Breaking Changes
None. The schema is more specific but still accepts all valid Metabase query formats via
passthrough().