Skip to main content
Glama
Unstructured-IO

Unstructured API MCP Server

Official

partition_local_file

Convert local files into structured JSON or Markdown data using AI-powered transformation strategies for documents, PDFs, and images.

Instructions

Transform a local file into structured data using the Unstructured API.

Args:
    input_file_path: The absolute path to the file.
    output_file_dir: The absolute path to the directory where the output file should be saved.
    strategy: The strategy for transformation.
        Available strategies:
            VLM - most advanced transformation suitable for difficult PDFs and Images
            hi_res - high resolution transformation suitable for most document types
            fast - fast transformation suitable for PDFs with extractable text
            auto - automatically choose the best strategy based on the input file
    vlm_model: The VLM model to use for the transformation.
    vlm_model_provider: The VLM model provider to use for the transformation.
    output_type: The type of output to generate. Options: 'json' for json
                 or 'md' for markdown.

Returns:
    A string containing the structured data or a message indicating the output file
    path with the structured data.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
input_file_pathYes
output_file_dirYes
strategyNovlm
vlm_modelNoclaude-3-5-sonnet-20241022
vlm_model_providerNoanthropic
output_typeNojson

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The main handler function for the 'partition_local_file' tool. It processes a local file using the Unstructured API, handles parameters like strategy and model, calls the API, and saves the output as JSON or Markdown.
    async def partition_local_file(
        input_file_path: str,
        output_file_dir: str,
        strategy: Strategy = Strategy.VLM,
        vlm_model: VLMModel = VLMModel.CLAUDE_3_5_SONNET_20241022,
        vlm_model_provider: VLMModelProvider = VLMModelProvider.ANTHROPIC,
        output_type: Literal["json", "md"] = "json",
    ) -> str:
        """
        Transform a local file into structured data using the Unstructured API.
    
        Args:
            input_file_path: The absolute path to the file.
            output_file_dir: The absolute path to the directory where the output file should be saved.
            strategy: The strategy for transformation.
                Available strategies:
                    VLM - most advanced transformation suitable for difficult PDFs and Images
                    hi_res - high resolution transformation suitable for most document types
                    fast - fast transformation suitable for PDFs with extractable text
                    auto - automatically choose the best strategy based on the input file
            vlm_model: The VLM model to use for the transformation.
            vlm_model_provider: The VLM model provider to use for the transformation.
            output_type: The type of output to generate. Options: 'json' for json
                         or 'md' for markdown.
    
        Returns:
            A string containing the structured data or a message indicating the output file
            path with the structured data.
        """
    
        input_path = Path(input_file_path)
        output_dir_path = Path(output_file_dir)
    
        if output_type not in ["json", "md"]:
            return f"Invalid output type '{output_type}'. Must be 'json' or 'md'."
    
        try:
            with input_path.open("rb") as content:
                partition_params = PartitionParameters(
                    files=Files(
                        content=content,
                        file_name=input_path.name,
                    ),
                    strategy=strategy,
                    vlm_model=vlm_model,
                    vlm_model_provider=vlm_model_provider,
                )
                response = await call_api(partition_params)
        except Exception as e:
            return f"Failed to partition file: {e}"
    
        output_dir_path.mkdir(parents=True, exist_ok=True)
    
        output_file = output_dir_path / input_path.with_suffix(f".{output_type}").name
    
        if output_type == "json":
            json_elements_as_str = json.dumps(response, indent=2)
            output_file.write_text(json_elements_as_str, encoding="utf-8")
        else:
            markdown = construct_markdown(response, input_path.name)
            output_file.write_text(markdown, encoding="utf-8")
    
        return f"Partitioned file {input_file_path} to {output_file} successfully."
  • Registers the partition_local_file tool with the MCP server using the FastMCP tool decorator.
    def register_unstructured_api_tools(mcp: FastMCP):
        mcp.tool()(partition_local_file)
  • Helper function that prepares parameters and calls the Unstructured API's partition_async endpoint.
    async def call_api(partition_params: PartitionParameters) -> list[dict]:
        partition_params.split_pdf_page = True
        partition_params.split_pdf_allow_failed = True
        partition_params.split_pdf_concurrency_level = 15
    
        request = PartitionRequest(partition_parameters=partition_params)
    
        res = await client.general.partition_async(request=request)
        return res.elements
  • Helper function to convert the API response elements into a Markdown formatted string.
    def construct_markdown(elements_list: list[dict[str, Any]], file_name: str) -> str:
        """
        Constructs a markdown representation from the response data.
    
        Args:
            elements_list: The response data from the API call as a list of elements.
            file_name: The name of the input file.
    
        Returns:
            A markdown string.
        """
        markdown = f"# {file_name}\n\n"
    
        for element in elements_list:
            element_type = element.get("type", "")
            text = element.get("text", "")
    
            if element_type == "Table":
                text_as_html = element.get("metadata", {}).get("text_as_html", "<></>")
                markdown += f"{text_as_html}\n\n"
            elif element_type == "Header":
                markdown += f"## {text}\n\n"
            else:
                markdown += f"{text}\n\n"
    
        return markdown
Behavior3/5

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

With no annotations provided, the description carries full burden. It mentions the transformation process and output options but lacks details about error conditions, performance characteristics, file size limits, or authentication requirements. The description doesn't contradict any annotations since none exist.

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

Conciseness4/5

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

The description is well-structured with clear sections (Args, Returns) and bullet points for strategies. It's appropriately sized for a 6-parameter tool, though some strategy descriptions could be more concise. Every sentence adds value, with no redundant information.

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

Completeness4/5

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

Given the tool's complexity (6 parameters, transformation operation) and no annotations, the description does well. It explains parameters thoroughly and mentions the return value. However, it lacks information about error handling, file format support, or transformation limitations that would be helpful for a file processing tool.

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

Parameters5/5

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

With 0% schema description coverage, the description provides excellent parameter semantics beyond the bare schema. It explains each parameter's purpose, provides strategy descriptions with use cases, lists available options for output_type, and clarifies what each parameter represents. This fully compensates for the schema's lack of descriptions.

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 ('Transform a local file into structured data') and identifies the technology used ('using the Unstructured API'). It distinguishes this tool from all sibling tools which deal with workflows, connectors, and jobs rather than file processing.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context through strategy explanations (e.g., 'VLM - most advanced transformation suitable for difficult PDFs and Images'), but doesn't explicitly state when to use this tool versus alternatives or mention any prerequisites. No sibling tools appear to offer similar file transformation capabilities.

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/Unstructured-IO/UNS-MCP'

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