Skip to main content
Glama
MikelA92
by MikelA92

get_generated_sql

Generate the actual SQL query that Metabase creates from query builder parameters to understand how your visual queries translate to database commands.

Instructions

📝 [MODERATE RISK] Get the generated SQL for a query-builder card with parameters. Use this to see the actual SQL that Metabase generates from query builder parameters. Risk: Moderate - executes queries to generate SQL.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
cardIdYesThe card ID
parametersYesQuery parameters object

Implementation Reference

  • The primary handler function implementing the get_generated_sql tool logic. It retrieves a query-builder card, applies the provided parameters to its dataset_query, posts to Metabase's /api/dataset endpoint to generate the SQL, extracts the SQL from the response, and formats it for MCP output.
    async getGeneratedSQL(cardId, parameters) { Validators.validateCardId(cardId); this.logger.debug('Getting generated SQL', { cardId, parameters }); // Get the base card const baseCard = await this.apiClient.makeRequest(`/api/card/${cardId}`); if (baseCard.dataset_query.type !== 'query') { throw new Error('Card is not a query-builder card'); } // Create a modified dataset query with the provided parameters const modifiedQuery = { ...baseCard.dataset_query, query: { ...baseCard.dataset_query.query, ...parameters, }, }; // Use the query endpoint to get the generated SQL const body = { database: baseCard.dataset_query.database, type: 'query', query: modifiedQuery.query, }; const results = await this.apiClient.makeRequest('/api/dataset', { method: 'POST', body: JSON.stringify(body), }); // Extract SQL if available const sql = results.query || results.native?.query || 'SQL not available in response'; return { content: [ { type: 'text', text: `Generated SQL for Query Builder Card: Card ID: ${cardId} Card Name: ${baseCard.name} Parameters Applied: ${JSON.stringify(parameters, null, 2)} Generated SQL: ${sql} Full Query Response: ${JSON.stringify(results, null, 2)}`, }, ], }; }
  • The JSON schema definition for the get_generated_sql tool, including input validation for cardId (integer) and parameters (object). This is exported in TOOL_DEFINITIONS and used by the MCP server for tool listing and validation.
    { name: 'get_generated_sql', description: '📝 [MODERATE RISK] Get the generated SQL for a query-builder card with parameters. Use this to see the actual SQL that Metabase generates from query builder parameters. Risk: Moderate - executes queries to generate SQL.', inputSchema: { type: 'object', properties: { cardId: { type: 'integer', description: 'The card ID', minimum: 1, }, parameters: { type: 'object', description: 'Query parameters object', additionalProperties: true, }, }, required: ['cardId', 'parameters'], }, },
  • Registration of the tool handler in the MCP server's executeTool switch statement. Routes calls to 'get_generated_sql' to the CardHandlers.getGeneratedSQL method.
    case 'get_generated_sql': return await this.cardHandlers.getGeneratedSQL(args.cardId, args.parameters);
  • Underlying helper method in MetabaseClient that implements the core logic for generating SQL from query-builder cards with parameters. Used by the server handlers via apiClient.makeRequest.
    async getGeneratedSQL(cardId, parameters) { Validators.validateCardId(cardId); // Get the base card const baseCard = await this.makeRequest(`/api/card/${cardId}`); if (baseCard.dataset_query.type !== 'query') { throw new ValidationError( 'Card is not a query-builder card', 'cardId', cardId ); } // Create a modified dataset query with the provided parameters const modifiedQuery = { ...baseCard.dataset_query, query: { ...baseCard.dataset_query.query, ...parameters, }, }; // Use the query endpoint to get the generated SQL const body = { database: baseCard.dataset_query.database, type: 'query', query: modifiedQuery.query, }; // Try to get the SQL by using the query endpoint const results = await this.makeRequest('/api/dataset', { method: 'POST', body: JSON.stringify(body), }); // Extract SQL if available const sql = results.query || results.native?.query || 'SQL not available in response'; return { cardId: cardId, cardName: baseCard.name, parameters: parameters, generatedSQL: sql, fullResults: results, }; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/MikelA92/metabase-mcp-mab'

If you have feedback or need assistance with the MCP directory API, please join our Discord server