query
Run SQL SELECT queries on MySQL or MongoDB databases via a standardized MCP server interface, enabling data retrieval and analysis for integrated database operations.
Instructions
Execute a SELECT query
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| params | No | Query parameters (optional) | |
| sql | Yes | SQL SELECT query |
Input Schema (JSON Schema)
{
"properties": {
"params": {
"description": "Query parameters (optional)",
"items": {
"type": [
"string",
"number",
"boolean",
"null"
]
},
"type": "array"
},
"sql": {
"description": "SQL SELECT query",
"type": "string"
}
},
"required": [
"sql"
],
"type": "object"
}
Implementation Reference
- src/index.ts:690-720 (handler)The handler function that validates the SQL query is a SELECT statement, executes it on the MySQL connection with optional parameters, and returns the results as JSON.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 defining the parameters for the query tool: 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)Registration of the 'query' tool in the tools list returned by listToolsRequest, including name, description, and 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:539-540 (registration)Switch case in CallToolRequest handler that dispatches 'query' tool calls to the handleQuery method.case 'query': return await this.handleQuery(request.params.arguments);