Skip to main content
Glama
stinkgen

Trino MCP Server

by stinkgen

execute_query

Execute SQL queries on Trino databases to retrieve and analyze data directly from distributed storage systems.

Instructions

    Execute a SQL query against Trino.
    
    Args:
        sql: The SQL query to execute.
        catalog: Optional catalog name to use for the query.
        schema: Optional schema name to use for the query.
        
    Returns:
        Dict[str, Any]: Query results including metadata.
    

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sqlYes
catalogNo
schemaNo

Implementation Reference

  • The primary handler function for the 'execute_query' MCP tool. It is decorated with @mcp.tool() and formats query results from the TrinoClient, providing preview rows and metadata.
    @mcp.tool()
    def execute_query(
        sql: str, 
        catalog: Optional[str] = None, 
        schema: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        Execute a SQL query against Trino.
        
        Args:
            sql: The SQL query to execute.
            catalog: Optional catalog name to use for the query.
            schema: Optional schema name to use for the query.
            
        Returns:
            Dict[str, Any]: Query results including metadata.
        """
        logger.info(f"Executing query: {sql}")
        
        try:
            result = client.execute_query(sql, catalog, schema)
            
            # Format the result in a structured way
            formatted_result = {
                "query_id": result.query_id,
                "columns": result.columns,
                "row_count": result.row_count,
                "query_time_ms": result.query_time_ms
            }
            
            # Add preview of results (first 20 rows)
            preview_rows = []
            max_preview_rows = min(20, len(result.rows))
            
            for i in range(max_preview_rows):
                row_dict = {}
                for j, col in enumerate(result.columns):
                    row_dict[col] = result.rows[i][j]
                preview_rows.append(row_dict)
                
            formatted_result["preview_rows"] = preview_rows
            
            # Include a resource path for full results
            formatted_result["resource_path"] = f"trino://query/{result.query_id}"
            
            return formatted_result
        
        except Exception as e:
            error_msg = str(e)
            logger.error(f"Query execution failed: {error_msg}")
            return {
                "error": error_msg,
                "query": sql
            }
  • The registration point where register_trino_tools is called on the FastMCP instance, which defines and registers the execute_query tool.
    logger.info("Registering resources and tools")
    register_trino_resources(mcp, trino_client)
    register_trino_tools(mcp, trino_client)
  • The underlying TrinoClient.execute_query method that performs the actual database query execution using the trino-python-client library.
        self, 
        sql: str, 
        catalog: Optional[str] = None, 
        schema: Optional[str] = None
    ) -> TrinoQueryResult:
        """
        Execute a SQL query against Trino.
        
        Important note on catalog handling: This method properly sets the catalog by updating
        the connection parameters, rather than using unreliable "USE catalog" statements. The catalog
        is passed directly to the connection, which is more reliable than SQL-based catalog switching.
        
        Args:
            sql: The SQL query to execute.
            catalog: Optional catalog name to use for the query.
            schema: Optional schema name to use for the query.
            
        Returns:
            TrinoQueryResult: The result of the query.
        """
        # If we're switching catalogs or don't have a connection, we need to reconnect
        use_catalog = catalog or self.current_catalog
        
        if self.conn and (use_catalog != self.current_catalog):
            logger.info(f"Switching catalog from {self.current_catalog} to {use_catalog}, reconnecting...")
            self.disconnect()
        
        # Update current catalog and schema
        self.current_catalog = use_catalog
        if schema:
            self.current_schema = schema
            
        # Update the config catalog before connecting
        if use_catalog:
            self.config.catalog = use_catalog
        
        # Ensure connection with updated catalog
        self.ensure_connection()
        
        # Create a cursor
        cursor = self.conn.cursor()
        
        # If we have a schema, try to set it
        # This still uses a USE statement, but catalogs are now set in the connection
        if self.current_schema:
            try:
                logger.debug(f"Setting schema to {self.current_schema}")
                
                # Make sure to include catalog with schema to avoid errors
                if self.current_catalog:
                    cursor.execute(f"USE {self.current_catalog}.{self.current_schema}")
                else:
                    logger.warning("Cannot set schema without catalog")
            except Exception as e:
                logger.warning(f"Failed to set schema: {e}")
        
        try:
            # Execute the query and time it
            logger.debug(f"Executing query: {sql}")
            start_time = time.time()
            cursor.execute(sql)
            query_time = time.time() - start_time
            
            # Fetch the query ID, metadata and results
            query_id = cursor.stats.get("queryId", "unknown")
            columns = [desc[0] for desc in cursor.description] if cursor.description else []
            rows = cursor.fetchall() if cursor.description else []
            
            return TrinoQueryResult(
                query_id=query_id,
                columns=columns,
                rows=rows,
                query_time_ms=query_time * 1000,
                row_count=len(rows)
            )
        except Exception as e:
            logger.error(f"Query execution failed: {e}")
            raise
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the action ('Execute a SQL query') and return type ('Dict[str, Any]: Query results including metadata'), but lacks critical details like permissions required, whether queries are read-only or mutating, timeouts, error handling, or rate limits. For a tool with no annotations, this is insufficient.

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 well-structured and appropriately sized. It starts with a clear purpose statement, followed by bullet-like sections for 'Args' and 'Returns'. Each sentence adds value without redundancy, though the 'Returns' section could be more specific given the lack of an output schema.

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 the tool's complexity (executing SQL queries), lack of annotations, and no output schema, the description is moderately complete. It covers the basic purpose and parameters but misses behavioral context (e.g., safety, performance) and detailed return value explanation. It's adequate as a starting point but has clear gaps for effective agent use.

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

Parameters4/5

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

The description adds significant value beyond the input schema, which has 0% description coverage. It explains each parameter's purpose: 'sql: The SQL query to execute', 'catalog: Optional catalog name to use for the query', and 'schema: Optional schema name to use for the query'. This clarifies semantics that the schema alone doesn't provide, though it doesn't detail format constraints or 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: 'Execute a SQL query against Trino.' This specifies the verb ('Execute') and resource ('SQL query'), and identifies the target system ('Trino'). However, it doesn't explicitly differentiate from sibling tools like 'cancel_query' or 'inspect_table' beyond the core action.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools ('cancel_query', 'inspect_table'), suggest prerequisites, or outline scenarios where this tool is appropriate versus others. Usage is implied only by the tool's name and purpose.

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/stinkgen/trino_mcp'

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