Skip to main content
Glama

list_merge_fields

Extract merge field names from Word documents to identify template variables for data integration and document automation workflows.

Instructions

Extract all MERGEFIELD names from a document or template.

Args: filepath: Path to the document

Returns: Dictionary with list of merge field names

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filepathYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The `list_merge_fields` tool implementation which uses `safe_open_document` to load the document and parses the XML structure to find `MERGEFIELD` field codes.
    @app.tool()
    def list_merge_fields(filepath: str) -> dict[str, Any]:
        """
        Extract all MERGEFIELD names from a document or template.
    
        Args:
            filepath: Path to the document
    
        Returns:
            Dictionary with list of merge field names
        """
        logger.info("Listing merge fields", extra={"tool": "list_merge_fields", "filepath": filepath})
    
        try:
            doc = safe_open_document(filepath)
            merge_fields = set()
    
            # Search for MERGEFIELD fields in document
            for paragraph in doc.paragraphs:
                for run in paragraph.runs:
                    # Look for field codes
                    if run.element.xpath(".//w:instrText"):
                        for instr in run.element.xpath(".//w:instrText"):
                            text = instr.text or ""
                            if "MERGEFIELD" in text:
                                # Extract field name
                                parts = text.split()
                                if len(parts) > 1:
                                    merge_fields.add(parts[1])
    
            return {
                "status": "success",
                "filepath": filepath,
                "merge_fields": list(sorted(merge_fields)),
                "count": len(merge_fields),
            }
        except DocxMcpError as e:
            logger.warning(e.message, extra={"tool": "list_merge_fields", "error_code": e.error_code})
            return {"status": "error", "error": e.message, "error_code": e.error_code}
        except Exception as e:
            logger.error(f"Unexpected error listing merge fields: {str(e)}")
            return {"status": "error", "error": str(e)}
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 operation is an extraction (read-only) and mentions the return format (dictionary), but lacks critical details: error handling (e.g., invalid filepath), performance characteristics (e.g., large document processing), authentication needs, or whether it modifies the document. For a tool with zero annotation coverage, this leaves significant behavioral gaps.

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 extremely concise and well-structured: a clear purpose statement followed by dedicated 'Args' and 'Returns' sections. Every sentence earns its place—no redundant information, no fluff. The front-loaded purpose statement immediately communicates the tool's function.

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

Completeness3/5

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

Given the tool's moderate complexity (single parameter, read operation) and the presence of an output schema (which handles return value documentation), the description is minimally adequate. However, with no annotations and incomplete parameter semantics, it lacks sufficient context for safe, informed use—particularly around error conditions and behavioral constraints that aren't covered by structured fields.

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

Parameters4/5

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

The schema has 0% description coverage, so the description must compensate. It explicitly documents the single parameter ('filepath: Path to the document'), adding clear semantics beyond the bare schema. However, it doesn't specify format details (e.g., absolute/relative paths, supported file extensions) or constraints (e.g., file must exist). Given the low schema coverage, this provides good but incomplete parameter context.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Extract all MERGEFIELD names') and target resource ('from a document or template'), distinguishing it from sibling tools like 'fill_merge_fields' (which modifies merge fields) and 'list_content_controls' (which lists different document elements). The verb 'extract' precisely conveys the read-only nature of the operation.

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), exclusions (e.g., file format limitations), or comparisons with related tools like 'get_document_properties' or 'list_content_controls'. The agent receives no contextual usage instructions.

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

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/Andrew82106/LLM_Docx_Agent_MCP'

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