get_all_comments
Extract all comments from a Word document for review or analysis. This tool simplifies identifying and accessing user annotations within the file.
Instructions
Extract all comments from a Word document.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes |
Input Schema (JSON Schema)
{
"properties": {
"filename": {
"title": "Filename",
"type": "string"
}
},
"required": [
"filename"
],
"type": "object"
}
Implementation Reference
- word_document_server/main.py:403-406 (registration)MCP tool registration for 'get_all_comments' using FastMCP 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)
- Primary handler function that loads the Word document, extracts comments using core utility, handles errors, and returns formatted JSON 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)
- Core helper function that extracts all comments by accessing the document's comments XML part via relationships and XPath, with fallback to paragraph scanning.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