File size: 1,845 Bytes
4dcbc64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Container entrypoint for the M3GNet FastMCP service.



The script searches for ``start_mcp.py`` in the extracted project tree and

executes it with sensible defaults. This avoids hard-coding the deployment

layout, which is helpful when only the ``mcp_output`` folder is uploaded to a

Service (e.g., HuggingFace Space).

"""

from __future__ import annotations

import os
import sys
from pathlib import Path


def find_start_script(root: Path) -> Path | None:
    """Return the first ``start_mcp.py`` found within ``root`` (depth <= 3)."""

    max_depth = 3
    queue = [(root, 0)]
    while queue:
        current, depth = queue.pop(0)
        if not current.is_dir():
            continue
        # Look for the script in the current directory first
        candidate = current / "start_mcp.py"
        if candidate.is_file():
            return candidate
        if depth >= max_depth:
            continue
        for child in current.iterdir():
            if child.is_dir():
                queue.append((child, depth + 1))
    return None


def main() -> None:
    root = Path("/app")
    script = find_start_script(root)
    if script is None:
        print("[ERROR] 未找到 start_mcp.py。/app 目录内容如下:", file=sys.stderr)
        for path in root.rglob("*"):
            if path.is_file():
                print(f" - {path.relative_to(root)}", file=sys.stderr)
        sys.exit(1)

    host = os.environ.get("MCP_HOST", "0.0.0.0")
    port = os.environ.get("MCP_PORT", "7860")
    mode = os.environ.get("MCP_MODE", "sse")

    print(f"[INFO] 启动 FastMCP 服务:{script} (mode={mode}, host={host}, port={port})")
    os.execv(sys.executable, [sys.executable, str(script), "--mode", mode, "--host", host, "--port", port])


if __name__ == "__main__":
    main()