Skip to main content
Glama
MikelA92

Metabase MCP Server

by MikelA92

get_generated_sql

Retrieve the SQL code generated by Metabase from query builder parameters to verify and understand the actual database queries being executed.

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 core handler function for 'get_generated_sql'. Fetches the query-builder card by ID, merges provided parameters into the query structure, posts to Metabase /api/dataset endpoint to generate the SQL, extracts the SQL from response, and formats a response with the generated SQL.
      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)}`,
            },
          ],
        };
      }
  • Defines the tool schema including name, description, and inputSchema for validation (cardId: integer, parameters: object). Used by 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'],
      },
    },
  • Registers the tool in the MCP server's executeTool switch statement, dispatching calls to the CardHandlers.getGeneratedSQL method.
    case 'get_generated_sql':
      return await this.cardHandlers.getGeneratedSQL(args.cardId, args.parameters);
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It adds valuable context: 'Risk: Moderate - executes queries to generate SQL.' This warns that the tool may trigger query execution (potentially resource-intensive) and clarifies the moderate risk level. However, it doesn't detail response format, error handling, or rate limits, which would enhance transparency further.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise and front-loaded: the first sentence states the purpose clearly, and the second adds risk context. Both sentences earn their place by providing essential information. However, the emoji and bracketed risk label add slight clutter without adding critical value.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no annotations and no output schema, the description is moderately complete. It covers purpose and risk, but lacks details on return values (e.g., SQL string format), error cases, or performance implications. For a tool with moderate risk and two parameters, this leaves gaps that could hinder agent understanding.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents both parameters (cardId and parameters). The description adds minimal param semantics beyond the schema, only implying that parameters affect SQL generation. Baseline 3 is appropriate as the schema does the heavy lifting, but the description doesn't compensate with additional details like parameter format examples.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Get the generated SQL for a query-builder card with parameters.' It specifies the verb ('Get'), resource ('generated SQL'), and context ('query-builder card with parameters'). However, it doesn't explicitly differentiate from siblings like execute_card_query or execute_query_builder_card, which likely execute queries rather than just retrieving SQL.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context: 'Use this to see the actual SQL that Metabase generates from query builder parameters.' This suggests it's for inspection/debugging rather than execution. However, it doesn't explicitly state when to use this vs. alternatives (e.g., execute_card_query for running queries) or any prerequisites/exclusions, leaving some ambiguity.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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