File size: 2,279 Bytes
7a1e4b1
 
 
9494bec
 
7a1e4b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9494bec
7a1e4b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9494bec
 
 
7a1e4b1
9494bec
7a1e4b1
 
 
 
9494bec
 
7a1e4b1
9494bec
7a1e4b1
 
 
9494bec
 
7a1e4b1
9494bec
7a1e4b1
 
9494bec
7a1e4b1
 
 
 
9494bec
 
7a1e4b1
 
 
 
 
 
9494bec
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from __future__ import annotations

import importlib
import os
import sys
from pathlib import Path
from typing import Any

try:
    _fastapi_module = importlib.import_module("fastapi")
    FastAPI = getattr(_fastapi_module, "FastAPI", None)
except Exception:
    FastAPI = None

BASE_DIR = Path(__file__).resolve().parent
PLUGIN_DIR = BASE_DIR / "cclib" / "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="cclib MCP info app", version="1.0.0")


def _extract_tools(mcp_app: Any) -> list[dict[str, str]]:
    tools = getattr(mcp_app, "tools", None)
    results: list[dict[str, str]] = []

    if isinstance(tools, dict):
        iterable = tools.values()
    elif isinstance(tools, list):
        iterable = tools
    else:
        iterable = []

    for item in iterable:
        name = getattr(item, "name", None)
        description = getattr(item, "description", None)
        if not name and isinstance(item, dict):
            name = str(item.get("name", ""))
            description = str(item.get("description", ""))
        if name:
            results.append({"name": str(name), "description": str(description or "")})

    return results


@app.get("/")
def root() -> dict[str, Any]:
    return {
        "service": "cclib MCP deployment",
        "mcp_http_endpoint": "/mcp",
        "supplementary_app": True,
        "transport": os.environ.get("MCP_TRANSPORT", "stdio"),
    }


@app.get("/health")
def health() -> dict[str, str]:
    return {"status": "healthy"}


@app.get("/tools")
def list_tools() -> dict[str, Any]:
    try:
        mcp_service_module = importlib.import_module("mcp_service")
        create_app = getattr(mcp_service_module, "create_app")
        mcp_app = create_app()
        return {"tools": _extract_tools(mcp_app)}
    except Exception as exc:
        return {"tools": [], "error": f"{type(exc).__name__}: {exc}"}


if __name__ == "__main__":
    port = int(os.getenv("PORT", "7860"))
    try:
        import uvicorn
    except Exception as exc:
        raise RuntimeError("uvicorn is required for local app.py execution") from exc

    uvicorn.run(app, host="0.0.0.0", port=port)