| """Supplementary FastAPI app for local inspection (not MCP runtime entrypoint).""" |
|
|
| from __future__ import annotations |
|
|
| import os |
| import importlib |
| import sys |
| from pathlib import Path |
| from typing import Any |
|
|
| try: |
| FastAPI = getattr(importlib.import_module("fastapi"), "FastAPI", None) |
| except Exception: |
| FastAPI = None |
|
|
| PLUGIN_DIR = Path(__file__).resolve().parent / "biopython" / "mcp_output" / "mcp_plugin" |
| plugin_dir_str = str(PLUGIN_DIR) |
| if plugin_dir_str not in sys.path: |
| sys.path.insert(0, plugin_dir_str) |
|
|
| if FastAPI is not None: |
| app = FastAPI(title="biopython-mcp-info", version="1.0.0") |
| else: |
| class _FallbackApp: |
| def get(self, *_args: Any, **_kwargs: Any): |
| def decorator(func): |
| return func |
|
|
| return decorator |
|
|
| app = _FallbackApp() |
| PORT = int(os.getenv("PORT", "7860")) |
|
|
|
|
| def _extract_tools(app_obj: Any) -> list[dict[str, str]]: |
| tools_attr = getattr(app_obj, "tools", None) |
| if tools_attr is None: |
| return [] |
|
|
| if isinstance(tools_attr, dict): |
| items = tools_attr.values() |
| else: |
| items = tools_attr |
|
|
| tools: list[dict[str, str]] = [] |
| for item in items: |
| if isinstance(item, dict): |
| name = str(item.get("name", "")) |
| description = str(item.get("description", "")) |
| else: |
| name = str(getattr(item, "name", getattr(item, "__name__", ""))) |
| description = str(getattr(item, "description", "")) |
| if name: |
| tools.append({"name": name, "description": description}) |
| return tools |
|
|
|
|
| @app.get("/") |
| def root() -> dict[str, Any]: |
| return { |
| "service": "biopython-mcp-deployment", |
| "mcp_transport": os.getenv("MCP_TRANSPORT", "stdio"), |
| "mcp_port": os.getenv("MCP_PORT", "8000"), |
| "info_port": PORT, |
| "note": "This FastAPI app is supplementary and does not run the MCP server.", |
| } |
|
|
|
|
| @app.get("/health") |
| def health() -> dict[str, str]: |
| return {"status": "healthy"} |
|
|
|
|
| @app.get("/tools") |
| def tools() -> dict[str, Any]: |
| try: |
| create_app = getattr(importlib.import_module("mcp_service"), "create_app") |
|
|
| mcp_app = create_app() |
| return {"tools": _extract_tools(mcp_app)} |
| except Exception as exc: |
| return {"tools": [], "error": str(exc)} |
|
|