execute_query
Execute a Redash query by providing its ID and optional parameters to retrieve results.
Instructions
Execute a Redash query and return results
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| queryId | Yes | ID of the query to execute | |
| parameters | No | Parameters to pass to the query (if any) |
Implementation Reference
- src/index.ts:282-307 (handler)The MCP tool handler function for 'execute_query'. Validates params via executeQuerySchema, calls redashClient.executeQuery(), and returns the result as JSON text.
async function executeQuery(params: z.infer<typeof executeQuerySchema>) { try { const { queryId, parameters } = params; const result = await redashClient.executeQuery(queryId, parameters); return { content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; } catch (error) { console.error(`Error executing query ${params.queryId}:`, error); return { isError: true, content: [ { type: "text", text: `Error executing query ${params.queryId}: ${error instanceof Error ? error.message : String(error)}` } ] }; } } - src/index.ts:277-280 (schema)Input schema (Zod) for the execute_query tool, defining queryId (number) and optional parameters (record of any).
const executeQuerySchema = z.object({ queryId: z.coerce.number(), parameters: z.record(z.any()).optional() }); - src/index.ts:1684-1699 (registration)Registration of the 'execute_query' tool in the MCP ListTools handler, including its name, description, and JSON Schema input definition.
{ name: "execute_query", description: "Execute a Redash query and return results", inputSchema: { type: "object", properties: { queryId: { type: "number", description: "ID of the query to execute" }, parameters: { type: "object", description: "Parameters to pass to the query (if any)", additionalProperties: true } }, required: ["queryId"] } }, - src/index.ts:2356-2358 (registration)Dispatch in CallToolRequestSchema switch statement routing the 'execute_query' tool name to the executeQuery handler function.
case "execute_query": logger.debug(`Handling execute_query`); return await executeQuery(executeQuerySchema.parse(args)); - src/redashClient.ts:489-523 (helper)The underlying Redash API client method executeQuery() that sends a POST request to /api/queries/{queryId}/results, optionally polls async job results, and returns a RedashQueryResult.
// Execute a query and return results async executeQuery(queryId: number, parameters?: Record<string, any>): Promise<RedashQueryResult> { try { const response = await this.client.post(`/api/queries/${queryId}/results`, { parameters }); if (response.data.job) { // Query is being executed asynchronously, poll for results return await this.pollQueryResults(response.data.job.id); } return response.data; } catch (error) { if (axios.isAxiosError(error)) { const axiosError = error as AxiosError; logger.error(`Error executing query ${queryId}: ${axiosError.message}`); // Extract detailed error information if (axiosError.response) { const statusCode = axiosError.response.status; const errorData = axiosError.response.data as any; const errorMessage = errorData?.message || errorData?.error || JSON.stringify(errorData); throw new Error(`Failed to execute query ${queryId}: Redash API error (${statusCode}): ${errorMessage}`); } else if (axiosError.request) { throw new Error(`Failed to execute query ${queryId}: No response received from Redash API: ${axiosError.message}`); } else { throw new Error(`Failed to execute query ${queryId}: ${axiosError.message}`); } } else { // Handle non-axios errors const errorMessage = error instanceof Error ? error.message : String(error); logger.error(`Error executing query ${queryId}: ${errorMessage}`); throw new Error(`Failed to execute query ${queryId}: ${errorMessage}`); } } }