Skip to main content
Glama

modify_table_cell

Edit or insert content into a specific table cell in a Microsoft Word document while preserving the existing non-header cell style. Specify file, table index, row, column, and content to update.

Instructions

Modify or add content to a specific table cell, following the style of existing non-header cells.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
columnYes
contentYes
filenameYes
rowYes
table_indexYes

Implementation Reference

  • The core handler function that implements the logic to modify a specific cell in a table within a Word document. It validates inputs, loads the document, accesses the table and cell, copies styling from other cells, sets the new content, and saves the document.
    async def modify_table_cell(filename: str, table_index: int, row: int, column: int, content: str) -> str:
        """Modify or add content to a specific table cell.
        
        Args:
            filename: Path to the Word document
            table_index: Index of the table (0-based)
            row: Row index (0-based)
            column: Column index (0-based)
            content: Text content to set in the cell
        """
        filename = ensure_docx_extension(filename)
        
        # Ensure numeric parameters are the correct type
        try:
            table_index = int(table_index)
            row = int(row)
            column = int(column)
        except (ValueError, TypeError):
            return "Invalid parameter: table_index, row, and column must be integers"
        
        if not os.path.exists(filename):
            return f"Document {filename} does not exist"
        
        # Check if file is writeable
        is_writeable, error_message = check_file_writeable(filename)
        if not is_writeable:
            return f"Cannot modify document: {error_message}. Consider creating a copy first."
        
        try:
            doc = Document(filename)
            
            # Validate table index
            if table_index < 0 or table_index >= len(doc.tables):
                return f"Invalid table index. Document has {len(doc.tables)} tables (0-{len(doc.tables)-1})."
            
            table = doc.tables[table_index]
            
            # Validate row and column indices
            if row < 0 or row >= len(table.rows):
                return f"Invalid row index. Table has {len(table.rows)} rows (0-{len(table.rows)-1})."
            
            if column < 0 or column >= len(table.columns):
                return f"Invalid column index. Table has {len(table.columns)} columns (0-{len(table.columns)-1})."
            
            # Get the target cell
            cell = table.cell(row, column)
            
            # Find a reference cell to copy style from (preferably a non-header cell)
            reference_cell = None
            reference_style = None
            
            # Look for a non-header cell in the same table to copy style from
            for r in range(len(table.rows)):
                for c in range(len(table.columns)):
                    if r != row or c != column:  # Don't use the target cell itself
                        try:
                            ref_cell = table.cell(r, c)
                            if ref_cell.text.strip():  # Find a cell with content
                                reference_cell = ref_cell
                                break
                        except IndexError:
                            continue
                if reference_cell:
                    break
            
            # If no reference cell found, use the first cell with content
            if not reference_cell:
                for r in range(len(table.rows)):
                    for c in range(len(table.columns)):
                        try:
                            ref_cell = table.cell(r, c)
                            if ref_cell.text.strip():
                                reference_cell = ref_cell
                                break
                        except IndexError:
                            continue
                    if reference_cell:
                        break
            
            # Clear existing content and add new content
            cell.text = content
            
            # If we found a reference cell, copy its formatting
            if reference_cell and reference_cell.paragraphs:
                ref_paragraph = reference_cell.paragraphs[0]
                if ref_paragraph.runs:
                    ref_run = ref_paragraph.runs[0]
                    
                    # Apply the same formatting to the new content
                    if cell.paragraphs:
                        target_paragraph = cell.paragraphs[0]
                        if target_paragraph.runs:
                            target_run = target_paragraph.runs[0]
                            
                            # Copy font properties
                            if hasattr(ref_run.font, 'bold') and ref_run.font.bold is not None:
                                target_run.font.bold = ref_run.font.bold
                            if hasattr(ref_run.font, 'italic') and ref_run.font.italic is not None:
                                target_run.font.italic = ref_run.font.italic
                            if hasattr(ref_run.font, 'underline') and ref_run.font.underline is not None:
                                target_run.font.underline = ref_run.font.underline
                            if hasattr(ref_run.font, 'size') and ref_run.font.size is not None:
                                target_run.font.size = ref_run.font.size
                            if hasattr(ref_run.font, 'name') and ref_run.font.name is not None:
                                target_run.font.name = ref_run.font.name
                            if hasattr(ref_run.font, 'color') and ref_run.font.color.rgb is not None:
                                target_run.font.color.rgb = ref_run.font.color.rgb
            
            doc.save(filename)
            return f"Cell content updated successfully at table {table_index}, row {row}, column {column}."
        except Exception as e:
            return f"Failed to modify table cell: {str(e)}"
  • The MCP tool registration using @mcp.tool() decorator. This wrapper function defines the tool interface and delegates to the actual implementation imported as modify_table_cell_func.
    @mcp.tool()
    async def modify_table_cell(filename: str, table_index: int, row: int, column: int, content: str):
        """Modify or add content to a specific table cell, following the style of existing non-header cells."""
        return await modify_table_cell_func(filename, table_index, row, column, content)
  • Import of the handler function from content_tools.py, aliased for use in the registration wrapper.
    from word_document_server.tools.content_tools import modify_table_cell as modify_table_cell_func
Behavior2/5

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

With no annotations, the description carries full burden but provides minimal behavioral context. It mentions style adherence but lacks details on permissions, whether modifications are destructive/reversible, error handling (e.g., invalid cell coordinates), or output expectations. This is inadequate for a mutation tool with zero annotation coverage.

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

Conciseness4/5

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

The description is a single, efficient sentence that front-loads the core action. However, it could be more structured by separating purpose from constraints, and some words like 'specific' are redundant given the parameter names.

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

Completeness2/5

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

For a 5-parameter mutation tool with no annotations and no output schema, the description is incomplete. It lacks crucial context: behavioral traits (e.g., side effects), parameter meanings, error cases, and output format. The style constraint is helpful but insufficient for safe and effective use.

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

Parameters3/5

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

Schema description coverage is 0%, so the description must compensate but adds no parameter-specific information. It implies parameters for targeting (table, row, column) and content, but doesn't explain semantics like coordinate systems, content formatting, or filename requirements. Baseline 3 is not met due to lack of compensation for poor schema coverage.

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 states the action ('Modify or add content') and target ('specific table cell'), with the qualifier 'following the style of existing non-header cells' adding specificity. It distinguishes from obvious siblings like 'add_table' or 'delete_table', though not explicitly compared to similar tools like 'format_table'.

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 explicit guidance on when to use this tool versus alternatives is provided. The description implies usage for table cell editing but doesn't mention prerequisites (e.g., document must exist), exclusions (e.g., cannot modify header cells), or compare to siblings like 'format_table' for style changes versus content updates.

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

Related 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/franlealp1/mcp-word'

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