File size: 1,638 Bytes
905fbb6
 
76cf5f0
 
905fbb6
 
 
 
 
 
 
 
 
 
 
 
 
76cf5f0
905fbb6
 
 
 
76cf5f0
 
 
905fbb6
76cf5f0
905fbb6
 
 
 
76cf5f0
 
905fbb6
76cf5f0
905fbb6
 
 
76cf5f0
 
905fbb6
 
 
 
 
 
 
 
 
 
 
 
 
76cf5f0
 
 
905fbb6
 
76cf5f0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 / "backtrader" / "mcp_output" / "mcp_plugin"

if str(PLUGIN_DIR) not in sys.path:
    sys.path.insert(0, str(PLUGIN_DIR))

app = FastAPI(title="backtrader-mcp-info", version="1.0.0")


def _extract_tool_info(tool_obj: Any) -> dict[str, str]:
    name = getattr(tool_obj, "name", None) or getattr(tool_obj, "__name__", "unknown")
    description = getattr(tool_obj, "description", None) or getattr(tool_obj, "__doc__", "") or ""
    return {"name": str(name), "description": str(description).strip()}


@app.get("/")
def root() -> dict[str, Any]:
    return {
        "service": "backtrader MCP deployment",
        "mcp_transport": os.getenv("MCP_TRANSPORT", "stdio"),
        "mcp_port": int(os.getenv("MCP_PORT", "8000")),
        "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]:
    from mcp_service import create_app

    mcp = create_app()
    raw_tools = getattr(mcp, "tools", [])

    if isinstance(raw_tools, dict):
        items = [_extract_tool_info(value) for value in raw_tools.values()]
    else:
        items = [_extract_tool_info(item) for item in raw_tools]

    return {"count": len(items), "tools": items}


if __name__ == "__main__":
    import uvicorn

    port = int(os.getenv("PORT", "7860"))
    uvicorn.run(app, host="0.0.0.0", port=port)