markitdown-mcp 套件

把 MarkItDown 包成 FastMCP server,一個工具 + 三種 transport
檔案:packages/markitdown-mcp/src/markitdown_mcp/__main__.py(140 行)

大方向

整個套件只有一個檔案有實際邏輯。核心是 FastMCP("markitdown") 實例 + 一個 @mcp.tool() 工具;--http 模式用 Starlette 同時提供 Streamable HTTP 與 SSE。


1. 工具定義

convert_to_markdown(uri: str) -> str 工具接收 http:/https:/file:/data: URI,回傳 Markdown。

一行核心邏輯:MarkItDown(enable_plugins=check_plugins_enabled()).convert_uri(uri).markdown這是整個 MCP 套件唯一暴露的工具——所有的格式知識都在核心,這裡只做轉發。

check_plugins_enabled() -> bool 工具讀環境變數 MARKITDOWN_ENABLE_PLUGINS 決定要不要開外掛。

true/1/yes(不分大小寫)都算開啟。用環境變數而非參數,方便容器/部署層設定。

2. HTTP / SSE 模式

create_starlette_app(mcp_server, *, debug=False) Starlette同時掛載 /sse 與 /mcp 兩條路由。

SseServerTransport("/messages/")StreamableHTTPSessionManager,routes:

  • /sse — SSE 長連線(handle_sse)。
  • /mcp — Streamable HTTP(handle_streamable_http)。
  • /messages/ — SSE 的回應 POST 端點。
main() 入口--http/--sse 切 transport;非本機綁定會印大警告。

argparse 收 --http / --sse / --host / --port--sse 是棄用別名。 特別的工程細節:host 不是 127.0.0.1/localhost 時,會向 stderr 印一段 非常明確的安全警告(無認證、以使用者權限運作、能讀任何檔案)。這就是 「預設只綁 localhost」背後的防呆。