Skip to main content
Glama
aliyun

Adb MySQL MCP Server

Official
by aliyun

execute_sql

Execute SQL queries in Adb MySQL Cluster to retrieve data, update records, or perform database operations through the MCP server interface.

Instructions

Execute a SQL query in the Adb MySQL Cluster

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe SQL query to execute

Implementation Reference

  • The @app.call_tool() handler implements the logic for 'execute_sql' by extracting the query from arguments, connecting to the MySQL database using get_db_config(), executing the query, formatting results as CSV text content, and handling errors.
    @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()
  • The input schema definition for the 'execute_sql' tool, specifying a required 'query' string property.
    Tool(
        name="execute_sql",
        description="Execute a SQL query in the Adb MySQL Cluster",
        inputSchema={
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "The SQL query to execute"
                }
            },
            "required": ["query"]
        },
    ),
  • Registration of the 'execute_sql' tool (among others) via the @app.list_tools() decorator, which returns the list of available tools with their schemas.
    @app.list_tools()
    async def list_tools() -> list[Tool]:
        return [
            Tool(
                name="execute_sql",
                description="Execute a SQL query in the Adb MySQL Cluster",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "The SQL query to execute"
                        }
                    },
                    "required": ["query"]
                },
            ),
            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"]
                },
            ),
            Tool(
                name="get_execution_plan",
                description="Get the actual execution plan with runtime statistics for a SQL query",
                inputSchema={
                    "type": "object",
                    "properties": {
                        "query": {
                            "type": "string",
                            "description": "The SQL query to analyze"
                        }
                    },
                    "required": ["query"]
                }
            )
        ]
  • Helper function to retrieve MySQL database configuration from environment variables, used by the tool handler.
    def get_db_config():
        config = {
            "host": os.getenv("ADB_MYSQL_HOST", "localhost"),
            "port": int(os.getenv("ADB_MYSQL_PORT", 3306)),
            "user": os.getenv("ADB_MYSQL_USER"),
            "password": os.getenv("ADB_MYSQL_PASSWORD"),
            "database": os.getenv("ADB_MYSQL_DATABASE"),
        }
    
        if not all([config["user"], config["password"], config["database"]]):
            raise ValueError("Missing required database configuration")
    
        return config

Tool Definition Quality

Score is being calculated. Check back soon.

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/aliyun/alibabacloud-adb-mysql-mcp-server'

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