run-raw-sql
Execute raw SQL queries with parameters to directly interact with database tables. Admin access required for data modification operations.
Instructions
Execute raw SQL query with optional parameters. Admin access required. Use with caution as it can modify data directly.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| apiKey | No | API key for authentication (optional if provided via --api_key) | |
| params | No | ||
| query | Yes |
Implementation Reference
- src/shared/tools.ts:463-503 (handler)The core handler function for the 'run-raw-sql' MCP tool. It constructs a RawSQLRequest, sends a POST to the backend API endpoint `/api/database/advance/rawsql`, processes the response with handleApiResponse, adds background context, and handles errors appropriately.withUsageTracking('run-raw-sql', async ({ apiKey, query, params }) => { try { const actualApiKey = getApiKey(apiKey); const requestBody: RawSQLRequest = { query, params: params || [], }; const response = await fetch(`${API_BASE_URL}/api/database/advance/rawsql`, { method: 'POST', headers: { 'x-api-key': actualApiKey, 'Content-Type': 'application/json', }, body: JSON.stringify(requestBody), }); const result = await handleApiResponse(response); return await addBackgroundContext({ content: [ { type: 'text', text: formatSuccessMessage('SQL query executed', result), }, ], }); } catch (error) { const errMsg = error instanceof Error ? error.message : 'Unknown error occurred'; return { content: [ { type: 'text', text: `Error executing SQL query: ${errMsg}`, }, ], isError: true, }; } })
- src/shared/tools.ts:453-504 (registration)The MCP server.tool registration for 'run-raw-sql', including description, input schema, and wrapped handler.server.tool( 'run-raw-sql', 'Execute raw SQL query with optional parameters. Admin access required. Use with caution as it can modify data directly.', { apiKey: z .string() .optional() .describe('API key for authentication (optional if provided via --api_key)'), ...rawSQLRequestSchema.shape, }, withUsageTracking('run-raw-sql', async ({ apiKey, query, params }) => { try { const actualApiKey = getApiKey(apiKey); const requestBody: RawSQLRequest = { query, params: params || [], }; const response = await fetch(`${API_BASE_URL}/api/database/advance/rawsql`, { method: 'POST', headers: { 'x-api-key': actualApiKey, 'Content-Type': 'application/json', }, body: JSON.stringify(requestBody), }); const result = await handleApiResponse(response); return await addBackgroundContext({ content: [ { type: 'text', text: formatSuccessMessage('SQL query executed', result), }, ], }); } catch (error) { const errMsg = error instanceof Error ? error.message : 'Unknown error occurred'; return { content: [ { type: 'text', text: `Error executing SQL query: ${errMsg}`, }, ], isError: true, }; } }) );
- src/shared/tools.ts:456-462 (schema)Zod input schema for the tool: optional apiKey and spread from rawSQLRequestSchema (provides 'query' string and optional 'params' array). rawSQLRequestSchema imported from '@insforge/shared-schemas'.{ apiKey: z .string() .optional() .describe('API key for authentication (optional if provided via --api_key)'), ...rawSQLRequestSchema.shape, },
- src/shared/tools.ts:10-14 (schema)Import of rawSQLRequestSchema and RawSQLRequest type used in the tool's input schema and request body.import { CreateBucketRequest, createBucketRequestSchema, rawSQLRequestSchema, RawSQLRequest,
- src/shared/tools.ts:174-188 (helper)Helper wrapper function that adds usage tracking around tool handlers, used for 'run-raw-sql'.function withUsageTracking<T extends unknown[], R>( toolName: string, handler: (...args: T) => Promise<R> ): (...args: T) => Promise<R> { return async (...args: T): Promise<R> => { try { const result = await handler(...args); await trackToolUsage(toolName, true); return result; } catch (error) { await trackToolUsage(toolName, false); throw error; } }; }