run_sql_query
Execute read-only SQL SELECT queries on a MySQL database using the Model Context Protocol server for data retrieval and analysis.
Instructions
Executes a read-only SQL query (SELECT statements only) against the MySQL database.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The SQL SELECT query to execute. |
Implementation Reference
- src/index.ts:209-255 (handler)Implements the run_sql_query tool: validates input, checks for SELECT query, executes via MySQL pool, returns JSON results or error.private async handleReadQuery(request: any, transactionId: string) { if (!isValidSqlQueryArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'Invalid SQL query arguments.' ); } const query = request.params.arguments.query; if (!isReadOnlyQuery(query)) { throw new McpError( ErrorCode.InvalidParams, 'Only SELECT queries are allowed with run_sql_query tool.' ); } console.error(`[${transactionId}] Executing SELECT query: ${query}`); try { const [rows] = await this.pool.query(query); console.error(`[${transactionId}] Query executed successfully`); return { content: [ { type: 'text', text: JSON.stringify(rows, null, 2), }, ], }; } catch (error) { console.error(`[${transactionId}] Query error:`, error); if (error instanceof Error) { return { content: [ { type: 'text', text: `MySQL error: ${error.message}`, }, ], isError: true, }; } throw error; } }
- src/index.ts:94-107 (registration)Registers the run_sql_query tool in the list of available tools, including name, description, and input schema.{ name: 'run_sql_query', description: 'Executes a read-only SQL query (SELECT statements only) against the MySQL database.', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The SQL SELECT query to execute.', }, }, required: ['query'], }, },
- src/index.ts:187-188 (registration)Dispatches calls to the run_sql_query tool to the handleReadQuery method.case 'run_sql_query': return this.handleReadQuery(request, transactionId);
- src/index.ts:97-106 (schema)Defines the input schema for the run_sql_query tool: object with required 'query' string.inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'The SQL SELECT query to execute.', }, }, required: ['query'], },
- src/index.ts:30-31 (helper)Helper function to check if a query is a read-only SELECT query.const isReadOnlyQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('select');