query
Run SELECT queries on MySQL databases using SQL commands and optional parameters, enabling efficient data retrieval through the MCP-MySQL Server interface.
Instructions
Execute a SELECT query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| params | No | An optional array of parameters (as strings) to bind to the SQL query placeholders (e.g., ?). | |
| sql | Yes | The SQL SELECT query string to execute. |
Implementation Reference
- src/index.ts:615-625 (handler)The handler function for the 'query' tool. Validates that the SQL is a SELECT query, executes it using executeQuery helper, and returns the results as a JSON-formatted text content.private async handleQuery(args: QueryArgs): Promise<QueryResult> { this.validateSqlInput(args.sql, ['SELECT']); const rows = await this.executeQuery(args.sql, args.params || []); return { content: [{ type: 'text', text: JSON.stringify(rows, null, 2) }] }; }
- src/index.ts:424-442 (schema)Input schema definition for the 'query' tool, specifying the sql string (required) and optional params array.inputSchema: { type: 'object', properties: { sql: { type: 'string', description: 'The SQL SELECT query string to execute.', }, params: { type: 'array', items: { type: 'string', description: 'A parameter value (as string) to bind to the query.' }, description: 'An optional array of parameters (as strings) to bind to the SQL query placeholders (e.g., ?).', optional: true, }, }, required: ['sql'], },
- src/index.ts:421-443 (registration)Registration of the 'query' tool in the ListTools response, including name, description, and input schema.{ name: 'query', description: 'Execute a SELECT query', inputSchema: { type: 'object', properties: { sql: { type: 'string', description: 'The SQL SELECT query string to execute.', }, params: { type: 'array', items: { type: 'string', description: 'A parameter value (as string) to bind to the query.' }, description: 'An optional array of parameters (as strings) to bind to the SQL query placeholders (e.g., ?).', optional: true, }, }, required: ['sql'], }, },
- src/index.ts:243-251 (helper)Helper method that ensures database connection, executes the SQL query using mysql2 pool.query, handles errors, and returns the result.private async executeQuery<T>(sql: string, params: any[] = []): Promise<T> { const pool = await this.ensureConnection(); try { const [result] = await pool.query(sql, params); return result as T; } catch (error) { this.handleDatabaseError(error); } }
- src/index.ts:58-68 (schema)TypeScript interfaces defining the input QueryArgs and output QueryResult for the query tool.interface QueryResult { content: Array<{ type: 'text'; text: string; }>; } interface QueryArgs { sql: string; params?: Array<string | number | boolean | null>; }