query
Run SELECT queries on MySQL databases using SQL statements and optional parameters via the MCP MySQL Server, enabling data retrieval and interaction with database systems.
Instructions
Execute a SELECT query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| params | No | Query parameters (optional) | |
| sql | Yes | SQL SELECT query |
Implementation Reference
- src/index.ts:563-573 (handler)The main handler function for the 'query' tool that validates the SQL is a SELECT statement, executes the query using the private executeQuery method, 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:375-395 (registration)Registration of the 'query' tool in the ListTools response, defining its name, description, and input schema.{ 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:58-68 (schema)TypeScript interfaces defining the input arguments (QueryArgs: sql and optional params) and output structure (QueryResult: text content) for the query tool.interface QueryResult { content: Array<{ type: 'text'; text: string; }>; } interface QueryArgs { sql: string; params?: Array<string | number | boolean | null>; }
- src/index.ts:243-251 (helper)Core helper method that performs the actual database query execution after ensuring connection, handling MySQL pool query and errors.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:521-522 (registration)Dispatch case in the CallToolRequest handler that routes 'query' tool calls to the handleQuery method.case 'query': return await this.handleQuery(request.params.arguments as unknown as QueryArgs);