| from __future__ import annotations |
|
|
| import os |
| import sys |
| import importlib |
| from pathlib import Path |
| from typing import Any |
|
|
| from fastapi import FastAPI |
|
|
| PLUGIN_DIR = Path(__file__).resolve().parent / "stanza" / "mcp_output" / "mcp_plugin" |
| if str(PLUGIN_DIR) not in sys.path: |
| sys.path.insert(0, str(PLUGIN_DIR)) |
|
|
| app = FastAPI(title="stanza-mcp-info", version="1.0.0") |
|
|
|
|
| @app.get("/") |
| def root() -> dict[str, Any]: |
| return { |
| "name": "stanza MCP Service", |
| "description": "Supplementary info app for local development.", |
| "mcp_entrypoint": "stanza/mcp_output/start_mcp.py", |
| "mcp_http_endpoint": "/mcp", |
| } |
|
|
|
|
| @app.get("/health") |
| def health() -> dict[str, str]: |
| return {"status": "healthy"} |
|
|
|
|
| @app.get("/tools") |
| def tools() -> dict[str, Any]: |
| try: |
| module = importlib.import_module("mcp_service") |
| create_app = getattr(module, "create_app") |
| mcp = create_app() |
| tool_records: list[dict[str, Any]] = [] |
|
|
| registry = getattr(mcp, "tools", None) |
| if registry is None: |
| registry = getattr(mcp, "_tools", None) |
|
|
| if isinstance(registry, dict): |
| for name, tool_obj in registry.items(): |
| desc = getattr(tool_obj, "description", "") |
| tool_records.append({"name": str(name), "description": desc}) |
| elif registry is not None: |
| for tool_obj in registry: |
| name = getattr(tool_obj, "name", str(tool_obj)) |
| desc = getattr(tool_obj, "description", "") |
| tool_records.append({"name": name, "description": desc}) |
|
|
| return {"status": "ok", "count": len(tool_records), "tools": tool_records} |
| except Exception as exc: |
| return {"status": "error", "count": 0, "tools": [], "error": str(exc)} |
|
|
|
|
| if __name__ == "__main__": |
| import uvicorn |
|
|
| port = int(os.getenv("PORT", "7860")) |
| uvicorn.run(app, host="0.0.0.0", port=port) |
|
|