| from __future__ import annotations |
|
|
| import os |
| import sys |
| import importlib |
| from pathlib import Path |
| from typing import Any |
|
|
| try: |
| _fastapi = importlib.import_module("fastapi") |
| FastAPI = getattr(_fastapi, "FastAPI", None) |
| except Exception: |
| FastAPI = None |
|
|
| CURRENT_DIR = Path(__file__).resolve().parent |
| PLUGIN_DIR = CURRENT_DIR / "molmass" / "mcp_output" / "mcp_plugin" |
| if str(PLUGIN_DIR) not in sys.path: |
| sys.path.insert(0, str(PLUGIN_DIR)) |
|
|
| if FastAPI is None: |
| raise RuntimeError("fastapi is required to run app.py") |
|
|
| app = FastAPI(title="molmass MCP Info API", version="1.0.0") |
|
|
|
|
| @app.get("/") |
| def root() -> dict[str, Any]: |
| return { |
| "service": "molmass-mcp", |
| "description": "Supplementary info API for local development.", |
| "mcp_entrypoint": "molmass/mcp_output/start_mcp.py", |
| "mcp_http_path": "/mcp", |
| } |
|
|
|
|
| @app.get("/health") |
| def health() -> dict[str, str]: |
| return {"status": "healthy"} |
|
|
|
|
| @app.get("/tools") |
| def tools() -> dict[str, Any]: |
| create_app = importlib.import_module("mcp_service").create_app |
|
|
| mcp = create_app() |
| tool_items = [] |
| for tool in getattr(mcp, "tools", []): |
| tool_items.append( |
| { |
| "name": getattr(tool, "name", getattr(tool, "__name__", "unknown")), |
| "description": getattr(tool, "description", ""), |
| } |
| ) |
| return {"count": len(tool_items), "tools": tool_items} |
|
|
|
|
| if __name__ == "__main__": |
| import uvicorn |
|
|
| port = int(os.getenv("PORT", "7860")) |
| uvicorn.run(app, host="0.0.0.0", port=port) |
|
|