mysql_query
Execute SQL queries on MySQL databases to retrieve, update, or manage data through prepared statements with secure SSL connections.
Instructions
Execute a SQL query on the connected MySQL database
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | SQL query to execute | |
| parameters | No | Parameters for prepared statement (optional) |
Input Schema (JSON Schema)
{
"properties": {
"parameters": {
"description": "Parameters for prepared statement (optional)",
"items": {
"type": "string"
},
"type": "array"
},
"query": {
"description": "SQL query to execute",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/index.ts:306-344 (handler)The `handleQuery` method is the primary handler for the `mysql_query` tool. It checks for an active database connection, validates the query input, executes the SQL query using the MySQL pool with optional parameters, handles both row-returning and non-row-returning results, and returns formatted text content.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)}`); } }
- src/index.ts:139-155 (schema)The input schema for the `mysql_query` tool defines the expected parameters: a required `query` string and an optional `parameters` array of strings for prepared statements.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 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)Handler dispatch case in the CallToolRequest switch statement that routes `mysql_query` calls to the `handleQuery` method.case "mysql_query": return await this.handleQuery(args);