get_query_plan
Analyze SQL queries by generating query plans for improved database performance. Optimize execution strategies directly within the Adb MySQL MCP Server.
Instructions
Get the query plan for a SQL query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The SQL query to analyze |
Implementation Reference
- The unified tool handler function that implements the logic for 'get_query_plan' by wrapping the user query in an EXPLAIN statement and executing it to retrieve the query plan.@app.call_tool() async def call_tool(name: str, arguments: dict) -> list[TextContent]: """Execute SQL commands.""" config = get_db_config() if name == "execute_sql": query = arguments.get("query") if not query: raise ValueError("Query is required") elif name == "get_query_plan": query = arguments.get("query") if not query: raise ValueError("Query is required") query = f"EXPLAIN {query}" elif name == "get_execution_plan": query = arguments.get("query") if not query: raise ValueError("Query is required") query = f"EXPLAIN ANALYZE {query}" else: raise ValueError(f"Unknown tool: {name}") conn = pymysql.connect(**config) conn.autocommit(True) cursor = conn.cursor() try: # Execute the query cursor.execute(query) columns = [desc[0] for desc in cursor.description] rows = cursor.fetchall() result = [",".join(map(str, row)) for row in rows] return [TextContent(type="text", text="\n".join([",".join(columns)] + result))] except Exception as e: return [TextContent(type="text", text=f"Error executing query: {str(e)}")] finally: if cursor: cursor.close() if conn.open: conn.close()
- src/adb_mysql_mcp_server/server.py:137-150 (registration)Registration of the 'get_query_plan' tool in the list_tools() method, defining its metadata and input schema.Tool( name="get_query_plan", description="Get the query plan for a SQL query", inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "The SQL query to analyze" } }, "required": ["query"] }, ),