climlab / app.py
ghh1125's picture
Upload 14 files
079b2ee verified
Raw
History Blame Contribute Delete
1.7 kB
import os
import sys
import importlib
from pathlib import Path
try:
FastAPI = importlib.import_module("fastapi").FastAPI
except Exception:
FastAPI = None
PLUGIN_DIR = Path(__file__).resolve().parent / "climlab" / "mcp_output" / "mcp_plugin"
if str(PLUGIN_DIR) not in sys.path:
sys.path.insert(0, str(PLUGIN_DIR))
if FastAPI is None:
class _FallbackApp:
def get(self, _: str):
def decorator(func):
return func
return decorator
app = _FallbackApp()
else:
app = FastAPI(title="climlab MCP info app", version="1.0.0")
@app.get("/")
def root() -> dict:
return {
"name": "climlab-mcp",
"description": "Supplementary API for local development and tool discovery.",
"mcp_http_path": "/mcp",
"port": int(os.getenv("PORT", "7860")),
}
@app.get("/health")
def health() -> dict:
return {"status": "healthy"}
@app.get("/tools")
def tools() -> dict:
mcp_service = importlib.import_module("mcp_service")
create_app = getattr(mcp_service, "create_app")
mcp = create_app()
tool_entries = []
raw_tools = getattr(mcp, "tools", [])
if isinstance(raw_tools, dict):
iterable = raw_tools.values()
else:
iterable = raw_tools
for item in iterable:
if isinstance(item, dict):
name = item.get("name", "unknown")
description = item.get("description", "")
else:
name = getattr(item, "name", "unknown")
description = getattr(item, "description", "")
tool_entries.append({"name": name, "description": description})
return {"count": len(tool_entries), "tools": tool_entries}