delete_data
Remove data from MySQL database tables by executing SQL DELETE FROM statements, facilitating precise data management through the MySQL MCP Server.
Instructions
从 MySQL 数据库表中删除数据
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | 要执行的 SQL DELETE FROM 语句 |
Input Schema (JSON Schema)
{
"properties": {
"query": {
"description": "要执行的 SQL DELETE FROM 语句",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/index.ts:426-476 (handler)The main handler function for the 'delete_data' tool. Validates input arguments, checks if query is DELETE FROM using isDeleteQuery helper, executes the query on MySQL connection pool, logs with transactionId, and returns formatted 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 (registration)Registration of the 'delete_data' tool in the ListTools response, including name, description, and input schema.{ name: 'delete_data', description: '从 MySQL 数据库表中删除数据', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL DELETE FROM 语句', }, }, required: ['query'], }, },
- src/index.ts:161-170 (schema)Input schema definition for the 'delete_data' tool, specifying the required 'query' parameter as a string.inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL DELETE FROM 语句', }, }, required: ['query'], },
- src/index.ts:48-49 (helper)Helper function to validate if the provided SQL query starts with 'delete from', used in the handler for input validation.const isDeleteQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('delete from');