run-raw-sql
Execute raw SQL queries with optional parameters to directly modify database data. Requires admin access and caution due to direct database interaction.
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) | |
| query | Yes | ||
| params | No |
Implementation Reference
- src/shared/tools.ts:460-500 (handler)The handler function for the 'run-raw-sql' tool. It authenticates using the API key, constructs a RawSQLRequest with the provided query and parameters, sends a POST request to the backend API endpoint `/api/database/advance/rawsql`, handles the response, adds background context if applicable, and returns formatted success/error messages.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-459 (schema)Input parameters schema defined using Zod. Includes an optional 'apiKey' field and spreads the shape of 'rawSQLRequestSchema' which provides 'query' (string) and 'params' (array) fields for the SQL execution.{ apiKey: z .string() .optional() .describe('API key for authentication (optional if provided via --api_key)'), ...rawSQLRequestSchema.shape, },
- src/shared/tools.ts:450-501 (registration)Registration of the 'run-raw-sql' tool on the MCP server using server.tool(). Specifies the tool name, description, input schema, and references the wrapped handler function with usage tracking.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, }; } }) );