Skip to main content
Glama
marekrost

mcp-server-spreadsheet

sql_query

Execute SQL SELECT queries on spreadsheet data to analyze, filter, and join information across sheets using a database-like interface.

Instructions

Execute a read-only SQL SELECT query against the spreadsheet data.

Every sheet in the workbook is loaded as a database table, with the header row defining column names and data rows below it. Returns results as a list of {column: value} objects.

Only SELECT (and WITH ... SELECT) statements are accepted. Use sql_execute for INSERT, UPDATE, or DELETE.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileYesPath to the spreadsheet file
sqlYesSQL SELECT statement to execute. Each sheet is a table (quote names with double quotes if they contain spaces). Supports WHERE, ORDER BY, LIMIT, GROUP BY, HAVING, JOINs across sheets, DISTINCT, UNION, subqueries, and aggregates (COUNT, SUM, AVG, MIN, MAX). Example: SELECT name, revenue FROM Sales WHERE status = 'Active' ORDER BY revenue DESC LIMIT 20
header_rowNo1-based row number containing column headers. Defaults to 1.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The `sql_query` tool is implemented here. It takes a spreadsheet file path and a SQL SELECT statement, loads the sheets into DuckDB, executes the query, and returns the results.
    def sql_query(
        file: Annotated[str, Field(description="Path to the spreadsheet file")],
        sql: Annotated[str, Field(description=(
            "SQL SELECT statement to execute. Each sheet is a table (quote names "
            "with double quotes if they contain spaces). "
            "Supports WHERE, ORDER BY, LIMIT, GROUP BY, HAVING, JOINs across "
            "sheets, DISTINCT, UNION, subqueries, and aggregates (COUNT, SUM, "
            "AVG, MIN, MAX). "
            "Example: SELECT name, revenue FROM Sales WHERE status = 'Active' "
            "ORDER BY revenue DESC LIMIT 20"
        ))],
        header_row: Annotated[int, Field(description="1-based row number containing column headers. Defaults to 1.")] = 1,
    ) -> list[dict]:
        """Execute a read-only SQL SELECT query against the spreadsheet data.
    
        Every sheet in the workbook is loaded as a database table, with the
        header row defining column names and data rows below it. Returns
        results as a list of {column: value} objects.
    
        Only SELECT (and WITH ... SELECT) statements are accepted. Use
        sql_execute for INSERT, UPDATE, or DELETE.
        """
        sql_stripped = sql.strip().rstrip(";")
        first_keyword = sql_stripped.split()[0].upper() if sql_stripped else ""
        if first_keyword not in ("SELECT", "WITH"):
            raise ValueError(
                "sql_query only accepts SELECT statements (or WITH ... SELECT). "
                "Use sql_execute for INSERT/UPDATE/DELETE."
            )
    
        wb = load_workbook(file)
        conn = _load_sheets_to_duckdb(wb, header_row)
    
        result = conn.execute(sql_stripped)
        columns = [desc[0] for desc in result.description]
        return [dict(zip(columns, row)) for row in result.fetchall()]
  • The `sql_query` function is registered as an MCP tool using the `@mcp.tool()` decorator.
    @mcp.tool()
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 effectively communicates key behaviors: the read-only nature, how spreadsheet data is structured (sheets as tables with headers), the return format (list of objects), and statement restrictions. It doesn't mention error handling, performance limits, or authentication needs, but covers the essential operational context.

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

Conciseness5/5

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

The description is efficiently structured with four sentences that each serve a distinct purpose: stating the core function, explaining data mapping, specifying return format, and providing usage boundaries. There is no wasted text, and key information is front-loaded.

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

Completeness5/5

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

Given the tool's complexity (SQL execution on spreadsheets), the description provides complete context: purpose, data model, return format, and usage boundaries. With an output schema present, it doesn't need to explain return values, and the 100% schema coverage handles parameters. The description fills all necessary gaps beyond structured fields.

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 all three parameters thoroughly. The description adds minimal parameter-specific information beyond the schema, mainly reinforcing that sheets become tables and headers define columns. It meets the baseline for high schema coverage without adding significant extra semantic value.

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: 'Execute a read-only SQL SELECT query against the spreadsheet data.' It specifies the verb ('execute'), resource ('SQL SELECT query'), and target ('spreadsheet data'), and distinguishes it from its sibling sql_execute by explicitly stating it's for SELECT queries only.

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

Usage Guidelines5/5

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

The description provides explicit guidance on when to use this tool vs. alternatives: 'Only SELECT (and WITH ... SELECT) statements are accepted. Use sql_execute for INSERT, UPDATE, or DELETE.' This clearly defines the scope and names the alternative tool for other SQL operations.

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/marekrost/mcp-server-spreadsheet'

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