execute
Run INSERT, UPDATE, or DELETE queries on MySQL databases. Provide SQL statements and optional parameters for precise database modifications using a secure connection.
Instructions
Execute an INSERT, UPDATE, or DELETE query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| params | No | An optional array of parameters (as strings) to bind to the SQL query placeholders (e.g., ?). | |
| sql | Yes | The SQL query string (INSERT, UPDATE, DELETE) to execute. |
Implementation Reference
- src/index.ts:627-637 (handler)Core handler function for the 'execute' tool. Validates that the SQL is an INSERT, UPDATE, or DELETE statement and executes it using the executeQuery helper, returning the result as formatted JSON.private async handleExecute(args: QueryArgs): Promise<QueryResult> { this.validateSqlInput(args.sql, ['INSERT', 'UPDATE', 'DELETE']); const result = await this.executeQuery(args.sql, args.params || []); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; }
- src/index.ts:444-466 (registration)Registers the 'execute' tool in the ListToolsRequestSchema handler, providing name, description, and input schema for tool discovery.{ name: 'execute', description: 'Execute an INSERT, UPDATE, or DELETE query', inputSchema: { type: 'object', properties: { sql: { type: 'string', description: 'The SQL query string (INSERT, UPDATE, DELETE) to execute.', }, params: { type: 'array', items: { type: 'string', description: 'A parameter value (as string) to bind to the query.' }, description: 'An optional array of parameters (as strings) to bind to the SQL query placeholders (e.g., ?).', optional: true, }, }, required: ['sql'], }, },
- src/index.ts:575-576 (registration)Dispatches incoming CallTool requests for 'execute' to the handleExecute method.case 'execute': return await this.handleExecute(request.params.arguments as unknown as QueryArgs);
- src/index.ts:243-251 (helper)Helper method that performs the actual database query execution using the MySQL pool, with error handling.private async executeQuery<T>(sql: string, params: any[] = []): Promise<T> { const pool = await this.ensureConnection(); try { const [result] = await pool.query(sql, params); return result as T; } catch (error) { this.handleDatabaseError(error); } }
- src/index.ts:65-68 (schema)TypeScript interface defining the input arguments for the 'execute' tool (and 'query' tool).interface QueryArgs { sql: string; params?: Array<string | number | boolean | null>; }