Spaces:
Running on Zero
Running on Zero
File size: 1,848 Bytes
53d9817 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | from __future__ import annotations
import json
from pathlib import Path
from mcp.server.fastmcp import FastMCP
from universal_parser.core.engine import parse
from universal_parser.core.router import FORMAT_REGISTRY
from universal_parser.exports.to_chunks import to_chunks
from universal_parser.exports.to_markdown import to_markdown
# Initialize FastMCP Server
mcp = FastMCP(
"universal-parser",
instructions="A zero-GPU universal document ingestion engine for RAG and AI agents.",
)
@mcp.tool()
def parse_document(file_path: str, output_format: str = "markdown") -> str:
"""
Parse any local document (PDF, DOCX, XLSX, PPTX, HTML, EPUB, CSV, JSON, XML, Parquet, Email, Image).
Args:
file_path: Path to the document.
output_format: 'markdown', 'chunks', or 'json'.
"""
path = Path(file_path)
if not path.exists():
return f"Error: File not found: {file_path}"
try:
doc = parse(path)
if output_format == "markdown":
return to_markdown(doc)
elif output_format == "chunks":
chunks = to_chunks(doc, max_tokens=512)
return json.dumps([c.__dict__ for c in chunks], indent=2)
elif output_format == "json":
return doc.model_dump_json(indent=2)
else:
return f"Error: Unknown output format '{output_format}'. Use 'markdown', 'chunks' or 'json'."
except Exception as e: # noqa: BLE001
return f"Error parsing document: {e!s}"
@mcp.tool()
def list_supported_formats() -> list[str]:
"""List all document file formats supported by Universal Parser."""
return sorted({ft.name.lower() for ft in FORMAT_REGISTRY})
def run_server() -> None:
"""Run the MCP server over standard I/O (stdio)."""
mcp.run(transport="studio")
if __name__ == "__main__":
run_server()
|