Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/markitdown-mcp/src/markitdown_mcp/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import contextlib
import sys
import os
Expand All @@ -20,7 +21,11 @@
@mcp.tool()
async def convert_to_markdown(uri: str) -> str:
"""Convert a resource described by an http:, https:, file: or data: URI to markdown"""
return MarkItDown(enable_plugins=check_plugins_enabled()).convert_uri(uri).markdown
result = await asyncio.to_thread(
MarkItDown(enable_plugins=check_plugins_enabled()).convert_uri,
uri,
)
return result.markdown


def check_plugins_enabled() -> bool:
Expand Down
32 changes: 32 additions & 0 deletions packages/markitdown-mcp/tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from __future__ import annotations

import threading
from types import SimpleNamespace

import pytest

from markitdown_mcp import __main__ as main


@pytest.mark.anyio
async def test_convert_to_markdown_runs_conversion_off_event_loop(
monkeypatch: pytest.MonkeyPatch,
) -> None:
event_loop_thread = threading.get_ident()
conversion_thread: list[int] = []

class FakeMarkItDown:
def __init__(self, *, enable_plugins: bool) -> None:
self.enable_plugins = enable_plugins

def convert_uri(self, uri: str) -> SimpleNamespace:
conversion_thread.append(threading.get_ident())
return SimpleNamespace(markdown=f"converted:{uri}")

monkeypatch.setattr(main, "MarkItDown", FakeMarkItDown)

result = await main.convert_to_markdown("https://example.com/test.txt")

assert result == "converted:https://example.com/test.txt"
assert conversion_thread == [conversion_thread[0]]
assert conversion_thread[0] != event_loop_thread