update_item
Modify specific attributes of an item in a DynamoDB table using defined update expressions, attribute mappings, and values, with optional conditions and return options.
Instructions
Updates specific attributes of an item in a table
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| conditionExpression | No | Condition for update (optional) | |
| expressionAttributeNames | Yes | Attribute name mappings | |
| expressionAttributeValues | Yes | Values for the update expression | |
| key | Yes | Primary key of the item to update | |
| returnValues | No | What values to return | |
| tableName | Yes | Name of the table | |
| updateExpression | Yes | Update expression (e.g., 'SET #n = :name') |
Implementation Reference
- src/index.ts:412-437 (handler)The handler function that executes the 'update_item' tool logic by constructing and sending an UpdateItemCommand to DynamoDB.async function updateItem(params: any) { try { const command = new UpdateItemCommand({ TableName: params.tableName, Key: marshall(params.key), UpdateExpression: params.updateExpression, ExpressionAttributeNames: params.expressionAttributeNames, ExpressionAttributeValues: marshall(params.expressionAttributeValues), ConditionExpression: params.conditionExpression, ReturnValues: params.returnValues || "NONE", }); const response = await dynamoClient.send(command); return { success: true, message: `Item updated successfully in table ${params.tableName}`, attributes: response.Attributes ? unmarshall(response.Attributes) : null, }; } catch (error) { console.error("Error updating item:", error); return { success: false, message: `Failed to update item: ${error}`, }; } }
- src/index.ts:130-146 (schema)Input schema and metadata definition for the 'update_item' tool.const UPDATE_ITEM_TOOL: Tool = { name: "update_item", description: "Updates specific attributes of an item in a table", inputSchema: { type: "object", properties: { tableName: { type: "string", description: "Name of the table" }, key: { type: "object", description: "Primary key of the item to update" }, updateExpression: { type: "string", description: "Update expression (e.g., 'SET #n = :name')" }, expressionAttributeNames: { type: "object", description: "Attribute name mappings" }, expressionAttributeValues: { type: "object", description: "Values for the update expression" }, conditionExpression: { type: "string", description: "Condition for update (optional)" }, returnValues: { type: "string", enum: ["NONE", "ALL_OLD", "UPDATED_OLD", "ALL_NEW", "UPDATED_NEW"], description: "What values to return" }, }, required: ["tableName", "key", "updateExpression", "expressionAttributeNames", "expressionAttributeValues"], }, };
- src/index.ts:598-600 (registration)Registration of the 'update_item' tool (as UPDATE_ITEM_TOOL) in the listTools response.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [CREATE_TABLE_TOOL, UPDATE_CAPACITY_TOOL, PUT_ITEM_TOOL, GET_ITEM_TOOL, QUERY_TABLE_TOOL, SCAN_TABLE_TOOL, DESCRIBE_TABLE_TOOL, LIST_TABLES_TOOL, CREATE_GSI_TOOL, UPDATE_GSI_TOOL, CREATE_LSI_TOOL, UPDATE_ITEM_TOOL], }));
- src/index.ts:623-625 (registration)Dispatch/registration of the 'update_item' tool handler in the CallToolRequest switch statement.case "update_item": result = await updateItem(args); break;