Skip to main content
Glama
Liu-creators

MySQL MCP

by Liu-creators

update_data

Update records in a MySQL table by specifying the table, field-value pairs for new data, and a WHERE condition with parameters. Supports optional database configuration.

Instructions

更新表中的数据

Args:
    table_name: 表名
    data: 要更新的数据,字段名和值的字典
    condition: WHERE条件子句
    params: 条件参数列表
    db_config: 数据库连接配置参数,如果为None则使用默认配置
    
Returns:
    包含更新结果的字典

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
table_nameYes
dataYes
conditionYes
paramsNo
db_configNo

Implementation Reference

  • The 'update_data' tool handler function decorated with @mcp.tool(). Updates rows in a MySQL table by building a SET clause from the data dict and applying a WHERE condition with parameterized queries.
    @mcp.tool()
    async def update_data(table_name: str, data: Dict[str, Any], condition: str, params: Optional[List[Any]] = None, db_config: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
        """更新表中的数据
        
        Args:
            table_name: 表名
            data: 要更新的数据,字段名和值的字典
            condition: WHERE条件子句
            params: 条件参数列表
            db_config: 数据库连接配置参数,如果为None则使用默认配置
            
        Returns:
            包含更新结果的字典
        """
        if not table_name or not data or not condition:
            return {"error": "表名、数据和条件不能为空"}
        
        if params is None:
            params = []
            
        try:
            conn = get_connection(db_config)
            cursor = conn.cursor()
            
            set_clause = ", ".join([f"{key} = %s" for key in data.keys()])
            values = list(data.values()) + params
            
            update_sql = f"UPDATE {table_name} SET {set_clause} WHERE {condition}"
            cursor.execute(update_sql, values)
            conn.commit()
            
            return {
                "success": True,
                "affected_rows": cursor.rowcount,
                "message": f"表 {table_name} 中的数据更新成功"
            }
        except Error as e:
            error_message = f"更新数据失败: {str(e)}"
            if "doesn't exist" in str(e):
                error_message += f"\n原因:表 {table_name} 不存在"
            elif "Unknown column" in str(e):
                error_message += "\n原因:更新数据中包含表中不存在的列"
            elif "cannot be null" in str(e):
                error_message += "\n原因:某个NOT NULL字段被设置为NULL值"
            elif "Duplicate entry" in str(e):
                error_message += "\n原因:更新后的数据违反了唯一键约束"
            elif "Data too long" in str(e):
                error_message += "\n原因:更新的数据超出了字段的长度限制"
            elif "syntax error" in str(e).lower():
                error_message += f"\n原因:WHERE条件 '{condition}' 存在语法错误"
            return {"error": error_message}
        except Exception as e:
            return {"error": f"更新数据时发生未知错误: {str(e)}"}
        finally:
            if 'conn' in locals() and conn.is_connected():
                cursor.close()
                conn.close()
  • mysql-mcp.py:351-351 (registration)
    The '@mcp.tool()' decorator that registers 'update_data' as an MCP tool.
    @mcp.tool()
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are present, and the description does not disclose behavioral traits like required permissions, the irreversible nature of updates, or side effects. It only states the return type ('包含更新结果的字典'), which is insufficient for an AI agent making safe decisions.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness3/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description uses a docstring format with bullet-like lines, which is acceptable but not optimally concise. Some details (e.g., default config) could be streamlined, and the structure is not highly scannable for AI.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

The description covers all 5 parameters but does not specify default behavior for db_config or error handling. With no output schema, the return value description ('包含更新结果的字典') is vague. Gaps remain in what constitutes a successful update.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 0%, but the description explains each parameter's purpose (e.g., '要更新的数据,字段名和值的字典' for data) beyond the schema's type information. This adds meaningful guidance, though it could be more precise about the condition format.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly uses the verb '更新' (update) and specifies the resource '表中的数据' (data in table), which distinguishes it from insertion or deletion. However, it does not explicitly differentiate from sibling tools like insert_data or delete_data, leaving room for confusion.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives (e.g., insert for new records, delete for removal). The description lacks context on prerequisites, such as whether the table must exist or what conditions are appropriate.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Liu-creators/mysql-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server