Skip to main content
Glama

protect_document

Add a password to secure a Word document, ensuring only authorized users can access its content. Ideal for safeguarding sensitive information.

Instructions

Add password protection to a Word document.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
passwordYes

Implementation Reference

  • The core handler function that implements password-based encryption protection for Word documents using msoffcrypto library. Handles file reading, encryption, writing back, error recovery, and metadata cleanup.
    async def protect_document(filename: str, password: str) -> str:
        """Add password protection to a Word document.
    
        Args:
            filename: Path to the Word document
            password: Password to protect the document with
        """
        filename = ensure_docx_extension(filename)
    
        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 protect document: {error_message}"
    
        try:
            # Read the original file content
            with open(filename, "rb") as infile:
                original_data = infile.read()
    
            # Create an msoffcrypto file object from the original data
            file = msoffcrypto.OfficeFile(io.BytesIO(original_data))
            file.load_key(password=password) # Set the password for encryption
    
            # Encrypt the data into an in-memory buffer
            encrypted_data_io = io.BytesIO()
            
            file.encrypt(password=password, outfile=encrypted_data_io) 
    
            # Overwrite the original file with the encrypted data
            with open(filename, "wb") as outfile:
                outfile.write(encrypted_data_io.getvalue())
    
            
            base_path, _ = os.path.splitext(filename)
            metadata_path = f"{base_path}.protection"
            if os.path.exists(metadata_path):
                os.remove(metadata_path)
    
            return f"Document {filename} encrypted successfully with password."
    
        except Exception as e:
            # Attempt to restore original file content on failure
            try:
                if 'original_data' in locals():
                    with open(filename, "wb") as outfile:
                        outfile.write(original_data)
                    return f"Failed to encrypt document {filename}: {str(e)}. Original file restored."
                else:
                     return f"Failed to encrypt document {filename}: {str(e)}. Could not restore original file."
            except Exception as restore_e:
                 return f"Failed to encrypt document {filename}: {str(e)}. Also failed to restore original file: {str(restore_e)}"
  • MCP tool registration in the main server file. Defines the tool entrypoint with @mcp.tool() decorator and delegates execution to the implementation in protection_tools.py.
    @mcp.tool()
    async def protect_document(filename: str, password: str):
        """Add password protection to a Word document."""
        return await protection_tools.protect_document(filename, password)
  • Import statement exposing the protect_document function from protection_tools.py for use in the tools package, facilitating access in main.py.
    from word_document_server.tools.protection_tools import (
        protect_document, add_restricted_editing,
        add_digital_signature, verify_document
    )
Behavior2/5

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

With no annotations provided, the description carries full burden for behavioral disclosure. It states the action ('Add password protection') which implies a write/mutation operation, but doesn't describe what happens to the original document (e.g., overwritten vs. new copy), whether the operation is reversible, what permissions are needed, or error conditions. For a security-related mutation tool with zero annotation coverage, this is insufficient.

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

Conciseness5/5

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

The description is a single, efficient sentence that states the core function without unnecessary words. It's appropriately sized for a straightforward tool and front-loads the essential information. Every word earns its place in conveying the basic purpose.

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?

Given a mutation tool with security implications, no annotations, no output schema, and 0% schema description coverage for 2 parameters, the description is incomplete. It covers the basic purpose but lacks crucial context about behavior, parameters, outcomes, and error handling that an agent needs to use this tool effectively and safely.

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

Parameters2/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 information. It doesn't explain what 'filename' refers to (path, name in storage, etc.), format requirements for the password, or constraints on either parameter. With 2 required parameters and no schema descriptions, the description fails to provide needed semantic context.

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 ('Add password protection') and target resource ('to a Word document'), making the purpose immediately understandable. It distinguishes from sibling tools like 'unprotect_document' by specifying the opposite operation. However, it doesn't explicitly differentiate from other document modification tools like 'format_text' or 'convert_to_pdf' beyond the specific protection function.

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?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., document must exist), when not to use it (e.g., if document is already protected), or direct alternatives among siblings like 'unprotect_document' for removing protection. The agent must infer usage from the tool name alone.

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