update_data
Execute SQL UPDATE statements to modify data in MySQL database tables using the MySQL MCP Server, with results returned in JSON format.
Instructions
更新 MySQL 数据库表中的数据
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | 要执行的 SQL UPDATE 语句 |
Input Schema (JSON Schema)
{
"properties": {
"query": {
"description": "要执行的 SQL UPDATE 语句",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/index.ts:373-423 (handler)The core handler function for the 'update_data' MCP tool. It validates the input, ensures the SQL query starts with 'UPDATE', executes it using the MySQL pool, logs progress, and returns a standardized success or error response in MCP format.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:144-157 (registration)Registration of the 'update_data' tool in the MCP server's ListTools response, including name, description, and input schema definition.{ name: 'update_data', description: '更新 MySQL 数据库表中的数据', inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL UPDATE 语句', }, }, required: ['query'], }, },
- src/index.ts:147-156 (schema)Input schema definition for the 'update_data' tool, specifying a required 'query' string parameter for the SQL UPDATE statement.inputSchema: { type: 'object', properties: { query: { type: 'string', description: '要执行的 SQL UPDATE 语句', }, }, required: ['query'], },
- src/index.ts:44-45 (helper)Helper function used by the handler to validate that the provided SQL query is an UPDATE statement.const isUpdateQuery = (query: string): boolean => query.trim().toLowerCase().startsWith('update');
- src/index.ts:202-203 (registration)Dispatch case in the MCP CallToolRequestHandler switch statement that routes 'update_data' calls to the handleUpdateData method.case 'update_data': return this.handleUpdateData(request, transactionId);