MCPcopy Index your code
hub / github.com/PDFMathTranslate/PDFMathTranslate / create_mcp_app

Function create_mcp_app

pdf2zh/mcp_server.py:16–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14
15
16def create_mcp_app() -> FastMCP:
17 mcp = FastMCP("pdf2zh")
18
19 @mcp.tool()
20 async def translate_pdf(
21 file: str, lang_in: str, lang_out: str, ctx: Context
22 ) -> str:
23 """
24 translate given pdf. Argument `file` is absolute path of input pdf,
25 `lang_in` and `lang_out` is translate from and to language, and
26 should be like google translate lang_code. `lang_in` can be `auto`
27 if you can't determine input language.
28 """
29
30 with open(file, "rb") as f:
31 file_bytes = f.read()
32 await ctx.log(level="info", message=f"start translate {file}")
33 with contextlib.redirect_stdout(io.StringIO()):
34 doc_mono_bytes, doc_dual_bytes = translate_stream(
35 file_bytes,
36 lang_in=lang_in,
37 lang_out=lang_out,
38 service="google",
39 model=ModelInstance.value,
40 thread=4,
41 )
42 await ctx.log(level="info", message="translate complete")
43 output_path = Path(os.path.dirname(file))
44 filename = os.path.splitext(os.path.basename(file))[0]
45 doc_mono = output_path / f"{filename}-mono.pdf"
46 doc_dual = output_path / f"{filename}-dual.pdf"
47 with open(doc_mono, "wb") as f:
48 f.write(doc_mono_bytes)
49 with open(doc_dual, "wb") as f:
50 f.write(doc_dual_bytes)
51 return f"""------------
52 translate complete
53 mono pdf file: {doc_mono.absolute()}
54 dual pdf file: {doc_dual.absolute()}
55 """
56
57 return mcp
58
59
60def create_starlette_app(mcp_server: Server, *, debug: bool = False) -> Starlette:

Callers 2

mainFunction · 0.90
mcp_server.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected