update_data
Execute SQL UPDATE statements to modify existing records in MySQL database tables, enabling data correction and maintenance operations.
Instructions
更新 MySQL 数据库表中的数据
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | 要执行的 SQL UPDATE 语句 |
Implementation Reference
- src/index.ts:373-422 (handler)The handleUpdateData method implements the core logic for the 'update_data' tool: validates the input arguments, ensures the query is an UPDATE statement, executes it using the MySQL connection pool, logs the transaction, and returns a formatted success or error response.private async handleUpdateData(request: any, transactionId: string) { if (!isValidSqlQueryArgs(request.params.arguments)) { throw new McpError( ErrorCode.InvalidParams, 'SQL 查询参数无效。' ); } const query = request.params.arguments.query; if (!isUpdateQuery(query)) { throw new McpError( ErrorCode.InvalidParams, 'update_data 工具仅允许 UPDATE 查询。' ); } console.error(`[${transactionId}] 执行 UPDATE 查询: ${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:147-155 (schema)The inputSchema defines the expected parameters for the 'update_data' tool: an object with a required 'query' string property containing the SQL UPDATE statement.inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL UPDATE 语句', }, }, required: ['query'],
- src/index.ts:144-157 (registration)The tool descriptor for 'update_data' is registered in the ListTools response, providing the name, description, and input schema.{ name: 'update_data', description: '更新 MySQL 数据库表中的数据', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL UPDATE 语句', }, }, required: ['query'], }, },
- src/index.ts:202-203 (registration)Dispatch in the CallToolRequestSchema handler routes 'update_data' calls to the handleUpdateData method.case 'update_data': return this.handleUpdateData(request, transactionId);
- src/index.ts:44-45 (helper)Helper function that checks if a given SQL query starts with 'update' (case-insensitive), used for validation in the update_data handler.const isUpdateQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('update');