query
Execute SELECT queries on MySQL databases to retrieve data using SQL statements with optional parameters for dynamic filtering.
Instructions
Execute a SELECT query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes | SQL SELECT query | |
| params | No | Query parameters (optional) |
Implementation Reference
- src/index.ts:253-283 (handler)The main handler function for the 'query' tool, which executes SELECT queries on the MySQL database after validation.private async handleQuery(args: any) { await this.ensureConnection(); if (!args.sql) { throw new McpError(ErrorCode.InvalidParams, 'SQL query is required'); } if (!args.sql.trim().toUpperCase().startsWith('SELECT')) { throw new McpError( ErrorCode.InvalidParams, 'Only SELECT queries are allowed with query tool' ); } try { const [rows] = await this.connection!.query(args.sql, args.params || []); return { content: [ { type: 'text', text: JSON.stringify(rows, null, 2), }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Query execution failed: ${getErrorMessage(error)}` ); } }
- src/index.ts:125-145 (schema)The tool definition including name, description, and input schema for the 'query' tool in the listTools response.{ name: 'query', description: 'Execute a SELECT query', inputSchema: { type: 'object', properties: { sql: { type: 'string', description: 'SQL SELECT query', }, params: { type: 'array', items: { type: ['string', 'number', 'boolean', 'null'], }, description: 'Query parameters (optional)', }, }, required: ['sql'], }, },
- src/index.ts:197-198 (registration)The switch case that routes the 'query' tool call to the handleQuery function.case 'query': return await this.handleQuery(request.params.arguments);