delete_data
Remove records from MySQL database tables using SQL DELETE statements to manage data cleanup and maintenance.
Instructions
从 MySQL 数据库表中删除数据
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | 要执行的 SQL DELETE FROM 语句 |
Implementation Reference
- src/index.ts:426-476 (handler)Implements the core logic for the 'delete_data' tool: validates input arguments, checks if the SQL query starts with 'DELETE FROM', executes the query on the MySQL connection pool, logs the transaction, handles errors, and returns a structured success or error response.private async handleDeleteData(request: any, transactionId: string) { if (!isValidSqlQueryArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'SQL 查询参数无效。' ); } const query = request.params.arguments.query; if (!isDeleteQuery(query)) { throw new McpError( ErrorCode.InvalidParams, 'delete_data 工具仅允许 DELETE FROM 查询。' ); } console.error(`[${transactionId}] 执行 DELETE 查询: ${query}`); try { const [result] = await this.pool.query(query); console.error(`[${transactionId}] 数据删除成功`); return { content: [ { type: 'text', text: JSON.stringify({ success: true, message: '数据删除成功', result }, null, 2), }, ], }; } catch (error) { console.error(`[${transactionId}] 查询出错:`, error); if (error instanceof Error) { return { content: [ { type: 'text', text: `MySQL 错误: ${error.message}`, }, ], isError: true, }; } throw error; } }
- src/index.ts:158-171 (schema)Defines the metadata (name, description) and input schema (object with required 'query' string field) for the 'delete_data' tool, returned in response to list_tools requests.{ name: 'delete_data', description: '从 MySQL 数据库表中删除数据', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL DELETE FROM 语句', }, }, required: ['query'], }, },
- src/index.ts:204-205 (registration)Registers the dispatching of 'delete_data' tool calls to the specific handleDeleteData handler function within the CallToolRequestSchema request handler switch statement.case 'delete_data': return this.handleDeleteData(request, transactionId);
- src/index.ts:48-49 (helper)Helper function specifically used by the delete_data handler to validate that the provided SQL query is a DELETE FROM statement.const isDeleteQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('delete from');