add_image_caption
Add captions to images in Word documents by specifying image index, text, and type (Figure, Table, or Equation) for better document organization and accessibility.
Instructions
Add a caption to an image in the document.
Args: filepath: Path to the document image_index: Index of the image (0-based) caption_text: Caption text caption_type: Type of caption (Figure, Table, Equation)
Returns: Dictionary with status
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filepath | Yes | ||
| image_index | Yes | ||
| caption_text | Yes | ||
| caption_type | No | Figure |
Implementation Reference
- src/docx_mcp/server.py:929-975 (handler)The implementation of the add_image_caption tool, which adds a caption to an image in a Word document.
def add_image_caption( filepath: str, image_index: int, caption_text: str, caption_type: str = "Figure", ) -> dict[str, Any]: """ Add a caption to an image in the document. Args: filepath: Path to the document image_index: Index of the image (0-based) caption_text: Caption text caption_type: Type of caption (Figure, Table, Equation) Returns: Dictionary with status """ logger.info("Adding image caption", extra={"tool": "add_image_caption", "filepath": filepath}) try: doc = safe_open_document(filepath) # Find image location (simplified - assumes last paragraph with image) # In a production system, would need more robust image tracking if len(doc.paragraphs) > 0: # Add caption paragraph after last paragraph caption_paragraph = doc.add_paragraph() caption_run = caption_paragraph.add_run(f"{caption_type}: {caption_text}") caption_run.italic = True caption_paragraph.style = "Caption" safe_save_document(doc, filepath) logger.info("Caption added successfully", extra={"filepath": filepath}) return { "status": "success", "filepath": filepath, "caption": caption_text, "message": "Caption added successfully", } else: raise DocumentError("Document has no paragraphs", filepath) except DocxMcpError as e: logger.warning(e.message, extra={"tool": "add_image_caption", "error_code": e.error_code}) return {"status": "error", "error": e.message, "error_code": e.error_code} except Exception as e: - src/docx_mcp/server.py:928-928 (registration)The registration of the add_image_caption tool using the @app.tool() decorator.
@app.tool()