Skip to main content
Glama
Darkstar326

MCP MySQL Server

by Darkstar326

mysql_query

Execute SQL queries on MySQL databases to retrieve, update, or manage data through the MCP MySQL Server.

Instructions

Execute a SQL query on the connected MySQL database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesSQL query to execute
parametersNoParameters for prepared statement (optional)

Implementation Reference

  • The primary handler function for the 'mysql_query' tool. It validates input, executes the SQL query using the MySQL pool with optional parameters, handles SELECT (returns rows) and other queries (returns affected rows/insert ID), and returns formatted results or errors.
    private async handleQuery(args: any) {
      if (!this.pool) {
        throw new Error("Not connected to MySQL. Use mysql_connect first.");
      }
    
      const { query, parameters = [] } = args;
      
      if (!query || typeof query !== "string") {
        throw new Error("Query is required and must be a string");
      }
    
      try {
        const [results, fields] = await this.pool.execute(query, parameters);
        
        // Handle different types of results
        if (Array.isArray(results)) {
          return {
            content: [
              {
                type: "text",
                text: `Query executed successfully. ${results.length} rows affected.\n\nResults:\n${JSON.stringify(results, null, 2)}`,
              },
            ],
          };
        } else {
          const resultInfo = results as mysql.ResultSetHeader;
          return {
            content: [
              {
                type: "text",
                text: `Query executed successfully.\nAffected rows: ${resultInfo.affectedRows}\nInserted ID: ${resultInfo.insertId || "N/A"}`,
              },
            ],
          };
        }
      } catch (error) {
        throw new Error(`Query execution failed: ${error instanceof Error ? error.message : String(error)}`);
      }
    }
  • Defines the JSON Schema for input validation of the mysql_query tool, specifying the required 'query' string and optional 'parameters' array of strings.
    inputSchema: {
      type: "object",
      properties: {
        query: {
          type: "string",
          description: "SQL query to execute",
        },
        parameters: {
          type: "array",
          description: "Parameters for prepared statement (optional)",
          items: {
            type: "string",
          },
        },
      },
      required: ["query"],
    },
  • src/index.ts:136-156 (registration)
    Registration of the mysql_query tool in the ListTools handler response, including name, description, and input schema.
    {
      name: "mysql_query",
      description: "Execute a SQL query on the connected MySQL database",
      inputSchema: {
        type: "object",
        properties: {
          query: {
            type: "string",
            description: "SQL query to execute",
          },
          parameters: {
            type: "array",
            description: "Parameters for prepared statement (optional)",
            items: {
              type: "string",
            },
          },
        },
        required: ["query"],
      },
    },
  • src/index.ts:251-252 (registration)
    Switch case in the CallToolRequest handler that routes mysql_query invocations to the handleQuery method.
    case "mysql_query":
      return await this.handleQuery(args);

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observed

TDQS

C2.9/5.0
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 but lacks critical details: it does not specify whether queries are read-only or mutating, potential side effects (e.g., data modification), error handling, or performance implications (e.g., rate limits). This is inadequate for a tool that could perform destructive operations.

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 a single, efficient sentence with zero waste. It is front-loaded with the core purpose and appropriately sized, making it easy for an agent to parse quickly without unnecessary elaboration.

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

Completeness2/5

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

Given the complexity of SQL execution (potential for mutations, errors, and varied outputs) and the absence of annotations and output schema, the description is incomplete. It fails to address key contextual aspects like return formats, safety warnings, or connection requirements, leaving significant gaps for agent understanding.

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 both parameters (query and parameters). The description adds no additional meaning beyond what the schema provides, such as query syntax examples or parameter usage details. The baseline score of 3 reflects adequate but minimal value addition from the description.

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 action ('Execute a SQL query') and target resource ('on the connected MySQL database'), providing a specific verb+resource combination. However, it does not differentiate from sibling tools like mysql_describe_table or mysql_list_tables, which also involve database operations but with different purposes.

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 does not mention prerequisites (e.g., requiring a connection via mysql_connect), exclusions (e.g., avoiding certain query types), or comparisons to siblings like mysql_list_tables for specific tasks, leaving the agent with minimal context for tool selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.