execute_adhoc_query
Execute an ad-hoc SQL query against a specified data source without saving it; the temporary query is automatically deleted after execution.
Instructions
Execute an ad-hoc query without saving it to Redash. Creates a temporary query that is automatically deleted after execution.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | SQL query to execute | |
| dataSourceId | Yes | ID of the data source to query against |
Implementation Reference
- src/index.ts:1756-1767 (registration)Registration of the execute_adhoc_query MCP tool in the ListToolsRequestSchema handler
{ name: "execute_adhoc_query", description: "Execute an ad-hoc query without saving it to Redash. Creates a temporary query that is automatically deleted after execution.", inputSchema: { type: "object", properties: { query: { type: "string", description: "SQL query to execute" }, dataSourceId: { type: "number", description: "ID of the data source to query against" } }, required: ["query", "dataSourceId"] } }, - src/index.ts:477-502 (handler)MCP tool handler for execute_adhoc_query - parses params and delegates to redashClient.executeAdhocQuery
async function executeAdhocQuery(params: z.infer<typeof executeAdhocQuerySchema>) { try { const { query, dataSourceId } = params; const result = await redashClient.executeAdhocQuery(query, dataSourceId); return { content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; } catch (error) { logger.error(`Error executing adhoc query: ${error}`); return { isError: true, content: [ { type: "text", text: `Error executing adhoc query: ${error instanceof Error ? error.message : String(error)}` } ] }; } } - src/index.ts:471-475 (schema)Zod schema for execute_adhoc_query input validation (query string and dataSourceId number)
// Tool: execute_adhoc_query const executeAdhocQuerySchema = z.object({ query: z.string(), dataSourceId: z.coerce.number() }); - src/redashClient.ts:637-667 (handler)Core implementation: executes an ad-hoc SQL query via Redash API POST /api/query_results, supports async job polling
async executeAdhocQuery(query: string, dataSourceId: number): Promise<RedashQueryResult> { try { logger.info(`Executing adhoc query: ${query.substring(0, 100)}...`); // Prepare the request payload const payload = { query: query, data_source_id: dataSourceId, max_age: 0, // Force fresh results (no cache) apply_auto_limit: true, // Apply auto limit like in the web version parameters: {} }; logger.debug(`Sending adhoc query request: ${JSON.stringify(payload)}`); // Execute the query directly without creating a query object const response = await this.client.post('/api/query_results', payload); // Handle async execution if job is returned if (response.data.job) { logger.debug(`Query is being executed asynchronously, job ID: ${response.data.job.id}`); return await this.pollQueryResults(response.data.job.id); } return response.data; } catch (error) { logger.error(`Error executing adhoc query: ${error}`); throw new Error(`Failed to execute adhoc query: ${error instanceof Error ? error.message : String(error)}`); } } - src/index.ts:2380-2382 (registration)Dispatch handler for execute_adhoc_query in the CallToolRequestSchema handler
case "execute_adhoc_query": logger.debug(`Handling execute_adhoc_query`); return await executeAdhocQuery(executeAdhocQuerySchema.parse(args));