Skip to main content
Glama

extract_form_fields

Extract form fields and their properties from PDF documents to access structured data for processing or analysis.

Instructions

Extract all form fields from a PDF

Args:
    pdf_path: Path to the PDF file

Returns:
    Dictionary of form field names and their properties

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pdf_pathYes

Implementation Reference

  • The core handler implementation for the 'extract_form_fields' tool. Decorated with @mcp.tool() for automatic registration and schema generation from type hints and docstring. Extracts form fields from PDF using PyMuPDF, with special logic for radio button and choice field options.
    @mcp.tool()
    def extract_form_fields(pdf_path: str) -> Dict[str, Any]:
        """
        Extract all form fields from a PDF
    
        Args:
            pdf_path: Path to the PDF file
    
        Returns:
            Dictionary of form field names and their properties
        """
        try:
            doc = fitz.open(pdf_path)
            result = {}
            radio_button_options = {}  # To collect radio button states
    
            # First pass: collect all radio button options
            for page in doc:
                for widget in page.widgets():
                    field_name = widget.field_name
                    field_type = widget.field_type
                    
                    # Collect radio button options
                    if field_type == 5:  # RadioButton
                        if field_name not in radio_button_options:
                            radio_button_options[field_name] = set()
                        
                        try:
                            # Get button states from the widget
                            states = widget.button_states()
                            if states and 'normal' in states:
                                # Add all non-'Off' options to our set
                                for state in states['normal']:
                                    if state != 'Off':
                                        # Replace HTML entity codes with actual characters
                                        option = state.replace('#20', ' ')
                                        radio_button_options[field_name].add(option)
                        except Exception:
                            pass
    
            # Second pass: extract all form fields
            for page in doc:
                for widget in page.widgets():
                    field_name = widget.field_name
                    field_value = widget.field_value
                    field_type = widget.field_type
                    field_type_name = widget.field_type_string
    
                    field_info = {
                        "type": field_type_name.lower(),
                        "value": field_value,
                        "field_type_id": field_type,
                    }
                    
                    # Add radio button options
                    if field_type == 5 and field_name in radio_button_options:
                        options = list(radio_button_options[field_name])
                        if options:
                            field_info["options"] = options
                    
                    # Add choice field options (combobox, listbox)
                    elif field_type == 3:  # Choice field
                        try:
                            # Get the field options
                            field_options = widget.choice_values
                            if field_options:
                                field_info["options"] = field_options
                        except AttributeError:
                            pass
    
                    # Only add if not already in results
                    if field_name not in result:
                        result[field_name] = field_info
    
            doc.close()
            return result
        except Exception as e:
            return {"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 what the tool does but doesn't describe important behaviors: whether it works with encrypted PDFs, what happens with malformed PDFs, if there are size limitations, what 'properties' are included in the return dictionary, or error conditions. The description is minimal and lacks operational context.

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 perfectly concise and well-structured: a clear purpose statement followed by Args and Returns sections. Every sentence earns its place - the purpose is front-loaded, and the parameter/return information is efficiently presented without redundancy. No wasted words.

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 no annotations, no output schema, and 0% schema description coverage, the description is incomplete. It doesn't explain what 'properties' are included in the return dictionary, how nested form fields are handled, what happens with empty forms, or error scenarios. For a tool that extracts structured data from PDFs, more context about the output format and limitations is needed.

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

Parameters3/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. It adds the meaning of 'pdf_path' as 'Path to the PDF file', which provides basic semantics beyond the schema's title 'Pdf Path'. However, it doesn't elaborate on path format (absolute/relative), supported file systems, or expected PDF characteristics. With only one parameter, this is adequate but not comprehensive.

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 tool's purpose as 'Extract all form fields from a PDF' - a specific verb ('extract') and resource ('form fields from a PDF'). It distinguishes from sibling tools like 'extract_text' (extracts text content) and 'highlight_form_field' (highlights specific fields). However, it doesn't explicitly contrast with 'list_pdfs' or 'render_pdf_page', keeping it from a perfect 5.

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 when to choose 'extract_form_fields' over 'extract_text' for form-specific extraction, or when to use 'highlight_form_field' for individual field operations. There's no context about prerequisites or typical use cases.

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/Wildebeest/mcp_pdf_forms'

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