set_cell_value
Update spreadsheet cell values using row and column coordinates to modify CSV data programmatically. Specify target cell by index or name and set new value with automatic change tracking.
Instructions
Set value of specific cell with coordinate targeting.
Supports column name or index, tracks old and new values. Returns operation result with coordinates and data type.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| row_index | Yes | Row index (0-based) to update cell in | |
| column | Yes | Column name or column index (0-based) to update | |
| value | Yes | New value to set in the cell (str, int, float, bool, or None) |
Implementation Reference
- The core handler function implementing the set_cell_value tool. It validates row and column indices, retrieves old value, handles type conversion for numeric columns, updates the pandas DataFrame cell, captures new value, and returns a SetCellResult with coordinates, old/new values, and data type.def set_cell_value( ctx: Annotated[Context, Field(description="FastMCP context for session access")], row_index: Annotated[int, Field(description="Row index (0-based) to update cell in")], column: Annotated[ str | int, Field(description="Column name or column index (0-based) to update"), ], value: Annotated[ CellValue, Field(description="New value to set in the cell (str, int, float, bool, or None)"), ], ) -> SetCellResult: """Set value of specific cell with coordinate targeting. Supports column name or index, tracks old and new values. Returns operation result with coordinates and data type. """ session_id = ctx.session_id _session, df = get_session_data(session_id) # Validate row index if row_index < 0 or row_index >= len(df): msg = f"Row index {row_index} out of range (0-{len(df) - 1})" raise ToolError(msg) # Handle column specification if isinstance(column, int): # Column index if column < 0 or column >= len(df.columns): msg = f"Column index {column} out of range (0-{len(df.columns) - 1})" raise ToolError(msg) column_name = df.columns[column] else: # Column name if column not in df.columns: raise ColumnNotFoundError(column, list(df.columns)) column_name = column # Get the old value for tracking old_value = df.iloc[row_index, df.columns.get_loc(column_name)] # type: ignore[index] if pd.isna(old_value): old_value = None elif hasattr(old_value, "item"): # numpy scalar old_value = old_value.item() # type: ignore[assignment] # Set the new value with explicit type conversion to avoid dtype compatibility warnings col_idx = df.columns.get_loc(column_name) current_dtype = df[column_name].dtype # Access dtype through column name instead # Convert value to match column dtype if possible converted_value: CellValue try: if pd.api.types.is_numeric_dtype(current_dtype) and isinstance(value, str): numeric_result = pd.to_numeric(value, errors="coerce") # Convert pandas numeric to Python type for CellValue compatibility if pd.isna(numeric_result): converted_value = None else: converted_value = ( float(numeric_result) if isinstance(numeric_result, (int, float)) else numeric_result.item() ) else: converted_value = value except (ValueError, TypeError): converted_value = value df.iloc[row_index, col_idx] = converted_value # type: ignore[index] # Get the new value for tracking (after pandas type conversion) new_value = df.iloc[row_index, df.columns.get_loc(column_name)] # type: ignore[index] if pd.isna(new_value): new_value = None elif hasattr(new_value, "item"): # numpy scalar new_value = new_value.item() # type: ignore[assignment] # Get column data type data_type = str(df[column_name].dtype) # No longer recording operations (simplified MCP architecture) return SetCellResult( coordinates={"row": row_index, "column": column_name}, old_value=old_value, new_value=new_value, data_type=data_type, )
- src/databeak/servers/row_operations_server.py:639-651 (registration)Registration of the set_cell_value tool (line 646) along with other row operation tools on the FastMCP row_operations_server instance.row_operations_server = FastMCP( "DataBeak-RowOperations", instructions="Row operations server for DataBeak", ) # Register functions directly as MCP tools (no wrapper functions needed) row_operations_server.tool(name="get_cell_value")(get_cell_value) row_operations_server.tool(name="set_cell_value")(set_cell_value) row_operations_server.tool(name="get_row_data")(get_row_data) row_operations_server.tool(name="get_column_data")(get_column_data) row_operations_server.tool(name="insert_row")(insert_row) row_operations_server.tool(name="delete_row")(delete_row) row_operations_server.tool(name="update_row")(update_row)
- Pydantic model defining the output schema for the set_cell_value tool, including coordinates, old_value, new_value, and data_type.class SetCellResult(BaseToolResponse): """Response model for cell update operations.""" coordinates: dict[str, str | int] = Field( description="Cell coordinates with row index and column name", ) old_value: str | int | float | bool | None = Field( description="Previous cell value before update", ) new_value: str | int | float | bool | None = Field(description="New cell value after update") data_type: str = Field(description="Pandas data type of the column")