Skip to content

server

Class info

🛈 DocStrings

convert_to_markdown async

convert_to_markdown(
    source_path: str,
    target_folder: str,
    provider: ConverterShorthand = "mistral",
    languages: list[SupportedLanguage] | None = None,
    page_range: str | None = None,
) -> str

Convert a PDF to markdown and images.

Parameters:

Name Type Description Default
source_path str

Path to the source document (PDF, powerpoint, images, ...)

required
target_folder str

Path where to save the resulting markdown and images

required
provider ConverterShorthand

The provider to use for conversion.

'mistral'
languages list[SupportedLanguage] | None

Used languages in the document, if known.

None
page_range str | None

1-based page range, for example "1,4-7,9-10"

None
Source code in src/docler_mcp/server.py
@mcp.tool(
    annotations=ToolAnnotations(
        title="Convert to Markdown",
        read_only_hint=False,
        idempotent_hint=True,
    )
)
async def convert_to_markdown(
    # ctx: Context,
    source_path: str,
    target_folder: str,
    provider: ConverterShorthand = "mistral",
    languages: list[SupportedLanguage] | None = None,
    page_range: str | None = None,
) -> str:
    """Convert a PDF to markdown and images.

    Args:
        source_path: Path to the source document (PDF, powerpoint, images, ...)
        target_folder: Path where to save the resulting markdown and images
        provider: The provider to use for conversion.
        languages: Used languages in the document, if known.
        page_range: 1-based page range, for example "1,4-7,9-10"
    """
    try:
        target = UPath(target_folder).expanduser()
        source = UPath(source_path).expanduser()
        target.mkdir(parents=True, exist_ok=True)
        converters: list[type[DocumentConverter]] = DocumentConverter.get_available_providers()
        converter_cls = next(i for i in converters if provider == i.NAME)
        converter = converter_cls(languages=languages, page_range=page_range)
        # await ctx.info(f"Starting conversion for {source_path} using {provider!r}...")
        document = await converter.convert_file(source)
        # await ctx.info(f"Exporting to {target}...")
        await document.export_to_directory(target)
        files_str = "\n- ".join(str(i) for i in target.iterdir())
    except FileNotFoundError as e:
        return f"Error: {e}. Is the path correct? Current cwd is {UPath.cwd()}."
    else:
        return f"Conversion successful. Files created in {target_folder}:\n{files_str}"