Skip to main content
Glama
nilsir

MCP Server MySQL

by nilsir

query

Execute SELECT queries to retrieve data from MySQL databases, enabling data reading operations through prepared statements.

Instructions

Execute a SELECT query and return results. Use this for reading data.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sqlYesSQL SELECT query to execute
paramsNoQuery parameters for prepared statement

Implementation Reference

  • The asynchronous handler function for the 'query' tool. It validates the SQL query to ensure it is read-only, executes the SELECT query using the MySQL connection pool with optional parameters, formats the results, and returns both text and structured content.
    async ({ sql, params }) => {
      // Validate that this is a read-only query
      validateReadOnlyQuery(sql);
    
      const p = await getPool();
      const [rows] = await p.query<RowDataPacket[]>(sql, params || []);
    
      const output = {
        rows: rows as Record<string, unknown>[],
        rowCount: rows.length,
      };
    
      return {
        content: [
          {
            type: "text" as const,
            text: JSON.stringify(rows, null, 2),
          },
        ],
        structuredContent: output,
      };
    }
  • Zod input schema for the 'query' tool defining the required 'sql' string parameter and optional 'params' array for prepared statements.
    {
      sql: z.string().describe("SQL SELECT query to execute"),
      params: z.array(z.unknown()).optional().describe("Query parameters for prepared statement"),
    },
  • src/index.ts:143-172 (registration)
    Registration of the 'query' tool on the MCP server using server.tool(), including name, description, input schema, and handler function.
    server.tool(
      "query",
      "Execute a SELECT query and return results. Use this for reading data.",
      {
        sql: z.string().describe("SQL SELECT query to execute"),
        params: z.array(z.unknown()).optional().describe("Query parameters for prepared statement"),
      },
      async ({ sql, params }) => {
        // Validate that this is a read-only query
        validateReadOnlyQuery(sql);
    
        const p = await getPool();
        const [rows] = await p.query<RowDataPacket[]>(sql, params || []);
    
        const output = {
          rows: rows as Record<string, unknown>[],
          rowCount: rows.length,
        };
    
        return {
          content: [
            {
              type: "text" as const,
              text: JSON.stringify(rows, null, 2),
            },
          ],
          structuredContent: output,
        };
      }
    );
  • Helper function used by the 'query' handler to validate that the SQL statement is read-only by checking against a list of forbidden modifying keywords.
    function validateReadOnlyQuery(sql: string): void {
      const normalizedSql = sql.trim().toUpperCase();
    
      // List of forbidden keywords for read-only queries
      const forbiddenKeywords = [
        "INSERT",
        "UPDATE",
        "DELETE",
        "DROP",
        "CREATE",
        "ALTER",
        "TRUNCATE",
        "RENAME",
        "REPLACE",
        "GRANT",
        "REVOKE",
        "LOCK",
        "UNLOCK",
      ];
    
      for (const keyword of forbiddenKeywords) {
        if (normalizedSql.startsWith(keyword)) {
          throw new Error(
            `${keyword} operations are not allowed in query tool. Use the execute tool for data modifications or appropriate DDL tools for schema changes.`
          );
        }
      }
    }
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. It mentions the tool is for reading data, which implies non-destructive behavior, but lacks details on permissions, rate limits, error handling, or result format. This is inadequate for a tool with potential complexity in database interactions.

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 very concise, consisting of two short sentences that are front-loaded with the core purpose. Every word earns its place, with no wasted information, making it easy to scan and understand quickly.

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) and lack of annotations or output schema, the description is minimally complete. It covers the basic purpose and usage but misses critical behavioral details like result format, error cases, or security considerations, which are important for database tools.

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 ('sql' and 'params') adequately. The description adds no additional meaning beyond what the schema provides, such as examples or constraints, but doesn't contradict it, meeting the baseline for high coverage.

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 with a specific verb ('Execute') and resource ('SELECT query'), and indicates it's for reading data. However, it doesn't explicitly differentiate from sibling tools like 'execute' or 'describe_table', which might also involve querying or reading operations.

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

Usage Guidelines3/5

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

The description provides implied usage guidance by stating 'Use this for reading data,' which suggests it's for read-only operations. However, it doesn't explicitly mention when not to use it or name alternatives like 'execute' for non-SELECT queries, leaving some ambiguity.

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/nilsir/mcp-server-mysql'

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