Skip to main content
Glama
kdqed
by kdqed

density_heatmap

Visualize data distribution patterns by creating a 2D histogram from SQL query results. Generate density heatmaps for CSV and Parquet data sources to analyze spatial relationships and concentration areas.

Instructions

Run query against specified source and make a 2d-histogram aka. Density Heatmap using result For both csv and parquet sources, use DuckDB SQL syntax Use 'CSV' as the table name in the SQL query for csv sources. Use 'PARQUET' as the table name in the SQL query for parquet sources.

This will return an image of the plot

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
source_idYesThe data source to run the query on
queryYesSQL query to run on the data source
column_xYesColumn name from SQL result to use on the X-axis
column_yYesColumn name from SQL result to use on the Y-axis
nbins_xNoOptional; number of bins to use on the X-axis
nbins_yNoOptional; number of bins to use on the Y-axis

Implementation Reference

  • The main handler function for the 'density_heatmap' MCP tool. It takes data source ID, SQL query, column names for X/Y axes, optional bin counts, executes the query, generates a density heatmap using plotly.express.density_heatmap, converts the figure to a base64-encoded PNG ImageContent object for MCP response, or returns an error string.
    def density_heatmap(self,
        source_id: Annotated[
            str, Field(description='The data source to run the query on')
        ],  
        query: Annotated[
            str, Field(description='SQL query to run on the data source')
        ],
        column_x: Annotated[
            str, Field(description='Column name from SQL result to use on the X-axis')
        ],
        column_y: Annotated[
            str, Field(description='Column name from SQL result to use on the Y-axis')
        ],
        nbins_x: Annotated[
            int | None, Field(description='Optional; number of bins to use on the X-axis')
        ] = None,
        nbins_y: Annotated[
            int | None, Field(description='Optional; number of bins to use on the Y-axis')
        ] = None,
    ) -> str | ImageContent:
        """
        Run query against specified source and make a 2d-histogram aka. Density Heatmap using result
        For both csv and parquet sources, use DuckDB SQL syntax
        Use 'CSV' as the table name in the SQL query for csv sources.
        Use 'PARQUET' as the table name in the SQL query for parquet sources.
    
        This will return an image of the plot
        """
    
        try:
            df = self._get_df_from_source(source_id, query)
            fig = px.density_heatmap(df, x=column_x, y=column_y, nbinsx=nbins_x, nbinsy=nbins_y)
            fig.update_xaxes(autotickangles=[0, 45, 60, 90])
            fig.update_yaxes(autotickangles=[0, 45, 60, 90])
    
            return _fig_to_image(fig)
        except Exception as e:
            return str(e)
  • Registers all tools from ZaturnTools instance (which includes density_heatmap) to the FastMCP server by converting each tool function to a Tool object and calling add_tool.
    for tool_function in zaturn_tools.tools:
        zaturn_mcp.add_tool(Tool.from_function(tool_function))
  • Adds the density_heatmap method to the self.tools list in Visualizations class __init__, making it available for collection into higher-level tool sets like ZaturnTools.
    self.tools = [
        self.scatter_plot,
        self.line_plot,
        self.histogram,
        self.strip_plot,
        self.box_plot,
        self.bar_plot,
    
        self.density_heatmap,
        self.polar_scatter,
        self.polar_line,
    ]
  • Includes all tools from Visualizations class (including density_heatmap) into the ZaturnTools.tools list by unpacking visualizations.Visualizations(data_sources).tools.
    self.tools = [
        *core.Core(data_sources).tools,
        *visualizations.Visualizations(data_sources).tools,
  • Utility function to convert a Plotly figure to an MCP-compatible ImageContent object (base64 PNG), used by density_heatmap and all other visualization tools.
    def _fig_to_image(fig):
        fig_encoded = b64encode(fig.to_image(format='png')).decode()
        img_b64 = "data:image/png;base64," + fig_encoded
        
        return ImageContent(
            type = 'image',
            data = fig_encoded,
            mimeType = 'image/png',
            annotations = None,
        )
  • Utility method to retrieve data source by ID, execute the provided SQL query using query_utils, and return Pandas DataFrame, shared by density_heatmap and other viz tools.
    def _get_df_from_source(self, source_id, query):
        source = self.data_sources.get(source_id)
        if not source:
            raise Exception(f"Source {source_id} Not Found")
                
        return query_utils.execute_query(source, query)
Behavior3/5

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

With no annotations provided, the description carries full burden. It discloses that the tool returns an image and specifies SQL syntax requirements, but doesn't mention potential behavioral aspects like performance characteristics, error handling, data size limitations, or whether the operation is read-only versus mutating. It adds some context but leaves significant gaps.

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 appropriately sized with 4 sentences. It's front-loaded with the core purpose, followed by technical details and output information. There's some redundancy ('aka. Density Heatmap') but overall it's efficient with zero wasted sentences.

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 6 parameters, no annotations, and no output schema, the description provides adequate but incomplete context. It explains the core functionality and SQL requirements but lacks information about the returned image format, error conditions, performance considerations, or how this differs from similar visualization tools. For a complex data visualization tool, more completeness would be helpful.

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 parameters are well-documented in the schema. The description adds minimal parameter semantics beyond the schema - it clarifies that 'query' uses DuckDB SQL syntax with specific table names, but doesn't explain relationships between parameters or provide additional context for the 6 parameters. Baseline 3 is appropriate.

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

Purpose5/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: 'Run query against specified source and make a 2d-histogram aka. Density Heatmap using result'. It specifies the verb ('make'), resource ('2d-histogram/Density Heatmap'), and distinguishes from siblings like 'histogram' (1D) and 'scatter_plot' (non-binned).

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 by mentioning SQL syntax requirements and table naming conventions for CSV/parquet sources, but doesn't explicitly state when to use this tool versus alternatives like 'scatter_plot' or 'histogram'. It provides technical prerequisites but no comparative guidance.

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/kdqed/zaturn'

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