execute_hg_select_sql
Query data from Hologres databases by executing SELECT SQL statements. Enables AI agents to retrieve specific information directly through the Hologres MCP Server interface.
Instructions
Execute SELECT SQL to query data from Hologres database.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The (SELECT) SQL query to execute in Hologres database. |
Implementation Reference
- src/hologres_mcp_server/utils.py:48-79 (handler)Core function that executes the SQL query for tools like execute_hg_select_sql. Handles connection, serverless mode, execution, and formats SELECT results as comma-separated values with headers.def handle_call_tool(tool_name, query, serverless = False): """Handle callTool method.""" config = get_db_config() try: with connect_with_retry() as conn: with conn.cursor() as cursor: # 特殊处理 serverless computing 查询 if serverless: cursor.execute("set hg_computing_resource='serverless'") # Execute the query cursor.execute(query) # 特殊处理 ANALYZE 命令 if tool_name == "gather_hg_table_statistics": return f"Successfully {query}" # 处理其他有返回结果的查询 if cursor.description: # SELECT query columns = [desc[0] for desc in cursor.description] rows = cursor.fetchall() result = [",".join(map(str, row)) for row in rows] return "\n".join([",".join(columns)] + result) elif tool_name == "execute_dml_sql": # Non-SELECT query row_count = cursor.rowcount return f"Query executed successfully. {row_count} rows affected." else: return "Query executed successfully" except Exception as e: return f"Error executing query: {str(e)}"
- Specific dispatch and validation logic within the main @app.call_tool() handler for the execute_hg_select_sql tool: extracts the query parameter and ensures it's a valid SELECT statement.if name == "execute_hg_select_sql": query = arguments.get("query") if not query: raise ValueError("Query is required") if not re.match(r"^\s*WITH\s+.*?SELECT\b", query, re.IGNORECASE) and not re.match(r"^\s*SELECT\b", query, re.IGNORECASE): raise ValueError("Query must be a SELECT statement or start with WITH followed by a SELECT statement")
- src/hologres_mcp_server/server.py:347-360 (registration)Registration of the execute_hg_select_sql tool in the @app.list_tools() function, including description and input schema.Tool( name="execute_hg_select_sql", description="Execute SELECT SQL to query data from Hologres database.", inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "The (SELECT) SQL query to execute in Hologres database." } }, "required": ["query"] } ),
- Input schema definition for the execute_hg_select_sql tool: requires a 'query' string parameter.inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "The (SELECT) SQL query to execute in Hologres database." } }, "required": ["query"] }