GStools / app.py
ghh1125's picture
Upload 14 files
a9a9f1a verified
from __future__ import annotations
import os
import sys
import importlib
from pathlib import Path
from typing import Any, Dict, List
try:
FastAPI = importlib.import_module("fastapi").FastAPI
except Exception: # pragma: no cover
FastAPI = None
ROOT_DIR = Path(__file__).resolve().parent
PLUGIN_DIR = ROOT_DIR / "GStools" / "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="GStools MCP Info Service", version="1.0.0")
@app.get("/")
def root() -> Dict[str, Any]:
return {
"service": "GStools MCP Deployment",
"mcp_endpoint": "/mcp",
"transport": os.getenv("MCP_TRANSPORT", "stdio"),
"default_port": int(os.getenv("PORT", "7860")),
"note": "This FastAPI app is supplementary and does not run MCP transport.",
}
@app.get("/health")
def health() -> Dict[str, str]:
return {"status": "healthy"}
@app.get("/tools")
def tools() -> Dict[str, List[Dict[str, Any]]]:
module = importlib.import_module("mcp_service")
create_app = getattr(module, "create_app")
mcp = create_app()
discovered: List[Dict[str, Any]] = []
tools_obj = getattr(mcp, "tools", None)
if isinstance(tools_obj, dict):
for name, tool in tools_obj.items():
description = getattr(tool, "description", "")
discovered.append({"name": str(name), "description": description})
elif isinstance(tools_obj, list):
for tool in tools_obj:
name = getattr(tool, "name", str(tool))
description = getattr(tool, "description", "")
discovered.append({"name": name, "description": description})
return {"tools": discovered}
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "7860"))
uvicorn.run(app, host="0.0.0.0", port=port)