get_all_comments
Extract all comments from a Microsoft Word document to review feedback, track changes, or analyze document collaboration.
Instructions
Extract all comments from a Word document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes |
Implementation Reference
- Primary handler function for the get_all_comments tool. Loads the Word document, extracts all comments using the core helper, handles errors, and returns a JSON-formatted response.async def get_all_comments(filename: str) -> str: """ Extract all comments from a Word document. Args: filename: Path to the Word document Returns: JSON string containing all comments with metadata """ filename = ensure_docx_extension(filename) if not os.path.exists(filename): return json.dumps({ 'success': False, 'error': f'Document {filename} does not exist' }, indent=2) try: # Load the document doc = Document(filename) # Extract all comments comments = extract_all_comments(doc) # Return results return json.dumps({ 'success': True, 'comments': comments, 'total_comments': len(comments) }, indent=2) except Exception as e: return json.dumps({ 'success': False, 'error': f'Failed to extract comments: {str(e)}' }, indent=2)
- word_document_server/main.py:403-406 (registration)MCP tool registration using @mcp.tool() decorator. This is the entry point handler that delegates to the implementation in comment_tools.@mcp.tool() def get_all_comments(filename: str): """Extract all comments from a Word document.""" return comment_tools.get_all_comments(filename)
- Core helper function that performs the low-level extraction of all comments from the docx Document object, handling both direct comments part and fallback methods.def extract_all_comments(doc: DocumentType) -> List[Dict[str, Any]]: """ Extract all comments from a Word document. Args: doc: The Document object to extract comments from Returns: List of dictionaries containing comment information """ comments = [] # Access the document's comment part if it exists try: # Get the document part document_part = doc.part # Find comments part through relationships comments_part = None for rel_id, rel in document_part.rels.items(): if 'comments' in rel.reltype and 'comments' == rel.reltype.split('/')[-1]: comments_part = rel.target_part break if comments_part: # Extract comments from the comments part using proper xpath syntax comment_elements = comments_part.element.xpath('.//w:comment') for idx, comment_element in enumerate(comment_elements): comment_data = extract_comment_data(comment_element, idx) if comment_data: comments.append(comment_data) # If no comments found, try alternative approach if not comments: # Fallback: scan paragraphs for comment references comments = extract_comments_from_paragraphs(doc) except Exception as e: # If direct access fails, try alternative approach comments = extract_comments_from_paragraphs(doc) return comments
- Export of the get_all_comments function from comment_tools module, making it available for import.from word_document_server.tools.comment_tools import ( get_all_comments, get_comments_by_author, get_comments_for_paragraph )