query
Execute SQL SELECT queries to retrieve data from MySQL and MongoDB databases through a standardized interface, supporting query parameters for dynamic operations.
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:690-720 (handler)The handler function that executes SELECT SQL queries on the MySQL database using the provided SQL and optional parameters. Ensures connection, validates SELECT only, executes query, and returns JSON results.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:262-278 (schema)Input schema definition for the 'query' tool, specifying required 'sql' string and optional 'params' array.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:259-279 (registration)Tool registration in the listTools response, including name, description, and inputSchema for the 'query' tool.{ 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:539-540 (registration)Dispatch case in CallToolRequestSchema handler that routes 'query' tool calls to the handleQuery method.case 'query': return await this.handleQuery(request.params.arguments);