Skip to main content
Glama
sajithrw

MCP MySQL Server

by sajithrw

mysql_query

Execute SQL queries on MySQL databases to retrieve, update, or manage data using prepared statements with optional parameters.

Instructions

Execute a SQL query on the connected MySQL database

Input Schema

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

Implementation Reference

  • The core handler function for the 'mysql_query' tool. It checks for an active connection, validates the query input, executes the SQL using mysql2/promise pool.execute(), distinguishes between row-returning queries and modifications, formats results as text content, and throws formatted 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)}`); } }
  • Input schema for the mysql_query tool, defining 'query' as required string and optional 'parameters' array of strings for safe parameterized queries.
    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)
    Tool registration in the ListToolsRequestSchema handler, specifying the name, description, and inputSchema advertised to MCP clients.
    { 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)
    Dispatch/registration in the CallToolRequestSchema switch statement, routing mysql_query calls to the handleQuery method.
    case "mysql_query": return await this.handleQuery(args);

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

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