Skip to main content
Glama

convert_pdf_file

Convert PDF files to Markdown format using OCR technology, preserving document structure and extracting images for easier editing and content reuse.

Instructions

Convert a local PDF file to Markdown. Output is saved in a new folder named after the PDF in its original directory.

Args:
    file_path: Path to a local PDF file or multiple paths separated by spaces, commas, or newlines.

Returns:
    A dictionary with the conversion results.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The primary handler function for the 'convert_pdf_file' MCP tool. It processes local PDF file(s), uses Mistral AI OCR to convert to markdown, saves output files and images in a directory next to the original PDF, and returns results. Registered via @mcp.tool() decorator.
    @mcp.tool()
    async def convert_pdf_file(file_path: str) -> Dict[str, Any]:
        """
        Convert a local PDF file to Markdown. Output is saved in a new folder named after the PDF in its original directory.
    
        Args:
            file_path: Path to a local PDF file or multiple paths separated by spaces, commas, or newlines.
    
        Returns:
            A dictionary with the conversion results.
        """
        if not MISTRAL_API_KEY:
            return {"success": False, "error": "Missing API key, please set environment variable MISTRAL_API_KEY"}
    
        try:
            client = Mistral(api_key=MISTRAL_API_KEY)
        except Exception as e:
            return {"success": False, "error": f"Error initializing Mistral client: {e}"}
    
        file_paths = parse_input_string(file_path)
        results = []
    
        for path_str in file_paths:
            try:
                input_path = Path(path_str)
                if not input_path.exists() or not input_path.name.lower().endswith('.pdf'):
                    results.append({"file_path": path_str, "success": False, "error": "File does not exist or is not a PDF."})
                    continue
    
                # Create a new directory for output next to the original file
                output_dir = input_path.parent / input_path.stem
                output_dir.mkdir(parents=True, exist_ok=True)
    
                output_md_path = output_dir / f"{input_path.stem}.md"
    
                with open(input_path, "rb") as pdf_file:
                    base64_pdf = base64.b64encode(pdf_file.read()).decode('utf-8')
    
                ocr_response = client.ocr.process(
                    model="mistral-ocr-latest",
                    document={"type": "document_url", "document_url": f"data:application/pdf;base64,{base64_pdf}"},
                    include_image_base64=True
                )
    
                markdown_content, saved_images = save_ocr_response_to_markdown_and_images(
                    ocr_response, output_md_path, output_dir
                )
    
                if markdown_content is not None:
                    results.append({
                        "file_path": path_str,
                        "success": True,
                        "markdown_file": str(output_md_path),
                        "images": saved_images,
                        "output_directory": str(output_dir),
                        "content_length": len(markdown_content)
                    })
                else:
                    results.append({"file_path": path_str, "success": False, "error": "Could not save markdown or images."})
    
            except Exception as e:
                results.append({"file_path": path_str, "success": False, "error": f"Error processing file '{path_str}': {e}"})
    
        return {"success": any(r.get("success", False) for r in results), "results": results}
  • Helper utility to parse the input file_path string into a list of individual paths, handling quotes, commas, spaces, and newlines.
    def parse_input_string(input_string: str) -> List[str]:
        """Parses a string of paths or URLs separated by spaces, commas, or newlines."""
        if (input_string.startswith('"') and input_string.endswith('"')) or \
           (input_string.startswith("'") and input_string.endswith("'")):
            input_string = input_string[1:-1]
        items = " ".join(input_string.replace(",", " ").split()).split()
        cleaned_items = []
        for item in items:
            if (item.startswith('"') and item.endswith('"')) or \
               (item.startswith("'") and item.endswith("'")):
                cleaned_items.append(item[1:-1])
            else:
                cleaned_items.append(item)
        return [item for item in cleaned_items if item]
  • Helper function that saves the OCR response's markdown content to a file and extracts/saves embedded images to disk.
    def save_ocr_response_to_markdown_and_images(ocr_response, output_md_path, output_dir_for_images):
        """
        Saves the markdown content from each page of the OCR response to a file
        and saves any associated images.
        """
        full_markdown_content = []
        saved_images = []
        try:
            with open(output_md_path, "wt", encoding='utf-8') as f:
                for page in ocr_response.pages:
                    f.write(page.markdown)
                    full_markdown_content.append(page.markdown)
                    for image in page.images:
                        saved_image_path = save_image(image, output_dir_for_images)
                        if saved_image_path:
                            saved_images.append(saved_image_path)
            return "".join(full_markdown_content), saved_images
        except Exception as e:
            print(f"Error saving markdown file '{output_md_path}' or processing images: {e}")
            return None, []
Behavior3/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It describes the conversion process, output format (Markdown), and storage behavior (saved in a new folder), but doesn't mention error handling, performance characteristics, file size limits, or permission requirements. It provides basic operational context but lacks comprehensive behavioral details.

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 efficiently structured with three focused sentences: purpose statement, parameter explanation, and return value description. Each sentence adds distinct value without redundancy. The information is front-loaded with the core functionality stated first.

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 moderate complexity (file conversion with storage implications), no annotations, and an output schema present, the description covers the essential operations well. It explains what the tool does, parameter usage, and mentions the return format (though output schema handles details). However, it could better address error cases or limitations given the file system operations involved.

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 description coverage is 0%, so the description must fully compensate. It explains the 'file_path' parameter meaningfully, specifying it accepts 'a local PDF file or multiple paths separated by spaces, commas, or newlines,' which adds crucial semantic information beyond the schema's basic type declaration. However, it doesn't detail path format requirements or validation rules.

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 ('Convert a local PDF file to Markdown') and the resource ('PDF file'), distinguishing it from the sibling tool 'convert_pdf_url' which handles URLs rather than local files. The description explicitly mentions the output format and storage location, making the purpose unambiguous.

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

Usage Guidelines4/5

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

The description provides clear context for when to use this tool (for local PDF files) versus the sibling tool (for URLs), though it doesn't explicitly state 'when not to use' or mention alternative tools beyond the sibling. The guidance is sufficient to differentiate use cases but lacks explicit exclusions or prerequisites.

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/zicez/mcp-pdf2md'

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