chemlib / app.py
ghh1125's picture
Upload 14 files
7d527d2 verified
Raw
History Blame Contribute Delete
1.61 kB
from __future__ import annotations
import os
import sys
from pathlib import Path
from typing import Any
from fastapi import FastAPI
BASE_DIR = Path(__file__).resolve().parent
PLUGIN_DIR = BASE_DIR / "chemlib" / "mcp_output" / "mcp_plugin"
if str(PLUGIN_DIR) not in sys.path:
sys.path.insert(0, str(PLUGIN_DIR))
app = FastAPI(title="chemlib MCP Supplementary App")
def _tool_to_dict(tool: Any) -> dict[str, str]:
name = getattr(tool, "name", None)
description = getattr(tool, "description", None)
if isinstance(tool, dict):
name = name or tool.get("name")
description = description or tool.get("description")
return {
"name": str(name) if name is not None else "unknown",
"description": str(description) if description is not None else "",
}
@app.get("/")
def root() -> dict[str, Any]:
return {
"service": "chemlib-mcp",
"description": "Supplementary info API for chemlib MCP deployment.",
"mcp_http_endpoint": "/mcp",
"note": "This FastAPI app is not the MCP server entry point.",
}
@app.get("/health")
def health() -> dict[str, str]:
return {"status": "healthy"}
@app.get("/tools")
def list_tools() -> dict[str, Any]:
import importlib
mcp_service = importlib.import_module("mcp_service")
mcp = mcp_service.create_app()
tools = getattr(mcp, "tools", [])
return {"tools": [_tool_to_dict(tool) for tool in tools]}
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "7860"))
uvicorn.run("app:app", host="0.0.0.0", port=port, reload=False)